Calling size on a broken symlink throws a NoSuchFileException,
VinceMacBuche opened this issue · comments
I got a NoSuchFileException on a call to size on a Directory that contains a broken symlink (a symlink that points to a non existing File) (code is here: https://github.com/Normation/rudder/blob/054143ae75787b7a1d22661f9a2fe6cf94cd24a8/webapp/sources/rudder/rudder-rest/src/main/scala/com/normation/rudder/rest/internal/SharedFilesAPI.scala#L93 )
Even when using option returnZeroIfMissing I still get the same error
It happens because, the call to size, call nio.Files.size which get files attributes with default linkOptions, which is "Follow_links" with Unix file systems. (I was running on Java8, maybe it has changed in more recent JVM)
I think catching also the NoSuchFileException when returnZeroIfMissing could be a good solution, but I let you judge if it the case or not (I'm going to make a pr to add this, feel free to ignore it is not what you think it needs)
I also made a small worksheet to replicate this case:
import better.files.File.LinkOptions
import better.files._
import better.files.File.VisitOptions
val dir = File.root / "tmp" / "dir"
val file = dir / "file"
val link = dir / "link"
val brokenLink = dir / "brokenLink"
if (dir.notExists(LinkOptions.noFollow)) dir.createDirectory()
if (file.notExists(LinkOptions.noFollow)) file.createFile()
if (link.notExists(LinkOptions.noFollow)) link.symbolicLinkTo(file)
if (brokenLink.notExists(LinkOptions.noFollow)) brokenLink.symbolicLinkTo(dir / "brokenFile")
dir.size(true)(VisitOptions.default)
Btw, thanks for better-files, i really like it :)
Edit: We are using better-files 3.9.1 on Scala 2.13.3, I don't know if it's important but it could