fileinfo.go 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. package winio
  2. import (
  3. "os"
  4. "syscall"
  5. "unsafe"
  6. )
  7. //sys getFileInformationByHandleEx(h syscall.Handle, class uint32, buffer *byte, size uint32) (err error) = GetFileInformationByHandleEx
  8. //sys setFileInformationByHandle(h syscall.Handle, class uint32, buffer *byte, size uint32) (err error) = SetFileInformationByHandle
  9. type FileBasicInfo struct {
  10. CreationTime, LastAccessTime, LastWriteTime, ChangeTime syscall.Filetime
  11. FileAttributes uintptr // includes padding
  12. }
  13. func GetFileBasicInfo(f *os.File) (*FileBasicInfo, error) {
  14. bi := &FileBasicInfo{}
  15. if err := getFileInformationByHandleEx(syscall.Handle(f.Fd()), 0, (*byte)(unsafe.Pointer(bi)), uint32(unsafe.Sizeof(*bi))); err != nil {
  16. return nil, &os.PathError{"GetFileInformationByHandleEx", f.Name(), err}
  17. }
  18. return bi, nil
  19. }
  20. func SetFileBasicInfo(f *os.File, bi *FileBasicInfo) error {
  21. if err := setFileInformationByHandle(syscall.Handle(f.Fd()), 0, (*byte)(unsafe.Pointer(bi)), uint32(unsafe.Sizeof(*bi))); err != nil {
  22. return &os.PathError{"SetFileInformationByHandle", f.Name(), err}
  23. }
  24. return nil
  25. }