Class: MemFs::File
Defined Under Namespace
Classes: Stat
Constant Summary
collapse
- ALT_SEPARATOR =
nil
- MODE_MAP =
{
'r' => RDONLY,
'r+' => RDWR,
'w' => CREAT | TRUNC | WRONLY,
'w+' => CREAT | TRUNC | RDWR,
'a' => CREAT | APPEND | WRONLY,
'a+' => CREAT | APPEND | RDWR
}
- SEPARATOR =
'/'
- SUCCESS =
0
Instance Attribute Summary collapse
#autoclose, #close_on_exec
Class Method Summary
collapse
-
.absolute_path(path, dir_string = fs.pwd) ⇒ Object
-
.atime(path) ⇒ Object
-
.chmod(mode_int, *paths) ⇒ Object
-
.chown(uid, gid, *paths) ⇒ Object
-
.ctime(path) ⇒ Object
-
.exists?(path) ⇒ Boolean
(also: exist?)
-
.expand_path(file_name, dir_string = fs.pwd) ⇒ Object
-
.ftype(path) ⇒ Object
-
.identical?(path1, path2) ⇒ Boolean
-
.lchmod(mode_int, *file_names) ⇒ Object
-
.lchown(uid, gid, *paths) ⇒ Object
-
.link(old_name, new_name) ⇒ Object
-
.lstat(path) ⇒ Object
-
.mtime(path) ⇒ Object
-
.open(filename, mode = RDONLY, *perm_and_opt) ⇒ Object
-
.readlink(path) ⇒ Object
-
.realdirpath(path, dir_string = fs.pwd) ⇒ Object
-
.realpath(path, dir_string = fs.pwd) ⇒ Object
-
.rename(old_name, new_name) ⇒ Object
-
.reset! ⇒ Object
-
.size(path) ⇒ Object
-
.size?(path) ⇒ Boolean
-
.stat(path) ⇒ Object
-
.symlink(old_name, new_name) ⇒ Object
-
.symlink?(path) ⇒ Boolean
-
.truncate(path, length) ⇒ Object
-
.umask(integer = nil) ⇒ Object
-
.unlink(*paths) ⇒ Object
(also: delete)
-
.utime(atime, mtime, *file_names) ⇒ Object
Instance Method Summary
collapse
read
#<<, #advise, #autoclose?, #binmode, #binmode?, #close, #close_on_exec?, #closed?, #each, #each_byte, #each_char, #eof?, #external_encoding, #pos, #print, #printf, #puts, #read, #seek, #stat, #write
Constructor Details
#initialize(filename, mode = File::RDONLY, *perm_and_or_opt) ⇒ File
Returns a new instance of File.
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/memfs/file.rb', line 234
def initialize(filename, mode = File::RDONLY, *perm_and_or_opt)
opt = perm_and_or_opt.last.is_a?(Hash) ? perm_and_or_opt.pop : {}
perm = perm_and_or_opt.shift
if perm_and_or_opt.size > 0
fail ArgumentError, 'wrong number of arguments (4 for 1..3)'
end
@path = filename
@external_encoding = opt[:external_encoding] && Encoding.find(opt[:external_encoding])
self.closed = false
self.opening_mode = str_to_mode_int(mode)
fs.touch(filename) if create_file?
self.entry = fs.find(filename)
entry.content.clear if truncate_file?
end
|
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
232
233
234
|
# File 'lib/memfs/file.rb', line 232
def path
@path
end
|
Class Method Details
.absolute_path(path, dir_string = fs.pwd) ⇒ Object
75
76
77
|
# File 'lib/memfs/file.rb', line 75
def self.absolute_path(path, dir_string = fs.pwd)
original_file_class.absolute_path(path, dir_string)
end
|
.atime(path) ⇒ Object
79
80
81
|
# File 'lib/memfs/file.rb', line 79
def self.atime(path)
stat(path).atime
end
|
.chmod(mode_int, *paths) ⇒ Object
83
84
85
86
87
|
# File 'lib/memfs/file.rb', line 83
def self.chmod(mode_int, *paths)
paths.each do |path|
fs.chmod mode_int, path
end
end
|
.chown(uid, gid, *paths) ⇒ Object
89
90
91
92
93
94
|
# File 'lib/memfs/file.rb', line 89
def self.chown(uid, gid, *paths)
paths.each do |path|
fs.chown(uid, gid, path)
end
paths.size
end
|
.ctime(path) ⇒ Object
96
97
98
|
# File 'lib/memfs/file.rb', line 96
def self.ctime(path)
stat(path).ctime
end
|
.exists?(path) ⇒ Boolean
Also known as:
exist?
100
101
102
|
# File 'lib/memfs/file.rb', line 100
def self.exists?(path)
!!fs.find(path)
end
|
.expand_path(file_name, dir_string = fs.pwd) ⇒ Object
105
106
107
|
# File 'lib/memfs/file.rb', line 105
def self.expand_path(file_name, dir_string = fs.pwd)
original_file_class.expand_path(file_name, dir_string)
end
|
.ftype(path) ⇒ Object
109
110
111
|
# File 'lib/memfs/file.rb', line 109
def self.ftype(path)
fs.find!(path) && lstat(path).ftype
end
|
.identical?(path1, path2) ⇒ Boolean
115
116
117
118
119
|
# File 'lib/memfs/file.rb', line 115
def self.identical?(path1, path2)
fs.find!(path1).dereferenced === fs.find!(path2).dereferenced
rescue Errno::ENOENT
false
end
|
.lchmod(mode_int, *file_names) ⇒ Object
121
122
123
124
125
|
# File 'lib/memfs/file.rb', line 121
def self.lchmod(mode_int, *file_names)
file_names.each do |file_name|
fs.chmod mode_int, file_name
end
end
|
.lchown(uid, gid, *paths) ⇒ Object
127
128
129
|
# File 'lib/memfs/file.rb', line 127
def self.lchown(uid, gid, *paths)
chown uid, gid, *paths
end
|
.link(old_name, new_name) ⇒ Object
131
132
133
134
|
# File 'lib/memfs/file.rb', line 131
def self.link(old_name, new_name)
fs.link old_name, new_name
SUCCESS
end
|
.lstat(path) ⇒ Object
136
137
138
|
# File 'lib/memfs/file.rb', line 136
def self.lstat(path)
Stat.new(path)
end
|
.mtime(path) ⇒ Object
140
141
142
|
# File 'lib/memfs/file.rb', line 140
def self.mtime(path)
stat(path).mtime
end
|
.open(filename, mode = RDONLY, *perm_and_opt) ⇒ Object
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/memfs/file.rb', line 144
def self.open(filename, mode = RDONLY, *perm_and_opt)
file = new(filename, mode, *perm_and_opt)
if block_given?
yield file
else
file
end
ensure
file.close if file && block_given?
end
|
.readlink(path) ⇒ Object
156
157
158
|
# File 'lib/memfs/file.rb', line 156
def self.readlink(path)
fs.find!(path).target
end
|
.realdirpath(path, dir_string = fs.pwd) ⇒ Object
160
161
162
|
# File 'lib/memfs/file.rb', line 160
def self.realdirpath(path, dir_string = fs.pwd)
loose_dereference_path(absolute_path(path, dir_string))
end
|
.realpath(path, dir_string = fs.pwd) ⇒ Object
164
165
166
|
# File 'lib/memfs/file.rb', line 164
def self.realpath(path, dir_string = fs.pwd)
dereference_path(absolute_path(path, dir_string))
end
|
.rename(old_name, new_name) ⇒ Object
168
169
170
171
|
# File 'lib/memfs/file.rb', line 168
def self.rename(old_name, new_name)
fs.rename(old_name, new_name)
SUCCESS
end
|
.reset! ⇒ Object
173
174
175
|
# File 'lib/memfs/file.rb', line 173
def self.reset!
@umask = original_file_class.umask
end
|
.size(path) ⇒ Object
177
178
179
|
# File 'lib/memfs/file.rb', line 177
def self.size(path)
fs.find!(path).size
end
|
.size?(path) ⇒ Boolean
181
182
183
184
185
186
187
188
|
# File 'lib/memfs/file.rb', line 181
def self.size?(path)
file = fs.find(path)
if file && file.size > 0
file.size
else
false
end
end
|
.stat(path) ⇒ Object
190
191
192
|
# File 'lib/memfs/file.rb', line 190
def self.stat(path)
Stat.new(path, true)
end
|
.symlink(old_name, new_name) ⇒ Object
194
195
196
197
|
# File 'lib/memfs/file.rb', line 194
def self.symlink(old_name, new_name)
fs.symlink old_name, new_name
SUCCESS
end
|
.symlink?(path) ⇒ Boolean
199
200
201
|
# File 'lib/memfs/file.rb', line 199
def self.symlink?(path)
lstat_query(path, :symlink?)
end
|
.truncate(path, length) ⇒ Object
203
204
205
206
|
# File 'lib/memfs/file.rb', line 203
def self.truncate(path, length)
fs.find!(path).content.truncate(length)
SUCCESS
end
|
.umask(integer = nil) ⇒ Object
208
209
210
211
212
213
214
|
# File 'lib/memfs/file.rb', line 208
def self.umask(integer = nil)
old_value = @umask || original_file_class.umask
@umask = integer if integer
old_value
end
|
.unlink(*paths) ⇒ Object
Also known as:
delete
216
217
218
219
220
221
|
# File 'lib/memfs/file.rb', line 216
def self.unlink(*paths)
paths.each do |path|
fs.unlink(path)
end
paths.size
end
|
.utime(atime, mtime, *file_names) ⇒ Object
224
225
226
227
228
229
230
|
# File 'lib/memfs/file.rb', line 224
def self.utime(atime, mtime, *file_names)
file_names.each do |file_name|
fs.find!(file_name).atime = atime
fs.find!(file_name).mtime = mtime
end
file_names.size
end
|
Instance Method Details
#atime ⇒ Object
254
255
256
|
# File 'lib/memfs/file.rb', line 254
def atime
File.atime(path)
end
|
#chmod(mode_int) ⇒ Object
258
259
260
261
|
# File 'lib/memfs/file.rb', line 258
def chmod(mode_int)
fs.chmod(mode_int, path)
SUCCESS
end
|
#chown(uid, gid = nil) ⇒ Object
263
264
265
266
|
# File 'lib/memfs/file.rb', line 263
def chown(uid, gid = nil)
fs.chown(uid, gid, path)
SUCCESS
end
|
#ctime ⇒ Object
268
269
270
|
# File 'lib/memfs/file.rb', line 268
def ctime
File.ctime(path)
end
|
#flock ⇒ Object
272
273
274
|
# File 'lib/memfs/file.rb', line 272
def flock(*)
SUCCESS
end
|
#lstat ⇒ Object
280
281
282
|
# File 'lib/memfs/file.rb', line 280
def lstat
File.lstat(path)
end
|
#mtime ⇒ Object
276
277
278
|
# File 'lib/memfs/file.rb', line 276
def mtime
File.mtime(path)
end
|
#size ⇒ Object
284
285
286
|
# File 'lib/memfs/file.rb', line 284
def size
entry.size
end
|
#truncate(integer) ⇒ Object
288
289
290
|
# File 'lib/memfs/file.rb', line 288
def truncate(integer)
File.truncate(path, integer)
end
|