Class: MemFs::Dir

Inherits:
Object
  • Object
show all
Extended by:
FilesystemAccess
Includes:
Enumerable, FilesystemAccess
Defined in:
lib/memfs/dir.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Dir

Returns a new instance of Dir.



96
97
98
99
100
101
# File 'lib/memfs/dir.rb', line 96

def initialize(path)
  self.entry = fs.find_directory!(path)
  self.state = :open
  @pos = 0
  self.max_seek = 0
end

Instance Attribute Details

#posObject

Returns the value of attribute pos.



9
10
11
# File 'lib/memfs/dir.rb', line 9

def pos
  @pos
end

Class Method Details

.[](*patterns) ⇒ Object



11
12
13
# File 'lib/memfs/dir.rb', line 11

def self.[](*patterns)
  glob(patterns)
end

.chdir(path, &block) ⇒ Object



15
16
17
18
# File 'lib/memfs/dir.rb', line 15

def self.chdir(path, &block)
  fs.chdir path, &block
  0
end

.chroot(path) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/memfs/dir.rb', line 20

def self.chroot(path)
  fail Errno::EPERM, path unless Process.uid.zero?

  dir = fs.find_directory!(path)
  dir.name = '/'
  fs.root = dir
  0
end

.entries(dirname, _opts = {}) ⇒ Object



29
30
31
# File 'lib/memfs/dir.rb', line 29

def self.entries(dirname, _opts = {})
  fs.entries(dirname)
end

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

Returns:

  • (Boolean)


33
34
35
# File 'lib/memfs/dir.rb', line 33

def self.exists?(path)
  File.directory?(path)
end

.foreach(dirname, &block) ⇒ Object



38
39
40
41
42
# File 'lib/memfs/dir.rb', line 38

def self.foreach(dirname, &block)
  return to_enum(__callee__, dirname) unless block

  entries(dirname).each(&block)
end

.getwdObject Also known as: pwd



44
45
46
# File 'lib/memfs/dir.rb', line 44

def self.getwd
  fs.getwd
end

.glob(patterns, flags = 0) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/memfs/dir.rb', line 49

def self.glob(patterns, flags = 0)
  patterns = [*patterns]
  list = fs.paths.select do |path|
    patterns.any? do |pattern|
      File.fnmatch?(pattern, path, flags | GLOB_FLAGS)
    end
  end
  # FIXME: ugly special case for /* and /
  list.delete('/') if patterns.first == '/*'
  return list unless block_given?
  list.each { |path| yield path }
  nil
end

.home(*args) ⇒ Object



63
64
65
# File 'lib/memfs/dir.rb', line 63

def self.home(*args)
  original_dir_class.home(*args)
end

.mkdir(path, mode = 0777) ⇒ Object



67
68
69
# File 'lib/memfs/dir.rb', line 67

def self.mkdir(path, mode = 0777)
  fs.mkdir path, mode
end

.open(dirname) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/memfs/dir.rb', line 71

def self.open(dirname)
  dir = new(dirname)

  if block_given?
    yield dir
  else
    dir
  end
ensure
  dir && dir.close if block_given?
end

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



83
84
85
# File 'lib/memfs/dir.rb', line 83

def self.rmdir(path)
  fs.rmdir path
end

.tmpdirObject



87
88
89
# File 'lib/memfs/dir.rb', line 87

def self.tmpdir
  '/tmp'
end

Instance Method Details

#closeObject



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

def close
  fail IOError, 'closed directory' if state == :closed
  self.state = :closed
end

#each(&block) ⇒ Object



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

def each(&block)
  return to_enum(__callee__) unless block
  entry.entry_names.each(&block)
end

#pathObject Also known as: to_path



113
114
115
# File 'lib/memfs/dir.rb', line 113

def path
  entry.path
end

#readObject



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

def read
  name = entries[pos]
  @pos += 1
  self.max_seek = pos
  name
end

#rewindObject



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

def rewind
  @pos = 0
  self
end

#seek(position) ⇒ Object



135
136
137
138
# File 'lib/memfs/dir.rb', line 135

def seek(position)
  @pos = position if (0..max_seek).cover?(position)
  self
end

#tellObject



140
141
142
# File 'lib/memfs/dir.rb', line 140

def tell
  @pos
end