...
Source file
src/syscall/dirent.go
Documentation: syscall
1
2
3
4
5
6
7 package syscall
8
9 import (
10 "runtime"
11 "unsafe"
12 )
13
14
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]
31 return uint64(b[1]) | uint64(b[0])<<8
32 case 4:
33 _ = b[3]
34 return uint64(b[3]) | uint64(b[2])<<8 | uint64(b[1])<<16 | uint64(b[0])<<24
35 case 8:
36 _ = b[7]
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]
50 return uint64(b[0]) | uint64(b[1])<<8
51 case 4:
52 _ = b[3]
53 return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24
54 case 8:
55 _ = b[7]
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
64
65
66
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
82
83 if ino == 0 && runtime.GOOS != "wasip1" {
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
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