Skip to content

Commit

Permalink
fix for relativeTo with paths containing dots
Browse files Browse the repository at this point in the history
  • Loading branch information
oldergod committed Apr 15, 2024
1 parent a647246 commit 4df5c8c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions okio/src/commonMain/kotlin/okio/internal/Path.kt
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ internal inline fun Path.commonRelativeTo(other: Path): Path {
"Impossible relative path to resolve: $this and $other"
}

if (other.bytes == DOT) {
// Anything relative to "." is itself!
return this
}

val buffer = Buffer()
val slash = other.slash ?: slash ?: Path.DIRECTORY_SEPARATOR.toSlash()
for (i in firstNewSegmentIndex until otherSegments.size) {
Expand Down
18 changes: 18 additions & 0 deletions okio/src/commonTest/kotlin/okio/PathTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,24 @@ class PathTest {
assertRelativeTo(f, e, ".".toPath())
}

@Test
fun relativeUnixDot() {
val a = "Users/jesse/hello.txt".toPath()
val b = ".".toPath()
assertRelativeTo(a, b, "../../..".toPath(), sameAsNio = false)
assertRelativeTo(b, a, "Users/jesse/hello.txt".toPath(), sameAsNio = false)

val c = "Users/./jesse/hello.txt".toPath()
val d = "Admin/Secret".toPath()
assertRelativeTo(c, d, "../../../Admin/Secret".toPath())
assertRelativeTo(d, c, "../../Users/jesse/hello.txt".toPath())

val e = "Users/".toPath()
val f = "Users/.".toPath()
assertRelativeTo(e, f, ".".toPath())
assertRelativeTo(f, e, ".".toPath())
}

// Note that we handle the normalized version of the paths when computing relative paths.
@Test
fun relativeToUnnormalizedPath() {
Expand Down

0 comments on commit 4df5c8c

Please sign in to comment.