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.



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

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



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

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

#chown(uid, gid, path) ⇒ Object



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

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

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

#dirname(path) ⇒ Object



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

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

#entries(path) ⇒ Object



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

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

#find(path) ⇒ Object



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

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

#find!(path) ⇒ Object



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

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

#find_directory!(path) ⇒ Object

Raises:

  • (Errno::ENOTDIR)


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

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

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

  entry
end

#find_parent!(path) ⇒ Object



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

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

#getwdObject Also known as: pwd



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

def getwd
  working_directory.path
end

Raises:

  • (Errno::EEXIST)


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

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)


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

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

#rename(old_name, new_name) ⇒ Object



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

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)


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

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

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

  directory.delete
end

Raises:

  • (Errno::EEXIST)


121
122
123
124
125
# File 'lib/memfs/file_system.rb', line 121

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



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/memfs/file_system.rb', line 127

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)


141
142
143
144
145
146
147
# File 'lib/memfs/file_system.rb', line 141

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

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

  entry.delete
end