class Ole::Storage::DirClass
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.
Public Class Methods
new(ole)
click to toggle source
# File lib/ole/storage/file_system.rb, line 254 def initialize ole @ole = ole @pwd = '' end
Public Instance Methods
chdir(orig_path) { || ... }
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/ole/storage/file_system.rb, line 322 def foreach path, &block entries(path).each(&block) end
mkdir(path)
click to toggle source
# 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)
click to toggle source
as for file, explicit alias to inhibit block
# File lib/ole/storage/file_system.rb, line 277 def new path open path end
open(path) { |dir| ... }
click to toggle source
# 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
pwd()
click to toggle source
pwd is always stored without the trailing slash. we handle the root case here
# File lib/ole/storage/file_system.rb, line 283 def pwd return '/' if @pwd.empty? @pwd end
Also aliased as: getwd
rmdir(path)
click to toggle source
# 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
Private Instance Methods
dirent_from_path(path, orig_path=nil)
click to toggle source
orig_path
is just so that we can use the requested path in the error messages even if it has been already modified
# File lib/ole/storage/file_system.rb, line 261 def dirent_from_path path, orig_path=nil orig_path ||= path dirent = @ole.dirent_from_path path raise Errno::ENOENT, orig_path unless dirent raise Errno::ENOTDIR, orig_path unless dirent.dir? dirent end