Class: Fixi::Command::Check

Inherits:
Object
  • Object
show all
Defined in:
lib/fixi/command/check.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.arghelpObject



8
9
10
# File 'lib/fixi/command/check.rb', line 8

def self.arghelp
  "[<dir>|<file>]"
end

.detailsObject



12
13
14
# File 'lib/fixi/command/check.rb', line 12

def self.details
  "If no argument is given, the current directory ('.') is assumed."
end

.synopsisObject



4
5
6
# File 'lib/fixi/command/check.rb', line 4

def self.synopsis
  "Verify the fixity of files in the index"
end

Instance Method Details

#execute(args) ⇒ Object



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fixi/command/check.rb', line 16

def execute args
  opts = Trollop::options args do
    banner Fixi::Command.banner "check"
    opt :absolute, "Show absolute paths. By default, paths are reported
      relative to the index root.".pack
    opt :algorithms, "Checksum algorithm(s) to use if shallow isn't specified.
      This is a comma-separated list, which may include md5, sha1, sha256, sha384,
      sha512, and must be a subset of the indexed algorithms. If unspecified,
      defaults to all indexed algorithms.".pack, :short => 'l', :type => :string
    opt :shallow, "Do shallow comparisons when determining which files have
      changed. If specified, only file sizes and mtimes will be used. By 
      default, checksums will also be computed and compared if necessary.".pack
    opt :verbose, "For modified files, show which attribute changed.
      By default, only the path is shown.".pack
  end
  path = File.expand_path(args[0] || ".")
  index = Fixi::Index.new(path)
 
  # if algorithms specified, must be a subset of those indexed
  opts[:algorithms] ||= index.algorithms
  set = Set.new(index.algorithms.split(","))
  subset = Set.new(opts[:algorithms].split(","))
  raise "Specified algorithm(s) must be a subset of #{index.algorithms}" unless subset.subset?(set)

  index.each(args[0]) do |hash|
    relpath = hash['relpath']
    abspath = index.rootpath + '/' + relpath
    if index.file_in_scope(relpath)
      if File.exists?(abspath)
        size = File.size(abspath)
        mtime = File.mtime(abspath).to_i
        if size != hash['size']
          detail = opts[:verbose] ? "size=#{size} " : ""
          puts "M #{detail}#{opts[:absolute] ? abspath : relpath}"
        elsif File.mtime(abspath).to_i != hash['mtime']
          detail = opts[:verbose] ? "mtime=#{Time.at(mtime).utc.iso8601} " : ""
          puts "M #{detail}#{opts[:absolute] ? abspath : relpath}"
        elsif not opts[:shallow]
          hexdigests = Fixi::hexdigests(Fixi::digests(opts[:algorithms]), abspath)
          i = 0
          opts[:algorithms].split(',').each do |algorithm|
            if hexdigests[i] != hash[algorithm]
              detail = opts[:verbose] ? "#{algorithm}=#{hexdigests[i]} " : ""
              puts "M #{detail}#{opts[:absolute] ? abspath : relpath}"
            end
            i += 1
          end
        end
      else
        puts "D #{opts[:absolute] ? abspath : relpath}"
      end
    else
      puts "X #{opts[:absolute] ? abspath : relpath}"
    end
  end

  index.find(path) do |abspath|
    relpath = index.relpath(abspath)
    unless index.contains?(relpath)
      puts "A #{opts[:absolute] ? abspath : relpath}"
    end
  end
end