Source file src/syscall/dirent.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 unix || (js && wasm) || wasip1
     6  
     7  package syscall
     8  
     9  import (
    10  	"internal/goarch"
    11  	"runtime"
    12  	"unsafe"
    13  )
    14  
    15  // readInt returns the size-bytes unsigned integer in native byte order at offset off.
    16  func readInt(b []byte, off, size uintptr) (u uint64, ok bool) {
    17  	if len(b) < int(off+size) {
    18  		return 0, false
    19  	}
    20  	if goarch.BigEndian {
    21  		return readIntBE(b[off:], size), true
    22  	}
    23  	return readIntLE(b[off:], size), true
    24  }
    25  
    26  func readIntBE(b []byte, size uintptr) uint64 {
    27  	switch size {
    28  	case 1:
    29  		return uint64(b[0])
    30  	case 2:
    31  		_ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
    32  		return uint64(b[1]) | uint64(b[0])<<8
    33  	case 4:
    34  		_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
    35  		return uint64(b[3]) | uint64(b[2])<<8 | uint64(b[1])<<16 | uint64(b[0])<<24
    36  	case 8:
    37  		_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
    38  		return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
    39  			uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
    40  	default:
    41  		panic("syscall: readInt with unsupported size")
    42  	}
    43  }
    44  
    45  func readIntLE(b []byte, size uintptr) uint64 {
    46  	switch size {
    47  	case 1:
    48  		return uint64(b[0])
    49  	case 2:
    50  		_ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
    51  		return uint64(b[0]) | uint64(b[1])<<8
    52  	case 4:
    53  		_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
    54  		return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24
    55  	case 8:
    56  		_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
    57  		return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
    58  			uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
    59  	default:
    60  		panic("syscall: readInt with unsupported size")
    61  	}
    62  }
    63  
    64  // ParseDirent parses up to max directory entries in buf,
    65  // appending the names to names. It returns the number of
    66  // bytes consumed from buf, the number of entries added
    67  // to names, and the new names slice.
    68  func ParseDirent(buf []byte, max int, names []string) (consumed int, count int, newnames []string) {
    69  	origlen := len(buf)
    70  	count = 0
    71  	for max != 0 && len(buf) > 0 {
    72  		reclen, ok := direntReclen(buf)
    73  		if !ok || reclen > uint64(len(buf)) {
    74  			return origlen, count, names
    75  		}
    76  		rec := buf[:reclen]
    77  		buf = buf[reclen:]
    78  		ino, ok := direntIno(rec)
    79  		if !ok {
    80  			break
    81  		}
    82  		// See src/os/dir_unix.go for the reason why this condition is
    83  		// excluded on wasip1.
    84  		if ino == 0 && runtime.GOOS != "wasip1" { // File absent in directory.
    85  			continue
    86  		}
    87  		const namoff = uint64(unsafe.Offsetof(Dirent{}.Name))
    88  		namlen, ok := direntNamlen(rec)
    89  		if !ok || namoff+namlen > uint64(len(rec)) {
    90  			break
    91  		}
    92  		name := rec[namoff : namoff+namlen]
    93  		for i, c := range name {
    94  			if c == 0 {
    95  				name = name[:i]
    96  				break
    97  			}
    98  		}
    99  		// Check for useless names before allocating a string.
   100  		if string(name) == "." || string(name) == ".." {
   101  			continue
   102  		}
   103  		max--
   104  		count++
   105  		names = append(names, string(name))
   106  	}
   107  	return origlen - len(buf), count, names
   108  }
   109  

View as plain text