...

Source file src/github.com/Microsoft/go-winio/fileinfo.go

Documentation: github.com/Microsoft/go-winio

     1  //go:build windows
     2  // +build windows
     3  
     4  package winio
     5  
     6  import (
     7  	"os"
     8  	"runtime"
     9  	"unsafe"
    10  
    11  	"golang.org/x/sys/windows"
    12  )
    13  
    14  // FileBasicInfo contains file access time and file attributes information.
    15  type FileBasicInfo struct {
    16  	CreationTime, LastAccessTime, LastWriteTime, ChangeTime windows.Filetime
    17  	FileAttributes                                          uint32
    18  	_                                                       uint32 // padding
    19  }
    20  
    21  // GetFileBasicInfo retrieves times and attributes for a file.
    22  func GetFileBasicInfo(f *os.File) (*FileBasicInfo, error) {
    23  	bi := &FileBasicInfo{}
    24  	if err := windows.GetFileInformationByHandleEx(
    25  		windows.Handle(f.Fd()),
    26  		windows.FileBasicInfo,
    27  		(*byte)(unsafe.Pointer(bi)),
    28  		uint32(unsafe.Sizeof(*bi)),
    29  	); err != nil {
    30  		return nil, &os.PathError{Op: "GetFileInformationByHandleEx", Path: f.Name(), Err: err}
    31  	}
    32  	runtime.KeepAlive(f)
    33  	return bi, nil
    34  }
    35  
    36  // SetFileBasicInfo sets times and attributes for a file.
    37  func SetFileBasicInfo(f *os.File, bi *FileBasicInfo) error {
    38  	if err := windows.SetFileInformationByHandle(
    39  		windows.Handle(f.Fd()),
    40  		windows.FileBasicInfo,
    41  		(*byte)(unsafe.Pointer(bi)),
    42  		uint32(unsafe.Sizeof(*bi)),
    43  	); err != nil {
    44  		return &os.PathError{Op: "SetFileInformationByHandle", Path: f.Name(), Err: err}
    45  	}
    46  	runtime.KeepAlive(f)
    47  	return nil
    48  }
    49  
    50  // FileStandardInfo contains extended information for the file.
    51  // FILE_STANDARD_INFO in WinBase.h
    52  // https://docs.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-file_standard_info
    53  type FileStandardInfo struct {
    54  	AllocationSize, EndOfFile int64
    55  	NumberOfLinks             uint32
    56  	DeletePending, Directory  bool
    57  }
    58  
    59  // GetFileStandardInfo retrieves ended information for the file.
    60  func GetFileStandardInfo(f *os.File) (*FileStandardInfo, error) {
    61  	si := &FileStandardInfo{}
    62  	if err := windows.GetFileInformationByHandleEx(windows.Handle(f.Fd()),
    63  		windows.FileStandardInfo,
    64  		(*byte)(unsafe.Pointer(si)),
    65  		uint32(unsafe.Sizeof(*si))); err != nil {
    66  		return nil, &os.PathError{Op: "GetFileInformationByHandleEx", Path: f.Name(), Err: err}
    67  	}
    68  	runtime.KeepAlive(f)
    69  	return si, nil
    70  }
    71  
    72  // FileIDInfo contains the volume serial number and file ID for a file. This pair should be
    73  // unique on a system.
    74  type FileIDInfo struct {
    75  	VolumeSerialNumber uint64
    76  	FileID             [16]byte
    77  }
    78  
    79  // GetFileID retrieves the unique (volume, file ID) pair for a file.
    80  func GetFileID(f *os.File) (*FileIDInfo, error) {
    81  	fileID := &FileIDInfo{}
    82  	if err := windows.GetFileInformationByHandleEx(
    83  		windows.Handle(f.Fd()),
    84  		windows.FileIdInfo,
    85  		(*byte)(unsafe.Pointer(fileID)),
    86  		uint32(unsafe.Sizeof(*fileID)),
    87  	); err != nil {
    88  		return nil, &os.PathError{Op: "GetFileInformationByHandleEx", Path: f.Name(), Err: err}
    89  	}
    90  	runtime.KeepAlive(f)
    91  	return fileID, nil
    92  }
    93  

View as plain text