ev/ftools.rb in rwdtinker-1.67 vs ev/ftools.rb in rwdtinker-1.68
- old
+ new
@@ -1,34 +1,24 @@
require "ftools"
class Dir
- def self.mkdirrec(dir)
- pdir = File.dirname(dir)
-
- if not pdir.empty? and not File.directory?(pdir)
- Dir.mkdirrec(pdir)
- end
-
- Dir.mkdir(dir) rescue nil
- end
-
def self.copy(from, to)
if File.directory?(from)
pdir = Dir.pwd
todir = File.expand_path(to)
- mkdirrec(todir)
+ File.mkpath(todir)
Dir.chdir(from)
Dir.new(".").each do |e|
Dir.copy(e, todir+"/"+e) if not [".", ".."].include?(e)
end
Dir.chdir(pdir)
else
todir = File.dirname(File.expand_path(to))
- mkdirrec(todir)
+ File.mkpath(todir)
File.copy(from, to)
end
end
@@ -36,29 +26,39 @@
Dir.copy(from, to)
Dir.rm_rf(from)
end
def self.rm_rf(entry)
+ File.chmod(0755, entry)
+
if File.ftype(entry) == "directory"
pdir = Dir.pwd
Dir.chdir(entry)
Dir.new(".").each do |e|
Dir.rm_rf(e) if not [".", ".."].include?(e)
end
Dir.chdir(pdir)
- Dir.delete(entry)
+ begin
+ Dir.delete(entry)
+ rescue => e
+ $stderr.puts e.message
+ end
else
- File.delete(entry)
+ begin
+ File.delete(entry)
+ rescue => e
+ $stderr.puts e.message
+ end
end
end
def self.find(entry=nil, mask=nil)
entry = "." if entry.nil?
- entry = entry.gsub!(/[\/\\]*$/, "") unless entry.nil?
+ entry = entry.gsub(/[\/\\]*$/, "") unless entry.nil?
mask = /^#{mask}$/i if mask.kind_of?(String)
res = []
@@ -75,12 +75,12 @@
res += Dir.find(e, mask).collect{|e| entry+"/"+e} unless [".", ".."].include?(e)
end
ensure
Dir.chdir(pdir)
end
- rescue Errno::EACCES => error
- puts error
+ rescue Errno::EACCES => e
+ $stderr.puts e.message
end
else
res += [entry] if mask.nil? or entry =~ mask
end
@@ -97,11 +97,11 @@
File.touch(file) unless File.file?(file)
# Rollback
if File.file?(backupfile) and File.file?(controlfile)
- $stdout.puts "Restoring #{file}..."
+ $stderr.puts "Restoring #{file}..."
File.copy(backupfile, file) # Rollback from phase 3
end
# Reset
@@ -137,10 +137,14 @@
res
end
def self.touch(file)
- File.open(file, "a"){|f|}
+ if File.exists?(file)
+ File.utime(Time.now, File.mtime(file), file)
+ else
+ File.open(file, "a"){|f|}
+ end
end
def self.which(file)
res = nil