# # Author:: Tim Hinderliter () # Copyright:: Copyright (c) 2010 Opscode, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require 'chef/log' require 'uuidtools' class Chef # == Chef::Checksum # Checksum for an individual file; e.g., used for sandbox/cookbook uploading # to track which files the system already manages. class Checksum attr_accessor :checksum, :create_time # Creates a new Chef::Checksum object. # === Arguments # checksum::: the MD5 content hash of the file # # === Returns # object:: Duh. :) def initialize(checksum=nil) @create_time = Time.now.iso8601 @checksum = checksum @original_committed_file_location = nil end def to_json(*a) result = { :checksum => checksum, :create_time => create_time, :json_class => self.class.name, :chef_type => 'checksum', :name => checksum } result.to_json(*a) end def self.json_create(o) checksum = new(o['checksum']) checksum.create_time = o['create_time'] checksum end ## # On-Disk Checksum File Repo (Chef Server API) ## def file_location File.join(checksum_repo_directory, checksum) end def checksum_repo_directory File.join(Chef::Config.checksum_path, checksum[0..1]) end private # Deletes the file backing this checksum from the on-disk repo. # Purging the checksums is how users can get back to a valid state if # they've deleted files, so we silently swallow Errno::ENOENT here. def purge_file FileUtils.rm(file_location) rescue Errno::ENOENT true end end end