/
dir.rb
247 lines (194 loc) · 4.8 KB
/
dir.rb
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
# depends on: array.rb
class Dir
class DirEntry < FFI::Struct
layout :d_name, :char_array, Rubinius::RUBY_CONFIG['rbx.platform.dir.d_name']
end
include Enumerable
GLOB_RECURSIVE = -2
def self.[](pattern)
glob(pattern, 0)
end
def self.glob(pattern, flags = 0)
original_dir = Dir.pwd
files = Array.new 0
period = (flags & File::FNM_DOTMATCH) > 0
period_excludes = ["..", "."]
max_depth = glob_max_depth(pattern)
Dir.foreach(original_dir) do |entry|
if match = glob_match(pattern, entry, flags)
files << match
end
if File.directory?(entry) and (max_depth == GLOB_RECURSIVE or max_depth > 0) and
!period_excludes.include?(entry)
next if entry =~ /^\./ and !period
search_deeper(files, entry, pattern, flags, 1, max_depth)
end
end
files.to_a
end
def self.search_deeper(files, path, pattern, flags, depth, max_depth) # needs depth
period = (flags & File::FNM_DOTMATCH) > 0
period_excludes = ["..", "."]
paths = [[path,depth]]
until paths.empty?
path, depth = paths.shift
Dir.foreach(path) do |entry|
next if period_excludes.include?(entry)
file = File.join(path, entry)
if match = glob_match(pattern, file, flags)
files << match
end
if File.directory?(file) and (max_depth == GLOB_RECURSIVE or depth < max_depth)
next if entry =~ /^\./ and !period
paths << [file, depth + 1]
end
end
end
end
def self.glob_match(pattern, entry, flags)
if glob_fnmatch? pattern, entry, flags
entry
elsif File.directory?(entry) and glob_fnmatch?(pattern, "#{entry}/", flags)
"#{entry}/"
else
nil
end
end
# This is going to need to be modified for {}
def self.glob_fnmatch?(pattern, entry, flags)
File.fnmatch?(pattern, entry, flags)
end
def self.glob_max_depth(pattern)
# TODO: Change to check for path seperator rather than /
if pattern =~ /\*{2}/
# Special case for Dir["**"]
seps = pattern.split("/").size - 1
pattern_ends_in_slash = pattern[pattern.size - 1] == ?/
if seps == 0 and !pattern_ends_in_slash
max_depth = 0
else
max_depth = GLOB_RECURSIVE
end
else
max_depth = pattern.split("/").size - 1
end
end
def self.chdir(path = ENV['HOME'])
if block_given?
original_path = self.getwd
Platform::POSIX.chdir path
begin
value = yield path
ensure
Platform::POSIX.chdir original_path
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 = self.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 self.foreach(path)
self.open(path) do |dir|
while s = dir.read
yield s
end
end
nil
end
def initialize(path)
@dirptr = Platform::POSIX.opendir(path)
if @dirptr.nil?
Errno.handle path
end
@path = path
end
def path
raise IOError, "closed directory" if @dirptr.nil?
@path
end
def close
raise IOError, "closed directory" if @dirptr.nil?
Platform::POSIX.closedir(@dirptr)
@dirptr = nil
end
def read
raise IOError, "closed directory" if @dirptr.nil?
dir_entry_ptr = Platform::POSIX.readdir(@dirptr)
return nil if dir_entry_ptr.nil?
DirEntry.new(dir_entry_ptr)[:d_name]
end
def each
raise IOError, "closed directory" if @dirptr.nil?
while s = read
yield s
end
self
end
def pos
raise IOError, "closed directory" if @dirptr.nil?
end
alias_method :tell, :pos
def pos=(position)
raise IOError, "closed directory" if @dirptr.nil?
seek(position)
position
end
def seek(position)
raise IOError, "closed directory" if @dirptr.nil?
self
end
def rewind
raise IOError, "closed directory" if @dirptr.nil?
Platform::POSIX.rewinddir(@dirptr)
self
end
class << self
alias_method :pwd, :getwd
alias_method :delete, :rmdir
alias_method :unlink, :rmdir
end
end