Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PUP-5018) Cannot unlink dangling Windows symlinks #4147

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/puppet/file_system/windows.rb
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I understand what you meant by nil coming back.

::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
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