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

Make File.readable? and .writable? follow symlinks on Windows #14514

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
68 changes: 68 additions & 0 deletions spec/std/file_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,24 @@ describe "File" do
it "gives false when a component of the path is a file" do
File.exists?(datapath("dir", "test_file.txt", "")).should be_false
end

it "follows symlinks" do
with_tempfile("good_symlink.txt", "bad_symlink.txt") do |good_path, bad_path|
File.symlink(File.expand_path(datapath("test_file.txt")), good_path)
File.symlink(File.expand_path(datapath("non_existing_file.txt")), bad_path)

File.exists?(good_path).should be_true
File.exists?(bad_path).should be_false
end
end
end

describe "executable?" do
it "gives true" do
crystal = Process.executable_path || pending! "Unable to locate compiler executable"
File.executable?(crystal).should be_true
end

it "gives false" do
File.executable?(datapath("test_file.txt")).should be_false
end
Expand All @@ -214,6 +229,17 @@ describe "File" do
it "gives false when a component of the path is a file" do
File.executable?(datapath("dir", "test_file.txt", "")).should be_false
end

it "follows symlinks" do
with_tempfile("good_symlink_x.txt", "bad_symlink_x.txt") do |good_path, bad_path|
crystal = Process.executable_path || pending! "Unable to locate compiler executable"
File.symlink(File.expand_path(crystal), good_path)
File.symlink(File.expand_path(datapath("non_existing_file.txt")), bad_path)

File.executable?(good_path).should be_true
File.executable?(bad_path).should be_false
end
end
end

describe "readable?" do
Expand Down Expand Up @@ -248,7 +274,28 @@ describe "File" do
File.readable?(path).should be_false
end
end

it "follows symlinks" do
with_tempfile("good_symlink_r.txt", "bad_symlink_r.txt", "unreadable.txt") do |good_path, bad_path, unreadable|
File.write(unreadable, "")
File.chmod(unreadable, 0o222)
pending_if_superuser!

File.symlink(File.expand_path(datapath("test_file.txt")), good_path)
File.symlink(File.expand_path(unreadable), bad_path)

File.readable?(good_path).should be_true
File.readable?(bad_path).should be_false
end
end
{% end %}

it "gives false when the symbolic link destination doesn't exist" do
with_tempfile("missing_symlink_r.txt") do |missing_path|
File.symlink(File.expand_path(datapath("non_existing_file.txt")), missing_path)
File.readable?(missing_path).should be_false
end
end
end

describe "writable?" do
Expand All @@ -272,6 +319,27 @@ describe "File" do
File.writable?(path).should be_false
end
end

it "follows symlinks" do
with_tempfile("good_symlink_w.txt", "bad_symlink_w.txt", "readonly.txt") do |good_path, bad_path, readonly|
File.write(readonly, "")
File.chmod(readonly, 0o444)
pending_if_superuser!

File.symlink(File.expand_path(datapath("test_file.txt")), good_path)
File.symlink(File.expand_path(readonly), bad_path)

File.writable?(good_path).should be_true
File.writable?(bad_path).should be_false
end
end

it "gives false when the symbolic link destination doesn't exist" do
with_tempfile("missing_symlink_w.txt") do |missing_path|
File.symlink(File.expand_path(datapath("non_existing_file.txt")), missing_path)
File.writable?(missing_path).should be_false
end
end
end

describe "file?" do
Expand Down
17 changes: 10 additions & 7 deletions src/crystal/system/win32/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,28 @@ module Crystal::System::File
end

def self.exists?(path, *, follow_symlinks = true)
if follow_symlinks
path = realpath?(path) || return false
end
accessible?(path, check_writable: false)
accessible?(path, check_writable: false, follow_symlinks: follow_symlinks)
end

def self.readable?(path) : Bool
accessible?(path, check_writable: false)
accessible?(path, check_writable: false, follow_symlinks: true)
end

def self.writable?(path) : Bool
accessible?(path, check_writable: true)
accessible?(path, check_writable: true, follow_symlinks: true)
end

def self.executable?(path) : Bool
# NOTE: this always follows symlinks:
# https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getbinarytypew#remarks
LibC.GetBinaryTypeW(System.to_wstr(path), out result) != 0
end

private def self.accessible?(path, *, check_writable)
private def self.accessible?(path, *, check_writable, follow_symlinks)
if follow_symlinks
path = realpath?(path) || return false
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: more idiomatic version:

Suggested change
path = realpath?(path) || return false
return false unless path = realpath?(path)

Copy link
Member

Choose a reason for hiding this comment

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

@Sija I disagree on being more idiomatic. IMO it makes a lot more sense to focus on the assignment and an escape condition. A trailing assignment is very easy to overlook.

end

attributes = LibC.GetFileAttributesW(System.to_wstr(path))
return false if attributes == LibC::INVALID_FILE_ATTRIBUTES
return true if attributes.bits_set?(LibC::FILE_ATTRIBUTE_DIRECTORY)
Expand Down
Loading