87fa7530d1cd94d233bde02fcc2a7be293fb85ac
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)
Dev Minor cleanup and bug fix f...

Dev authored 7 years ago

22)       ::FileUtils.chmod(file.mode.to_i(8), file.path)
Dev Now detecting difference in...

Dev authored 7 years ago

23) 
24)       file.changed = true
Dev Working everything.

Dev authored 7 years ago

25)       0
Dev Now detecting difference in...

Dev authored 7 years ago

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) 
Dev Working everything.

Dev authored 7 years ago

34)       begin
35)         ::FileUtils.copy_entry(file.src_path, file.path, remove_destination:  true)
36)       rescue
37)         error_msg = "Could not copy #{file.src_path} to #{file.path}."
38)         @logger.fatal(error_msg)
39)         return 1
40)       end
41) 
42)       file.state = ::RCM::File::PRESENT
Dev Now detecting difference in...

Dev authored 7 years ago

43)       file.changed = true
Dev Working everything.

Dev authored 7 years ago

44)       0
Dev Now detecting difference in...

Dev authored 7 years ago

45)     end
46) 
47)     def remove(file)
Dev Converging the state as def...

Dev authored 7 years ago

48)       return unless ::File.file?(file.path)
49) 
50)       @logger.debug("Removing #{file.path}.")
Dev Now detecting difference in...

Dev authored 7 years ago

51)       ::FileUtils.rm(file.path)
Dev Working everything.

Dev authored 7 years ago

52)       file.state = ::RCM::File::ABSENT
Dev Converging the state as def...

Dev authored 7 years ago

53)       file.changed = true
Dev Working everything.

Dev authored 7 years ago

54)       0
Dev Now detecting difference in...

Dev authored 7 years ago

55)     end
56) 
57)     def initialize(logger)
58)       @logger = logger
59)     end
60) 
61)     def get_current_state(wanted_files)
62)       got = {}
63)       uids = get_id_to_name_mapping('/etc/passwd')
64)       gids = get_id_to_name_mapping('/etc/group')
65)       wanted_files.each do |path, p|
Dev Working everything.

Dev authored 7 years ago

66)         f = ::RCM::File.new(path, '', '', '', '', ::RCM::File::ABSENT)
Dev Now detecting difference in...

Dev authored 7 years ago

67)         # Put in an empty object if file does not exist
68)         unless ::File.exist?(path)
69)           got[path] = f
70)           next
71)         end
72) 
73)         f_stat = ::File.stat(path)
74)         f.path = path
75)         f.owner = uids[f_stat.uid.to_s]
76)         f.group = gids[f_stat.gid.to_s]
77)         f.mode = f_stat.mode.to_s(8)[-4, 4]
Dev Working everything.

Dev authored 7 years ago

78)         f.state = ::RCM::File::PRESENT
Dev Now detecting difference in...

Dev authored 7 years ago

79)         got[path] = f
Dev Debugging why index.html wa...

Dev authored 7 years ago

80)         @logger.debug(f)
Dev Now detecting difference in...

Dev authored 7 years ago

81)       end
82) 
83)       got
84)     end
85)   end
Dev Refactoring. Adding pkg mgmt.

Dev authored 7 years ago

86) end