Class: MemFs::File

Inherits:
IO
  • Object
show all
Extended by:
FilesystemAccess, SingleForwardable
Includes:
Enumerable, FilesystemAccess
Defined in:
lib/memfs/file.rb,
lib/memfs/file/stat.rb

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
}.freeze
SEPARATOR =
'/'.freeze
SUCCESS =
0

Instance Attribute Summary collapse

Attributes inherited from IO

#autoclose, #close_on_exec

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from IO

#<<, #advise, #autoclose?, #binmode, #binmode?, #close, #close_on_exec?, #closed?, #each, #each_byte, #each_char, #eof?, #external_encoding, #pos, #print, #printf, #puts, read, #read, #seek, #stat, write, #write

Constructor Details

#initialize(filename, mode = File::RDONLY, *perm_and_or_opt) ⇒ File

Returns a new instance of File.



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
# File 'lib/memfs/file.rb', line 237

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_and_or_opt.shift
  if perm_and_or_opt.any?
    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)
  # FIXME: this is an ugly way to ensure a symlink has a target
  entry.dereferenced

  if entry.respond_to?(:pos=)
    entry.pos = 0
  end
  entry.content.clear if truncate_file?
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



235
236
237
# File 'lib/memfs/file.rb', line 235

def path
  @path
end

Class Method Details

.absolute_path(path, dir_string = fs.pwd) ⇒ Object



78
79
80
# File 'lib/memfs/file.rb', line 78

def self.absolute_path(path, dir_string = fs.pwd)
  original_file_class.absolute_path(path, dir_string)
end

.atime(path) ⇒ Object



82
83
84
# File 'lib/memfs/file.rb', line 82

def self.atime(path)
  stat(path).atime
end

.chmod(mode_int, *paths) ⇒ Object



86
87
88
89
90
# File 'lib/memfs/file.rb', line 86

def self.chmod(mode_int, *paths)
  paths.each do |path|
    fs.chmod mode_int, path
  end
end

.chown(uid, gid, *paths) ⇒ Object



92
93
94
95
96
97
# File 'lib/memfs/file.rb', line 92

def self.chown(uid, gid, *paths)
  paths.each do |path|
    fs.chown(uid, gid, path)
  end
  paths.size
end

.ctime(path) ⇒ Object



99
100
101
# File 'lib/memfs/file.rb', line 99

def self.ctime(path)
  stat(path).ctime
end

.exists?(path) ⇒ Boolean Also known as: exist?

Returns:

  • (Boolean)


103
104
105
# File 'lib/memfs/file.rb', line 103

def self.exists?(path)
  !!fs.find(path)
end

.expand_path(file_name, dir_string = fs.pwd) ⇒ Object



108
109
110
# File 'lib/memfs/file.rb', line 108

def self.expand_path(file_name, dir_string = fs.pwd)
  original_file_class.expand_path(file_name, dir_string)
end

.ftype(path) ⇒ Object



112
113
114
# File 'lib/memfs/file.rb', line 112

def self.ftype(path)
  fs.find!(path) && lstat(path).ftype
end

.identical?(path1, path2) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
122
# File 'lib/memfs/file.rb', line 118

def self.identical?(path1, path2)
  fs.find!(path1).dereferenced === fs.find!(path2).dereferenced
rescue Errno::ENOENT
  false
end

.lchmod(mode_int, *file_names) ⇒ Object



124
125
126
127
128
# File 'lib/memfs/file.rb', line 124

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



130
131
132
# File 'lib/memfs/file.rb', line 130

def self.lchown(uid, gid, *paths)
  chown uid, gid, *paths
end


134
135
136
137
# File 'lib/memfs/file.rb', line 134

def self.link(old_name, new_name)
  fs.link old_name, new_name
  SUCCESS
end

.lstat(path) ⇒ Object



139
140
141
# File 'lib/memfs/file.rb', line 139

def self.lstat(path)
  Stat.new(path)
end

.mtime(path) ⇒ Object



143
144
145
# File 'lib/memfs/file.rb', line 143

def self.mtime(path)
  stat(path).mtime
end

