Module: Chef::FileAccessControl::Unix

Included in:
Chef::FileAccessControl
Defined in:
lib/chef/file_access_control/unix.rb

Constant Summary collapse

UINT =
(1 << 32)
UID_MAX =
(1 << 32) - 10

Instance Method Summary collapse

Instance Method Details

#current_gidObject


82
83
84
# File 'lib/chef/file_access_control/unix.rb', line 82

def current_gid
  gid_from_resource(current_resource)
end

#current_modeObject


134
135
136
# File 'lib/chef/file_access_control/unix.rb', line 134

def current_mode
  mode_from_resource(current_resource)
end

#current_uidObject


58
59
60
# File 'lib/chef/file_access_control/unix.rb', line 58

def current_uid
  uid_from_resource(current_resource)
end

#describe_changesObject


46
47
48
49
50
51
52
# File 'lib/chef/file_access_control/unix.rb', line 46

def describe_changes
  changes = []
  changes << "change mode from '#{mode_to_s(current_mode)}' to '#{mode_to_s(target_mode)}'" if should_update_mode?
  changes << "change owner from '#{current_resource.owner}' to '#{resource.owner}'" if should_update_owner?
  changes << "change group from '#{current_resource.group}' to '#{resource.group}'" if should_update_group?
  changes
end

#gid_from_resource(resource) ⇒ Object


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/chef/file_access_control/unix.rb', line 86

def gid_from_resource(resource)
  return nil if resource == nil or resource.group.nil?
  if resource.group.kind_of?(String)
    diminished_radix_complement( Etc.getgrnam(resource.group).gid )
  elsif resource.group.kind_of?(Integer)
    resource.group
  else
    Chef::Log.error("The `group` parameter of the #@resource resource is set to an invalid value (#{resource.owner.inspect})")
    raise ArgumentError, "cannot resolve #{resource.group.inspect} to gid, group must be a string or integer"
  end
rescue ArgumentError
  provider.requirements.assert(:create, :create_if_missing, :touch) do |a|
    a.assertion { false }
    a.failure_message(Chef::Exceptions::GroupIDNotFound, "cannot determine group id for '#{resource.group}', does the group exist on this system?")
    a.whyrun("Assuming group #{resource.group} would have been created")
  end
  return nil
end

#mode_from_resource(res) ⇒ Object


121
122
123
124
# File 'lib/chef/file_access_control/unix.rb', line 121

def mode_from_resource(res)
  return nil if res == nil or res.mode.nil?
  (res.mode.respond_to?(:oct) ? res.mode.oct : res.mode.to_i) & 007777
end

#mode_to_s(mode) ⇒ Object


130
131
132
# File 'lib/chef/file_access_control/unix.rb', line 130

def mode_to_s(mode)
  mode.nil? ? "" : "0#{mode.to_s(8)}"
end

#requires_changes?Boolean

TODO factor this up

Returns:

  • (Boolean)

42
43
44
# File 'lib/chef/file_access_control/unix.rb', line 42

def requires_changes?
  should_update_mode? || should_update_owner? || should_update_group?
end

#set_allObject


35
36
37
38
39
# File 'lib/chef/file_access_control/unix.rb', line 35

def set_all
  set_owner
  set_group
  set_mode
end

#set_all!Object


29
30
31
32
33
# File 'lib/chef/file_access_control/unix.rb', line 29

def set_all!
  set_owner!
  set_group!
  set_mode!
end

#set_groupObject


117
118
119
# File 'lib/chef/file_access_control/unix.rb', line 117

def set_group
  set_group! if should_update_group?
end

#set_group!Object


109
110
111
112
113
114
115
# File 'lib/chef/file_access_control/unix.rb', line 109

def set_group!
  unless target_gid.nil?
    chown(nil, target_gid, file)
    Chef::Log.info("#{log_string} group changed to #{target_gid}")
    modified
  end
end

#set_modeObject


150
151
152
# File 'lib/chef/file_access_control/unix.rb', line 150

def set_mode
  set_mode! if should_update_mode?
end

#set_mode!Object


142
143
144
145
146
147
148
# File 'lib/chef/file_access_control/unix.rb', line 142

def set_mode!
  unless target_mode.nil?
    chmod(target_mode, file)
    Chef::Log.info("#{log_string} mode changed to #{target_mode.to_s(8)}")
    modified
  end
end

#set_ownerObject


74
75
76
# File 'lib/chef/file_access_control/unix.rb', line 74

def set_owner
  set_owner! if should_update_owner?
end

#set_owner!Object


66
67
68
69
70
71
72
# File 'lib/chef/file_access_control/unix.rb', line 66

def set_owner!
  unless target_uid.nil?
    chown(target_uid, nil, file)
    Chef::Log.info("#{log_string} owner changed to #{target_uid}")
    modified
  end
end

#should_update_group?Boolean

Returns:

  • (Boolean)

105
106
107
# File 'lib/chef/file_access_control/unix.rb', line 105

def should_update_group?
  !target_gid.nil? && target_gid != current_gid
end

#should_update_mode?Boolean

Returns:

  • (Boolean)

138
139
140
# File 'lib/chef/file_access_control/unix.rb', line 138

def should_update_mode?
  !target_mode.nil? && current_mode != target_mode 
end

#should_update_owner?Boolean

Returns:

  • (Boolean)

62
63
64
# File 'lib/chef/file_access_control/unix.rb', line 62

def should_update_owner?
  !target_uid.nil? && target_uid != current_uid
end

#statObject


154
155
156
157
158
159
160
# File 'lib/chef/file_access_control/unix.rb', line 154

def stat
  if File.symlink?(file)
    @stat ||= File.lstat(file)
  else
    @stat ||= File.stat(file)
  end
end

#target_gidObject


78
79
80
# File 'lib/chef/file_access_control/unix.rb', line 78

def target_gid
  gid_from_resource(resource)
end

#target_modeObject


126
127
128
# File 'lib/chef/file_access_control/unix.rb', line 126

def target_mode
  mode_from_resource(resource)
end

#target_uidObject


54
55
56
# File 'lib/chef/file_access_control/unix.rb', line 54

def target_uid
  uid_from_resource(resource)
end