Class: MemFs::FileSystem

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/memfs/file_system.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileSystem

Returns a new instance of FileSystem.



90
91
92
# File 'lib/memfs/file_system.rb', line 90

def initialize
  clear!
end

Instance Attribute Details

#registred_entriesObject

Returns the value of attribute registred_entries.



12
13
14
# File 'lib/memfs/file_system.rb', line 12

def registred_entries
  @registred_entries
end

#rootObject

Returns the value of attribute root.



12
13
14
# File 'lib/memfs/file_system.rb', line 12

def root
  @root
end

#working_directoryObject

Returns the value of attribute working_directory.



12
13
14
# File 'lib/memfs/file_system.rb', line 12

def working_directory
  @working_directory
end

Instance Method Details

#basename(path) ⇒ Object



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

def basename(path)
  File.basename(path)
end

#chdir(path) ⇒ Object



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

def chdir(path)
  destination = find_directory!(path)

  previous_directory = working_directory
  self.working_directory = destination

  yield if block_given?
ensure
  self.working_directory = previous_directory if block_given?
end

#chmod(mode_int, file_name) ⇒ Object



38
39
40
# File 'lib/memfs/file_system.rb', line 38

def chmod(mode_int, file_name)
  find!(file_name).mode = mode_int
end

#chown(uid, gid, path) ⇒ Object



42
43
44
45
46
# File 'lib/memfs/file_system.rb', line 42

def chown(uid, gid, path)
  entry = find!(path).dereferenced
  entry.uid = uid if uid && uid != -1
  entry.gid = gid if gid && gid != -1
end

#clear!Object



31
32
33
34
35
36
# File 'lib/memfs/file_system.rb', line 31

def clear!
  MemFs.reset_platform_root!
  self.root = Fake::Directory.new(MemFs.platform_root)
  mkdir File.join(MemFs.platform_root, 'tmp')
  chdir MemFs.platform_root
end

#dirname(path) ⇒ Object



48
49
50
# File 'lib/memfs/file_system.rb', line 48

def dirname(path)
  File.dirname(path)
end

#entries(path) ⇒ Object



52
53
54
# File 'lib/memfs/file_system.rb', line 52

def entries(path)
  find_directory!(path).entry_names
end

#find(path) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/memfs/file_system.rb', line 56

def find(path)
  path = MemFs.normalize_path(path)

  if MemFs.root_path?(path)
    root
  elsif dirname(path) == '.'
    working_directory.find(path)
  else
    root.find(path)
  end
end

#find!(path) ⇒ Object



68
69
70
# File 'lib/memfs/file_system.rb', line 68

def find!(path)
  find(path) || fail(Errno::ENOENT, path)
end

#find_directory!(path) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/memfs/file_system.rb', line 72

def find_directory!(path)
  entry = find!(path).dereferenced

  fail Errno::ENOTDIR, path unless entry.is_a?(Fake::Directory)

  entry
end

#find_parent!(path) ⇒ Object



80
81
82
83
# File 'lib/memfs/file_system.rb', line 80

def find_parent!(path)
  parent_path = dirname(path)
  find_directory!(parent_path)
end

#getwdObject Also known as: pwd



85
86
87
# File 'lib/memfs/file_system.rb', line 85

def getwd
  working_directory.path
end


94
95
96
97
98
99
100
101
102
# File 'lib/memfs/file_system.rb', line 94

def link(old_name, new_name)
  file = find!(old_name)

  fail Errno::EEXIST, "(#{old_name}, #{new_name})" if find(new_name)

  link = file.dup
  link.name = basename(new_name)
  find_parent!(new_name).add_entry link
end

#mkdir(path, mode = 0o777) ⇒ Object



104
105
106
107
108
109
# File 'lib/memfs/file_system.rb', line 104

def mkdir(path, mode = 0o777)
  fail Errno::EEXIST, path if find(path)
  directory = Fake::Directory.new(path)
  directory.mode = mode
  find_parent!(path).add_entry directory
end

#pathsObject



111
112
113
# File 'lib/memfs/file_system.rb', line 111

def paths
  root.paths
end

#rename(old_name, new_name) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/memfs/file_system.rb', line 115

def rename(old_name, new_name)
  file = find!(old_name)
  file.delete

  file.name = basename(new_name)
  find_parent!(new_name).add_entry(file)
end

#rmdir(path) ⇒ Object



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

def rmdir(path)
  directory = find!(path)

  fail Errno::ENOTEMPTY, path unless directory.empty?

  directory.delete
end


131
132
133
134
135
# File 'lib/memfs/file_system.rb', line 131

def symlink(old_name, new_name)
  fail Errno::EEXIST, new_name if find(new_name)

  find_parent!(new_name).add_entry Fake::Symlink.new(new_name, old_name)
end

#touch(*paths) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/memfs/file_system.rb', line 137

def touch(*paths)
  paths.each do |path|
    entry = find(path)

    unless entry
      entry = Fake::File.new(path)
      parent_dir = find_parent!(path)
      parent_dir.add_entry entry
    end

    entry.touch
  end
end


151
152
153
154
155
156
157
# File 'lib/memfs/file_system.rb', line 151

def unlink(path)
  entry = find!(path)

  fail Errno::EPERM, path if entry.is_a?(Fake::Directory)

  entry.delete
end