module QdumpfsUtils
Public Instance Methods
chown_if_root(type, src, today)
click to toggle source
# File lib/qdumpfs/util.rb, line 201 def chown_if_root(type, src, today) return unless Process.uid == 0 and type != "unsupported" if type == "symlink" if File.respond_to?(:lchown) stat = File.lstat(src) File.lchown(stat.uid, stat.gid, today) end else stat = File.stat(src) File.chown(stat.uid, stat.gid, today) end end
convert_bytes(bytes)
click to toggle source
# File lib/qdumpfs/util.rb, line 140 def convert_bytes(bytes) if bytes < 1024 sprintf("%dB", bytes) elsif bytes < 1024 * 1000 # 1000kb sprintf("%.1fKB", bytes.to_f / 1024) elsif bytes < 1024 * 1024 * 1000 # 1000mb sprintf("%.1fMB", bytes.to_f / 1024 / 1024) else sprintf("%.1fGB", bytes.to_f / 1024 / 1024 / 1024) end end
copy(src, dest)
click to toggle source
incomplete substitute for cp -p
# File lib/qdumpfs/util.rb, line 132 def copy(src, dest) stat = File.stat(src) copy_file(src, dest) File.chmod(0200, dest) if windows? File.utime(stat.atime, stat.mtime, dest) File.chmod(stat.mode, dest) # not necessary. just to make sure end
copy_file(src, dest)
click to toggle source
We don't use File.copy for calling @interval_proc.
# File lib/qdumpfs/util.rb, line 109 def copy_file(src, dest) begin File.open(src, 'rb') {|r| File.open(dest, 'wb') {|w| block_size = (r.stat.blksize or 8192) i = 0 while true block = r.sysread(block_size) w.syswrite(block) i += 1 @written_bytes += block.size end } } rescue EOFError => e # puts e.message, e.backtrace end unless FileTest.file?(dest) raise "copy_file fails #{dest}" end end
create_latest_symlink(dest, today)
click to toggle source
# File lib/qdumpfs/util.rb, line 245 def create_latest_symlink(dest, today) # 最新のバックアップに"latest"というシンボリックリンクをはる(Windowsだと動かない) latest_day = File.dirname(make_relative_path(today, dest)) latest_symlink = File.join(dest, "latest") # puts "force_symlink #{latest_day} #{latest_symlink}" File.force_symlink(latest_day, latest_symlink) end
datedir(date)
click to toggle source
# File lib/qdumpfs/util.rb, line 266 def datedir(date) s = File::SEPARATOR sprintf "%d%s%02d%s%02d", date.year, s, date.month, s, date.day end
detect_type(src, latest = nil)
click to toggle source
# File lib/qdumpfs/util.rb, line 165 def detect_type(src, latest = nil) type = "unsupported" if File.real_directory?(src) type = "directory" else if latest and File.real_file?(latest) case File.ftype(src) when "file" same_file = same_file?(src, latest) # p "same_file? #{src} #{latest} result=#{same_file}" if same_file type = "unchanged" else type = "updated" end when "link" # the latest backup file is a real file but the # current source file is changed to symlink. type = "symlink" end else case File.ftype(src) when "file" type = "new_file" when "link" type = "symlink" end end end return type end
fmt(time)
click to toggle source
# File lib/qdumpfs/util.rb, line 197 def fmt(time) time.strftime('%Y/%m/%d %H:%M:%S') end
fputs(file, msg)
click to toggle source
# File lib/qdumpfs/util.rb, line 226 def fputs(file, msg) puts msg file.puts msg end
make_relative_path(path, base)
click to toggle source
# File lib/qdumpfs/util.rb, line 221 def make_relative_path(path, base) pattern = sprintf("^%s%s?", Regexp.quote(base), File::SEPARATOR) path.sub(Regexp.new(pattern), "") end
past_date?(year, month, day, t)
click to toggle source
# File lib/qdumpfs/util.rb, line 271 def past_date?(year, month, day, t) ([year, month, day] <=> [t.year, t.month, t.day]) < 0 end
restore_dir_attributes(dirs)
click to toggle source
# File lib/qdumpfs/util.rb, line 214 def restore_dir_attributes(dirs) dirs.each {|dir, stat| File.utime(stat.atime, stat.mtime, dir) File.chmod(stat.mode, dir) } end
same_directory?(src, dest)
click to toggle source
# File lib/qdumpfs/util.rb, line 253 def same_directory?(src, dest) src = File.expand_path(src) dest = File.expand_path(dest) return src == dest end
same_file?(f1, f2)
click to toggle source
# File lib/qdumpfs/util.rb, line 152 def same_file?(f1, f2) # File.real_file?(f1) and File.real_file?(f2) and # File.size(f1) == File.size(f2) and File.mtime(f1) == File.mtime(f2) real_file = File.real_file?(f1) and File.real_file?(f2) same_size = File.size(f1) == File.size(f2) # mtime1 = File.mtime(f1).strftime('%F %T.%N') # mtime2 = File.mtime(f2).strftime('%F %T.%N') same_mtime = File.mtime(f1).to_i == File.mtime(f2).to_i # p "#{real_file} #{same_size} #{same_mtime}(#{mtime1}<=>#{mtime2})" real_file and same_size and same_mtime end
sub_directory?(src, dest)
click to toggle source
# File lib/qdumpfs/util.rb, line 259 def sub_directory?(src, dest) src = File.expand_path(src) dest = File.expand_path(dest) src += File::SEPARATOR unless /#{File::SEPARATOR}$/.match(src) return /^#{Regexp.quote(src)}/.match(dest) end
time_diff(start_time, end_time)
click to toggle source
# File lib/qdumpfs/util.rb, line 231 def time_diff(start_time, end_time) seconds_diff = (start_time - end_time).to_i.abs hours = seconds_diff / 3600 seconds_diff -= hours * 3600 minutes = seconds_diff / 60 seconds_diff -= minutes * 60 seconds = seconds_diff '%02d:%02d:%02d' % [hours, minutes, seconds] end
to_time(date)
click to toggle source
# File lib/qdumpfs/util.rb, line 283 def to_time(date) Time.local(date.year, date.month, date.day) end
to_unix_path(path)
click to toggle source
# File lib/qdumpfs/util.rb, line 279 def to_unix_path(path) path.gsub(/\\/, '/') end
to_win_path(path)
click to toggle source
# File lib/qdumpfs/util.rb, line 275 def to_win_path(path) path.gsub(/\//, '\\') end