forked from jpedrosa/sua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
218 lines (202 loc) · 5.7 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import Glibc
import Sua
import CSua
var count = 0
func trackDir(pathBytes: [UInt8]) {
let cap = UnsafePointer<CChar>(pathBytes)
guard let dp = opendir(cap) else {
// Ignore in case permission denies access to the directory.
return
}
defer {
closedir(dp)
}
var entry = readdir(dp)
while entry != nil {
count += 1
if entry!.pointee.d_type == 4 {
var dirName = entry!.pointee.d_name
withUnsafePointer(&dirName) { ptr in
let len = Int(Sys.strlen(sp: UnsafePointer<CChar>(ptr)))
let b = UnsafeBufferPointer<UInt8>(start: UnsafePointer<UInt8>(ptr),
count: len)
if len <= 2 && b[0] == 46 && (len == 1 || b[1] == 46) {
// Ignore Dot file: . ..
} else {
var cp = [UInt8]()
cp.reserveCapacity(pathBytes.count + len + 1)
cp += pathBytes
cp.removeLast() // Remove null-byte 0
cp += b
cp.append(47)
cp.append(0) // Now append the trailing null-byte 0
trackDir(pathBytes: cp)
}
}
}
entry = readdir(dp)
}
}
func spelunkDir(path: String) {
guard let dp = opendir(path) else {
// Ignore in case permission denies access to the directory.
return
}
defer {
closedir(dp)
}
var entry = readdir(dp)
while entry != nil {
count += 1
if entry!.pointee.d_type == 4 {
var dirName = entry!.pointee.d_name
withUnsafePointer(&dirName) { ptr in
let name = String(cString: UnsafePointer<CChar>(ptr))
if name != "." && name != ".." {
spelunkDir(path: "\(path)\(name)/")
}
}
}
entry = readdir(dp)
}
}
func browseDir(pathBytes: [UInt8]) throws {
do {
let fb = try FileBrowser(pathBytes: pathBytes)
while fb.next() {
count += 1
if fb.type == .D {
let nameBytes = fb.nameBytes
let len = nameBytes.count
if len <= 2 && nameBytes[0] == 46 && (len == 1 || nameBytes[1] == 46) {
// Ignore Dot file: . ..
} else {
var cp = [UInt8]()
cp.reserveCapacity(pathBytes.count + nameBytes.count + 1)
cp += pathBytes
cp.removeLast() // Remove null-byte 0
cp += nameBytes
cp.append(47)
cp.append(0) // Now append the trailing null-byte 0
try browseDir(pathBytes: cp)
}
}
}
} catch {
// Ignore in case of errors or denied permission.
}
}
func browseDir2(pathBytes: [UInt8]) throws {
do {
let fb = try FileBrowser(pathBytes: pathBytes)
while fb.next() {
count += 1
if fb.type == .D {
var rn = fb.rawEntry.pointee.d_name
try withUnsafePointer(&rn) { ptr in
let len = Int(Sys.strlen(sp: UnsafePointer<CChar>(ptr)))
let b = UnsafeBufferPointer<UInt8>(start: UnsafePointer<UInt8>(ptr),
count: len)
if len <= 2 && b[0] == 46 && (len == 1 || b[1] == 46) {
// Ignore Dot file: . ..
} else {
var cp = [UInt8]()
cp.reserveCapacity(pathBytes.count + len + 1)
cp += pathBytes
cp.removeLast() // Remove null-byte 0
cp += b
cp.append(47)
cp.append(0) // Now append the trailing null-byte 0
try browseDir2(pathBytes: cp)
}
}
}
}
} catch {
// Ignore in case of errors or denied permission.
}
}
func peekDir(pathBytes: [UInt8]) {
var pb = pathBytes
var store = [([UInt8], OpaquePointer?)]()
store.reserveCapacity(128)
PATH: while store.count >= 0 { // Trick the compiler.
let cap = UnsafePointer<CChar>(pb)
var dp = opendir(cap)
if dp == nil {
// Ignore in case permission denies access to the directory.
if store.count > 0 {
(pb, dp) = store.removeLast()
} else {
break PATH
}
}
DIRP: while store.count >= 0 { // Trick the compiler.
var entry = readdir(dp!)
while entry != nil {
count += 1
if entry!.pointee.d_type == 4 {
var dirName = entry!.pointee.d_name
var ignore = true
withUnsafePointer(&dirName) { ptr in
let len = Int(Sys.strlen(sp: UnsafePointer<CChar>(ptr)))
let b = UnsafeBufferPointer<UInt8>(start: UnsafePointer<UInt8>(ptr),
count: len)
if len <= 2 && b[0] == 46 && (len == 1 || b[1] == 46) {
// Ignore Dot file: . ..
} else {
var cp = [UInt8]()
cp.reserveCapacity(pb.count + len + 1)
cp += pb
cp.removeLast() // Remove null-byte 0
cp += b
cp.append(47)
cp.append(0) // Now append the trailing null-byte 0
store.append((pb, dp!))
pb = cp
ignore = false
}
}
if !ignore {
continue PATH
}
}
entry = readdir(dp!)
}
closedir(dp!)
if store.count > 0 {
(pb, dp) = store.removeLast()
continue DIRP
} else {
break PATH
}
}
}
}
var sw = Stopwatch()
var samplePath = "/"
var a = [UInt8](samplePath.utf8)
a.append(0)
sw.start()
trackDir(pathBytes: a)
p("trackDir count: \(count) Elapsed: \(sw.millis)")
count = 0
sw.start()
spelunkDir(path: samplePath)
p("spelunkDir count: \(count) Elapsed: \(sw.millis)")
count = 0
sw.start()
try browseDir(pathBytes: a)
p("browseDir count: \(count) Elapsed: \(sw.millis)")
count = 0
sw.start()
try browseDir2(pathBytes: a)
p("browseDir2 count: \(count) Elapsed: \(sw.millis)")
count = 0
sw.start()
peekDir(pathBytes: a)
p("peekDir count: \(count) Elapsed: \(sw.millis)")
count = 0
sw.start()
trackDir(pathBytes: a)
p("**trackDir count: \(count) Elapsed: \(sw.millis)")