Source file src/os/dir_unix.go

     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  //go:build aix || dragonfly || freebsd || (js && wasm) || wasip1 || linux || netbsd || openbsd || solaris
     6  
     7  package os
     8  
     9  import (
    10  	"internal/goarch"
    11  	"io"
    12  	"runtime"
    13  	"sync"
    14  	"syscall"
    15  	"unsafe"
    16  )
    17  
    18  // Auxiliary information if the File describes a directory
    19  type dirInfo struct {
    20  	mu   sync.Mutex
    21  	buf  *[]byte // buffer for directory I/O
    22  	nbuf int     // length of buf; return value from Getdirentries
    23  	bufp int     // location of next record in buf.
    24  }
    25  
    26  const (
    27  	// More than 5760 to work around https://golang.org/issue/24015.
    28  	blockSize = 8192
    29  )
    30  
    31  var dirBufPool = sync.Pool{
    32  	New: func() any {
    33  		// The buffer must be at least a block long.
    34  		buf := make([]byte, blockSize)
    35  		return &buf
    36  	},
    37  }
    38  
    39  func (d *dirInfo) close() {
    40  	if d.buf != nil {
    41  		dirBufPool.Put(d.buf)
    42  		d.buf = nil
    43  	}
    44  }
    45  
    46  func (f *File) readdir(n int, mode readdirMode) (names []string, dirents []DirEntry, infos []FileInfo, err error) {
    47  	// If this file has no dirInfo, create one.
    48  	d := f.dirinfo.Load()
    49  	if d == nil {
    50  		d = new(dirInfo)
    51  		f.dirinfo.Store(d)
    52  	}
    53  	d.mu.Lock()
    54  	defer d.mu.Unlock()
    55  	if d.buf == nil {
    56  		d.buf = dirBufPool.Get().(*[]byte)
    57  	}
    58  
    59  	// Change the meaning of n for the implementation below.
    60  	//
    61  	// The n above was for the public interface of "if n <= 0,
    62  	// Readdir returns all the FileInfo from the directory in a
    63  	// single slice".
    64  	//
    65  	// But below, we use only negative to mean looping until the
    66  	// end and positive to mean bounded, with positive
    67  	// terminating at 0.
    68  	if n == 0 {
    69  		n = -1
    70  	}
    71  
    72  	for n != 0 {
    73  		// Refill the buffer if necessary
    74  		if d.bufp >= d.nbuf {
    75  			d.bufp = 0
    76  			var errno error
    77  			d.nbuf, errno = f.pfd.ReadDirent(*d.buf)
    78  			runtime.KeepAlive(f)
    79  			if errno != nil {
    80  				return names, dirents, infos, &PathError{Op: "readdirent", Path: f.name, Err: errno}
    81  			}
    82  			if d.nbuf <= 0 {
    83  				// Optimization: we can return the buffer to the pool, there is nothing else to read.
    84  				dirBufPool.Put(d.buf)
    85  				d.buf = nil
    86  				break // EOF
    87  			}
    88  		}
    89  
    90  		// Drain the buffer
    91  		buf := (*d.buf)[d.bufp:d.nbuf]
    92  		reclen, ok := direntReclen(buf)
    93  		if !ok || reclen > uint64(len(buf)) {
    94  			break
    95  		}
    96  		rec := buf[:reclen]
    97  		d.bufp += int(reclen)
    98  		ino, ok := direntIno(rec)
    99  		if !ok {
   100  			break
   101  		}
   102  		// When building to wasip1, the host runtime might be running on Windows
   103  		// or might expose a remote file system which does not have the concept
   104  		// of inodes. Therefore, we cannot make the assumption that it is safe
   105  		// to skip entries with zero inodes.
   106  		if ino == 0 && runtime.GOOS != "wasip1" {
   107  			continue
   108  		}
   109  		const namoff = uint64(unsafe.Offsetof(syscall.Dirent{}.Name))
   110  		namlen, ok := direntNamlen(rec)
   111  		if !ok || namoff+namlen > uint64(len(rec)) {
   112  			break
   113  		}
   114  		name := rec[namoff : namoff+namlen]
   115  		for i, c := range name {
   116  			if c == 0 {
   117  				name = name[:i]
   118  				break
   119  			}
   120  		}
   121  		// Check for useless names before allocating a string.
   122  		if string(name) == "." || string(name) == ".." {
   123  			continue
   124  		}
   125  		if n > 0 { // see 'n == 0' comment above
   126  			n--
   127  		}
   128  		if mode == readdirName {
   129  			names = append(names, string(name))
   130  		} else if mode == readdirDirEntry {
   131  			de, err := newUnixDirent(f.name, string(name), direntType(rec))
   132  			if IsNotExist(err) {
   133  				// File disappeared between readdir and stat.
   134  				// Treat as if it didn't exist.
   135  				continue
   136  			}
   137  			if err != nil {
   138  				return nil, dirents, nil, err
   139  			}
   140  			dirents = append(dirents, de)
   141  		} else {
   142  			info, err := lstat(f.name + "/" + string(name))
   143  			if IsNotExist(err) {
   144  				// File disappeared between readdir + stat.
   145  				// Treat as if it didn't exist.
   146  				continue
   147  			}
   148  			if err != nil {
   149  				return nil, nil, infos, err
   150  			}
   151  			infos = append(infos, info)
   152  		}
   153  	}
   154  
   155  	if n > 0 && len(names)+len(dirents)+len(infos) == 0 {
   156  		return nil, nil, nil, io.EOF
   157  	}
   158  	return names, dirents, infos, nil
   159  }
   160  
   161  // readInt returns the size-bytes unsigned integer in native byte order at offset off.
   162  func readInt(b []byte, off, size uintptr) (u uint64, ok bool) {
   163  	if len(b) < int(off+size) {
   164  		return 0, false
   165  	}
   166  	if goarch.BigEndian {
   167  		return readIntBE(b[off:], size), true
   168  	}
   169  	return readIntLE(b[off:], size), true
   170  }
   171  
   172  func readIntBE(b []byte, size uintptr) uint64 {
   173  	switch size {
   174  	case 1:
   175  		return uint64(b[0])
   176  	case 2:
   177  		_ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
   178  		return uint64(b[1]) | uint64(b[0])<<8
   179  	case 4:
   180  		_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
   181  		return uint64(b[3]) | uint64(b[2])<<8 | uint64(b[1])<<16 | uint64(b[0])<<24
   182  	case 8:
   183  		_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
   184  		return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
   185  			uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
   186  	default:
   187  		panic("syscall: readInt with unsupported size")
   188  	}
   189  }
   190  
   191  func readIntLE(b []byte, size uintptr) uint64 {
   192  	switch size {
   193  	case 1:
   194  		return uint64(b[0])
   195  	case 2:
   196  		_ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
   197  		return uint64(b[0]) | uint64(b[1])<<8
   198  	case 4:
   199  		_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
   200  		return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24
   201  	case 8:
   202  		_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
   203  		return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
   204  			uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
   205  	default:
   206  		panic("syscall: readInt with unsupported size")
   207  	}
   208  }
   209  

View as plain text