-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for ChangeTime in Windows, adding HasChangeTime() and …
…HasBirthTime() to timespec
- Loading branch information
Showing
5 changed files
with
117 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package times | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
"time" | ||
"unsafe" | ||
) | ||
|
||
type timespecEx struct { | ||
atime | ||
mtime | ||
ctime | ||
btime | ||
} | ||
|
||
// StatFile finds a Windows Timespec with ChangeTime. | ||
func StatFile(file *os.File) (Timespec, error) { | ||
var fileInfo fileBasicInfo | ||
if err := getFileInformationByHandleEx(syscall.Handle(file.Fd()), &fileInfo); err != nil { | ||
return nil, err | ||
} | ||
|
||
var t timespecEx | ||
t.atime.v = time.Unix(0, fileInfo.LastAccessTime.Nanoseconds()) | ||
t.mtime.v = time.Unix(0, fileInfo.LastWriteTime.Nanoseconds()) | ||
t.ctime.v = time.Unix(0, fileInfo.ChangeTime.Nanoseconds()) | ||
t.btime.v = time.Unix(0, fileInfo.CreationTime.Nanoseconds()) | ||
return t, nil | ||
} | ||
|
||
func statEx(file string) (Timespec, error) { | ||
if findProcErr != nil { | ||
// fail fast, don't bother opening the file | ||
return nil, findProcErr | ||
} | ||
|
||
f, err := os.Open(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
return StatFile(f) | ||
} | ||
|
||
var ( | ||
findProcErr error | ||
procGetFileInformationByHandleEx *syscall.Proc | ||
) | ||
|
||
func init() { | ||
var modkernel32 *syscall.DLL | ||
if modkernel32, findProcErr = syscall.LoadDLL("kernel32.dll"); findProcErr == nil { | ||
procGetFileInformationByHandleEx, findProcErr = modkernel32.FindProc("GetFileInformationByHandleEx") | ||
} | ||
} | ||
|
||
// fileBasicInfo holds the C++ data for FileTimes. | ||
// | ||
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364217(v=vs.85).aspx | ||
type fileBasicInfo struct { | ||
CreationTime syscall.Filetime | ||
LastAccessTime syscall.Filetime | ||
LastWriteTime syscall.Filetime | ||
ChangeTime syscall.Filetime | ||
FileAttributes uint32 | ||
_ uint32 // padding | ||
} | ||
|
||
type fileInformationClass int | ||
|
||
const ( | ||
fileBasicInfoClass fileInformationClass = iota | ||
) | ||
|
||
func getFileInformationByHandleEx(handle syscall.Handle, data *fileBasicInfo) (err error) { | ||
if findProcErr != nil { | ||
return findProcErr | ||
} | ||
|
||
r1, _, e1 := syscall.Syscall6(procGetFileInformationByHandleEx.Addr(), 4, uintptr(handle), uintptr(fileBasicInfoClass), uintptr(unsafe.Pointer(data)), unsafe.Sizeof(*data), 0, 0) | ||
if r1 == 0 { | ||
if e1 != 0 { | ||
err = error(e1) | ||
} else { | ||
err = syscall.EINVAL | ||
} | ||
} | ||
return | ||
} |
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