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.



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

def initialize
  clear!
end

Instance Attribute Details

#registred_entriesObject

Returns the value of attribute registred_entries.



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

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.



10
11
12
# File 'lib/memfs/file_system.rb', line 10

def working_directory
  @working_directory
end

Instance Method Details

#basename(path) ⇒ Object



14
15
16
# File 'lib/memfs/file_system.rb', line 14

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

#chdir(path, &block) ⇒ Object



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

def chdir(path, &block)
  destination = find_directory!(path)

  previous_directory = working_directory
  self.working_directory = destination

  block.call if block
ensure
  if block
    self.working_directory = previous_directory
  end
end

#chmod(mode_int, file_name) ⇒ Object



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

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

#chown(uid, gid, path) ⇒ Object



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

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
# File 'lib/memfs/file_system.rb', line 31

def clear!
  self.root = Fake::Directory.new('/')
  self.mkdir('/tmp')
  self.chdir('/')
end

#dirname(path) ⇒ Object



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

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

#entries(path) ⇒ Object



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

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

#find(path) ⇒ Object



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

def find(path)
  if path == '/'
    root
  elsif dirname(path) == '.'
    working_directory.find(path)
  else
    root.find(path)
  end
end

#find!(path) ⇒ Object



65
66
67
# File 'lib/memfs/file_system.rb', line 65

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

#find_directory!(path) ⇒ Object

Raises:

  • (Errno::ENOTDIR)


69
70
71
72
73
74
75
# File 'lib/memfs/file_system.rb', line 69

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

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

  entry
end

#find_parent!(path) ⇒ Object



77
78
79
80
# File 'lib/memfs/file_system.rb', line 77

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

#getwdObject Also known as: pwd



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

def getwd
  working_directory.path
end

Raises:

  • (Errno::EEXIST)


91
92
93
94
95
96
97
98
99
# File 'lib/memfs/file_system.rb', line 91

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

  raise 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) ⇒ Object

Raises:

  • (Errno::EEXIST)


101
102
103
104
# File 'lib/memfs/file_system.rb', line 101

def mkdir(path)
  raise Errno::EEXIST, path if find(path)
  find_parent!(path).add_entry Fake::Directory.new(path)
end

#pathsObject



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

def paths
  root.paths
end

#rename(old_name, new_name) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/memfs/file_system.rb', line 110

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

Raises:

  • (Errno::ENOTEMPTY)


118
119
120
121
122
123
124
# File 'lib/memfs/file_system.rb', line 118

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

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

  directory.delete
end

Raises:

  • (Errno::EEXIST)


126
127
128
129
130
# File 'lib/memfs/file_system.rb', line 126

def symlink(old_name, new_name)
  raise 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



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/memfs/file_system.rb', line 132

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

Raises:

  • (Errno::EPERM)


146
147
148
149
150
151
152
# File 'lib/memfs/file_system.rb', line 146

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

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

  entry.delete
end