@@ -46,12 +46,7 @@ def separator
46
46
end
47
47
48
48
def path_join ( parent , ent )
49
- return ent unless parent
50
- if parent == '/'
51
- "/#{ ent } "
52
- else
53
- "#{ parent } #{ separator } #{ ent } "
54
- end
49
+ Dir ::Glob . path_join ( parent , ent , separator )
55
50
end
56
51
end
57
52
@@ -61,12 +56,12 @@ def initialize(nxt, flags, dir)
61
56
@dir = dir
62
57
end
63
58
64
- def call ( matches , path )
59
+ def call ( matches , path , glob_base_dir )
65
60
full = path_join ( path , @dir )
66
61
67
62
# Don't check if full exists. It just costs us time
68
63
# and the downstream node will be able to check properly.
69
- @next . call matches , full
64
+ @next . call matches , full , glob_base_dir
70
65
end
71
66
end
72
67
@@ -76,31 +71,31 @@ def initialize(nxt, flags, name)
76
71
@name = name
77
72
end
78
73
79
- def call ( matches , parent )
74
+ def call ( matches , parent , glob_base_dir )
80
75
path = path_join ( parent , @name )
81
76
82
- if File . exist? path
77
+ if File . exist? path_join ( glob_base_dir , path )
83
78
matches << path
84
79
end
85
80
end
86
81
end
87
82
88
83
class RootDirectory < Node
89
- def call ( matches , path )
90
- @next . call matches , '/'
84
+ def call ( matches , path , glob_base_dir )
85
+ @next . call matches , '/' , glob_base_dir
91
86
end
92
87
end
93
88
94
89
class RecursiveDirectories < Node
95
- def call ( matches , start )
96
- return if !start || !File . exist? ( start )
90
+ def call ( matches , start , glob_base_dir )
91
+ return if !start || !File . exist? ( path_join ( glob_base_dir , start ) )
97
92
98
93
# Even though the recursive entry is zero width
99
94
# in this case, its left separator is still the
100
95
# dominant one, so we fix things up to use it.
101
96
switched = @next . dup
102
97
switched . separator = @separator
103
- switched . call matches , start
98
+ switched . call matches , start , glob_base_dir
104
99
105
100
stack = [ start ]
106
101
@@ -109,19 +104,19 @@ def call(matches, start)
109
104
until stack . empty?
110
105
path = stack . pop
111
106
begin
112
- dir = Dir . new ( path )
107
+ dir = Dir . new ( path_join ( glob_base_dir , path ) )
113
108
rescue Errno ::ENOTDIR
114
109
next
115
110
end
116
111
117
112
while ent = dir . read
118
113
next if ent == '.' || ent == '..'
119
114
full = path_join ( path , ent )
120
- mode = Truffle ::POSIX . truffleposix_lstat_mode ( full )
115
+ mode = Truffle ::POSIX . truffleposix_lstat_mode ( path_join ( glob_base_dir , full ) )
121
116
122
117
if Truffle ::StatOperations . directory? ( mode ) and ( allow_dots or ent . getbyte ( 0 ) != 46 ) # ?.
123
118
stack << full
124
- @next . call matches , full
119
+ @next . call matches , full , glob_base_dir
125
120
end
126
121
end
127
122
dir . close
@@ -130,7 +125,7 @@ def call(matches, start)
130
125
end
131
126
132
127
class StartRecursiveDirectories < Node
133
- def call ( matches , start )
128
+ def call ( matches , start , glob_base_dir )
134
129
raise 'invalid usage' if start
135
130
136
131
# Even though the recursive entry is zero width
@@ -139,38 +134,38 @@ def call(matches, start)
139
134
if @separator
140
135
switched = @next . dup
141
136
switched . separator = @separator
142
- switched . call matches , start
137
+ switched . call matches , start , glob_base_dir
143
138
else
144
- @next . call matches , start
139
+ @next . call matches , start , glob_base_dir
145
140
end
146
141
147
142
stack = [ ]
148
143
149
144
allow_dots = ( ( @flags & File ::FNM_DOTMATCH ) != 0 )
150
145
151
- dir = Dir . new ( '.' )
146
+ dir = Dir . new ( path_join ( glob_base_dir , '.' ) )
152
147
while ent = dir . read
153
148
next if ent == '.' || ent == '..'
154
- mode = Truffle ::POSIX . truffleposix_lstat_mode ( ent )
149
+ mode = Truffle ::POSIX . truffleposix_lstat_mode ( path_join ( glob_base_dir , ent ) )
155
150
156
151
if Truffle ::StatOperations . directory? ( mode ) and ( allow_dots or ent . getbyte ( 0 ) != 46 ) # ?.
157
152
stack << ent
158
- @next . call matches , ent
153
+ @next . call matches , ent , glob_base_dir
159
154
end
160
155
end
161
156
dir . close
162
157
163
158
until stack . empty?
164
159
path = stack . pop
165
- dir = Dir . new ( path )
160
+ dir = Dir . new ( path_join ( glob_base_dir , path ) )
166
161
while ent = dir . read
167
162
next if ent == '.' || ent == '..'
168
163
full = path_join ( path , ent )
169
- mode = Truffle ::POSIX . truffleposix_lstat_mode ( full )
164
+ mode = Truffle ::POSIX . truffleposix_lstat_mode ( path_join ( glob_base_dir , full ) )
170
165
171
166
if Truffle ::StatOperations . directory? ( mode ) and ent . getbyte ( 0 ) != 46 # ?.
172
167
stack << full
173
- @next . call matches , full
168
+ @next . call matches , full , glob_base_dir
174
169
end
175
170
end
176
171
dir . close
@@ -196,16 +191,16 @@ def initialize(nxt, flags, glob)
196
191
@glob . gsub! '**' , '*'
197
192
end
198
193
199
- def call ( matches , path )
200
- return if path and !File . exist? ( "#{ path } /." )
194
+ def call ( matches , path , glob_base_dir )
195
+ return if path and !File . exist? ( path_join ( glob_base_dir , "#{ path } /." ) )
201
196
202
- dir = Dir . new ( path ? path : '.' )
197
+ dir = Dir . new ( path_join ( glob_base_dir , path ? path : '.' ) )
203
198
while ent = dir . read
204
199
if match? ent
205
200
full = path_join ( path , ent )
206
201
207
- if File . directory? full
208
- @next . call matches , full
202
+ if File . directory? path_join ( glob_base_dir , full )
203
+ @next . call matches , full , glob_base_dir
209
204
end
210
205
end
211
206
end
@@ -214,11 +209,11 @@ def call(matches, path)
214
209
end
215
210
216
211
class EntryMatch < Match
217
- def call ( matches , path )
218
- return if path and !File . exist? ( "#{ path } /." )
212
+ def call ( matches , path , glob_base_dir )
213
+ return if path and !File . exist? ( "#{ path_join ( glob_base_dir , path ) } /." )
219
214
220
215
begin
221
- dir = Dir . new ( path ? path : '.' )
216
+ dir = Dir . new ( path_join ( glob_base_dir , path ? path : '.' ) )
222
217
rescue SystemCallError
223
218
return
224
219
end
@@ -233,8 +228,8 @@ def call(matches, path)
233
228
end
234
229
235
230
class DirectoriesOnly < Node
236
- def call ( matches , path )
237
- if path and File . exist? ( "#{ path } /." )
231
+ def call ( matches , path , glob_base_dir )
232
+ if path and File . exist? ( "#{ path_join ( glob_base_dir , path ) } /." )
238
233
matches << "#{ path } /"
239
234
end
240
235
end
@@ -322,21 +317,31 @@ def self.single_compile(glob, flags=0)
322
317
last
323
318
end
324
319
325
- def self . run ( node , all_matches = [ ] )
320
+ def self . run ( node , all_matches , glob_base_dir )
326
321
if ConstantEntry === node
327
- node . call all_matches , nil
322
+ node . call all_matches , nil , glob_base_dir
328
323
else
329
324
matches = [ ]
330
- node . call matches , nil
325
+ node . call matches , nil , glob_base_dir
331
326
# Truffle: ensure glob'd files are always sorted in consistent order,
332
327
# it avoids headaches due to platform differences (OS X is sorted, Linux not).
333
328
matches . sort!
334
329
all_matches . concat ( matches )
335
330
end
336
331
end
337
332
338
- def self . glob ( pattern , flags , matches = [ ] )
339
- # Rubygems uses Dir[] as a glorified File.exist? to check for multiple
333
+ def self . path_join ( parent , entry , separator = '/' )
334
+ return entry unless parent
335
+
336
+ if parent == '/'
337
+ "/#{ entry } "
338
+ else
339
+ "#{ parent } #{ separator } #{ entry } "
340
+ end
341
+ end
342
+
343
+ def self . glob ( base_dir , pattern , flags , matches )
344
+ # Rubygems uses Dir[] as a glorified File.exist? to check for multiple
340
345
# extensions. So we went ahead and sped up that specific case.
341
346
342
347
if flags == 0 and m = TRAILING_BRACES . match ( pattern )
@@ -349,17 +354,17 @@ def self.glob(pattern, flags, matches=[])
349
354
350
355
braces . split ( ',' ) . each do |s |
351
356
path = "#{ stem } #{ s } "
352
- if File . exist? path
357
+ if File . exist? path_join ( base_dir , path )
353
358
matches << path
354
359
end
355
360
end
356
361
357
362
# Split strips an empty closing part, so we need to add it back in
358
363
if braces . getbyte ( -1 ) == 44 # ?,
359
- matches << stem if File . exist? stem
364
+ matches << stem if File . exist? path_join ( base_dir , stem )
360
365
end
361
366
else
362
- matches << pattern if File . exist? ( pattern )
367
+ matches << pattern if File . exist? ( path_join ( base_dir , pattern ) )
363
368
end
364
369
365
370
return matches
@@ -370,10 +375,10 @@ def self.glob(pattern, flags, matches=[])
370
375
patterns = compile ( pattern , left_brace_index , flags )
371
376
372
377
patterns . each do |node |
373
- run node , matches
378
+ run node , matches , base_dir
374
379
end
375
380
elsif node = single_compile ( pattern , flags )
376
- run node , matches
381
+ run node , matches , base_dir
377
382
else
378
383
matches
379
384
end
0 commit comments