-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compare low-level windows eof and check temporary flag
- Loading branch information
1 parent
d4b22ea
commit eb11c3f
Showing
3 changed files
with
92 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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,69 @@ | ||
//go:build windows | ||
|
||
package sftp | ||
|
||
import ( | ||
"os" | ||
"unsafe" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
// FileStandardInfo contains extended information for the file. | ||
// FILE_STANDARD_INFO in WinBase.h | ||
// https://docs.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-file_standard_info | ||
type FileStandardInfo struct { | ||
AllocationSize, EndOfFile int64 | ||
NumberOfLinks uint32 | ||
DeletePending, Directory bool | ||
} | ||
|
||
type FileAttributeTagInfo struct { | ||
FileAttributes uint32 | ||
ReparseTag uint32 | ||
} | ||
|
||
func isTemporary(path string) (bool, error) { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return true, err | ||
} | ||
defer file.Close() | ||
|
||
at := &FileAttributeTagInfo{} | ||
err = windows.GetFileInformationByHandleEx(windows.Handle(file.Fd()), windows.FileAttributeTagInfo, (*byte)(unsafe.Pointer(at)), uint32(unsafe.Sizeof(*at))) | ||
if err != nil { | ||
return true, err | ||
} | ||
|
||
if at.FileAttributes&windows.FILE_ATTRIBUTE_TEMPORARY == windows.FILE_ATTRIBUTE_TEMPORARY { | ||
return true, nil | ||
} | ||
|
||
return false, nil | ||
} | ||
|
||
func inconsistentSize(path string) (bool, error) { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return true, err | ||
} | ||
defer file.Close() | ||
|
||
si := &FileStandardInfo{} | ||
err = windows.GetFileInformationByHandleEx(windows.Handle(file.Fd()), windows.FileStandardInfo, (*byte)(unsafe.Pointer(si)), uint32(unsafe.Sizeof(*si))) | ||
if err != nil { | ||
return true, err | ||
} | ||
|
||
stat, err := os.Lstat(path) | ||
if err != nil { | ||
return true, err | ||
} | ||
|
||
if si.EndOfFile == stat.Size() { | ||
return false, nil | ||
} | ||
|
||
return true, nil | ||
} |