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.



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

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



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

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



35
36
37
# File 'lib/memfs/file_system.rb', line 35

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

#chown(uid, gid, path) ⇒ Object



39
40
41
42
43
# File 'lib/memfs/file_system.rb', line 39

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



29
30
31
32
33
# File 'lib/memfs/file_system.rb', line 29

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

#dirname(path) ⇒ Object



45
46
47
# File 'lib/memfs/file_system.rb', line 45

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

#entries(path) ⇒ Object



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

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

#find(path) ⇒ Object



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

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

#find!(path) ⇒ Object



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

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

#find_directory!(path) ⇒ Object



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

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

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

  entry
end

#find_parent!(path) ⇒ Object



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

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

#getwdObject Also known as: pwd



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

def getwd
  working_directory.path
end


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

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 = 0777) ⇒ Object



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

def mkdir(path, mode = 0777)
  fail Errno::EEXIST, path if find(path)
  directory = Fake::Directory.new(path)
  directory.mode = mode
  find_parent!(path).add_entry directory
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



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

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

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

  directory.delete
end


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

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



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


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

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

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

  entry.delete
end