97a2c496119c93df2d89f814ac133cb63aa2ce94
Dev Now detecting difference in...

Dev authored 7 years ago

1) require_relative '../objects/rcm_file'
2) require_relative '../rcm_utils'
3) require 'fileutils'
4) 
Dev Refactoring. Adding pkg mgmt.

Dev authored 7 years ago

5) module RCM
Dev 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) 
Dev Converging the state as def...

Dev authored 7 years ago

19)       @logger.debug("Setting owner as #{file.owner}:#{file.group} and mode as #{file.mode} on #{file.path}")
Dev Now detecting difference in...

Dev authored 7 years ago

20)       # Will throw exception if something poopy happens
21)       ::FileUtils.chown(file.owner, file.group, file.path)
22)       ::FileUtils.chmod(file.mode, file.path)
23) 
24)       file.changed = true
25)       file
26)     end
27) 
28)     def copy(file)
29)       dir = ::File.dirname(file.path)
Dev Converging the state as def...

Dev authored 7 years ago

30)       @logger.debug("Ensuring #{dir} exsits.")
Dev Now detecting difference in...

Dev authored 7 years ago

31)       ::FileUtils.mkdir_p(dir)
Dev Converging the state as def...

Dev authored 7 years ago

32)       @logger.debug("Copying #{file.src_path} to #{file.path}.")
Dev Now detecting difference in...

Dev authored 7 years ago

33)       ::FileUtils.copy(file.src_path, file.path, remove_destination: true)
34) 
35)       file.changed = true
36)       file
37)     end
38) 
39)     def remove(file)
Dev Converging the state as def...

Dev authored 7 years ago

40)       return unless ::File.file?(file.path)
41) 
42)       @logger.debug("Removing #{file.path}.")
Dev Now detecting difference in...

Dev authored 7 years ago

43)       ::FileUtils.rm(file.path)
Dev Converging the state as def...

Dev authored 7 years ago

44)       file.changed = true
Dev Now detecting difference in...

Dev authored 7 years ago

45)       file
46)     end
47) 
48)     def initialize(logger)
49)       @logger = logger
50)     end
51) 
52)     def get_current_state(wanted_files)
53)       got = {}
54)       uids = get_id_to_name_mapping('/etc/passwd')
55)       gids = get_id_to_name_mapping('/etc/group')
56)       wanted_files.each do |path, p|
57)         f = ::RCM::File.new('', '', '', '', '')
58)         # Put in an empty object if file does not exist
59)         unless ::File.exist?(path)
60)           got[path] = f
61)           next
62)         end
63) 
64)         f_stat = ::File.stat(path)
65)         f.path = path
66)         f.owner = uids[f_stat.uid.to_s]
67)         f.group = gids[f_stat.gid.to_s]
68)         f.mode = f_stat.mode.to_s(8)[-4, 4]
69)         got[path] = f
70)       end
71) 
72)       got
73)     end
74)   end
Dev Refactoring. Adding pkg mgmt.

Dev authored 7 years ago

75) end