| |
| |
| |
|
|
| package os |
|
|
| import ( |
| "internal/syscall/windows" |
| "io" |
| "io/fs" |
| "runtime" |
| "sync" |
| "syscall" |
| "unsafe" |
| ) |
|
|
| |
| type dirInfo struct { |
| mu sync.Mutex |
| |
| |
| |
| buf *[]byte |
| bufp int |
| h syscall.Handle |
| vol uint32 |
| class uint32 |
| path string |
| } |
|
|
| const ( |
| |
| |
| |
| |
| |
| |
| dirBufSize = 64 * 1024 |
| ) |
|
|
| var dirBufPool = sync.Pool{ |
| New: func() any { |
| |
| buf := make([]byte, dirBufSize) |
| return &buf |
| }, |
| } |
|
|
| func (d *dirInfo) close() { |
| d.h = 0 |
| if d.buf != nil { |
| dirBufPool.Put(d.buf) |
| d.buf = nil |
| } |
| } |
|
|
| |
| |
| |
| var allowReadDirFileID = true |
|
|
| func (d *dirInfo) init(h syscall.Handle) { |
| d.h = h |
| d.class = windows.FileFullDirectoryRestartInfo |
| |
| |
|
|
| |
| |
| |
| |
| var flags uint32 |
| err := windows.GetVolumeInformationByHandle(h, nil, 0, &d.vol, nil, &flags, nil, 0) |
| if err != nil { |
| d.vol = 0 |
| |
| |
| return |
| } |
| if flags&windows.FILE_SUPPORTS_OBJECT_IDS == 0 { |
| |
| return |
| } |
| if allowReadDirFileID && flags&windows.FILE_SUPPORTS_OPEN_BY_FILE_ID != 0 { |
| |
| |
| d.class = windows.FileIdBothDirectoryRestartInfo |
| } else { |
| |
| |
| |
| d.path, _ = windows.FinalPath(h, windows.FILE_NAME_OPENED) |
| } |
| } |
|
|
| func (file *File) readdir(n int, mode readdirMode) (names []string, dirents []DirEntry, infos []FileInfo, err error) { |
| |
| var d *dirInfo |
| for { |
| d = file.dirinfo.Load() |
| if d != nil { |
| break |
| } |
| d = new(dirInfo) |
| d.init(file.pfd.Sysfd) |
| if file.dirinfo.CompareAndSwap(nil, d) { |
| break |
| } |
| |
| d.close() |
| } |
| d.mu.Lock() |
| defer d.mu.Unlock() |
| if d.buf == nil { |
| d.buf = dirBufPool.Get().(*[]byte) |
| } |
|
|
| wantAll := n <= 0 |
| if wantAll { |
| n = -1 |
| } |
| for n != 0 { |
| |
| if d.bufp == 0 { |
| err = windows.GetFileInformationByHandleEx(file.pfd.Sysfd, d.class, (*byte)(unsafe.Pointer(&(*d.buf)[0])), uint32(len(*d.buf))) |
| runtime.KeepAlive(file) |
| if err != nil { |
| if err == syscall.ERROR_NO_MORE_FILES { |
| |
| dirBufPool.Put(d.buf) |
| d.buf = nil |
| break |
| } |
| if err == syscall.ERROR_FILE_NOT_FOUND && |
| (d.class == windows.FileIdBothDirectoryRestartInfo || d.class == windows.FileFullDirectoryRestartInfo) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| break |
| } |
| if s, _ := file.Stat(); s != nil && !s.IsDir() { |
| err = &PathError{Op: "readdir", Path: file.name, Err: syscall.ENOTDIR} |
| } else { |
| err = &PathError{Op: "GetFileInformationByHandleEx", Path: file.name, Err: err} |
| } |
| return |
| } |
| if d.class == windows.FileIdBothDirectoryRestartInfo { |
| d.class = windows.FileIdBothDirectoryInfo |
| } else if d.class == windows.FileFullDirectoryRestartInfo { |
| d.class = windows.FileFullDirectoryInfo |
| } |
| } |
| |
| var islast bool |
| for n != 0 && !islast { |
| var nextEntryOffset uint32 |
| var nameslice []uint16 |
| entry := unsafe.Pointer(&(*d.buf)[d.bufp]) |
| if d.class == windows.FileIdBothDirectoryInfo { |
| info := (*windows.FILE_ID_BOTH_DIR_INFO)(entry) |
| nextEntryOffset = info.NextEntryOffset |
| nameslice = unsafe.Slice(&info.FileName[0], info.FileNameLength/2) |
| } else { |
| info := (*windows.FILE_FULL_DIR_INFO)(entry) |
| nextEntryOffset = info.NextEntryOffset |
| nameslice = unsafe.Slice(&info.FileName[0], info.FileNameLength/2) |
| } |
| d.bufp += int(nextEntryOffset) |
| islast = nextEntryOffset == 0 |
| if islast { |
| d.bufp = 0 |
| } |
| if (len(nameslice) == 1 && nameslice[0] == '.') || |
| (len(nameslice) == 2 && nameslice[0] == '.' && nameslice[1] == '.') { |
| |
| continue |
| } |
| name := syscall.UTF16ToString(nameslice) |
| if mode == readdirName { |
| names = append(names, name) |
| } else { |
| var f *fileStat |
| if d.class == windows.FileIdBothDirectoryInfo { |
| f = newFileStatFromFileIDBothDirInfo((*windows.FILE_ID_BOTH_DIR_INFO)(entry)) |
| } else { |
| f = newFileStatFromFileFullDirInfo((*windows.FILE_FULL_DIR_INFO)(entry)) |
| if d.path != "" { |
| |
| |
| |
| f.appendNameToPath = true |
| f.path = d.path |
| } |
| } |
| f.name = name |
| f.vol = d.vol |
| if mode == readdirDirEntry { |
| dirents = append(dirents, dirEntry{f}) |
| } else { |
| infos = append(infos, f) |
| } |
| } |
| n-- |
| } |
| } |
| if !wantAll && len(names)+len(dirents)+len(infos) == 0 { |
| return nil, nil, nil, io.EOF |
| } |
| return names, dirents, infos, nil |
| } |
|
|
| type dirEntry struct { |
| fs *fileStat |
| } |
|
|
| func (de dirEntry) Name() string { return de.fs.Name() } |
| func (de dirEntry) IsDir() bool { return de.fs.IsDir() } |
| func (de dirEntry) Type() FileMode { return de.fs.Mode().Type() } |
| func (de dirEntry) Info() (FileInfo, error) { return de.fs, nil } |
|
|
| func (de dirEntry) String() string { |
| return fs.FormatDirEntry(de) |
| } |
|
|