-
-
Notifications
You must be signed in to change notification settings - Fork 2
write: a finished file takes its name only while nobody holds it #150
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d0fea56
write: a finished file takes its name only while nobody holds it
donislawdev 96a997e
ci: the command line links golang.org/x/sys on Linux and macOS
donislawdev 18c35dc
write: the manifest's reservation in a file of its own, and Run back …
donislawdev 0982958
write: the five findings of the review of #150
donislawdev 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
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
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
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,92 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "errors" | ||
| "io/fs" | ||
| "os" | ||
| ) | ||
|
|
||
| // Publish puts a finished file under its final name, and refuses when anything | ||
| // already holds that name. | ||
| // | ||
| // Every file this tool finishes goes through here: each generated file, the | ||
| // manifest, the instructions beside it and a recipe written by "preset eject | ||
| // -o". They are all written whole under a temporary name first, so a run cut | ||
| // short never leaves half a file under a real one, and this is the step that | ||
| // gives the finished bytes their name. | ||
| // | ||
| // It replaced os.Rename, and the reason is O252, measured on 2026-09-29 with a | ||
| // second writer spinning on the same name (docs/O252-ANALIZA-2026-09-29.md). | ||
| // A rename REPLACES whatever is at the destination. So a file somebody else put | ||
| // under that name while we were writing - after our look, before our rename - | ||
| // was destroyed without a word. The measured loss was 2544 of 3000 names on | ||
| // NTFS, around 1900 of 1950 on ext4, tmpfs and overlay, and 999 of 1000 on an | ||
| // exFAT pendrive. The same writer through this function lost none on any of | ||
| // them, because the system settles "is the name free" and "take it" as one | ||
| // operation and says fs.ErrExist otherwise. | ||
| // | ||
| // The call differs by system, and the files beside this one each name theirs: | ||
| // MoveFileEx without MOVEFILE_REPLACE_EXISTING on Windows, renameat2 with | ||
| // RENAME_NOREPLACE on Linux, renamex_np with RENAME_EXCL on macOS. A hard link | ||
| // was the first idea and it is the fallback rather than the rule, because FAT | ||
| // and exFAT have none - measured on Linux, where link answers EPERM, and on | ||
| // Windows, where it answers "Incorrect function" even for a name that IS taken. | ||
| // | ||
| // Two fallbacks stand behind the call, for a filesystem that does not know it - | ||
| // some network shares, macOS on a FAT stick. Neither was reached on any disk | ||
| // measured, which is why each is taken only for an answer that means "this | ||
| // filesystem cannot do that" and never for any other failure: | ||
| // | ||
| // 1. a hard link to the final name, then the temporary name removed. It | ||
| // refuses a taken name the same way, where links exist at all. | ||
| // 2. the rename this replaced, after a look at the final name. That is the | ||
| // window O252 was about, and it is kept rather than refused because the | ||
| // alternative is a tool that cannot write to a stick at all. It is | ||
| // narrower than it was: it is only ever the last resort. | ||
| // | ||
| // The temporary name is ours in every case: on success it is gone, and on a | ||
| // refusal it is left for the caller, which created it and removes it. | ||
| func Publish(tmp, final string) error { | ||
| return PublishThrough(tmp, final, renameNoReplace, os.Link) | ||
| } | ||
|
|
||
| // PublishThrough is Publish with its two system calls passed in. | ||
| // | ||
| // It exists for one reason: the fallbacks are reached only on a filesystem this | ||
| // project's runners do not have, and a fallback nothing can reach is a defence | ||
| // nothing can turn red. So a guard walks the chain here on an ordinary disk, | ||
| // handing it calls that answer "unsupported". Publish is the only caller in the | ||
| // program, and it passes the real ones. | ||
| func PublishThrough(tmp, final string, noReplace, link func(string, string) error) error { | ||
| err := noReplace(tmp, final) | ||
| if err == nil { | ||
| return nil | ||
| } | ||
| if !errors.Is(err, errors.ErrUnsupported) { | ||
| return &os.LinkError{Op: "publish", Old: tmp, New: final, Err: err} | ||
| } | ||
|
|
||
| err = link(tmp, final) | ||
| if err == nil { | ||
| // The finished file has both names for a moment. The final one is | ||
| // what counts, so a failure to drop the other is not a failure of | ||
| // the publish - it leaves one of our temporary names behind, which | ||
| // verify already names as ours. | ||
| _ = os.Remove(tmp) | ||
| return nil | ||
| } | ||
| if !linkUnsupported(err) { | ||
| return &os.LinkError{Op: "publish", Old: tmp, New: final, Err: err} | ||
| } | ||
|
|
||
| // "I could not look" is not "nothing is there", and this is the one step | ||
| // that replaces what it lands on - so only a look that found nothing lets | ||
| // it through (the rule of review 2026-08-23, 3.7c, asked again on #150). | ||
| switch _, lookErr := os.Lstat(final); { | ||
| case lookErr == nil: | ||
| return &os.LinkError{Op: "publish", Old: tmp, New: final, Err: fs.ErrExist} | ||
| case !errors.Is(lookErr, fs.ErrNotExist): | ||
| return &os.LinkError{Op: "publish", Old: tmp, New: final, Err: lookErr} | ||
| } | ||
| return os.Rename(tmp, final) | ||
| } |
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,34 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| // renameNoReplace is renamex_np with RENAME_EXCL: the rename happens only when | ||
| // nothing is at the destination, and EEXIST otherwise. | ||
| // | ||
| // NOT MEASURED on a Mac - there is none to measure on (O153). The flag is | ||
| // documented for APFS and HFS+, and a filesystem that does not know it answers | ||
| // ENOTSUP, which sends Publish to its fallbacks rather than failing the write. | ||
| func renameNoReplace(from, to string) error { | ||
| err := unix.RenamexNp(from, to, unix.RENAME_EXCL) | ||
| if err == nil { | ||
| return nil | ||
| } | ||
| if errors.Is(err, unix.ENOTSUP) || errors.Is(err, unix.EOPNOTSUPP) || errors.Is(err, unix.EINVAL) { | ||
| return fmt.Errorf("%w: %w", errors.ErrUnsupported, err) | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| // linkUnsupported says whether a hard link failed because this filesystem has | ||
| // none. NOT MEASURED on a Mac. | ||
| func linkUnsupported(err error) bool { | ||
| return errors.Is(err, errors.ErrUnsupported) || | ||
| errors.Is(err, unix.EPERM) || | ||
| errors.Is(err, unix.ENOTSUP) || | ||
| errors.Is(err, unix.EOPNOTSUPP) | ||
| } |
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.