Skip to content

Commit

Permalink
fix issue where FileUtils.mv fails on some Windows machines (fixes be…
Browse files Browse the repository at this point in the history
  • Loading branch information
tknerr committed Oct 28, 2012
1 parent 0224219 commit 970ac83
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
15 changes: 15 additions & 0 deletions lib/berkshelf/location.rb
Expand Up @@ -171,6 +171,21 @@ def to_json
MultiJson.dump(self.to_hash, pretty: true)
end

# Fixes #140, which might be caused by this http://www.ruby-forum.com/topic/1044813
#
# When trying to move a file from src to dest using FileUtils.mv(src, dest),
# if we encounter Errno::EACCES, which seems to happen occasionally on Windows systems,
# try to copy/delete the file instead of moving it.
#
def move_file(src, dest)
begin
FileUtils.mv(src, dest, force: false)
rescue Errno::EACCES
FileUtils.cp_r(src, dest)
FileUtils.rm_rf(src)
end
end

private

def set_downloaded_status(state)
Expand Down
2 changes: 1 addition & 1 deletion lib/berkshelf/locations/chef_api_location.rb
Expand Up @@ -130,7 +130,7 @@ def download(destination)
scratch = download_files(cookbook.manifest)

cb_path = File.join(destination, "#{name}-#{version}")
FileUtils.mv(scratch, cb_path, force: true)
move_file(scratch, cb_path)

cached = CachedCookbook.from_store_path(cb_path)
validate_cached(cached)
Expand Down
2 changes: 1 addition & 1 deletion lib/berkshelf/locations/git_location.rb
Expand Up @@ -52,7 +52,7 @@ def download(destination)

cb_path = File.join(destination, "#{self.name}-#{Git.rev_parse(tmp_clone)}")
FileUtils.rm_rf(cb_path)
FileUtils.mv(tmp_clone, cb_path, force: true)
move_file(tmp_clone, cb_path)

cached = CachedCookbook.from_store_path(cb_path)
validate_cached(cached)
Expand Down
3 changes: 2 additions & 1 deletion lib/berkshelf/locations/site_location.rb
Expand Up @@ -53,7 +53,8 @@ def download(destination)
cb_path = File.join(destination, "#{name}-#{version}")

self.class.unpack(downloaded_tf.path, dir)
FileUtils.mv(File.join(dir, name), cb_path, force: true)

move_file(File.join(dir, name), cb_path)

cached = CachedCookbook.from_store_path(cb_path)
validate_cached(cached)
Expand Down

0 comments on commit 970ac83

Please sign in to comment.