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

View as plain text