-
Notifications
You must be signed in to change notification settings - Fork 595
Expand file tree
/
Copy pathdir_glob.rb
More file actions
532 lines (432 loc) · 12.1 KB
/
dir_glob.rb
File metadata and controls
532 lines (432 loc) · 12.1 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# -*- encoding: us-ascii -*-
class Dir
module Glob
class Node
def initialize(nxt, flags)
@flags = flags
@next = nxt
@separator = nil
end
attr_writer :separator
def separator
@separator || "/"
end
def path_join(parent, ent)
return ent unless parent
if parent == "/"
"/#{ent}"
else
"#{parent}#{separator}#{ent}"
end
end
end
class ConstantDirectory < Node
def initialize(nxt, flags, dir)
super nxt, flags
@dir = dir
end
def call(env, path)
full = path_join(path, @dir)
# Don't check if full exists. It just costs us time
# and the downstream node will be able to check properly.
@next.call env, full
end
end
class ConstantEntry < Node
def initialize(nxt, flags, name)
super nxt, flags
@name = name
end
def call(env, parent)
path = path_join(parent, @name)
if File.exists? path
env.matches << path
end
end
end
class ConstantSuffixEntry < Node
def initialize(nxt, flags, name, suffixes)
super nxt, flags
@name = name
@suffixes = suffixes
end
def call(env, parent)
stem = path_join(parent, @name)
@suffixes.each do |s|
path = "#{stem}#{s}"
if File.exists?(path)
env.matches << path
end
end
end
end
class RootDirectory < Node
def call(env, path)
@next.call env, "/"
end
end
class RecursiveDirectories < Node
def call(env, start)
return unless File.exists? start
# Even though the recursive entry is zero width
# in this case, it's left seperator is still the
# dominant one, so we fix things up to use it.
switched = @next.dup
switched.separator = @separator
switched.call env, start
stack = [start]
allow_dots = ((@flags & File::FNM_DOTMATCH) != 0)
until stack.empty?
path = stack.pop
begin
dir = Dir.new(path)
rescue Errno::ENOTDIR
next
end
while ent = dir.read
next if ent == "." || ent == ".."
full = path_join(path, ent)
if File.directory? full and (allow_dots or ent.getbyte(0) != 46) # ?.
stack << full
@next.call env, full
end
end
dir.close
end
end
end
class StartRecursiveDirectories < Node
def call(env, start)
raise "invalid usage" if start
# Even though the recursive entry is zero width
# in this case, it's left seperator is still the
# dominant one, so we fix things up to use it.
if @separator
switched = @next.dup
switched.separator = @separator
switched.call env, start
else
@next.call env, start
end
stack = []
allow_dots = ((@flags & File::FNM_DOTMATCH) != 0)
dir = Dir.new(".")
while ent = dir.read
next if ent == "." || ent == ".."
if File.directory? ent and (allow_dots or ent.getbyte(0) != 46) # ?.
stack << ent
@next.call env, ent
end
end
dir.close
until stack.empty?
path = stack.pop
dir = Dir.new(path)
while ent = dir.read
next if ent == "." || ent == ".."
full = path_join(path, ent)
if File.directory? full and ent.getbyte(0) != 46 # ?.
stack << full
@next.call env, full
end
end
dir.close
end
end
end
class Match < Node
def initialize(nxt, flags, glob)
super nxt, flags
@glob = glob
end
def match?(str)
File.fnmatch @glob, str, @flags
end
end
class DirectoryMatch < Match
def initialize(nxt, flags, glob)
super
@glob.gsub! "**", "*"
end
def call(env, path)
return if path and !File.exists?("#{path}/.")
dir = Dir.new(path ? path : ".")
while ent = dir.read
if match? ent
full = path_join(path, ent)
if File.directory? full
@next.call env, full
end
end
end
dir.close
end
end
class EntryMatch < Match
def call(env, path)
return if path and !File.exists?("#{path}/.")
begin
dir = Dir.new(path ? path : ".")
rescue SystemCallError
return
end
while ent = dir.read
if match? ent
env.matches << path_join(path, ent)
end
end
dir.close
end
end
class SuffixEntryMatch < Match
def initialize(nxt, flags, glob, suffixes)
super nxt, flags, glob
@suffixes = suffixes
end
def call(env, path)
return if path and !File.exists?("#{path}/.")
begin
dir = Dir.new(path ? path : ".")
rescue SystemCallError
return
end
while f = dir.read
@suffixes.each do |s|
ent = "#{f}#{s}"
if match? ent
env.matches << path_join(path, ent)
end
end
end
dir.close
end
end
class DirectoriesOnly < Node
def call(env, path)
if path and File.exists?("#{path}/.")
env.matches << "#{path}/"
end
end
end
class Environment
attr_reader :matches
def initialize(matches=[])
@matches = matches
end
end
def self.path_split(str)
start = 0
ret = []
last_match = nil
while match = %r!/+!.match_from(str, start)
cur_start, cur_end = match.full
ret << str.byteslice(start, cur_start - start)
ret << str.byteslice(cur_start, cur_end - cur_start)
start = cur_end
last_match = match
end
if last_match
ret << last_match.post_match
else
ret << str
end
# Trim from end
if !ret.empty?
while s = ret.last and s.empty?
ret.pop
end
end
ret
end
def self.single_compile(glob, flags=0, suffixes=nil)
parts = path_split(glob)
if glob.getbyte(-1) == 47 # ?/
last = DirectoriesOnly.new nil, flags
else
file = parts.pop
if /^[a-zA-Z0-9._]+$/.match(file)
if suffixes
last = ConstantSuffixEntry.new nil, flags, file, suffixes
else
last = ConstantEntry.new nil, flags, file
end
else
if suffixes
last = SuffixEntryMatch.new nil, flags, file, suffixes
else
last = EntryMatch.new nil, flags, file
end
end
end
until parts.empty?
last.separator = parts.pop
dir = parts.pop
if dir == "**"
if parts.empty?
last = StartRecursiveDirectories.new last, flags
else
last = RecursiveDirectories.new last, flags
end
elsif /^[^\*\?\]]+$/.match(dir)
while /^[^\*\?\]]+$/.match(parts[-2])
next_sep = parts.pop
next_sect = parts.pop
dir = next_sect << next_sep << dir
end
last = ConstantDirectory.new last, flags, dir
elsif !dir.empty?
last = DirectoryMatch.new last, flags, dir
end
end
if glob.getbyte(0) == 47 # ?/
last = RootDirectory.new last, flags
end
last
end
def self.run(node, matches=[])
env = Environment.new(matches)
node.call env, nil
env.matches
end
total = Rubinius::Config['glob.cache']
case total
when Fixnum
if total == 0
@glob_cache = nil
else
@glob_cache = Rubinius::LRUCache.new(total)
end
when false
@glob_cache = nil
else
@glob_cache = Rubinius::LRUCache.new(50)
end
def self.glob_cache
@glob_cache
end
def self.glob(pattern, flags, matches=[])
# Rubygems typicall uses Dir[] as basicly a glorified File.exists?
# to check for multiple extensions. So we went ahead and sped up
# that specific case.
if flags == 0 and
m = /^([a-zA-Z0-9_.\/\s]*[a-zA-Z0-9_.])(?:\{([^{}\/\*\?]*)\})?$/.match(pattern)
# no meta characters, so this is a glorified
# File.exists? check. We allow for a brace expansion
# only as a suffix.
if braces = m[2]
stem = m[1]
braces.split(",").each do |s|
path = "#{stem}#{s}"
if File.exists? path
matches << path
end
end
# Split strips an empty closing part, so we need to add it back in
if braces.getbyte(-1) == 44 # ?,
matches << stem if File.exists? stem
end
else
matches << pattern if File.exists?(pattern)
end
return matches
end
ec_key = nil
if gc = @glob_cache
ec_key = [pattern, flags]
if patterns = gc.retrieve(ec_key)
patterns.each do |node|
run node, matches
end
return matches
end
end
if pattern.include? "{"
patterns = compile(pattern, flags)
gc.set ec_key, patterns if ec_key
patterns.each do |node|
run node, matches
end
elsif node = single_compile(pattern, flags)
gc.set ec_key, [node] if ec_key
run node, matches
else
matches
end
end
def self.compile(pattern, flags=0, patterns=[])
escape = (flags & File::FNM_NOESCAPE) == 0
rbrace = nil
lbrace = nil
escapes = false
# Do a quick search for a { to start the search better
i = pattern.index("{")
# If there was a { found, then search
if i
nest = 0
data = pattern.data
total = pattern.size
while i < total
char = data.get_byte(i)
if char == 123 # ?{
lbrace = i if nest == 0
nest += 1
end
if char == 125 # ?}
nest -= 1
end
if nest == 0
rbrace = i
break
end
if char == 92 and escape # ?\\
escapes = true
i += 1
end
i += 1
end
end
# Detect if it's a simple suffix brace
# if !escapes and lbrace and rbrace == pattern.size - 1
# parts = pattern.substring(lbrace+1, rbrace - lbrace - 1).split(",")
# front = pattern.substring(0, lbrace)
# if node = single_compile(front, flags, parts)
# patterns << node
# return patterns
# end
# end
# There was a full {} expression detected, expand each part of it
# recursively.
if lbrace and rbrace
pos = lbrace
front = pattern[0...lbrace]
back = pattern[(rbrace + 1)..-1]
while pos < rbrace
nest = 0
pos += 1
last = pos
while pos < rbrace and not (pattern.getbyte(pos) == 44 and nest == 0) # ?,
nest += 1 if pattern.getbyte(pos) == 123 # ?{
nest -= 1 if pattern.getbyte(pos) == 125 # ?}
if pattern.getbyte(pos) == 92 and escape # ?\\
pos += 1
break if pos == rbrace
end
pos += 1
end
brace_pattern = "#{front}#{pattern[last...pos]}#{back}"
compile brace_pattern, flags, patterns
end
# No braces found, match the pattern normally
else
# Don't use .glob here because this code can detect properly
# if a { is a brace or a just a normal character, but .glob can't.
# if .glob is used and there is a { as a normal character, it will
# recurse forever.
if node = single_compile(pattern, flags)
patterns << node
end
end
return patterns
end
end
end