Skip to content

Instantly share code, notes, and snippets.

@KAllan357
Last active December 14, 2015 09:59
Show Gist options
  • Save KAllan357/5069014 to your computer and use it in GitHub Desktop.
Save KAllan357/5069014 to your computer and use it in GitHub Desktop.
diff --git a/lib/chef/provider/link.rb b/lib/chef/provider/link.rb
index b8017c4..9f4723e 100644
--- a/lib/chef/provider/link.rb
+++ b/lib/chef/provider/link.rb
@@ -93,7 +93,7 @@ class Chef
@current_resource.link_type != @new_resource.link_type
if @current_resource.to # nil if target_file does not exist
converge_by("unlink existing file at #{@new_resource.target_file}") do
- ::File.unlink(@new_resource.target_file)
+ file_class.unlink(@new_resource.target_file)
end
end
if @new_resource.link_type == :symbolic
@@ -122,7 +122,7 @@ class Chef
def action_delete
if @current_resource.to # Exists
converge_by ("delete link at #{@new_resource.target_file}") do
- ::File.delete(@new_resource.target_file)
+ file_class.delete(@new_resource.target_file)
Chef::Log.info("#{@new_resource} deleted")
end
end
diff --git a/lib/chef/win32/file.rb b/lib/chef/win32/file.rb
index d489c9c..a289943 100644
--- a/lib/chef/win32/file.rb
+++ b/lib/chef/win32/file.rb
@@ -113,6 +113,23 @@ class Chef
end
end
+ # Calls the appropriate delete on a Windows symbolic link. When symbolic
+ # links are created by Windows, Ruby's File.delete method cannot delete
+ # SYMBOLIC_LINK_FLAG_DIRECTORY links.
+ #
+ def self.delete(target_file)
+ if ::File.directory?(target_file)
+ ::Dir.delete(target_file)
+ else
+ ::File.delete(target_file)
+ end
+ end
+
+ # There is no unlink on Windows, so just delete the symbolic link.
+ def self.unlink(target_file)
+ delete(target_file)
+ end
+
# Gets the short form of a path (Administrator -> ADMINI~1)
def self.get_short_path_name(path)
path = path.to_wstring
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment