var h syscall.Handle
err := NtOpenFile(
&h,
- SYNCHRONIZE|DELETE,
+ SYNCHRONIZE|FILE_READ_ATTRIBUTES|DELETE,
objAttrs,
&IO_STATUS_BLOCK{},
FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE,
}
defer syscall.CloseHandle(h)
- const (
- FileDispositionInformation = 13
- FileDispositionInformationEx = 64
- )
+ if TestDeleteatFallback {
+ return deleteatFallback(h)
+ }
+
+ const FileDispositionInformationEx = 64
// First, attempt to delete the file using POSIX semantics
// (which permit a file to be deleted while it is still open).
// This matches the behavior of DeleteFileW.
+ //
+ // The following call uses features available on different Windows versions:
+ // - FILE_DISPOSITION_INFORMATION_EX: Windows 10, version 1607 (aka RS1)
+ // - FILE_DISPOSITION_POSIX_SEMANTICS: Windows 10, version 1607 (aka RS1)
+ // - FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE: Windows 10, version 1809 (aka RS5)
+ //
+ // Also, some file systems, like FAT32, don't support POSIX semantics.
err = NtSetInformationFile(
h,
&IO_STATUS_BLOCK{},
switch err {
case nil:
return nil
- case STATUS_CANNOT_DELETE, STATUS_DIRECTORY_NOT_EMPTY:
+ case STATUS_INVALID_INFO_CLASS, // the operating system doesn't support FileDispositionInformationEx
+ STATUS_INVALID_PARAMETER, // the operating system doesn't support one of the flags
+ STATUS_NOT_SUPPORTED: // the file system doesn't support FILE_DISPOSITION_INFORMATION_EX or one of the flags
+ return deleteatFallback(h)
+ default:
return err.(NTStatus).Errno()
}
+}
- // If the prior deletion failed, the filesystem either doesn't support
- // POSIX semantics (for example, FAT), or hasn't implemented
- // FILE_DISPOSITION_INFORMATION_EX.
- //
- // Try again.
- err = NtSetInformationFile(
+// TestDeleteatFallback should only be used for testing purposes.
+// When set, [Deleteat] uses the fallback path unconditionally.
+var TestDeleteatFallback bool
+
+// deleteatFallback is a deleteat implementation that strives
+// for compatibility with older Windows versions and file systems
+// over performance.
+func deleteatFallback(h syscall.Handle) error {
+ var data syscall.ByHandleFileInformation
+ if err := syscall.GetFileInformationByHandle(h, &data); err == nil && data.FileAttributes&syscall.FILE_ATTRIBUTE_READONLY != 0 {
+ // Remove read-only attribute. Reopen the file, as it was previously open without FILE_WRITE_ATTRIBUTES access
+ // in order to maximize compatibility in the happy path.
+ wh, err := ReOpenFile(h,
+ FILE_WRITE_ATTRIBUTES,
+ FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
+ syscall.FILE_FLAG_OPEN_REPARSE_POINT|syscall.FILE_FLAG_BACKUP_SEMANTICS,
+ )
+ if err != nil {
+ return err
+ }
+ err = SetFileInformationByHandle(
+ wh,
+ FileBasicInfo,
+ unsafe.Pointer(&FILE_BASIC_INFO{
+ FileAttributes: data.FileAttributes &^ FILE_ATTRIBUTE_READONLY,
+ }),
+ uint32(unsafe.Sizeof(FILE_BASIC_INFO{})),
+ )
+ syscall.CloseHandle(wh)
+ if err != nil {
+ return err
+ }
+ }
+
+ return SetFileInformationByHandle(
h,
- &IO_STATUS_BLOCK{},
- unsafe.Pointer(&FILE_DISPOSITION_INFORMATION{
+ FileDispositionInfo,
+ unsafe.Pointer(&FILE_DISPOSITION_INFO{
DeleteFile: true,
}),
- uint32(unsafe.Sizeof(FILE_DISPOSITION_INFORMATION{})),
- FileDispositionInformation,
+ uint32(unsafe.Sizeof(FILE_DISPOSITION_INFO{})),
)
- if st, ok := err.(NTStatus); ok {
- return st.Errno()
- }
- return err
}
func Renameat(olddirfd syscall.Handle, oldpath string, newdirfd syscall.Handle, newpath string) error {
//sys GetOverlappedResult(handle syscall.Handle, overlapped *syscall.Overlapped, done *uint32, wait bool) (err error)
//sys CreateNamedPipe(name *uint16, flags uint32, pipeMode uint32, maxInstances uint32, outSize uint32, inSize uint32, defaultTimeout uint32, sa *syscall.SecurityAttributes) (handle syscall.Handle, err error) [failretval==syscall.InvalidHandle] = CreateNamedPipeW
+//sys ReOpenFile(filehandle syscall.Handle, desiredAccess uint32, shareMode uint32, flagAndAttributes uint32) (handle syscall.Handle, err error)
+
// NTStatus corresponds with NTSTATUS, error values returned by ntdll.dll and
// other native functions.
type NTStatus uint32
STATUS_NOT_A_DIRECTORY NTStatus = 0xC0000103
STATUS_CANNOT_DELETE NTStatus = 0xC0000121
STATUS_REPARSE_POINT_ENCOUNTERED NTStatus = 0xC000050B
+ STATUS_NOT_SUPPORTED NTStatus = 0xC00000BB
+ STATUS_INVALID_PARAMETER NTStatus = 0xC000000D
+ STATUS_INVALID_INFO_CLASS NTStatus = 0xC0000003
)
const (
procModule32NextW = modkernel32.NewProc("Module32NextW")
procMoveFileExW = modkernel32.NewProc("MoveFileExW")
procMultiByteToWideChar = modkernel32.NewProc("MultiByteToWideChar")
+ procReOpenFile = modkernel32.NewProc("ReOpenFile")
procRtlLookupFunctionEntry = modkernel32.NewProc("RtlLookupFunctionEntry")
procRtlVirtualUnwind = modkernel32.NewProc("RtlVirtualUnwind")
procSetFileInformationByHandle = modkernel32.NewProc("SetFileInformationByHandle")
return
}
+func ReOpenFile(filehandle syscall.Handle, desiredAccess uint32, shareMode uint32, flagAndAttributes uint32) (handle syscall.Handle, err error) {
+ r0, _, e1 := syscall.Syscall6(procReOpenFile.Addr(), 4, uintptr(filehandle), uintptr(desiredAccess), uintptr(shareMode), uintptr(flagAndAttributes), 0, 0)
+ handle = syscall.Handle(r0)
+ if handle == 0 {
+ err = errnoErr(e1)
+ }
+ return
+}
+
func RtlLookupFunctionEntry(pc uintptr, baseAddress *uintptr, table unsafe.Pointer) (ret *RUNTIME_FUNCTION) {
r0, _, _ := syscall.Syscall(procRtlLookupFunctionEntry.Addr(), 3, uintptr(pc), uintptr(unsafe.Pointer(baseAddress)), uintptr(table))
ret = (*RUNTIME_FUNCTION)(unsafe.Pointer(r0))