Skip to content

Commit

Permalink
Merge pull request #4147 from Iristyle/ticket/stable/PUP-5018-Windows…
Browse files Browse the repository at this point in the history
…-unlink-fails-on-orphaned-links

(PUP-5018) Cannot unlink dangling Windows symlinks
  • Loading branch information
ferventcoder committed Aug 13, 2015
2 parents 71ba555 + 579d5ea commit d70c351
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/puppet/file_system/windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ def unlink(*file_names)
stat = Puppet::Util::Windows::File.stat(file_name) rescue nil

# sigh, Ruby + Windows :(
if stat && stat.ftype == 'directory'
if !stat
::File.unlink(file_name) rescue Dir.rmdir(file_name)
elsif stat.ftype == 'directory'
if Puppet::Util::Windows::File.symlink?(file_name)
Dir.rmdir(file_name)
else
Expand Down
20 changes: 20 additions & 0 deletions spec/unit/file_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,26 @@ def increment_counter_in_multiple_processes(file, num_procs, options)
expect(Puppet::FileSystem.readlink(symlink)).to eq(missing_file.to_s)
end

it "should be able to unlink a dangling symlink pointed at a file" do
symlink = tmpfile("somefile_link")
Puppet::FileSystem.symlink(file, symlink)
::File.delete(file)
Puppet::FileSystem.unlink(symlink)

expect(Puppet::FileSystem).to_not be_exist(file)
expect(Puppet::FileSystem).to_not be_exist(symlink)
end

it "should be able to unlink a dangling symlink pointed at a directory" do
symlink = tmpfile("somedir_link")
Puppet::FileSystem.symlink(dir, symlink)
Dir.rmdir(dir)
Puppet::FileSystem.unlink(symlink)

expect(Puppet::FileSystem).to_not be_exist(dir)
expect(Puppet::FileSystem).to_not be_exist(symlink)
end

it "should delete only the symlink and not the target when calling unlink instance method" do
[file, dir].each do |target|
symlink = tmpfile("#{Puppet::FileSystem.basename(target)}_link")
Expand Down

0 comments on commit d70c351

Please sign in to comment.