Class: VirtFS::VDir

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir_obj, path) ⇒ VDir

VDir initializer

Parameters:

  • dir_obj (VirtFS::FS::Dir)

    handle to filesystem specific dir obj

  • path (String)

    path at which the dir resides



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_objObject

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

Parameters:

  • glob_pattern (String, Regex)

    dir entry pattern to match

See Also:

  • Dir.[]


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

Parameters:

  • dir (String) (defaults to: nil)

    path to change working directory to

Raises:

  • (SystemCallError)

See Also:

  • Dir.chdir


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

Parameters:

  • dir (String)

    dir to change root dir to

See Also:

  • Dir.chroot


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

Parameters:

  • dir (String)

    dir to delete

See Also:

  • Dir.delete


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

Parameters:

  • dir (String)

    dir which to enumerate entries

Returns:

  • (Array<DirEntry>)

    array of dir entry instances

See Also:

  • Dir.entries


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

Parameters:

  • dir (String)

    directory path to verify

Returns:

  • (Boolean)

    indicating if 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

Parameters:

  • dir (String)

    dir which to lookup entries

Yields:

  • block to invoke



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

.getwdString Also known as: pwd

Returns current working directory.

Returns:

  • (String)

    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

Parameters:

  • glob_pattern (String)

    pattern to match

  • flags (Integer) (defaults to: 0)

    file match flags

Yields:

  • block invoked with each match if specified

See Also:



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

Parameters:

  • dir (String)

    path to create

  • permissions (Integer) (defaults to: 0700)

    initial permission to assign to dir



188
189
190
191
# File 'lib/virtfs/v_dir.rb', line 188

def mkdir(dir, permissions = 0700)
  VirtFS.fs_lookup_call(dir, true) { |p| dir_mkdir(p, permissions) }
  0
end

.new(dir, hash_args = {}) ⇒ Object

Instantiate new directory instance.

Parameters:

  • dir (String)

    path to dir to instantiate

  • hash_args (Hash) (defaults to: {})

    args to use when creating Dir instance

See Also:



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

Parameters:

  • dir (String)

    path to dir to instantiate

  • hash_args (Hash) (defaults to: {})

    args to use when creating Dir instance

Yields:

  • the directory instance

See Also:



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

#eachObject

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

#pathString Also known as: to_path

Returns path which dir resides.

Returns:

  • (String)

    path which dir resides



32
33
34
# File 'lib/virtfs/v_dir.rb', line 32

def path
  @open_path
end

#rewindObject



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