-
Notifications
You must be signed in to change notification settings - Fork 193
fix(tools): preserve file encoding on overwrite #988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PierrunoYT
wants to merge
7
commits into
Twigpine:main
Choose a base branch
from
PierrunoYT:fix/issue-967-preserve-file-encoding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a2e9bdf
fix(tools): preserve file encoding on overwrite
PierrunoYT 721d2c9
fix(tools): retain write observation after encoding restore
PierrunoYT 55b1be5
fix(tools): fail closed when an overwrite target cannot be read
PierrunoYT 8adf758
fix(tools): allow explicit overwrite encoding intent
ampagent 93df93b
test(tools): align unreadable overwrite coverage after rebase
PierrunoYT e0c8647
fix(tools): normalize lone CR during overwrite conversion
ampagent 5698374
fix(tools): reapply overwrite encoding after format-on-write
PierrunoYT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| //go:build !windows | ||
|
|
||
| package tools | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
| ) | ||
|
|
||
| // makeFileWriteOnly drops read permission while leaving the file writable, so | ||
| // the target can still be opened for writing but its prior bytes cannot be read. | ||
| // write_file must refuse that overwrite, since those bytes are the only evidence | ||
| // of the BOM and line endings to preserve. The returned func restores the | ||
| // original mode so the test can read the file back and the temp dir can be | ||
| // cleaned up. | ||
| func makeFileWriteOnly(t *testing.T, path string) func() { | ||
| t.Helper() | ||
| info, err := os.Stat(path) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| mode := info.Mode().Perm() | ||
| if err := os.Chmod(path, 0o200); err != nil { | ||
| t.Skipf("cannot drop read permission on this filesystem: %v", err) | ||
| } | ||
| return func() { | ||
| if err := os.Chmod(path, mode); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| //go:build windows | ||
|
|
||
| package tools | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "golang.org/x/sys/windows" | ||
| ) | ||
|
|
||
| // writeOnlyFileMask is FILE_GENERIC_WRITE, and deliberately not FILE_READ_DATA: | ||
| // os.Stat still reports the file and os.WriteFile still replaces it, but | ||
| // os.ReadFile is denied. Windows has no chmod, so the write-only shape has to be | ||
| // expressed as a DACL. | ||
| // | ||
| // FILE_READ_ATTRIBUTES keeps os.Stat cheap, DELETE lets t.TempDir clean up, and | ||
| // WRITE_DAC is required for the restore: an OWNER_RIGHTS ACE replaces the | ||
| // owner's implicit right to rewrite the descriptor, so it must be granted here. | ||
| const writeOnlyFileMask = "0x170196" | ||
|
|
||
| // makeFileWriteOnly replaces the file's DACL with a protected owner-only ACE | ||
| // that grants everything except reading its bytes, and returns a func restoring | ||
| // the descriptor it found. | ||
| func makeFileWriteOnly(t *testing.T, path string) func() { | ||
| t.Helper() | ||
| original, err := windows.GetNamedSecurityInfo(path, windows.SE_FILE_OBJECT, windows.DACL_SECURITY_INFORMATION) | ||
| if err != nil { | ||
| t.Skipf("cannot read the current DACL: %v", err) | ||
| } | ||
| originalDACL, _, err := original.DACL() | ||
| if err != nil { | ||
| t.Skipf("cannot parse the current DACL: %v", err) | ||
| } | ||
| writeOnly, err := windows.SecurityDescriptorFromString("D:P(A;;" + writeOnlyFileMask + ";;;OW)") | ||
| if err != nil { | ||
| t.Skipf("cannot build a write-only security descriptor: %v", err) | ||
| } | ||
| dacl, _, err := writeOnly.DACL() | ||
| if err != nil { | ||
| t.Skipf("cannot read the write-only DACL: %v", err) | ||
| } | ||
| if err := setFileDACL(path, dacl, true); err != nil { | ||
| t.Skipf("cannot apply a write-only DACL on this filesystem: %v", err) | ||
| } | ||
| return func() { | ||
| if err := setFileDACL(path, originalDACL, false); err != nil { | ||
| t.Fatalf("cannot restore the original DACL: %v", err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func setFileDACL(path string, dacl *windows.ACL, protected bool) error { | ||
| info := windows.SECURITY_INFORMATION(windows.DACL_SECURITY_INFORMATION) | ||
| if protected { | ||
| info |= windows.PROTECTED_DACL_SECURITY_INFORMATION | ||
| } else { | ||
| info |= windows.UNPROTECTED_DACL_SECURITY_INFORMATION | ||
| } | ||
| return windows.SetNamedSecurityInfo(path, windows.SE_FILE_OBJECT, info, nil, nil, dacl, nil) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.