Sha256: 0c9085fa5baaa2e27a526a45368f67128f7f2f096faf0cc568c59f4fb482ded8

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

require 'fileutils'
require 'digest/md5'

module FileTest
  module_function

  BUF_SIZE = 1024*1024

  # Are two files identical? This compares size and then checksum.
  def identical?(file1, file2)
    size(file1) == size(file2) && md5(file1) == md5(file2)
  end

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

  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

  # Show diff of two files.

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

end

Version data entries

4 entries across 2 versions & 1 rubygems

Version Path
proutils-0.3.0 work/icli/utils/fileutils.rb
proutils-0.3.1 work/icli/utils/fileutils.rb
proutils-0.3.1 lib/proutils/mint/fileutils.rb
proutils-0.3.0 lib/proutils/mint/fileutils.rb