github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/os/dir_windows.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package os 6 7 import ( 8 "io" 9 "runtime" 10 "syscall" 11 ) 12 13 func (file *File) readdir(n int) (fi []FileInfo, err error) { 14 if file == nil { 15 return nil, syscall.EINVAL 16 } 17 if !file.isdir() { 18 return nil, &PathError{"Readdir", file.name, syscall.ENOTDIR} 19 } 20 wantAll := n <= 0 21 size := n 22 if wantAll { 23 n = -1 24 size = 100 25 } 26 fi = make([]FileInfo, 0, size) // Empty with room to grow. 27 d := &file.dirinfo.data 28 for n != 0 && !file.dirinfo.isempty { 29 if file.dirinfo.needdata { 30 e := file.pfd.FindNextFile(d) 31 runtime.KeepAlive(file) 32 if e != nil { 33 if e == syscall.ERROR_NO_MORE_FILES { 34 break 35 } else { 36 err = &PathError{"FindNextFile", file.name, e} 37 if !wantAll { 38 fi = nil 39 } 40 return 41 } 42 } 43 } 44 file.dirinfo.needdata = true 45 name := syscall.UTF16ToString(d.FileName[0:]) 46 if name == "." || name == ".." { // Useless names 47 continue 48 } 49 f := newFileStatFromWin32finddata(d) 50 f.name = name 51 f.path = file.dirinfo.path 52 f.appendNameToPath = true 53 n-- 54 fi = append(fi, f) 55 } 56 if !wantAll && len(fi) == 0 { 57 return fi, io.EOF 58 } 59 return fi, nil 60 } 61 62 func (file *File) readdirnames(n int) (names []string, err error) { 63 fis, err := file.Readdir(n) 64 names = make([]string, len(fis)) 65 for i, fi := range fis { 66 names[i] = fi.Name() 67 } 68 return names, err 69 }