Module: FakeFS::FileUtils
Overview
FileUtils module
Instance Method Summary collapse
- #cd(dir, &block) ⇒ Object (also: #chdir)
- #chmod(mode, list, _options = {}) ⇒ Object
- #chmod_R(mode, list, _options = {}) ⇒ Object
- #chown(user, group, list, _options = {}) ⇒ Object
- #chown_R(user, group, list, _options = {}) ⇒ Object
- #compare_file(file1, file2) ⇒ Object (also: #cmp, #identical?)
- #copy_entry(src, dest, preserve = false, dereference_root = false, remove_destination = false) ⇒ Object
- #copy_file(src, dest, _preserve = false, _dereference = true) ⇒ Object
- #cp(src, dest, options = {}) ⇒ Object (also: #copy)
- #cp_r(src, dest, options = {}) ⇒ Object
- #ln_s(target, path, options = {}) ⇒ Object (also: #symlink)
- #ln_sf(target, path) ⇒ Object
- #mkdir(list, _ignored_options = {}) ⇒ Object
- #mkdir_p(list, options = {}) ⇒ Object (also: #mkpath, #makedirs)
- #mv(src, dest, options = {}) ⇒ Object (also: #move)
- #remove_entry_secure(path, force = false) ⇒ Object
- #rm(list, options = {}) ⇒ Object (also: #rm_r, #remove)
- #rm_f(list, options = {}) ⇒ Object (also: #safe_unlink)
- #rm_rf(list, options = {}) ⇒ Object (also: #rmtree)
- #rmdir(list, _options = {}) ⇒ Object
- #touch(list, options = {}) ⇒ Object
- #uptodate?(new, old_list) ⇒ Boolean
Instance Method Details
#cd(dir, &block) ⇒ Object Also known as: chdir
294 295 296 |
# File 'lib/fakefs/fileutils.rb', line 294 def cd(dir, &block) FileSystem.chdir(dir, &block) end |
#chmod(mode, list, _options = {}) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/fakefs/fileutils.rb', line 253 def chmod(mode, list, = {}) list = Array(list) list.each do |f| if File.exist?(f) File.chmod(mode, f) else raise Errno::ENOENT, f.to_s end end list end |
#chmod_R(mode, list, _options = {}) ⇒ Object
265 266 267 268 269 270 271 272 273 274 |
# File 'lib/fakefs/fileutils.rb', line 265 def chmod_R(mode, list, = {}) list = Array(list) list.each do |file| chmod(mode, file) [FileSystem.find_with_glob("#{file}/**/**")].flatten.each do |f| chmod(mode, f.to_s) end end list end |
#chown(user, group, list, _options = {}) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/fakefs/fileutils.rb', line 222 def chown(user, group, list, = {}) list = Array(list) list.each do |f| if File.exist?(f) uid = if user user.to_s =~ /\d+/ ? user.to_i : Etc.getpwnam(user).uid end gid = if group group.to_s =~ /\d+/ ? group.to_i : Etc.getgrnam(group).gid end File.chown(uid, gid, f) else raise Errno::ENOENT, f.to_s end end list end |
#chown_R(user, group, list, _options = {}) ⇒ Object
242 243 244 245 246 247 248 249 250 251 |
# File 'lib/fakefs/fileutils.rb', line 242 def chown_R(user, group, list, = {}) list = Array(list) list.each do |file| chown(user, group, file) [FileSystem.find_with_glob("#{file}/**/**")].flatten.each do |f| chown(user, group, f.to_s) end end list end |
#compare_file(file1, file2) ⇒ Object Also known as: cmp, identical?
299 300 301 302 |
# File 'lib/fakefs/fileutils.rb', line 299 def compare_file(file1, file2) # we do a strict comparison of both files content File.readlines(file1) == File.readlines(file2) end |
#copy_entry(src, dest, preserve = false, dereference_root = false, remove_destination = false) ⇒ Object
139 140 141 142 143 144 145 146 |
# File 'lib/fakefs/fileutils.rb', line 139 def copy_entry(src, dest, preserve = false, dereference_root = false, remove_destination = false) cp_r( src, dest, preserve: preserve, dereference_root: dereference_root, remove_destination: remove_destination ) end |
#copy_file(src, dest, _preserve = false, _dereference = true) ⇒ Object
134 135 136 137 |
# File 'lib/fakefs/fileutils.rb', line 134 def copy_file(src, dest, _preserve = false, _dereference = true) # Not a perfect match, but similar to what regular FileUtils does. cp(src, dest) end |
#cp(src, dest, options = {}) ⇒ Object Also known as: copy
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/fakefs/fileutils.rb', line 102 def cp(src, dest, = {}) raise Errno::ENOTDIR, dest.to_s if src.is_a?(Array) && !File.directory?(dest) raise Errno::ENOENT, dest.to_s unless File.exist?(dest) || File.exist?(File.dirname(dest)) # handle `verbose' flag RealFileUtils.cp src, dest, **.merge(noop: true) # handle `noop' flag return if [:noop] Array(src).each do |source| dst_file = FileSystem.find(dest) src_file = FileSystem.find(source) raise Errno::ENOENT, source.to_s unless src_file if dst_file && File.directory?(dst_file) FileSystem.add( File.join(dest, File.basename(source)), src_file.entry.clone(dst_file) ) else FileSystem.delete(dest) FileSystem.add(dest, src_file.entry.clone) end end nil end |
#cp_r(src, dest, options = {}) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/fakefs/fileutils.rb', line 148 def cp_r(src, dest, = {}) # handle `verbose' flag RealFileUtils.cp_r src, dest, **.merge(noop: true) # handle `noop' flag return if [:noop] Array(src).each do |source| dir = FileSystem.find(source) raise Errno::ENOENT, source.to_s unless dir new_dir = FileSystem.find(dest) raise Errno::EEXIST, dest.to_s if new_dir && !File.directory?(dest) raise Errno::ENOENT, dest.to_s if !new_dir && !FileSystem.find(dest.to_s + '/../') update_times = proc { |f| f.atime = f.mtime = Time.now } # This last bit is a total abuse and should be thought hard # about and cleaned up. if new_dir if src.to_s.end_with?('/.') dir.entries.each do |f| copy = f.clone(new_dir) walk_hierarchy(copy, &update_times) unless [:preserve] new_dir[f.name] = copy end else copy = dir.entry.clone(new_dir) walk_hierarchy(copy, &update_times) unless [:preserve] new_dir[dir.name] = copy end else copy = dir.entry.clone walk_hierarchy(copy, &update_times) unless [:preserve] FileSystem.add(dest, copy) end end nil end |
#ln_s(target, path, options = {}) ⇒ Object Also known as: symlink
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/fakefs/fileutils.rb', line 84 def ln_s(target, path, = {}) = { force: false }.merge() raise(Errno::EEXIST, path.to_s) if FileSystem.find(path) && ![:force] FileSystem.delete(path) if ![:force] && !Dir.exist?(File.dirname(path)) raise Errno::ENOENT, path.to_s end FileSystem.add(path, FakeSymlink.new(target)) end |
#ln_sf(target, path) ⇒ Object
96 97 98 |
# File 'lib/fakefs/fileutils.rb', line 96 def ln_sf(target, path) ln_s(target, path, force: true) end |
#mkdir(list, _ignored_options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/fakefs/fileutils.rb', line 36 def mkdir(list, = {}) list = [list] unless list.is_a?(Array) list.each do |path| parent = path.to_s.split('/') parent.pop unless parent.join == '' || parent.join == '.' || FileSystem.find(parent.join('/')) raise Errno::ENOENT, path.to_s end raise Errno::EEXIST, path.to_s if FileSystem.find(path) FileSystem.add(path, FakeDir.new) end end |
#mkdir_p(list, options = {}) ⇒ Object Also known as: mkpath, makedirs
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/fakefs/fileutils.rb', line 8 def mkdir_p(list, = {}) list = [list] unless list.is_a?(Array) list.each do |path| # FileSystem.add call adds all the necessary parent directories but # can't set their mode. Thus, we have to collect created directories # here and set the mode later. if [:mode] created_dirs = [] dir = path until Dir.exist?(dir) created_dirs << dir dir = File.dirname(dir) end end FileSystem.add(path, FakeDir.new) next unless [:mode] created_dirs.each do |d| File.chmod([:mode], d) end end end |
#mv(src, dest, options = {}) ⇒ Object Also known as: move
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/fakefs/fileutils.rb', line 188 def mv(src, dest, = {}) # handle `verbose' flag RealFileUtils.mv src, dest, **.merge(noop: true) # handle `noop' flag return if [:noop] Array(src).each do |path| if (target = FileSystem.find(path)) dest_path = if File.directory?(dest) File.join(dest, File.basename(path)) else dest end if File.directory?(dest_path) raise Errno::EEXIST, dest_path.to_s unless [:force] elsif File.directory?(File.dirname(dest_path)) FileSystem.delete(dest_path) FileSystem.delete(path) FileSystem.add(dest_path, target.entry.clone) else raise Errno::ENOENT, dest_path.to_s unless [:force] end else raise Errno::ENOENT, path.to_s end end nil end |
#remove_entry_secure(path, force = false) ⇒ Object
80 81 82 |
# File 'lib/fakefs/fileutils.rb', line 80 def remove_entry_secure(path, force = false) rm_rf(path, force: force) end |
#rm(list, options = {}) ⇒ Object Also known as: rm_r, remove
61 62 63 64 65 66 |
# File 'lib/fakefs/fileutils.rb', line 61 def rm(list, = {}) Array(list).each do |path| FileSystem.delete(path) || (![:force] && raise(Errno::ENOENT, path.to_s)) end end |
#rm_f(list, options = {}) ⇒ Object Also known as: safe_unlink
70 71 72 |
# File 'lib/fakefs/fileutils.rb', line 70 def rm_f(list, = {}) rm(list, .merge(force: true)) end |
#rm_rf(list, options = {}) ⇒ Object Also known as: rmtree
74 75 76 |
# File 'lib/fakefs/fileutils.rb', line 74 def rm_rf(list, = {}) rm_r(list, .merge(force: true)) end |
#rmdir(list, _options = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fakefs/fileutils.rb', line 49 def rmdir(list, = {}) list = [list] unless list.is_a?(Array) list.each do |l| parent = l.to_s.split('/') parent.pop raise Errno::ENOENT, l.to_s unless parent.join == '' || FileSystem.find(parent.join('/')) raise Errno::ENOENT, l.to_s unless FileSystem.find(l) raise Errno::ENOTEMPTY, l.to_s unless FileSystem.find(l).empty? rm(l) end end |
#touch(list, options = {}) ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/fakefs/fileutils.rb', line 276 def touch(list, = {}) Array(list).each do |f| if (fs = FileSystem.find(f)) now = Time.now fs.mtime = [:mtime] || now fs.atime = now else file = File.open(f, 'w') file.close if (mtime = [:mtime]) fs = FileSystem.find(f) fs.mtime = mtime end end end end |
#uptodate?(new, old_list) ⇒ Boolean
307 308 309 310 311 312 313 314 315 316 |
# File 'lib/fakefs/fileutils.rb', line 307 def uptodate?(new, old_list) return false unless File.exist?(new) new_time = File.mtime(new) old_list.each do |old| if File.exist?(old) return false unless new_time > File.mtime(old) end end true end |