github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/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  	if !file.dirinfo.isempty && file.pfd.Sysfd == syscall.InvalidHandle {
    21  		return nil, syscall.EINVAL
    22  	}
    23  	wantAll := n <= 0
    24  	size := n
    25  	if wantAll {
    26  		n = -1
    27  		size = 100
    28  	}
    29  	fi = make([]FileInfo, 0, size) // Empty with room to grow.
    30  	d := &file.dirinfo.data
    31  	for n != 0 && !file.dirinfo.isempty {
    32  		if file.dirinfo.needdata {
    33  			e := file.pfd.FindNextFile(d)
    34  			runtime.KeepAlive(file)
    35  			if e != nil {
    36  				if e == syscall.ERROR_NO_MORE_FILES {
    37  					break
    38  				} else {
    39  					err = &PathError{"FindNextFile", file.name, e}
    40  					if !wantAll {
    41  						fi = nil
    42  					}
    43  					return
    44  				}
    45  			}
    46  		}
    47  		file.dirinfo.needdata = true
    48  		name := syscall.UTF16ToString(d.FileName[0:])
    49  		if name == "." || name == ".." { // Useless names
    50  			continue
    51  		}
    52  		f := &fileStat{
    53  			name: name,
    54  			sys: syscall.Win32FileAttributeData{
    55  				FileAttributes: d.FileAttributes,
    56  				CreationTime:   d.CreationTime,
    57  				LastAccessTime: d.LastAccessTime,
    58  				LastWriteTime:  d.LastWriteTime,
    59  				FileSizeHigh:   d.FileSizeHigh,
    60  				FileSizeLow:    d.FileSizeLow,
    61  			},
    62  			path: file.dirinfo.path + `\` + name,
    63  		}
    64  		n--
    65  		fi = append(fi, f)
    66  	}
    67  	if !wantAll && len(fi) == 0 {
    68  		return fi, io.EOF
    69  	}
    70  	return fi, nil
    71  }
    72  
    73  func (file *File) readdirnames(n int) (names []string, err error) {
    74  	fis, err := file.Readdir(n)
    75  	names = make([]string, len(fis))
    76  	for i, fi := range fis {
    77  		names[i] = fi.Name()
    78  	}
    79  	return names, err
    80  }