dir.mx raw

   1  // Copyright 2016 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/fs"
   9  )
  10  
  11  type readdirMode int
  12  
  13  const (
  14  	readdirName readdirMode = iota
  15  	readdirDirEntry
  16  	readdirFileInfo
  17  )
  18  
  19  // Readdir reads the contents of the directory associated with file and
  20  // returns a slice of up to n FileInfo values, as would be returned
  21  // by Lstat, in directory order. Subsequent calls on the same file will yield
  22  // further FileInfos.
  23  //
  24  // If n > 0, Readdir returns at most n FileInfo structures. In this case, if
  25  // Readdir returns an empty slice, it will return a non-nil error
  26  // explaining why. At the end of a directory, the error is io.EOF.
  27  //
  28  // If n <= 0, Readdir returns all the FileInfo from the directory in
  29  // a single slice. In this case, if Readdir succeeds (reads all
  30  // the way to the end of the directory), it returns the slice and a
  31  // nil error. If it encounters an error before the end of the
  32  // directory, Readdir returns the FileInfo read until that point
  33  // and a non-nil error.
  34  //
  35  // Most clients are better served by the more efficient ReadDir method.
  36  func (f *File) Readdir(n int) ([]FileInfo, error) {
  37  	if f == nil {
  38  		return nil, ErrInvalid
  39  	}
  40  	_, _, infos, err := f.readdir(n, readdirFileInfo)
  41  	if infos == nil {
  42  		// Readdir has historically always returned a non-nil empty slice, never nil,
  43  		// even on error (except misuse with nil receiver above).
  44  		// Keep it that way to avoid breaking overly sensitive callers.
  45  		infos = []FileInfo{}
  46  	}
  47  	return infos, err
  48  }
  49  
  50  // Readdirnames reads the contents of the directory associated with file
  51  // and returns a slice of up to n names of files in the directory,
  52  // in directory order. Subsequent calls on the same file will yield
  53  // further names.
  54  //
  55  // If n > 0, Readdirnames returns at most n names. In this case, if
  56  // Readdirnames returns an empty slice, it will return a non-nil error
  57  // explaining why. At the end of a directory, the error is io.EOF.
  58  //
  59  // If n <= 0, Readdirnames returns all the names from the directory in
  60  // a single slice. In this case, if Readdirnames succeeds (reads all
  61  // the way to the end of the directory), it returns the slice and a
  62  // nil error. If it encounters an error before the end of the
  63  // directory, Readdirnames returns the names read until that point and
  64  // a non-nil error.
  65  func (f *File) Readdirnames(n int) (names []string, err error) {
  66  	if f == nil {
  67  		return nil, ErrInvalid
  68  	}
  69  	names, _, _, err = f.readdir(n, readdirName)
  70  	if names == nil {
  71  		// Readdirnames has historically always returned a non-nil empty slice, never nil,
  72  		// even on error (except misuse with nil receiver above).
  73  		// Keep it that way to avoid breaking overly sensitive callers.
  74  		names = []string{}
  75  	}
  76  	return names, err
  77  }
  78  
  79  // A DirEntry is an entry read from a directory
  80  // (using the ReadDir function or a File's ReadDir method).
  81  type DirEntry = fs.DirEntry
  82  
  83  // ReadDir reads the contents of the directory associated with the file f
  84  // and returns a slice of DirEntry values in directory order.
  85  // Subsequent calls on the same file will yield later DirEntry records in the directory.
  86  //
  87  // If n > 0, ReadDir returns at most n DirEntry records.
  88  // In this case, if ReadDir returns an empty slice, it will return an error explaining why.
  89  // At the end of a directory, the error is io.EOF.
  90  //
  91  // If n <= 0, ReadDir returns all the DirEntry records remaining in the directory.
  92  // When it succeeds, it returns a nil error (not io.EOF).
  93  func (f *File) ReadDir(n int) ([]DirEntry, error) {
  94  	if f == nil {
  95  		return nil, ErrInvalid
  96  	}
  97  	_, dirents, _, err := f.readdir(n, readdirDirEntry)
  98  	if dirents == nil {
  99  		// Match Readdir and Readdirnames: don't return nil slices.
 100  		dirents = []DirEntry{}
 101  	}
 102  	return dirents, err
 103  }
 104  
 105  // testingForceReadDirLstat forces ReadDir to call Lstat, for testing that code path.
 106  // This can be difficult to provoke on some Unix systems otherwise.
 107  var testingForceReadDirLstat bool
 108  
 109  // ReadDir reads the named directory,
 110  // returning all its directory entries sorted by filename.
 111  // If an error occurs reading the directory,
 112  // ReadDir returns the entries it was able to read before the error,
 113  // along with the error.
 114  func ReadDir(name string) ([]DirEntry, error) {
 115  	f, err := Open(name)
 116  	if err != nil {
 117  		return nil, err
 118  	}
 119  	defer f.Close()
 120  
 121  	dirs, err := f.ReadDir(-1)
 122  	// Simple insertion sort — no reflect-based sort.Slice needed.
 123  	for i := 1; i < len(dirs); i++ {
 124  		for j := i; j > 0 && dirs[j].Name() < dirs[j-1].Name(); j-- {
 125  			dirs[j], dirs[j-1] = dirs[j-1], dirs[j]
 126  		}
 127  	}
 128  	return dirs, err
 129  }
 130