chtimes_windows.go 712 B

123456789101112131415161718192021222324252627
  1. // +build windows
  2. package system
  3. import (
  4. "syscall"
  5. "time"
  6. )
  7. //setCTime will set the create time on a file. On Windows, this requires
  8. //calling SetFileTime and explicitly including the create time.
  9. func setCTime(path string, ctime time.Time) error {
  10. ctimespec := syscall.NsecToTimespec(ctime.UnixNano())
  11. pathp, e := syscall.UTF16PtrFromString(path)
  12. if e != nil {
  13. return e
  14. }
  15. h, e := syscall.CreateFile(pathp,
  16. syscall.FILE_WRITE_ATTRIBUTES, syscall.FILE_SHARE_WRITE, nil,
  17. syscall.OPEN_EXISTING, syscall.FILE_FLAG_BACKUP_SEMANTICS, 0)
  18. if e != nil {
  19. return e
  20. }
  21. defer syscall.Close(h)
  22. c := syscall.NsecToFiletime(syscall.TimespecToNsec(ctimespec))
  23. return syscall.SetFileTime(h, &c, nil, nil)
  24. }