summaryrefslogtreecommitdiff
path: root/lib/chef/provider/file.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chef/provider/file.rb')
-rw-r--r--lib/chef/provider/file.rb80
1 files changed, 61 insertions, 19 deletions
diff --git a/lib/chef/provider/file.rb b/lib/chef/provider/file.rb
index c0805674ec..7bb729cd87 100644
--- a/lib/chef/provider/file.rb
+++ b/lib/chef/provider/file.rb
@@ -47,58 +47,100 @@ class Chef
# Compare the ownership of a file. Returns true if they are the same, false if they are not.
def compare_owner
- case @new_resource.owner
- when /^\d+$/, Integer
- @set_user_id = @new_resource.owner.to_i
- @set_user_id == @current_resource.owner
- else
- # This raises an ArugmentError if you can't find the user
- user_info = Etc.getpwnam(@new_resource.owner)
- @set_user_id = user_info.uid
- @set_user_id == @current_resource.owner
+ if @new_resource.owner != nil
+ case @new_resource.owner
+ when /^\d+$/, Integer
+ @set_user_id = @new_resource.owner.to_i
+ @set_user_id == @current_resource.owner
+ else
+ # This raises an ArugmentError if you can't find the user
+ user_info = Etc.getpwnam(@new_resource.owner)
+ @set_user_id = user_info.uid
+ @set_user_id == @current_resource.owner
+ end
end
end
# Set the ownership on the file, assuming it is not set correctly already.
def set_owner
unless compare_owner
+ Chef::Log.info("Setting owner to #{@set_user_id} for #{@new_resource}")
::File.chown(@set_user_id, nil, @new_resource.path)
+ @new_resource.updated = true
end
end
# Compares the group of a file. Returns true if they are the same, false if they are not.
def compare_group
- case @new_resource.group
- when /^\d+$/, Integer
- @set_group_id = @new_resource.group.to_i
- @set_group_id == @current_resource.group
- else
- group_info = Etc.getgrnam(@new_resource.group)
- @set_group_id = group_info.gid
- @set_group_id == @current_resource.group
+ if @new_resource.group != nil
+ case @new_resource.group
+ when /^\d+$/, Integer
+ @set_group_id = @new_resource.group.to_i
+ @set_group_id == @current_resource.group
+ else
+ group_info = Etc.getgrnam(@new_resource.group)
+ @set_group_id = group_info.gid
+ @set_group_id == @current_resource.group
+ end
end
end
def set_group
unless compare_group
+ Chef::Log.info("Setting group to #{@set_group_id} for #{@new_resource}")
::File.chown(nil, @set_group_id, @new_resource.path)
+ @new_resource.updated = true
+ end
+ end
+
+ def compare_mode
+ if @new_resource.mode != nil
+ case @new_resource.mode
+ when /^\d+$/, Integer
+ real_mode = sprintf("%o" % (@new_resource.mode & 007777))
+ real_mode.to_i == @current_resource.mode.to_i
+ end
+ end
+ end
+
+ def set_mode
+ unless compare_mode && @new_resource.mode != nil
+ Chef::Log.info("Setting mode to #{sprintf("%o" % (@new_resource.mode & 007777))
+ } for #{@new_resource}")
+ ::File.chmod(@new_resource.mode.to_i, @new_resource.path)
+ @new_resource.updated = true
end
end
def action_create
unless ::File.exists?(@new_resource.path)
+ Chef::Log.info("Creating #{@new_resource} at #{@new_resource.path}")
::File.open(@new_resource.path, "w+") { |f| }
+ @new_resource.updated = true
end
- set_owner
- set_group
+ set_owner if @new_resource.owner != nil
+ set_group if @new_resource.group != nil
+ set_mode if @new_resource.mode != nil
end
def action_delete
if ::File.exists?(@new_resource.path) && ::File.writable?(@new_resource.path)
+ Chef::Log.info("Deleting #{@new_resource} at #{@new_resource.path}")
::File.delete(@new_resource.path)
+ @new_resource.updated = true
+ else
+ raise "Cannot delete #{@new_resource} at #{@new_resource_path}!"
end
end
+ def action_touch
+ action_create
+ time = Time.now
+ Chef::Log.info("Updating #{@new_resource} with new atime/mtime of #{time}")
+ ::File.utime(time, time, @new_resource.path)
+ @new_resource.updated = true
+ end
+
end
end
end \ No newline at end of file