// Stat returns a FileInfo structure describing the named file. // If there is an error, it will be of type *PathError. func Stat(name string) (fi FileInfo, err error) { if len(name) == 0 { return nil, &PathError{"Stat", name, syscall.Errno(syscall.ERROR_PATH_NOT_FOUND)} } if name == DevNull { return statDevNull() } var d syscall.Win32FileAttributeData e := syscall.GetFileAttributesEx(syscall.StringToUTF16Ptr(name), syscall.GetFileExInfoStandard, (*byte)(unsafe.Pointer(&d))) if e != nil { return nil, &PathError{"GetFileAttributesEx", name, e} } path := name if !isAbs(path) { cwd, _ := Getwd() path = cwd + `\` + path } return &fileStat{ name: basename(name), size: mkSize(d.FileSizeHigh, d.FileSizeLow), modTime: mkModTime(d.LastWriteTime), mode: mkMode(d.FileAttributes), sys: mkSys(path, d.LastAccessTime, d.CreationTime), }, nil }
// Lstat returns the FileInfo structure describing the named file. // If the file is a symbolic link, the returned FileInfo // describes the symbolic link. Lstat makes no attempt to follow the link. // If there is an error, it will be of type *PathError. func Lstat(name string) (FileInfo, error) { if len(name) == 0 { return nil, &PathError{"Lstat", name, syscall.Errno(syscall.ERROR_PATH_NOT_FOUND)} } if name == DevNull { return &devNullStat, nil } fs := &fileStat{name: basename(name)} namep, e := syscall.UTF16PtrFromString(name) if e != nil { return nil, &PathError{"Lstat", name, e} } e = syscall.GetFileAttributesEx(namep, syscall.GetFileExInfoStandard, (*byte)(unsafe.Pointer(&fs.sys))) if e != nil { return nil, &PathError{"GetFileAttributesEx", name, e} } fs.path = name if !isAbs(fs.path) { fs.path, e = syscall.FullPath(fs.path) if e != nil { return nil, e } } return fs, nil }
// Stat returns a FileInfo structure describing the named file and an error, if any. // If name names a valid symbolic link, the returned FileInfo describes // the file pointed at by the link and has fi.FollowedSymlink set to true. // If name names an invalid symbolic link, the returned FileInfo describes // the link itself and has fi.FollowedSymlink set to false. func Stat(name string) (fi FileInfo, err error) { if len(name) == 0 { return nil, &PathError{"Stat", name, syscall.Errno(syscall.ERROR_PATH_NOT_FOUND)} } var d syscall.Win32FileAttributeData e := syscall.GetFileAttributesEx(syscall.StringToUTF16Ptr(name), syscall.GetFileExInfoStandard, (*byte)(unsafe.Pointer(&d))) if e != nil { return nil, &PathError{"GetFileAttributesEx", name, e} } return toFileInfo(basename(name), d.FileAttributes, d.FileSizeHigh, d.FileSizeLow, d.CreationTime, d.LastAccessTime, d.LastWriteTime), nil }
func IsSymlink(path string) (bool, error) { var fa syscall.Win32FileAttributeData namep, err := syscall.UTF16PtrFromString(path) if err != nil { return false, errors.Trace(err) } err = syscall.GetFileAttributesEx(namep, syscall.GetFileExInfoStandard, (*byte)(unsafe.Pointer(&fa))) if err != nil { return false, errors.Trace(err) } return fa.FileAttributes&FILE_ATTRIBUTE_REPARSE_POINT != 0, nil }
func openDir(name string) (file *File, err error) { var mask string path := fixLongPath(name) if len(path) == 2 && path[1] == ':' || (len(path) > 0 && path[len(path)-1] == '\\') { // it is a drive letter, like C: mask = path + `*` } else { mask = path + `\*` } maskp, e := syscall.UTF16PtrFromString(mask) if e != nil { return nil, e } d := new(dirInfo) r, e := syscall.FindFirstFile(maskp, &d.data) if e != nil { // FindFirstFile returns ERROR_FILE_NOT_FOUND when // no matching files can be found. Then, if directory // exists, we should proceed. if e != syscall.ERROR_FILE_NOT_FOUND { return nil, e } var fa syscall.Win32FileAttributeData pathp, e := syscall.UTF16PtrFromString(path) if e != nil { return nil, e } e = syscall.GetFileAttributesEx(pathp, syscall.GetFileExInfoStandard, (*byte)(unsafe.Pointer(&fa))) if e != nil { return nil, e } if fa.FileAttributes&syscall.FILE_ATTRIBUTE_DIRECTORY == 0 { return nil, e } d.isempty = true } d.path = path if !isAbs(d.path) { d.path, e = syscall.FullPath(d.path) if e != nil { return nil, e } } f := newFile(r, name) f.dirinfo = d return f, nil }
// Lstat returns the FileInfo structure describing the named file. // If the file is a symbolic link, the returned FileInfo // describes the symbolic link. Lstat makes no attempt to follow the link. // If there is an error, it will be of type *PathError. func Lstat(name string) (FileInfo, error) { if len(name) == 0 { return nil, &PathError{"Lstat", name, syscall.Errno(syscall.ERROR_PATH_NOT_FOUND)} } if name == DevNull { return &devNullStat, nil } fs := &fileStat{name: basename(name)} namep, e := syscall.UTF16PtrFromString(fixLongPath(name)) if e != nil { return nil, &PathError{"Lstat", name, e} } e = syscall.GetFileAttributesEx(namep, syscall.GetFileExInfoStandard, (*byte)(unsafe.Pointer(&fs.sys))) if e != nil { if e != windows.ERROR_SHARING_VIOLATION { return nil, &PathError{"GetFileAttributesEx", name, e} } // try FindFirstFile now that GetFileAttributesEx failed var fd syscall.Win32finddata h, e2 := syscall.FindFirstFile(namep, &fd) if e2 != nil { return nil, &PathError{"FindFirstFile", name, e} } syscall.FindClose(h) fs.sys.FileAttributes = fd.FileAttributes fs.sys.CreationTime = fd.CreationTime fs.sys.LastAccessTime = fd.LastAccessTime fs.sys.LastWriteTime = fd.LastWriteTime fs.sys.FileSizeHigh = fd.FileSizeHigh fs.sys.FileSizeLow = fd.FileSizeLow } fs.path = name if !isAbs(fs.path) { fs.path, e = syscall.FullPath(fs.path) if e != nil { return nil, e } } return fs, nil }
func openDir(name string) (file *File, err error) { maskp, e := syscall.UTF16PtrFromString(name + `\*`) if e != nil { return nil, e } d := new(dirInfo) r, e := syscall.FindFirstFile(maskp, &d.data) if e != nil { // FindFirstFile returns ERROR_FILE_NOT_FOUND when // no matching files can be found. Then, if directory // exists, we should proceed. if e != syscall.ERROR_FILE_NOT_FOUND { return nil, e } var fa syscall.Win32FileAttributeData namep, e := syscall.UTF16PtrFromString(name) if e != nil { return nil, e } e = syscall.GetFileAttributesEx(namep, syscall.GetFileExInfoStandard, (*byte)(unsafe.Pointer(&fa))) if e != nil { return nil, e } if fa.FileAttributes&syscall.FILE_ATTRIBUTE_DIRECTORY == 0 { return nil, e } d.isempty = true } d.path = name if !isAbs(d.path) { d.path, e = syscall.FullPath(d.path) if e != nil { return nil, e } } f := newFile(r, name) f.dirinfo = d return f, nil }