Module: Antrapol::ToolRack::FileUtils::ClassMethods

Defined in:
lib/toolrack/file_utils.rb

Instance Method Summary collapse

Instance Method Details

#is_same?(*args, &block) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/toolrack/file_utils.rb', line 11

def is_same?(*args, &block)
  opts = { verbose: false } 
  if args.last.is_a?(Hash)
    opts.merge!(args.last)
    target = args[0..-2]
  else
    target = args
  end

  raise FileUtilsError, "is_same? requires at least 2 files" if target.length < 2

  res = true
  size = nil
  target.each do |f|
    raise FileUtilsError, "Given file #{f} to is_same? does not exist" if not File.exist?(f)

    if size.nil?
      size = File.size(f)
    elsif File.size(f) != size
      res = false 
      break
    end

  end

  if res
    prevDigRes = nil
    target.each do |f|
      dig = OpenSSL::Digest.new("SHA256")
      bufSize = 2048*1000
      File.open(f,"rb") do |f|
        dig.update(f.read(bufSize))
      end
      digRes = dig.digest

      STDOUT.puts "#{f} : #{to_hex(digRes)}" if opts[:verbose]

      if prevDigRes.nil?
        prevDigRes = digRes
      elsif prevDigRes != digRes 
        res = false
        break
      end
    end
  end

  res

end