func openDir(name string) (file *File, err error) { d := new(dirInfo) r, e := syscall.FindFirstFile(syscall.StringToUTF16Ptr(name+`\*`), &d.data) if e != nil { return nil, &PathError{"open", name, e} } f := NewFile(r, name) f.dirinfo = d return f, nil }
func openDir(name string) (file *File, err Error) { d := new(dirInfo) r, e := syscall.FindFirstFile(syscall.StringToUTF16Ptr(name+"\\*"), &d.stat.Windata) if e != 0 { return nil, &PathError{"open", name, Errno(e)} } f := NewFile(r, name) d.usefirststat = true f.dirinfo = d return f, nil }
func findFirst(pattern string) (*FileInfo, error) { pattern16, err := syscall.UTF16PtrFromString(pattern) if err != nil { return nil, err } this := new(FileInfo) this.handle, err = syscall.FindFirstFile(pattern16, &this.data1) if err != nil { return nil, err } return this, nil }
func openDir(name string) (file *File, err error) { d := new(dirInfo) r, e := syscall.FindFirstFile(syscall.StringToUTF16Ptr(name+`\*`), &d.data) if e != nil { return nil, &PathError{"open", name, e} } d.path = name if !isAbs(d.path) { cwd, _ := Getwd() d.path = cwd + `\` + d.path } f := NewFile(uintptr(r), name) f.dirinfo = d return f, 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 }
// normBase retruns the last element of path. func normBase(path string) (string, error) { p, err := syscall.UTF16PtrFromString(path) if err != nil { return "", err } var data syscall.Win32finddata h, err := syscall.FindFirstFile(p, &data) if err != nil { return "", err } syscall.FindClose(h) return syscall.UTF16ToString(data.FileName[:]), 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 }
func TestWin32finddata(t *testing.T) { dir, err := ioutil.TempDir("", "go-build") if err != nil { t.Fatalf("failed to create temp directory: %v", err) } defer os.RemoveAll(dir) path := filepath.Join(dir, "long_name.and_extension") f, err := os.Create(path) if err != nil { t.Fatalf("failed to create %v: %v", path, err) } f.Close() type X struct { fd syscall.Win32finddata got byte pad [10]byte // to protect ourselves } var want byte = 2 // it is unlikely to have this character in the filename x := X{got: want} pathp, _ := syscall.UTF16PtrFromString(path) h, err := syscall.FindFirstFile(pathp, &(x.fd)) if err != nil { t.Fatalf("FindFirstFile failed: %v", err) } err = syscall.FindClose(h) if err != nil { t.Fatalf("FindClose failed: %v", err) } if x.got != want { t.Fatalf("memory corruption: want=%d got=%d", want, x.got) } }