-
Notifications
You must be signed in to change notification settings - Fork 27
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
add windows support #5
Conversation
This replaces the os.Rename call with two separate implementations depending on target OS. On windows it uses the movefileex syscall to make the move atomic, unlike the implementation of os.Rename.
Fixes #1 |
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.
Thanks for the PR! Just two minor comments.
func replaceFile(source, destination string) error { | ||
src, err := syscall.UTF16PtrFromString(source) | ||
if err != nil { | ||
return &os.LinkError{"replace", source, destination, err} |
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.
here and below: could you please change the literal construction to use field names to be robust when os.LinkError changes? E.g. &os.LinkError{Op: "replace", Old: source, New: destination, Err: err}
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.
Ahh yeah, definitely.
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.
Let me know once you pushed the change :)
} | ||
|
||
// see http://msdn.microsoft.com/en-us/library/windows/desktop/aa365240(v=vs.85).aspx | ||
if err := moveFileEx(src, dest, movefile_replace_existing|movefile_write_through); err != nil { |
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.
FWIW, golang.org/x/sys/windows
has a wrapper for MoveFileExW
, so it could be used instead of generating one here.
golang/go#32188 (comment) says MoveFileEx is not atomic under windows, and ReplaceFile should instead be used. |
golang/go#22397 (comment) says nothing is atomic under windows. Closing this until someone comes up with new evidence. |
This replaces the os.Rename call with two separate implementations depending
on target OS. On windows it uses the movefileex syscall to make the move
atomic, unlike the implementation of os.Rename.
Needs tests, for sure.