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

View as plain text