Skip to content

Commit

Permalink
Only create relative symlinks in WasiFileSystem (#1321)
Browse files Browse the repository at this point in the history
* Only create relative symlinks in WasiFileSystem

Creating absolute symlinks is broken in WASI. This also works
better for preopens.

* Also handle canonicalizing relative paths
  • Loading branch information
squarejesse authored Aug 2, 2023
1 parent 094fd45 commit 386d18f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ abstract class AbstractFileSystemTest(
@Test
fun canonicalizeDotReturnsCurrentWorkingDirectory() {
if (fileSystem.isFakeFileSystem || fileSystem is ForwardingFileSystem) return
if (isWasiFileSystem) return // Canonicalize is limited on WASI.
val cwd = fileSystem.canonicalize(".".toPath())
val cwdString = cwd.toString()
val slash = Path.DIRECTORY_SEPARATOR
assertTrue(cwdString) {
if (isWrappingJimFileSystem) {
cwdString.endsWith("work")
} else if (isWasiFileSystem) {
cwdString.endsWith("/tmp")
} else {
cwdString.endsWith("okio${slash}okio") ||
cwdString.endsWith("${slash}okio-parent-okio-nodefilesystem-test") ||
Expand Down Expand Up @@ -233,7 +234,6 @@ abstract class AbstractFileSystemTest(
@Test
fun canonicalizeReturnsShallowerPath() {
if (!supportsSymlink()) return
if (isWasiFileSystem) return // Canonicalize is limited on WASI.
val base = fileSystem.canonicalize(base)

val expected = base / "a.txt"
Expand Down Expand Up @@ -269,12 +269,10 @@ abstract class AbstractFileSystemTest(
fileSystem.write("a.txt".toPath()) {
writeUtf8("hello, world!")
}
} else if (isWrappingJimFileSystem) {
} else if (isWrappingJimFileSystem || isWasiFileSystem) {
fileSystem.write("a.txt".toPath()) {
writeUtf8("hello, world!")
}
} else if (isWasiFileSystem) {
return // TODO: implement this behavior.
}

val entries = fileSystem.list(".".toPath())
Expand All @@ -284,7 +282,6 @@ abstract class AbstractFileSystemTest(
@Test
fun listOnRelativePathWhichIsNotDotReturnsRelativePaths() {
if (isNodeJsFileSystem) return
if (isWasiFileSystem) return // TODO: implement this behavior.

// Make sure there's always at least one file so our assertion is useful. We copy the first 2
// entries of the real working directory of the JVM to validate the results on all environment.
Expand All @@ -300,7 +297,7 @@ abstract class AbstractFileSystemTest(
fileSystem.write(apiDir / "okio.api".toPath()) {
writeUtf8("hello, world!")
}
} else if (isWrappingJimFileSystem) {
} else if (isWrappingJimFileSystem || isWasiFileSystem) {
val apiDir = "api".toPath()
fileSystem.createDirectory(apiDir)
fileSystem.write(apiDir / "okio.api".toPath()) {
Expand Down Expand Up @@ -336,7 +333,6 @@ abstract class AbstractFileSystemTest(
@Test
fun listOrNullOnRelativePathWhichIsNotDotReturnsRelativePaths() {
if (isNodeJsFileSystem) return
if (isWasiFileSystem) return // TODO: implement this behavior.

// Make sure there's always at least one file so our assertion is useful. We copy the first 2
// entries of the real working directory of the JVM to validate the results on all environment.
Expand Down Expand Up @@ -607,7 +603,6 @@ abstract class AbstractFileSystemTest(
@Test
fun listRecursivelyFollowsSymlinks() {
if (!supportsSymlink()) return
if (isWasiFileSystem) return // Symlinks to absolute paths are broken on WASI.

val baseA = base / "a"
val baseAA = baseA / "a"
Expand Down Expand Up @@ -639,7 +634,6 @@ abstract class AbstractFileSystemTest(
@Test
fun listRecursivelyOnSymlink() {
if (!supportsSymlink()) return
if (isWasiFileSystem) return // Symlinks to absolute paths are broken on WASI.

val baseA = base / "a"
val baseAA = baseA / "a"
Expand Down Expand Up @@ -673,7 +667,6 @@ abstract class AbstractFileSystemTest(
@Test
fun listRecursiveOnSymlinkWithSpecialCharacterNamedFiles() {
if (!supportsSymlink()) return
if (isWasiFileSystem) return // Symlinks to absolute paths are broken on WASI.

val baseA = base / "ä"
val baseASuperSaiyan = baseA / "超サイヤ人"
Expand All @@ -691,7 +684,6 @@ abstract class AbstractFileSystemTest(
@Test
fun listRecursivelyOnSymlinkCycleThrows() {
if (!supportsSymlink()) return
if (isWasiFileSystem) return // Symlinks to absolute paths are broken on WASI.

val baseA = base / "a"
val baseAB = baseA / "b"
Expand Down Expand Up @@ -2311,7 +2303,14 @@ abstract class AbstractFileSystemTest(
val maxTime = clock.now()

val sourceMetadata = fileSystem.metadata(source)
assertEquals(target, sourceMetadata.symlinkTarget)
// Okio's WasiFileSystem only creates relative symlinks.
assertEquals(
when {
isWasiFileSystem -> target.relativeTo(source.parent!!)
else -> target
},
sourceMetadata.symlinkTarget,
)
assertInRange(sourceMetadata.createdAt, minTime, maxTime)
}

Expand Down Expand Up @@ -2359,7 +2358,6 @@ abstract class AbstractFileSystemTest(
@Test
fun openSymlinkSource() {
if (!supportsSymlink()) return
if (isWasiFileSystem) return // Symlinks to absolute paths are broken on WASI.

val target = base / "symlink-target"
val source = base / "symlink-source"
Expand All @@ -2373,7 +2371,6 @@ abstract class AbstractFileSystemTest(
fun openSymlinkSink() {
if (!supportsSymlink()) return
if (isJimFileSystem()) return
if (isWasiFileSystem) return // Symlinks to absolute paths are broken on WASI.

val target = base / "symlink-target"
val source = base / "symlink-source"
Expand All @@ -2387,7 +2384,6 @@ abstract class AbstractFileSystemTest(
@Test
fun openFileWithDirectorySymlink() {
if (!supportsSymlink()) return
if (isWasiFileSystem) return // Symlinks to absolute paths are broken on WASI.

val baseA = base / "a"
val baseAA = base / "a" / "a"
Expand All @@ -2403,7 +2399,6 @@ abstract class AbstractFileSystemTest(
@Test
fun openSymlinkFileHandle() {
if (!supportsSymlink()) return
if (isWasiFileSystem) return // Symlinks to absolute paths are broken on WASI.

val target = base / "symlink-target"
val source = base / "symlink-source"
Expand All @@ -2418,7 +2413,6 @@ abstract class AbstractFileSystemTest(
@Test
fun listSymlinkDirectory() {
if (!supportsSymlink()) return
if (isWasiFileSystem) return // Symlinks to absolute paths are broken on WASI.

val baseA = base / "a"
val baseAA = base / "a" / "a"
Expand All @@ -2436,7 +2430,6 @@ abstract class AbstractFileSystemTest(
@Test
fun symlinkFileLastAccessedAt() {
if (!supportsSymlink()) return
if (isWasiFileSystem) return // Symlinks to absolute paths are broken on WASI.

val target = base / "symlink-target"
val source = base / "symlink-source"
Expand All @@ -2452,7 +2445,6 @@ abstract class AbstractFileSystemTest(
@Test
fun symlinkDirectoryLastAccessedAt() {
if (!supportsSymlink()) return
if (isWasiFileSystem) return // Symlinks to absolute paths are broken on WASI.

val baseA = base / "a"
val baseAA = base / "a" / "a"
Expand Down Expand Up @@ -2483,7 +2475,6 @@ abstract class AbstractFileSystemTest(
@Test
fun moveSymlinkDoesntMoveTargetFile() {
if (!supportsSymlink()) return
if (isWasiFileSystem) return // Symlinks to absolute paths are broken on WASI.

val target = base / "symlink-target"
val source1 = base / "symlink-source-1"
Expand All @@ -2493,7 +2484,14 @@ abstract class AbstractFileSystemTest(
fileSystem.atomicMove(source1, source2)
assertEquals("I am the target file", target.readUtf8())
assertEquals("I am the target file", source2.readUtf8())
assertEquals(target, fileSystem.metadata(source2).symlinkTarget)
// Okio's WasiFileSystem only creates relative symlinks.
assertEquals(
when {
isWasiFileSystem -> target.relativeTo(source1.parent!!)
else -> target
},
fileSystem.metadata(source2).symlinkTarget,
)
}

@Test
Expand Down Expand Up @@ -2525,7 +2523,6 @@ abstract class AbstractFileSystemTest(
@Test
fun followingRecursiveSymlinksIsOkay() {
if (!supportsSymlink()) return
if (isWasiFileSystem) return // Symlinks to absolute paths are broken on WASI.

val pathA = base / "symlink-a"
val pathB = base / "symlink-b"
Expand Down
Loading

0 comments on commit 386d18f

Please sign in to comment.