Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Compute and compare checksums before copying the new file in place

  • Loading branch information...
commit 55dd54459b9ffaed28e1290dcdef9de75f45306f 1 parent fadea36
Nathan Mueller authored
View
27 chef/lib/chef/provider/remote_file.rb
@@ -53,27 +53,28 @@ def do_remote_file(source, path)
raw_file = get_from_uri(source) ||
get_from_server(source, @current_resource.checksum) ||
get_from_local_cookbook(source)
+ @new_resource.checksum(self.checksum(raw_file.path))
- # If the file exists
- if ::File.exists?(@new_resource.path)
- # And it matches the checksum of the raw file
- @new_resource.checksum(self.checksum(raw_file.path))
- if @new_resource.checksum != @current_resource.checksum
+ if @new_resource.checksum == @current_resource.checksum
+ Chef::Log.debug("File #{@new_resouce} unchanged, not updating")
+ else
+ # If the file exists
+ if ::File.exists?(@new_resource.path)
# Updating target file, let's perform a backup!
Chef::Log.debug("#{@new_resource} changed from #{@current_resource.checksum} to #{@new_resource.checksum}")
Chef::Log.info("Updating #{@new_resource} at #{@new_resource.path}")
backup(@new_resource.path)
+ else
+ # We're creating a new file
+ Chef::Log.info("Creating #{@new_resource} at #{@new_resource.path}")
end
- else
- # We're creating a new file
- Chef::Log.info("Creating #{@new_resource} at #{@new_resource.path}")
- end
- FileUtils.cp(raw_file.path, @new_resource.path)
- @new_resource.updated = true
+ FileUtils.cp(raw_file.path, @new_resource.path)
+ @new_resource.updated = true
- # We're done with the file, so make sure to close it if it was open.
- raw_file.close unless raw_file.closed?
+ # We're done with the file, so make sure to close it if it was open.
+ raw_file.close unless raw_file.closed?
+ end
rescue Net::HTTPRetriableError => e
if e.response.kind_of?(Net::HTTPNotModified)
Chef::Log.debug("File #{path} is unchanged")
View
12 chef/spec/unit/provider/remote_file_spec.rb
@@ -185,12 +185,19 @@ def do_remote_file
end
- it "should set the new resource to updated" do
- @resource.should_receive(:updated=).with(true)
+ it "should not set the new resource to updated if checksums match" do
+ @resource.should_not_receive(:updated=).with(true)
+ do_remote_file
+ end
+
+ it "should set the new resource to updated if things changed" do
+ @resource.should_receive(:updated=).with(true)
+ @provider.current_resource.checksum("nomatch")
do_remote_file
end
it "should copy the raw file to the new resource" do
+ @provider.current_resource.checksum("nomatch")
FileUtils.should_receive(:cp).with(@tempfile.path, @resource.path).and_return(true)
do_remote_file
end
@@ -214,6 +221,7 @@ def do_remote_file
end
it "should close the file when done" do
+ @provider.current_resource.checksum("nomatch")
@tempfile.should_receive(:close)
do_remote_file
end
Please sign in to comment.
Something went wrong with that request. Please try again.