| 1 | //go:build aix || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris |
| 2 | |
| 3 | package direntscip-go gomod github.com/charlievieth/fastwalk . `github.com/charlievieth/fastwalk/internal/dirent`/ |
| 4 | |
| 5 | import ( |
| 6 | "encoding/binaryscip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/" |
| 7 | "osscip-go gomod github.com/golang/go/src go1.21 os/" |
| 8 | "runtimescip-go gomod github.com/golang/go/src go1.21 runtime/" |
| 9 | "syscallscip-go gomod github.com/golang/go/src go1.21 syscall/" |
| 10 | "unsafescip-go gomod github.com/golang/go/src go1.21 unsafe/" |
| 11 | ) |
| 12 | |
| 13 | const InvalidModescip-go gomod github.com/charlievieth/fastwalk . `github.com/charlievieth/fastwalk/internal/dirent`/InvalidMode. = osscip-go gomod github.com/golang/go/src go1.21 os/.FileModescip-go gomod github.com/golang/go/src go1.21 os/FileMode#(1<<32 - 1) |
| 14 | |
| 15 | // readInt returns the size-bytes unsigned integer in native byte order at offset off. |
| 16 | func readIntscip-go gomod github.com/charlievieth/fastwalk . `github.com/charlievieth/fastwalk/internal/dirent`/readInt().(b []byte, off, size uintptr) (uint64, bool) { |
| 17 | if len(b) < int(off+size) { |
| 18 | return 0, false |
| 19 | } |
| 20 | b = b[off:] |
| 21 | switch size { |
| 22 | case 1: |
| 23 | return uint64(b[0]), true |
| 24 | case 2: |
| 25 | return uint64(binaryscip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/.NativeEndianscip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/NativeEndian..Uint16scip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/littleEndian#Uint16().(b)), true |
| 26 | case 4: |
| 27 | return uint64(binaryscip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/.NativeEndianscip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/NativeEndian..Uint32scip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/littleEndian#Uint32().(b)), true |
| 28 | case 8: |
| 29 | return uint64(binaryscip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/.NativeEndianscip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/NativeEndian..Uint64scip-go gomod github.com/golang/go/src go1.21 `encoding/binary`/littleEndian#Uint64().(b)), true |
| 30 | default: |
| 31 | // This case is impossible if the tests pass. Previously, we panicked |
| 32 | // here but for performance reasons (escape analysis) it's better to |
| 33 | // trigger a panic via an out-of-bounds reference. |
| 34 | _ = b[len(b)] |
| 35 | return 0, false |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func Parsescip-go gomod github.com/charlievieth/fastwalk . `github.com/charlievieth/fastwalk/internal/dirent`/Parse().(buf []byte) (consumed int, name string, typ osscip-go gomod github.com/golang/go/src go1.21 os/.FileModescip-go gomod github.com/golang/go/src go1.21 os/FileMode#) { |
| 40 | |
| 41 | reclen, ok := direntReclenscip-go gomod github.com/charlievieth/fastwalk . `github.com/charlievieth/fastwalk/internal/dirent`/direntReclen().(buf) |
| 42 | if !ok || reclen > uint64(len(buf)) { |
| 43 | // WARN: this is a hard error because we consumed 0 bytes |
| 44 | // and not stopping here could lead to an infinite loop. |
| 45 | return 0, "", InvalidModescip-go gomod github.com/charlievieth/fastwalk . `github.com/charlievieth/fastwalk/internal/dirent`/InvalidMode. |
| 46 | } |
| 47 | consumed = int(reclen) |
| 48 | rec := buf[:reclen] |
| 49 | |
| 50 | ino, ok := direntInoscip-go gomod github.com/charlievieth/fastwalk . `github.com/charlievieth/fastwalk/internal/dirent`/direntIno().(rec) |
| 51 | if !ok { |
| 52 | return consumed, "", InvalidModescip-go gomod github.com/charlievieth/fastwalk . `github.com/charlievieth/fastwalk/internal/dirent`/InvalidMode. |
| 53 | } |
| 54 | // When building to wasip1, the host runtime might be running on Windows |
| 55 | // or might expose a remote file system which does not have the concept |
| 56 | // of inodes. Therefore, we cannot make the assumption that it is safe |
| 57 | // to skip entries with zero inodes. |
| 58 | if ino == 0 && runtimescip-go gomod github.com/golang/go/src go1.21 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.21 runtime/GOOS. != "wasip1" { |
| 59 | return consumed, "", InvalidModescip-go gomod github.com/charlievieth/fastwalk . `github.com/charlievieth/fastwalk/internal/dirent`/InvalidMode. |
| 60 | } |
| 61 | |
| 62 | typ = direntTypescip-go gomod github.com/charlievieth/fastwalk . `github.com/charlievieth/fastwalk/internal/dirent`/direntType().(buf) |
| 63 | |
| 64 | const namoff = uint64(unsafescip-go gomod github.com/golang/go/src go1.21 unsafe/.Offsetof(syscallscip-go gomod github.com/golang/go/src go1.21 syscall/.Direntscip-go gomod github.com/golang/go/src go1.21 syscall/Dirent#{}.Namescip-go gomod github.com/golang/go/src go1.21 syscall/Dirent#Name.)) |
| 65 | namlen, ok := direntNamlenscip-go gomod github.com/charlievieth/fastwalk . `github.com/charlievieth/fastwalk/internal/dirent`/direntNamlen().(rec) |
| 66 | if !ok || namoff+namlen > uint64(len(rec)) { |
| 67 | return consumed, "", InvalidModescip-go gomod github.com/charlievieth/fastwalk . `github.com/charlievieth/fastwalk/internal/dirent`/InvalidMode. |
| 68 | } |
| 69 | namebuf := rec[namoff : namoff+namlen] |
| 70 | for i, c := range namebuf { |
| 71 | if c == 0 { |
| 72 | namebuf = namebuf[:i] |
| 73 | break |
| 74 | } |
| 75 | } |
| 76 | // Check for useless names before allocating a string. |
| 77 | if string(namebuf) == "." { |
| 78 | name = "." |
| 79 | } else if string(namebuf) == ".." { |
| 80 | name = ".." |
| 81 | } else { |
| 82 | name = string(namebuf) |
| 83 | } |
| 84 | return consumed, name, typ |
| 85 | } |