evanphx / rubinius

Rubinius, the Ruby VM

This URL has Read+Write access

rubinius / kernel / common / dir.rb
100644 481 lines (383 sloc) 10.371 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
class Dir
  include Enumerable
 
  GLOB_VERBOSE = 1 << (4 * 8 - 1) # HACK
  GLOB_RECURSIVE = -2
 
  def self.[](pattern)
    glob(StringValue(pattern), 0)
  end
 
  def self.old_glob(pattern, flags = 0)
    matches = []
 
    glob_brace_expand pattern.to_str, flags & ~GLOB_VERBOSE, matches
 
    matches
  end
 
  def self.glob(pattern, flags=0)
    return [] if pattern.empty?
 
    # The new glob impl doesn't handle flags or curly expansion, so
    # use the old glob if those are used.
    if flags != 0 or pattern.include? "{"
      old_glob(pattern, flags)
    else
      node = Dir::Glob.compile pattern
      Dir::Glob.run node
    end
  end
 
  def self.glob0(pattern, flags, matches)
    root = pattern.dup
    path = ''
 
    if root[0] == ?/ then # HACK windows
      root = root[1..-1]
      path = '/'
    end
 
    list = glob_pattern root, flags
 
    glob_helper path, false, :unknown, :unknown, list, 0, 1, flags, matches
  end
 
  def self.find_dirsep(pattern, flags)
    escape = (flags & File::FNM_NOESCAPE) == 0
    open = false
 
    i = 0
    pattern.each_byte do |char|
      case char
      when ?[
        open = true
      when ?]
        open = false
      when ?/
        return i unless open
      when ?\\
        return i if escape and (i + 1 == pattern.length)
      end
      i += 1
    end
 
    pattern.length
  end
 
  def self.glob_brace_expand(pattern, flags, matches)
    escape = (flags & File::FNM_NOESCAPE) == 0
 
    rbrace = nil
    lbrace = nil
    nest = 0
 
    skip = false
 
    i = 0
    pattern.each_byte do |char|
      if skip then
        skip = false
        next
      end
 
      if char == ?{ and nest == 0 then
        lbrace = i
        nest += 1
      end
 
      if char == ?} and nest - 1 <= 0 then
        rbrace = i
        nest -= 1
      end
 
      skip = true if char == ?\\ and escape
      i += 1
    end
 
    if lbrace and rbrace then
      pos = lbrace
      front = pattern[0...lbrace]
      back = pattern[(rbrace + 1)..-1]
 
      while pos < rbrace do
        nest = 0
        pos += 1
        last = pos
 
        while pos < rbrace and not (pattern[pos] == ?, and nest == 0) do
          nest += 1 if pattern[pos] == ?{
          nest -= 1 if pattern[pos] == ?}
 
          if pattern[pos] == ?\\ and escape then
            pos += 1
            break if pos == rbrace
          end
 
          pos += 1
        end
 
        brace_pattern = "#{front}#{pattern[last...pos]}#{back}"
 
        glob_brace_expand brace_pattern, flags, matches
      end
 
    else
      glob0 pattern, flags, matches
    end
  end
 
  ##
  # +dirsep+:: Should '/' be placed before appending child's entry name to
  # +path+?
  # +exist+:: Does 'path' indicate an existing entry?
  # +isdir+:: Does 'path' indicate a directory or a symlink to a directory?
 
  def self.glob_helper(path, dirsep, exist, isdir, pattern, start, stop, flags, matches)
    status = nil
    plain = magic = recursive = match_all = match_dir = false
    escape = (flags & File::FNM_NOESCAPE) == 0
 
    last_type = nil
 
    pattern[start, stop].each_with_index do |part, i|
      if part[1] == :recursive then
        recursive = true
        part = pattern[i + 1]
      end
 
      case part[1]
      when :magic then
        magic = true
      when :match_all then
        match_all = true
      when :match_dir then
        match_dir = true
      when :plain then
        plain = true
      when :recursive then
        raise "continuous RECURSIVEs"
      end
    end
 
    unless path.empty? then
      if match_all and exist == :unknown then
        if stat = File::Stat.stat(path)
          exist = :yes
          isdir = if stat.directory? then
                    :yes
                  elsif stat.symlink? then
                    :unknown
                  else
                    :no
                  end
        else
          exist = isdir = :no
        end
      end
 
      if match_dir and isdir == :unknown then
        if stat = File::Stat.stat(path)
          exist = :yes
          isdir = stat.directory? ? :yes : :no
        else
          exist = isdir = :no
        end
      end
 
      matches << path if match_all and exist == :yes
 
      if match_dir and isdir == :yes then
        return if path.empty? and not dirsep
        tmp = join_path path, '', dirsep
        matches << tmp
      end
    end
 
    return if exist == :no or isdir == :no
 
    if magic or recursive then
      dir = path.empty? ? '.' : path
 
      return unless File.directory? dir
 
      begin
        Dir.foreach dir do |entry|
          buf = join_path path, entry, dirsep
          new_isdir = :unknown
          new_pattern = []
          copied = start
 
          if recursive and not %w[. ..].include?(entry) and
            File.fnmatch('*', entry, flags) then
            stat = File.stat buf
            new_isdir = if stat.directory? then
                          :yes
                        elsif stat.symlink? then
                          :unknown
                        else
                          :no
                        end
          end
 
          pattern[start, stop].each_with_index do |part, i|
            if part[1] == :recursive then
              if new_isdir == :yes then
                new_pattern << part
                copied += 1
              end
              part = pattern[start + i + 1]
              i += 1
            end
 
            if (part[1] == :plain or part[1] == :magic) and
              File.fnmatch part[0], entry, flags then
              new_pattern << pattern[start + i + 1]
              copied += 2
            end
          end
 
          length = new_pattern.length
 
          # @todo determine why pat would be nil
          pat = pattern[copied, pattern.size-copied]
          new_pattern.concat pat if pat
 
          glob_helper(buf, true, :yes, new_isdir, new_pattern, 0, length,
                      flags, matches)
        end
      rescue Errno::ENOTDIR
        # File.directory? may return true on entries in an fdesc file system
        return
      end
    elsif plain then
      copy_end = 0
      copy_pattern = pattern.dup
 
      pattern[start, stop].each_with_index do |part, i|
        copy_pattern[i] = nil unless part[1] == :plain
      end
 
      copy_pattern[start, stop].each_with_index do |part, i|
        next unless part
 
        new_pattern = []
        copied = i
        next_offset = i + 1
 
        name = part[0]
        name = name.gsub '\\', '' if escape
 
        new_pattern << copy_pattern[next_offset]
        copied += 1
 
        copy_pattern[(i+1), stop-(i+1)].each_with_index do |part2, j|
          if part2 and not File.fnmatch part2[0], name, flags then
            new_pattern << copy_pattern[next_offset + j]
            copied += 1
 
            copy_pattern[next_offset + j] = nil
          end
        end
 
        length = new_pattern.length
 
        new_pattern.concat copy_pattern[(copied + 1)..-1]
 
        buf = join_path path, name, dirsep
 
        glob_helper(buf, true, :unknown, :unknown, new_pattern,
                    0, length, flags, matches)
      end
    end
  end
 
  def self.glob_magic?(pattern, flags)
    escape = (flags & File::FNM_NOESCAPE) == 0
    nocase = (flags & File::FNM_CASEFOLD) == 0
 
    i = 0
    pattern.each_byte do |char|
      case char
      when ?*, ??, ?[ then
        return true
      when ?\\ then
        return false if escape && i == pattern.length
      else
       return true if char =~ /[a-z]/i && nocase # HACK FNM_SYSCASE
      end
      i += 1
    end
 
    false
  end
 
  def self.glob_pattern(pattern, flags)
    dirsep = false
    glob_pattern = []
 
    until pattern.empty? do
      part = []
 
      if pattern =~ /\A\*\*\// then
        # fold continuous :recursive for glob_helper
        pattern.sub!(/\A(\*\*\/)+/, '')
        part[0] = ''
        part[1] = :recursive
        dirsep = true
      else
        m = find_dirsep pattern, flags
 
        part[0] = pattern[0...m]
        part[1] = glob_magic?(part[0], flags) ? :magic : :plain
 
        unless pattern.length == m then
          dirsep = true
          pattern = pattern[(m+1), pattern.size-m]
        else
          dirsep = false
          pattern = ''
        end
      end
 
      glob_pattern << part
    end
 
    part = []
    part[0] = ''
    part[1] = dirsep ? :match_dir : :match_all
 
    glob_pattern << part
 
    glob_pattern
  end
 
  def self.join_path(p1, p2, dirsep)
    "#{p1}#{dirsep ? '/' : ''}#{p2}"
  end
  
  def self.chdir(path = ENV['HOME'])
    if block_given?
      original_path = self.getwd
      error = Platform::POSIX.chdir path
      Errno.handle(path) if error != 0
 
      begin
        value = yield path
      ensure
        error = Platform::POSIX.chdir original_path
        Errno.handle(original_path) if error != 0
      end
 
      return value
    else
      error = Platform::POSIX.chdir path
      if error != 0
        Errno.handle path
      end
      error
    end
  end
  
  def self.mkdir(path, mode = 0777)
    error = Platform::POSIX.mkdir(path, mode)
    if error != 0
      Errno.handle path
    end
    error
  end
  
  def self.rmdir(path)
    error = Platform::POSIX.rmdir(path)
    if error != 0
      Errno.handle path
    end
    error
  end
 
  def self.getwd
    buf = " " * 1024
    Platform::POSIX.getcwd(buf, buf.length)
  end
  
  def self.open(path)
    dir = new path
    if block_given?
      begin
        value = yield dir
      ensure
        dir.close
      end
 
      return value
    else
      return dir
    end
  end
 
  def self.entries(path)
    ret = []
 
    self.open(path) do |dir|
      while s = dir.read
        ret << s
      end
    end
 
    ret
  end
  
  def initialize(path)
    begin
      __open__ path
    rescue SystemCallError => e
      e.message << " - '#{path}'"
      raise e
    end
 
    @path = path
  end
  
  def path
    raise IOError, "closed directory" if closed?
 
    @path
  end
 
  SeekKind = 0
  RewindKind = 1
  TellKind = 2
 
  def pos
    __control__ TellKind, 0
  end
 
  alias_method :tell, :pos
 
  def pos=(position)
    seek(position)
 
    position
  end
 
  def seek(position)
    __control__ SeekKind, position
 
    self
  end
 
  def rewind
    __control__ RewindKind, 0
 
    self
  end
  
  class << self
    alias_method :pwd, :getwd
    alias_method :delete, :rmdir
    alias_method :unlink, :rmdir
  end
end