.open(filename, mode = RDONLY, *perm_and_opt) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/memfs/file.rb', line 147

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


159
160
161
# File 'lib/memfs/file.rb', line 159

def self.readlink(path)
  fs.find!(path).target
end

.realdirpath(path, dir_string = fs.pwd) ⇒ Object



163
164
165
# File 'lib/memfs/file.rb', line 163

def self.realdirpath(path, dir_string = fs.pwd)
  loose_dereference_path(absolute_path(path, dir_string))
end

.realpath(path, dir_string = fs.pwd) ⇒ Object



167
168
169
# File 'lib/memfs/file.rb', line 167

def self.realpath(path, dir_string = fs.pwd)
  dereference_path(absolute_path(path, dir_string))
end

.rename(old_name, new_name) ⇒ Object



171
172
173
174
# File 'lib/memfs/file.rb', line 171

def self.rename(old_name, new_name)
  fs.rename(old_name, new_name)
  SUCCESS
end

.reset!Object



176
177
178
# File 'lib/memfs/file.rb', line 176

def self.reset!
  @umask = original_file_class.umask
end

.size(path) ⇒ Object



180
181
182
# File 'lib/memfs/file.rb', line 180

def self.size(path)
  fs.find!(path).size
end

.size?(path) ⇒ Boolean

Returns:

  • (Boolean)


184
185
186
187
188
189
190
191
# File 'lib/memfs/file.rb', line 184

def self.size?(path)
  file = fs.find(path)
  if file && file.size > 0
    file.size
  else
    false
  end
end

.stat(path) ⇒ Object



193
194
195
# File 'lib/memfs/file.rb', line 193

def self.stat(path)
  Stat.new(path, true)
end


197
198
199
200
# File 'lib/memfs/file.rb', line 197

def self.symlink(old_name, new_name)
  fs.symlink old_name, new_name
  SUCCESS
end

.symlink?(path) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/memfs/file.rb', line 202

def self.symlink?(path)
  lstat_query(path, :symlink?)
end

.truncate(path, length) ⇒ Object



206
207
208
209
# File 'lib/memfs/file.rb', line 206

def self.truncate(path, length)
  fs.find!(path).content.truncate(length)
  SUCCESS
end

.umask(integer = nil) ⇒ Object



211
212
213
214
215
216
217
# File 'lib/memfs/file.rb', line 211

def self.umask(integer = nil)
  old_value = @umask || original_file_class.umask

  @umask = integer if integer

  old_value
end


219
220
221
222
223
224
# File 'lib/memfs/file.rb', line 219

def self.unlink(*paths)
  paths.each do |path|
    fs.unlink(path)
  end
  paths.size
end

.utime(atime, mtime, *file_names) ⇒ Object



227
228
229
230
231
232
233
# File 'lib/memfs/file.rb', line 227

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

#atimeObject



263
264
265
# File 'lib/memfs/file.rb', line 263

def atime
  File.atime(path)
end

#chmod(mode_int) ⇒ Object



267
268
269
270
# File 'lib/memfs/file.rb', line 267

def chmod(mode_int)
  fs.chmod(mode_int, path)
  SUCCESS
end

#chown(uid, gid = nil) ⇒ Object



272
273
274
275
# File 'lib/memfs/file.rb', line 272

def chown(uid, gid = nil)
  fs.chown(uid, gid, path)
  SUCCESS
end

#ctimeObject



277
278
279
# File 'lib/memfs/file.rb', line 277

def ctime
  File.ctime(path)
end

#flockObject



281
282
283
# File 'lib/memfs/file.rb', line 281

def flock(*)
  SUCCESS
end

#lstatObject



289
290
291
# File 'lib/memfs/file.rb', line 289

def lstat
  File.lstat(path)
end

#mtimeObject



285
286
287
# File 'lib/memfs/file.rb', line 285

def mtime
  File.mtime(path)
end

#sizeObject



293
294
295
# File 'lib/memfs/file.rb', line 293

def size
  entry.size
end

#truncate(integer) ⇒ Object



297
298
299
# File 'lib/memfs/file.rb', line 297

def truncate(integer)
  File.truncate(path, integer)
end