blob: 210804cd70caf5a3b4acb074ba8d70a7b260b7ae (
plain)
1
2
3
4
5
6
7
8
9
10
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
|
require 'digest'
module RCM
# Mixin that provides file-backup helpers for resource classes.
# Included by BasicFile so all file/directory/symlink resources share
# the same backup logic.
module FileBackup
# TODO: Make protected?
def backup!(file_path, checksum = nil)
return if @without_backup
suffix = if ::File.file?(file_path)
checksum.nil? ? Digest::SHA256.file(file_path).hexdigest : checksum
else
Time.now.strftime('%s-%L')
end
make_backup!(file_path, suffix)
end
def different?(file_a, file_b)
checksum_a = Digest::SHA256.file(file_a).hexdigest
checksum_b = Digest::SHA256.file(file_b).hexdigest
[checksum_a != checksum_b, checksum_a, checksum_b]
end
private
def make_backup!(file_path, suffix)
backup_dir = create_backup_directory!(file_path)
backup_path = "#{backup_dir}/#{::File.basename(file_path)}.#{suffix}"
return if ::File.exist?(backup_path)
do? "Backing up #{file_path} -> #{backup_path}" do
::File.rename(file_path, backup_path)
end
end
def create_backup_directory!(file_path)
backup_dir = "#{::File.dirname(file_path)}/.rcmbackup"
return backup_dir if ::File.directory?(backup_dir)
do? "Creating backup directory #{backup_dir}" do
Dir.mkdir(backup_dir)
end
backup_dir
end
end
end
|