Now detecting difference in...
Dev authored 7 years ago
|
6) class FileManager
7) def get_id_to_name_mapping(file)
8) id_to_name = {}
9) ::File.open(file, 'r').each do |l|
10) matches = /(?<name>\w+):.+:(?<id>\d+):.*\s/.match(l)
11) id_to_name[matches[:id]] = matches[:name]
12) end
13) id_to_name
14) end
15)
16) def apply_attributes(file)
17) raise 'apply_attributes only works with ::RCM::File' unless file.is_a?(::RCM::File)
18)
19) # Will throw exception if something poopy happens
20) ::FileUtils.chown(file.owner, file.group, file.path)
21) ::FileUtils.chmod(file.mode, file.path)
22)
23) file.changed = true
24) file
25) end
26)
27) def copy(file)
28) dir = ::File.dirname(file.path)
29) ::FileUtils.mkdir_p(dir)
30) ::FileUtils.copy(file.src_path, file.path, remove_destination: true)
31)
32) file.changed = true
33) file
34) end
35)
36) def remove(file)
37) file_existed = ::File.file?(file.path)
38) ::FileUtils.rm(file.path)
39) file.changed = true if file_existed
40) file
41) end
42)
43) def initialize(logger)
44) @logger = logger
45) end
46)
47) def get_current_state(wanted_files)
48) got = {}
49) uids = get_id_to_name_mapping('/etc/passwd')
50) gids = get_id_to_name_mapping('/etc/group')
51) wanted_files.each do |path, p|
52) f = ::RCM::File.new('', '', '', '', '')
53) # Put in an empty object if file does not exist
54) unless ::File.exist?(path)
55) got[path] = f
56) next
57) end
58)
59) f_stat = ::File.stat(path)
60) f.path = path
61) f.owner = uids[f_stat.uid.to_s]
62) f.group = gids[f_stat.gid.to_s]
63) f.mode = f_stat.mode.to_s(8)[-4, 4]
64) got[path] = f
65) end
66)
67) got
68) end
69) end
|