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

Allow File.delete to remove read-only files on Windows #13462

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions spec/std/file_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ describe "File" do
other = datapath("test_file.ini")

with_tempfile("test_file_symlink.txt") do |symlink|
File.symlink(File.real_path(file), symlink)
File.symlink(File.realpath(file), symlink)

File.same?(file, symlink).should be_false
File.same?(file, symlink, follow_symlinks: true).should be_true
Expand Down Expand Up @@ -514,7 +514,7 @@ describe "File" do
end
end

describe "delete" do
describe ".delete" do
it "deletes a file" do
with_tempfile("delete-file.txt") do |filename|
File.open(filename, "w") { }
Expand All @@ -533,6 +533,20 @@ describe "File" do
end
end

it "deletes a read-only file" do
with_tempfile("delete-file-dir") do |path|
Dir.mkdir(path)
File.chmod(path, 0o755)

filename = File.join(path, "foo")
File.open(filename, "w") { }
File.exists?(filename).should be_true
File.chmod(filename, 0o000)
File.delete(filename)
File.exists?(filename).should be_false
end
end

it "deletes? a file" do
with_tempfile("delete-file.txt") do |filename|
File.open(filename, "w") { }
Expand Down
22 changes: 2 additions & 20 deletions spec/support/tempfile.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def with_tempfile(*paths, file = __FILE__, &)
ensure
if SPEC_TEMPFILE_CLEANUP
paths.each do |path|
rm_rf(path) if File.exists?(path)
FileUtils.rm_rf(path) if File.exists?(path)
end
end
end
Expand Down Expand Up @@ -74,24 +74,6 @@ end

if SPEC_TEMPFILE_CLEANUP
at_exit do
rm_rf(SPEC_TEMPFILE_PATH) if Dir.exists?(SPEC_TEMPFILE_PATH)
end
end

private def rm_rf(path : String) : Nil
if Dir.exists?(path) && !File.symlink?(path)
Dir.each_child(path) do |entry|
src = File.join(path, entry)
rm_rf(src)
end
Dir.delete(path)
else
begin
File.delete(path)
rescue File::AccessDeniedError
# To be able to delete read-only files (e.g. ones under .git/) on Windows.
File.chmod(path, 0o666)
File.delete(path)
end
FileUtils.rm_rf(SPEC_TEMPFILE_PATH) if Dir.exists?(SPEC_TEMPFILE_PATH)
end
end
10 changes: 10 additions & 0 deletions src/crystal/system/win32/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,21 @@ module Crystal::System::File
return false
end

# Windows cannot delete read-only files, so we unset the attribute here, but
# restore it afterwards if deletion still failed
read_only_removed = false
if attributes.bits_set?(LibC::FILE_ATTRIBUTE_READONLY)
if LibC.SetFileAttributesW(win_path, attributes & ~LibC::FILE_ATTRIBUTE_READONLY) != 0
read_only_removed = true
end
end

# all reparse point directories should be deleted like a directory, not just
# symbolic links, so we don't care about the reparse tag here
is_reparse_dir = attributes.bits_set?(LibC::FILE_ATTRIBUTE_REPARSE_POINT) && attributes.bits_set?(LibC::FILE_ATTRIBUTE_DIRECTORY)
result = is_reparse_dir ? LibC._wrmdir(win_path) : LibC._wunlink(win_path)
return true if result == 0
LibC.SetFileAttributesW(win_path, attributes) if read_only_removed
raise ::File::Error.from_errno("Error deleting file", file: path)
end

Expand Down