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

IO.relativize is broken when base contains ".." #182

Closed
eed3si9n opened this issue Sep 27, 2018 · 0 comments · Fixed by #183
Closed

IO.relativize is broken when base contains ".." #182

eed3si9n opened this issue Sep 27, 2018 · 0 comments · Fixed by #183
Assignees

Comments

@eed3si9n
Copy link
Member

eed3si9n commented Sep 27, 2018

The inconsisnt behavior in JDK 11 reported in #85 exposed that IO.relativize is currently incorrect.

steps

Given that the base directory is /tmp/io-relativize/inside-dir/.. and that the target to be relativized is /tmp/io-relativize/meh.file, IOSpec states

IO.relativize(relativeRootDir, nestedFile).map(file) shouldBe Some(file("../../meh.file"))

problem

Some(file("../../meh.file")) is incorrect.

expectation

Some("meh.file")

notes

  def relativize(base: File, file: File): Option[String] = {
    val basePath = (if (base.isAbsolute) base else base.getCanonicalFile).toPath
    val filePath = (if (file.isAbsolute) file else file.getCanonicalFile).toPath
    if ((filePath startsWith basePath) || (filePath.normalize() startsWith basePath.normalize())) {
      val relativePath = catching(classOf[IllegalArgumentException]) opt (basePath relativize filePath)
      relativePath map (_.toString)
    } else None
  }

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/File.html#isAbsolute:

On UNIX systems, a pathname is absolute if its prefix is "/".

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/File.html#getCanonicalPath():

This typically involves removing redundant names such as "." and ".." from the pathname, resolving symbolic links (on UNIX platforms)

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Path.html#normalize()

Returns a path that is this path with redundant name elements eliminated.

See also #175

eed3si9n added a commit to eed3si9n/io that referenced this issue Sep 27, 2018
Fixes sbt#182

The inconsisnt behavior in JDK 11 reported in sbt#85 exposed that `IO.relativize` is currently incorrect.

The IOSpec states

```scala
IO.relativize(relativeRootDir, nestedFile).map(file) shouldBe Some(file("../../meh.file"))
```

however, given that the base directory is `/tmp/io-relativize/inside-dir/..` and that the target to be relativized is `/tmp/io-relativize/meh.file`, the correct answer should be `"meh.file"`, not `"../../meh.file"`.

This fixes the problem by avoiding the call to `getCanonicalFile`, which has the side effect of resolving symbolic links.
@eed3si9n eed3si9n self-assigned this Sep 27, 2018
eed3si9n added a commit to eed3si9n/io that referenced this issue Sep 27, 2018
Fixes sbt#182

The inconsisnt behavior in JDK 11 reported in sbt#85 exposed that `IO.relativize` is currently incorrect.

The IOSpec states

```scala
IO.relativize(relativeRootDir, nestedFile).map(file) shouldBe Some(file("../../meh.file"))
```

however, given that the base directory is `/tmp/io-relativize/inside-dir/..` and that the target to be relativized is `/tmp/io-relativize/meh.file`, the correct answer should be `"meh.file"`, not `"../../meh.file"`.

This fixes the problem by avoiding the call to `getCanonicalFile`, which has the side effect of resolving symbolic links.
eed3si9n added a commit to eed3si9n/io that referenced this issue Sep 28, 2018
Fixes sbt#182

The inconsisnt behavior in JDK 11 reported in sbt#85 exposed that `IO.relativize` is currently incorrect.

The IOSpec states

```scala
IO.relativize(relativeRootDir, nestedFile).map(file) shouldBe Some(file("../../meh.file"))
```

however, given that the base directory is `/tmp/io-relativize/inside-dir/..` and that the target to be relativized is `/tmp/io-relativize/meh.file`, the correct answer should be `"meh.file"`, not `"../../meh.file"`.

This fixes the problem by avoiding the call to `getCanonicalFile`, which has the side effect of resolving symbolic links.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant