Class: ChecksummerFile

Inherits:
Object
  • Object
show all
Defined in:
lib/checksummer_file.rb

Constant Summary collapse

REPLACE_MAPPING =
{
  :exists => :symlinked,
  :copied => :replaced
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ ChecksummerFile

Returns a new instance of ChecksummerFile.



9
10
11
12
13
# File 'lib/checksummer_file.rb', line 9

def initialize(attributes = {})
  attributes.each do |key, value|
    self.send :"#{key}=", value if self.respond_to?(:"#{key}=")
  end
end

Instance Attribute Details

#current_baseObject

Returns the value of attribute current_base.



59
60
61
# File 'lib/checksummer_file.rb', line 59

def current_base
  @current_base
end

#modified_atObject

Returns the value of attribute modified_at.



7
8
9
# File 'lib/checksummer_file.rb', line 7

def modified_at
  @modified_at
end

#original_lineObject

Returns the value of attribute original_line.



7
8
9
# File 'lib/checksummer_file.rb', line 7

def original_line
  @original_line
end

#original_pathObject

Returns the value of attribute original_path.



7
8
9
# File 'lib/checksummer_file.rb', line 7

def original_path
  @original_path
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/checksummer_file.rb', line 7

def path
  @path
end

#sizeObject

Returns the value of attribute size.



7
8
9
# File 'lib/checksummer_file.rb', line 7

def size
  @size
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/checksummer_file.rb', line 7

def type
  @type
end

Class Method Details

.from_line(line) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/checksummer_file.rb', line 24

def self.from_line(line)
  if path = line.chomp.split("\t").first
    ChecksummerFile.new(:original_line => line.chomp, :path => File.expand_path(path))
  else
    ChecksummerFile.new
  end
end

.from_paths(stream) ⇒ Object



32
33
34
# File 'lib/checksummer_file.rb', line 32

def self.from_paths(stream)
  stream.map { |path| self.new(:path => path.chomp.split("\t").first) }
end

Instance Method Details

#checksum_to!(base, options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/checksummer_file.rb', line 66

def checksum_to!(base, options = {})
  self.current_base = base
  status = { :original_line => original_line, :original_path => path }
  if path.to_s.strip.length == 0 || !File.exists?(path)
    status[:status] = :not_found
  elsif !File.symlink?(path)
    status[:status] = copy_to_checksummed_path
    status[:checksummed_path] = checksummed_path
    status[:checksum] = File.basename(checksummed_path)
    if options[:replace] == true
      do_symlink
      status[:status] = REPLACE_MAPPING[status[:status]] || status[:status]
    end
  else
    status[:status] = :was_symlink
    status[:realpath] = Pathname.new(path).realpath
    status[:checksum] = File.basename(status[:realpath]) if valid_checksum?(File.basename(status[:realpath]))
  end
  status
end

#checksummed_path(base = nil) ⇒ Object



53
54
55
56
57
# File 'lib/checksummer_file.rb', line 53

def checksummed_path(base = nil)
  base ||= self.current_base
  raise "no current base set" if base.nil?
  File.expand_path(md5_path(base))
end

#copy_to_checksummed_pathObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/checksummer_file.rb', line 36

def copy_to_checksummed_path
  if !File.exists?(checksummed_path)
    FileUtils.mkdir_p(File.dirname(checksummed_path))
    FileUtils.cp(path, "#{checksummed_path}.tmp")
    FileUtils.mv("#{checksummed_path}.tmp", checksummed_path)
    :copied
  else
    :exists
  end
end


47
48
49
50
51
# File 'lib/checksummer_file.rb', line 47

def do_symlink
  mtime = File.mtime(self.path)
  FileUtils.ln_sf(checksummed_path, self.path)
  FileUtils.touch(self.path, :mtime => mtime) rescue nil
end

#md5Object



20
21
22
# File 'lib/checksummer_file.rb', line 20

def md5
  @md5 ||= Digest::MD5.file(path).to_s
end

#md5_path(checksum_dir) ⇒ Object



91
92
93
# File 'lib/checksummer_file.rb', line 91

def md5_path(checksum_dir)
  "#{checksum_dir}/#{md5.split("")[0,4].join("/")}/#{md5}"
end

#valid_checksum?(checksum) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/checksummer_file.rb', line 87

def valid_checksum?(checksum)
  !checksum.to_s.match(/^[0-9a-f]{32}$/).nil?
end