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

Add File.same?(path1, path2) with follow_symlinks option #6161

Merged
merged 3 commits into from
Jun 4, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 21 additions & 2 deletions spec/std/file_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -230,21 +230,40 @@ describe "File" do
File.link(in_path, out_path)
File.exists?(out_path).should be_true
File.symlink?(out_path).should be_false
File.info(in_path).should eq(File.info(out_path))
File.same?(in_path, out_path).should be_true
ensure
File.delete(out_path) if File.exists?(out_path)
end
end
end

describe "same?" do
it "compares following symlinks by default or requested" do
file = "#{__DIR__}/data/test_file.txt"
symlink = "#{__DIR__}/data/test_file_symlink.txt"
other = "#{__DIR__}/data/test_file.ini"

begin
File.symlink(file, symlink)

File.same?(file, symlink).should be_true
File.same?(file, symlink, follow_symlinks: true).should be_true
File.same?(file, symlink, follow_symlinks: false).should be_false
File.same?(file, other).should be_false
ensure
File.delete(symlink) if File.exists?(symlink)
end
end
end

describe "symlink" do
it "creates a symbolic link" do
in_path = "#{__DIR__}/data/test_file.txt"
out_path = "#{__DIR__}/data/test_file_symlink.txt"
begin
File.symlink(in_path, out_path)
File.symlink?(out_path).should be_true
File.info(in_path).should eq(File.info(out_path))
File.same?(in_path, out_path).should be_true
ensure
File.delete(out_path) if File.exists?(out_path)
end
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/unix/file_info.cr
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct Crystal::System::FileInfo < ::File::Info
@stat.st_gid.to_u32
end

def ==(other : ::File::Info) : Bool
def same_file?(other : ::File::Info) : Bool
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a File#== as well, which does info on both file instances and then same_file??

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think the semantic of File#== to compare files is accurate. The file has more state that the file description.

Copy link
Contributor

Choose a reason for hiding this comment

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

It shouldn't do. But I guess this is fine for now...

Copy link
Contributor

Choose a reason for hiding this comment

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

This now needs an abstract def in src/file/info.cr.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated in the next commit

@stat.st_dev == other.@stat.st_dev && @stat.st_ino == other.@stat.st_ino
end
end
6 changes: 6 additions & 0 deletions src/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ class File < IO::FileDescriptor
Crystal::System::File.exists?(path)
end

# Returns `true` if *path1* and *path2* represents the same file.
# The comparison take symlinks in consideration if *follow_symlinks* is `true`.
def self.same?(path1 : String, path2 : String, follow_symlinks = true) : Bool
info(path1, follow_symlinks).same_file? info(path2, follow_symlinks)
end

# Returns the size of *filename* bytes. Raises `Errno` if the file at *path*
# does not exist.
#
Expand Down
4 changes: 2 additions & 2 deletions src/file/info.cr
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ class File
# The group ID that the file belongs to.
abstract def group : UInt32

# Two `File::Info`s are equal if and only if they are of the same file.
# Returns true if this `Info` and *other* are of the same file.
#
# On unix, this compares device and inode fields, and will compare equal for
# hard linked files.
abstract def ==(other : File::Info)
abstract def same_file?(other : File::Info) : Bool

# Returns true if this `Info` represents a standard file. Shortcut for
# `type.file?`.
Expand Down