Module: FileTest

Defined in:
lib/proutils/mint/fileutils.rb

Constant Summary collapse

BUF_SIZE =
1024*1024

Class Method Summary collapse

Class Method Details

.diff(file1, file2) ⇒ Object

Show diff of two files.



43
44
45
# File 'lib/proutils/mint/fileutils.rb', line 43

def diff(file1, file2)
  `diff #{file1} #{file2}`.strip
end

.identical?(file1, file2) ⇒ Boolean

Are two files identical? This compares size and then checksum.

Returns:

  • (Boolean)


10
11
12
# File 'lib/proutils/mint/fileutils.rb', line 10

def identical?(file1, file2)
  size(file1) == size(file2) && md5(file1) == md5(file2)
end

.md5(path) ⇒ Object

Return an md5 checkum. If a directory is given, will return a nested array of md5 checksums for all entries.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/proutils/mint/fileutils.rb', line 17

def md5(path)
  if File.directory?(path)
    md5_list = []
    crt_dir = Dir.new(path)
    crt_dir.each do |file_name|
      next if file_name == '.' || file_name == '..'
      md5_list << md5("#{crt_dir.path}#{file_name}")
    end
    md5_list
  else
    hasher = Digest::MD5.new
    open(path, "r") do |io|
      counter = 0
      while (!io.eof)
        readBuf = io.readpartial(BUF_SIZE)
        counter+=1
        #putc '.' if ((counter+=1) % 3 == 0)
        hasher.update(readBuf)
      end
    end
    return hasher.hexdigest
  end
end