-
Notifications
You must be signed in to change notification settings - Fork 587
Expand file tree
/
Copy pathdir.rb
More file actions
185 lines (141 loc) · 3.25 KB
/
Copy pathdir.rb
File metadata and controls
185 lines (141 loc) · 3.25 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
# -*- encoding: us-ascii -*-
class Dir
include Enumerable
FFI = Rubinius::FFI
def self.[](*patterns)
if patterns.size == 1
patterns = Rubinius::Type.coerce_to_path(patterns[0]).split("\0")
end
glob patterns
end
# files = []
# index = 0
# patterns.each do |pat|
# enc = Rubinius::Type.ascii_compatible_encoding pat
# Dir::Glob.glob pat, 0, files
# total = files.size
# while index < total
# Rubinius::Type.encode_string files[index], enc
# index += 1
# end
# end
# files
# end
def self.glob(pattern, flags=0, &block)
if pattern.kind_of? Array
patterns = pattern
else
pattern = Rubinius::Type.coerce_to_path pattern
return [] if pattern.empty?
patterns = pattern.split("\0")
end
matches = []
index = 0
patterns.each do |pat|
enc = Rubinius::Type.ascii_compatible_encoding pat
Dir::Glob.glob pat, flags, matches
total = matches.size
while index < total
Rubinius::Type.encode_string matches[index], enc
index += 1
end
end
if block
matches.each(&block)
return nil
end
return matches
end
def self.foreach(path)
return to_enum(:foreach, path) unless block_given?
open(path) do |dir|
while s = dir.read
yield s
end
end
nil
end
def self.join_path(p1, p2, dirsep)
"#{p1}#{dirsep ? '/' : ''}#{p2}"
end
def self.chdir(path = ENV['HOME'])
path = Rubinius::Type.coerce_to_path path
if block_given?
original_path = self.getwd
error = FFI::Platform::POSIX.chdir path
Errno.handle(path) if error != 0
begin
value = yield path
ensure
error = FFI::Platform::POSIX.chdir original_path
Errno.handle(original_path) if error != 0
end
return value
else
error = FFI::Platform::POSIX.chdir path
if error != 0
Errno.handle path
end
error
end
end
def self.mkdir(path, mode = 0777)
error = FFI::Platform::POSIX.mkdir(Rubinius::Type.coerce_to_path(path), mode)
if error != 0
Errno.handle path
end
error
end
def self.rmdir(path)
error = FFI::Platform::POSIX.rmdir(Rubinius::Type.coerce_to_path(path))
if error != 0
Errno.handle path
end
error
end
def self.getwd
buf = String.pattern Rubinius::PATH_MAX, 0
wd = FFI::Platform::POSIX.getcwd(buf, buf.length)
Rubinius::Type.external_string wd
end
def self.chroot(path)
ret = FFI::Platform::POSIX.chroot Rubinius::Type.coerce_to_path(path)
Errno.handle path
return ret
end
def each
return to_enum unless block_given?
while s = read
yield s
end
self
end
attr_reader :path
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
def inspect
"#<#{self.class}:#{object_id.to_s(16)} @path=#{@path}>"
end
class << self
alias_method :pwd, :getwd
alias_method :delete, :rmdir
alias_method :unlink, :rmdir
end
end