Class: VirtFS::VDir
- Inherits:
-
Object
- Object
- VirtFS::VDir
- Includes:
- DirInstanceDelegate
- Defined in:
- lib/virtfs/v_dir.rb
Overview
VirtFS Dir representation - implements the core Ruby Dir methods, dispatching to underlying mounted VirtFS filesystems
Instance Attribute Summary collapse
-
#fs_mod_obj ⇒ Object
rubocop:disable ClassLength.
Class Method Summary collapse
-
.[](glob_pattern) ⇒ Object
Return dir entries matching the specified glob pattern.
-
.chdir(dir = nil) ⇒ Object
Change working directory to specified dir.
-
.chroot(dir) ⇒ Object
Change root dir to specified dir.
-
.delete(dir) ⇒ Object
(also: unlink, rmdir)
Delete specified dir.
-
.entries(dir) ⇒ Array<DirEntry>
Return array containing entries in specified dir.
-
.exist?(dir) ⇒ Boolean
(also: exists?)
Return bool indicating if specified dir exists.
-
.foreach(dir) { ... } ⇒ Object
Invoke block for each entry in dir.
-
.getwd ⇒ String
(also: pwd)
Current working directory.
-
.glob(glob_pattern, flags = 0) { ... } ⇒ Object
Return directory entries matching specified glob pattern.
- .home(*args) ⇒ Object
-
.mkdir(dir, permissions = 0700) ⇒ Object
Make new dir at specified path.
-
.new(dir, hash_args = {}) ⇒ Object
Instantiate new directory instance.
-
.open(dir, hash_args = {}) { ... } ⇒ Object
Open specified existing dir and invoke block with it before closing.
Instance Method Summary collapse
-
#each ⇒ Object
Some methods need to return the Dir object.
-
#initialize(dir_obj, path) ⇒ VDir
constructor
VDir initializer.
-
#path ⇒ String
(also: #to_path)
Path which dir resides.
- #rewind ⇒ Object
- #seek(*args) ⇒ Object
Constructor Details
#initialize(dir_obj, path) ⇒ VDir
VDir initializer
16 17 18 19 |
# File 'lib/virtfs/v_dir.rb', line 16 def initialize(dir_obj, path) @open_path = path __setobj__(dir_obj) end |
Instance Attribute Details
#fs_mod_obj ⇒ Object
rubocop:disable ClassLength
5 6 7 |
# File 'lib/virtfs/v_dir.rb', line 5 def fs_mod_obj @fs_mod_obj end |
Class Method Details
.[](glob_pattern) ⇒ Object
Return dir entries matching the specified glob pattern
54 55 56 |
# File 'lib/virtfs/v_dir.rb', line 54 def [](glob_pattern) glob(glob_pattern, 0) end |
.chdir(dir = nil) ⇒ Object
Change working directory to specified dir
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/virtfs/v_dir.rb', line 63 def chdir(dir = nil) dir ||= VfsRealDir.home raise SystemCallError.new(dir, Errno::ENOENT::Errno) unless exist?(dir) if block_given? pwd = getwd begin VirtFS.dir_chdir(dir) return yield(getwd) ensure VirtFS.dir_chdir(pwd) end end VirtFS.dir_chdir(dir) 0 end |
.chroot(dir) ⇒ Object
Change root dir to specified dir
84 85 86 87 |
# File 'lib/virtfs/v_dir.rb', line 84 def chroot(dir) VirtFS.dir_chroot(dir) 0 end |
.delete(dir) ⇒ Object Also known as: unlink, rmdir
Delete specified dir
94 95 96 97 |
# File 'lib/virtfs/v_dir.rb', line 94 def delete(dir) VirtFS.fs_lookup_call(dir, true) { |p| dir_delete(p) } 0 end |
.entries(dir) ⇒ Array<DirEntry>
Return array containing entries in specified dir
109 110 111 |
# File 'lib/virtfs/v_dir.rb', line 109 def entries(dir) VirtFS.fs_lookup_call(dir) { |p| dir_entries(p) } end |
.exist?(dir) ⇒ Boolean Also known as: exists?
Return bool indicating if specified dir exists
118 119 120 121 122 123 124 125 |
# File 'lib/virtfs/v_dir.rb', line 118 def exist?(dir) begin fs, p = VirtFS.path_lookup(dir) rescue Errno::ENOENT return false end VirtFS.fs_call(fs) { dir_exist?(p) } end |
.foreach(dir) { ... } ⇒ Object
Invoke block for each entry in dir
132 133 134 |
# File 'lib/virtfs/v_dir.rb', line 132 def foreach(dir, &block) VirtFS.fs_lookup_call(dir) { |p| dir_foreach(p, &block) } end |
.getwd ⇒ String Also known as: pwd
Returns current working directory.
137 138 139 |
# File 'lib/virtfs/v_dir.rb', line 137 def getwd VirtFS.dir_getwd end |
.glob(glob_pattern, flags = 0) { ... } ⇒ Object
Return directory entries matching specified glob pattern
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 |
# File 'lib/virtfs/v_dir.rb', line 152 def glob(glob_pattern, flags = 0) search_path, specified_path, glob = VirtFS.dir_and_glob(glob_pattern) unless exist?(search_path) return [] unless block_given? return false end ra = [] unless block_given? VirtFS.find(search_path, VirtFS.glob_depth(glob)) do |p| next if p == search_path if search_path == VfsRealFile::SEPARATOR p.sub!(VfsRealFile::SEPARATOR, "") else p.sub!("#{search_path}#{VfsRealFile::SEPARATOR}", "") end next if p == "" next unless VfsRealFile.fnmatch(glob, p, flags) p = VfsRealFile.join(specified_path, p) if specified_path block_given? ? yield(p) : ra << p end block_given? ? false : ra.sort_by(&:downcase) end |
.home(*args) ⇒ Object
179 180 181 |
# File 'lib/virtfs/v_dir.rb', line 179 def home(*args) VfsRealDir.home(*args) end |
.mkdir(dir, permissions = 0700) ⇒ Object
Make new dir at specified path
188 189 190 191 |
# File 'lib/virtfs/v_dir.rb', line 188 def mkdir(dir, = 0700) VirtFS.fs_lookup_call(dir, true) { |p| dir_mkdir(p, ) } 0 end |
.new(dir, hash_args = {}) ⇒ Object
Instantiate new directory instance.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/virtfs/v_dir.rb', line 201 def new(dir, hash_args = {}) fs, p = VirtFS.path_lookup(dir) fs_obj = VirtFS.fs_call(fs) { dir_new(p, hash_args, dir, VDir.getwd) } obj = allocate if fs.thin_interface? obj.send(:initialize, ThinDirDelegator.new(fs_obj, dir, p, hash_args), dir) else obj.send(:initialize, fs_obj, dir) end # fs_mod_obj always points to the fs module's file object # for use by fs-specific extension modules obj.fs_mod_obj = fs_obj obj.extend(fs_obj.extension_module) if fs_obj.respond_to?(:extension_module) # fs-specific extension module obj end |
.open(dir, hash_args = {}) { ... } ⇒ Object
Open specified existing dir and invoke block with it before closing
227 228 229 230 231 232 233 234 235 |
# File 'lib/virtfs/v_dir.rb', line 227 def open(dir, hash_args = {}) dir_obj = new(dir, hash_args) return dir_obj unless block_given? begin return yield(dir_obj) ensure dir_obj.close end end |
Instance Method Details
#each ⇒ Object
Some methods need to return the Dir object. Methods in the delegator object can’t do that, so we intercept them and do it here.
26 27 28 29 |
# File 'lib/virtfs/v_dir.rb', line 26 def each return self if (rv = super) == __getobj__ rv end |
#path ⇒ String Also known as: to_path
Returns path which dir resides.
32 33 34 |
# File 'lib/virtfs/v_dir.rb', line 32 def path @open_path end |
#rewind ⇒ Object
37 38 39 40 |
# File 'lib/virtfs/v_dir.rb', line 37 def rewind super self end |
#seek(*args) ⇒ Object
42 43 44 45 |
# File 'lib/virtfs/v_dir.rb', line 42 def seek(*args) super self end |