-
Notifications
You must be signed in to change notification settings - Fork 51
use NtSetInformationFile if os.Rename fails #220
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
karman-docker
wants to merge
1
commit into
moby:main
Choose a base branch
from
karman-docker:add_retry_for_rename
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.
+117
−1
Open
Changes from all commits
Commits
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,9 @@ | ||
| //go:build !windows | ||
|
|
||
| package atomicwriter | ||
|
|
||
| import "os" | ||
|
|
||
| func atomicwriterRenameAt(oldpath, newpath string) error { | ||
| return os.Rename(oldpath, newpath) | ||
| } | ||
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,107 @@ | ||
| package atomicwriter | ||
|
|
||
| import ( | ||
| "os" | ||
| "unsafe" | ||
|
|
||
| "golang.org/x/sys/windows" | ||
| ) | ||
|
|
||
| // fileRenameInformation is the FILE_RENAME_INFORMATION structure used by | ||
| // NtSetInformationFile to rename a file. FileName is a variable-length | ||
| // field; callers must allocate a buffer large enough to hold the full name. | ||
| type fileRenameInformation struct { | ||
| ReplaceIfExists uint32 | ||
| RootDirectory windows.Handle | ||
| FileNameLength uint32 | ||
| FileName [1]uint16 | ||
| } | ||
|
|
||
| // fileRenameInformationEx is the FILE_RENAME_INFORMATION_EX structure used by | ||
| // NtSetInformationFile to rename a file. | ||
| type fileRenameInformationEx struct { | ||
| Flags uint32 | ||
| RootDirectory windows.Handle | ||
| FileNameLength uint32 | ||
| FileName [1]uint16 | ||
| } | ||
|
|
||
| // atomicwriterRenameAt renames oldpath to newpath using os.Rename. If it fails, | ||
| // it attempts to use NtSetInformationFile with FILE_RENAME_POSIX_SEMANTICS, | ||
| // which allows atomic replacement of a file even when the destination | ||
| // is open by another process. | ||
| func atomicwriterRenameAt(oldpath, newpath string) error { | ||
|
|
||
| err := os.Rename(oldpath, newpath) | ||
| if err == nil { | ||
| return nil | ||
| } | ||
| // Open the source file requesting DELETE access so we can rename it. | ||
| srcPtr, err := windows.UTF16PtrFromString(oldpath) | ||
| if err != nil { | ||
| return &os.PathError{Op: "rename", Path: oldpath, Err: err} | ||
| } | ||
| handle, err := windows.CreateFile( | ||
| srcPtr, | ||
| windows.DELETE, | ||
| windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE, | ||
| nil, | ||
| windows.OPEN_EXISTING, | ||
| windows.FILE_ATTRIBUTE_NORMAL, | ||
| 0, | ||
| ) | ||
| if err != nil { | ||
| return &os.PathError{Op: "rename", Path: oldpath, Err: err} | ||
| } | ||
| defer windows.CloseHandle(handle) | ||
|
|
||
| // NtSetInformationFile requires an absolute NT path (\??\C:\...) when | ||
| // RootDirectory is NULL. | ||
| ntNewPath := `\??\` + newpath | ||
| newPathUTF16, err := windows.UTF16FromString(ntNewPath) | ||
| if err != nil { | ||
| return &os.PathError{Op: "rename", Path: newpath, Err: err} | ||
| } | ||
|
|
||
| fileNameLen := len(newPathUTF16)*2 - 2 // byte length, excluding null terminator | ||
| renameInfoEx := fileRenameInformationEx{ | ||
| Flags: windows.FILE_RENAME_REPLACE_IF_EXISTS | | ||
| windows.FILE_RENAME_POSIX_SEMANTICS, | ||
| } | ||
| var dummyEx fileRenameInformationEx | ||
| bufferSizeEx := int(unsafe.Offsetof(dummyEx.FileName)) + fileNameLen | ||
| bufferEx := make([]byte, bufferSizeEx) | ||
| infoEx := (*fileRenameInformationEx)(unsafe.Pointer(&bufferEx[0])) | ||
| infoEx.Flags = renameInfoEx.Flags | ||
| infoEx.FileNameLength = uint32(fileNameLen) | ||
| copy((*[windows.MAX_LONG_PATH]uint16)(unsafe.Pointer(&infoEx.FileName[0]))[:fileNameLen/2:fileNameLen/2], newPathUTF16) | ||
|
|
||
| const ( | ||
| FileRenameInformation = 10 | ||
| FileRenameInformationEx = 65 | ||
| ) | ||
| var iosbEx windows.IO_STATUS_BLOCK | ||
|
|
||
| err = windows.NtSetInformationFile(handle, &iosbEx, &bufferEx[0], uint32(bufferSizeEx), FileRenameInformationEx) | ||
| if err == nil { | ||
| return nil | ||
| } | ||
|
|
||
| // If the extended rename fails, fall back to the original FILE_RENAME_INFORMATION | ||
| // which is supported on older versions of Windows. This may fail if the destination | ||
| // file is open by another process, but there's no way to detect that beforehand. | ||
|
|
||
| var dummy fileRenameInformation | ||
| bufferSize := int(unsafe.Offsetof(dummy.FileName)) + fileNameLen | ||
| buffer := make([]byte, bufferSize) | ||
| info := (*fileRenameInformation)(unsafe.Pointer(&buffer[0])) | ||
| info.ReplaceIfExists = windows.FILE_RENAME_REPLACE_IF_EXISTS | windows.FILE_RENAME_POSIX_SEMANTICS | ||
| info.FileNameLength = uint32(fileNameLen) | ||
| copy((*[windows.MAX_LONG_PATH]uint16)(unsafe.Pointer(&info.FileName[0]))[:fileNameLen/2:fileNameLen/2], newPathUTF16) | ||
|
|
||
| var iosb windows.IO_STATUS_BLOCK | ||
| if err := windows.NtSetInformationFile(handle, &iosb, &buffer[0], uint32(bufferSize), FileRenameInformation); err != nil { | ||
| return &os.PathError{Op: "rename", Path: newpath, Err: err} | ||
| } | ||
| return nil | ||
| } |
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.
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.
nit: stuttering
Also, why is it called rename At? Usually 'At' implies there's an dirfd provided, which is not the case.