Class: Ole::Storage::DirClass

Inherits:
Object
  • Object
show all
Defined in:
lib/ole/storage/file_system.rb

Overview

An instance of this class is supposed to provide similar methods to the class methods of Dir itself.

Fairly complete - like zip/zipfilesystem’s implementation, i provide everything except chroot and glob. glob could be done with a glob to regex conversion, and then simply match in the entries array… although recursive glob complicates that somewhat.

Dir.chroot, Dir.glob, Dir.[], and Dir.tmpdir is the complete list of methods still missing.

Defined Under Namespace

Classes: Dir

Instance Method Summary collapse

Constructor Details

#initialize(ole) ⇒ DirClass

Returns a new instance of DirClass.



254
255
256
257
# File 'lib/ole/storage/file_system.rb', line 254

def initialize ole
  @ole = ole
  @pwd = ''
end

Instance Method Details

#chdir(orig_path) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/ole/storage/file_system.rb', line 289

def chdir orig_path
  # make path absolute, squeeze slashes, and remove trailing slash
  path = @ole.file.expand_path(orig_path).squeeze('/').sub(/\/$/, '')
  # this is just for the side effects of the exceptions if invalid
  dirent_from_path path, orig_path
  if block_given?
    old_pwd = @pwd
    begin
      @pwd = path
      yield
    ensure
      @pwd = old_pwd
    end
  else
    @pwd = path
    0
  end
end

#entries(path) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/ole/storage/file_system.rb', line 308

def entries path
  dirent = dirent_from_path path
  # Not sure about adding on the dots...
  entries = %w[. ..] + dirent.children.map(&:name)
  # do some checks about un-reachable files
  seen = {}
  entries.each do |n|
    Log.warn "inaccessible file (filename contains slash) - #{n.inspect}" if n['/']
    Log.warn "inaccessible file (duplicate filename) - #{n.inspect}" if seen[n]
    seen[n] = true
  end
  entries
end

#foreach(path, &block) ⇒ Object



322
323
324
# File 'lib/ole/storage/file_system.rb', line 322

def foreach path, &block
  entries(path).each(&block)
end

#mkdir(path) ⇒ Object

Raises:

  • (Errno::EEXIST)


326
327
328
329
330
331
332
333
334
335
336
# File 'lib/ole/storage/file_system.rb', line 326

def mkdir path
  parent_path, basename = File.split @ole.file.expand_path(path)
  # note that we will complain about the full path despite accessing
  # the parent path. this is consistent with ::Dir
  parent = dirent_from_path parent_path, path
  # now, we first should ensure that it doesn't already exist
  # either as a file or a directory.
  raise Errno::EEXIST, path if parent/basename
  parent << Dirent.new(@ole, :type => :dir, :name => basename)
  0
end

#new(path) ⇒ Object

as for file, explicit alias to inhibit block



277
278
279
# File 'lib/ole/storage/file_system.rb', line 277

def new path
  open path
end

#open(path) {|dir| ... } ⇒ Object

Yields:



270
271
272
273
274
# File 'lib/ole/storage/file_system.rb', line 270

def open path
  dir = Dir.new path, entries(path)
  return dir unless block_given?
  yield dir
end

#pwdObject Also known as: getwd

pwd is always stored without the trailing slash. we handle the root case here



283
284
285
286
# File 'lib/ole/storage/file_system.rb', line 283

def pwd
  return '/' if @pwd.empty?
  @pwd
end

#rmdir(path) ⇒ Object Also known as: delete, unlink

Raises:

  • (Errno::ENOTEMPTY)


338
339
340
341
342
343
# File 'lib/ole/storage/file_system.rb', line 338

def rmdir path
  dirent = dirent_from_path path
  raise Errno::ENOTEMPTY, path unless dirent.children.empty?
  dirent.parent.delete dirent
  0 # hmmm. as per ::Dir ?
end