Module: QdumpfsUtils

Included in:
Qdumpfs::Command
Defined in:
lib/qdumpfs/util.rb

Instance Method Summary collapse

Instance Method Details

#chown_if_root(type, src, today) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/qdumpfs/util.rb', line 218

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



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/qdumpfs/util.rb', line 156

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

incomplete substitute for cp -p



143
144
145
146
147
148
149
# File 'lib/qdumpfs/util.rb', line 143

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

We don’t use File.copy for calling @interval_proc.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/qdumpfs/util.rb', line 115

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
  #         @write_bytes += block.size
  #       end
  #     }
  #   }
  # rescue EOFError
  #   # この実装だとファイルサイズがblock_sizeより小さい場合にEOFErrorが発生する
  #   # それはしかたがないので無視する
  #   #       puts e.message
  # end
  FileUtils.cp(src, dest)
  unless FileTest.file?(dest)
    raise "copy_file fails #{dest}"
  else
    @write_bytes +=  File.size(src)
  end
end


262
263
264
265
266
267
268
# File 'lib/qdumpfs/util.rb', line 262

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



283
284
285
286
# File 'lib/qdumpfs/util.rb', line 283

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



181
182
183
184
185
186
187
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
# File 'lib/qdumpfs/util.rb', line 181

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)
#          puts "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
 #       puts "latest=#{latest}"
      case File.ftype(src)
      when "file"
        type = "new_file"
      when "link"
        type = "symlink"
      end
    end
  end
  return type
end

#fmt(time) ⇒ Object



214
215
216
# File 'lib/qdumpfs/util.rb', line 214

def fmt(time)
  time.strftime('%Y/%m/%d %H:%M:%S')
end

#fputs(file, msg) ⇒ Object



243
244
245
246
# File 'lib/qdumpfs/util.rb', line 243

def fputs(file, msg)
  puts msg
  file.puts msg
end


151
152
153
154
# File 'lib/qdumpfs/util.rb', line 151

def link(src, dest)
  @link_bytes += File.size(src)
  File.force_link(src, dest)
end

#make_relative_path(path, base) ⇒ Object



238
239
240
241
# File 'lib/qdumpfs/util.rb', line 238

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

Returns:

  • (Boolean)


288
289
290
# File 'lib/qdumpfs/util.rb', line 288

def past_date?(year, month, day, t)
  ([year, month, day] <=> [t.year, t.month, t.day]) < 0
end

#restore_dir_attributes(dirs) ⇒ Object



231
232
233
234
235
236
# File 'lib/qdumpfs/util.rb', line 231

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

Returns:

  • (Boolean)


270
271
272
273
274
# File 'lib/qdumpfs/util.rb', line 270

def same_directory?(src, dest)
  src  = File.expand_path(src)
  dest = File.expand_path(dest)
  return src == dest
end

#same_file?(f1, f2) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/qdumpfs/util.rb', line 168

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

Returns:

  • (Boolean)


276
277
278
279
280
281
# File 'lib/qdumpfs/util.rb', line 276

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



248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/qdumpfs/util.rb', line 248

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



300
301
302
# File 'lib/qdumpfs/util.rb', line 300

def to_time(date)
  Time.local(date.year, date.month, date.day)
end

#to_unix_path(path) ⇒ Object



296
297
298
# File 'lib/qdumpfs/util.rb', line 296

def to_unix_path(path)
  path.gsub(/\\/, '/')
end

#to_win_path(path) ⇒ Object



292
293
294
# File 'lib/qdumpfs/util.rb', line 292

def to_win_path(path)
  path.gsub(/\//, '\\')
end