3

I'm using Crystal 0.25.0 and File.info(string).symlink? returns false when it should return true in the following sample:

`mkdir -p /tmp/delete`
 Dir.cd "/tmp/delete"
`rm -f b`
`touch a`
`ln -s a b`

puts File.info("b").symlink?.inspect  # false
puts File.info("b").type              # File
puts Process.run("test", "-L b".split).success? # true
puts Process.run("test", "-L a".split).success? # false

It seems to resolve the link. Is this the expected behaviour?

dgo.a
  • 2,634
  • 23
  • 35

2 Answers2

3

Yes, File.info follows symlinks by default. This is the expected behavior, but you can disable it by passing follow_symlinks: false to the method:

File.info("b", follow_symlinks: false).symlink? # => true

This behavior is documented under File.info in the API docs.

Sven
  • 5,155
  • 29
  • 53
  • Thanks. I kept looking at `File::Info.new`. I forgot to look at `File.info`. My mistake. – dgo.a Jun 17 '18 at 22:13
  • Don't worry about it, it's an easy thing to miss. `File::Info` is an abstract struct that you as a developer will most likely not want to use. It can be used by various archive library implementations like the `.tar`-format to provide an `File::Info` to the user of an in-memory archive without needing to write the data to disk and then rely on the OS to provide it (like linux provides `stat`). Hopefully that makes sense? – Sven Jun 17 '18 at 22:27
2

If you don't need the File::Info instance for anything else, you can just use File.symlink? which essentially calls info(path, follow_symlinks: false).symlink?.

Johannes Müller
  • 5,581
  • 1
  • 11
  • 25