-
Notifications
You must be signed in to change notification settings - Fork 1
fix: use atomic rename that refuses existing destinations #93
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,5 @@ module github.com/dolph/find-replace | |
| go 1.20 | ||
|
|
||
| require golang.org/x/tools v0.7.0 | ||
|
|
||
| require golang.org/x/sys v0.26.0 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| golang.org/x/tools v0.1.9 h1:j9KsMiaP1c3B0OTQGth0/k+miLGTgLsAFUCrF2vLcF8= | ||
| golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU= | ||
| golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= | ||
| golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
| golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4= | ||
| golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s= |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| //go:build darwin | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| func atomicRenameNoReplace(oldpath, newpath string) error { | ||
| err := unix.RenameatxNp(unix.AT_FDCWD, oldpath, unix.AT_FDCWD, newpath, unix.RENAME_EXCL) | ||
| if errors.Is(err, unix.EEXIST) { | ||
| return fmt.Errorf("refusing to rename %v to %v: %v already exists", oldpath, newpath, newpath) | ||
| } | ||
| return err | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| //go:build linux | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| func atomicRenameNoReplace(oldpath, newpath string) error { | ||
| err := unix.Renameat2(unix.AT_FDCWD, oldpath, unix.AT_FDCWD, newpath, unix.RENAME_NOREPLACE) | ||
| if errors.Is(err, unix.EEXIST) { | ||
| return fmt.Errorf("refusing to rename %v to %v: %v already exists", oldpath, newpath, newpath) | ||
| } | ||
| return err | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| //go:build !linux && !darwin && !windows | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| ) | ||
|
|
||
| func atomicRenameNoReplace(oldpath, newpath string) error { | ||
| if _, err := os.Stat(newpath); err == nil { | ||
| return fmt.Errorf("refusing to rename %v to %v: %v already exists", oldpath, newpath, newpath) | ||
| } else if !errors.Is(err, os.ErrNotExist) { | ||
| return fmt.Errorf("stat rename destination %v: %w", newpath, err) | ||
| } | ||
| return os.Rename(oldpath, newpath) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| //go:build windows | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| ) | ||
|
|
||
| func atomicRenameNoReplace(oldpath, newpath string) error { | ||
| if _, err := os.Stat(newpath); err == nil { | ||
| return fmt.Errorf("refusing to rename %v to %v: %v already exists", oldpath, newpath, newpath) | ||
| } else if !errors.Is(err, os.ErrNotExist) { | ||
| return fmt.Errorf("stat rename destination %v: %w", newpath, err) | ||
| } | ||
| return os.Rename(oldpath, newpath) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On Windows this still has the same TOCTOU clobber window: Useful? React with 👍 / 👎. |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the
!linux && !darwin && !windowsbuild, this helper is still a stat-then-os.Rename, and on Unix targets such as FreeBSD/Solarisrenamereplaces an existing non-directory destination. If another concurrent rename createsnewpathafter the check, including sibling files processed by this tool whose names map to the same replacement target, this path can silently overwrite that file rather than preserving the no-replace guarantee.Useful? React with 👍 / 👎.