Skip to content

Commit

Permalink
Support symlink for NioFSWrappingFS
Browse files Browse the repository at this point in the history
  • Loading branch information
oldergod committed Jun 19, 2023
1 parent 6d3968a commit 5750324
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,19 @@ abstract class AbstractFileSystemTest(
}

@Test
fun canonicalizeNoSuchFile() {
fun canonicalizeAbsolutePathNoSuchFile() {
assertFailsWith<FileNotFoundException> {
fileSystem.canonicalize(base / "no-such-file")
}
}

@Test
fun canonicalizeRelativePathNoSuchFile() {
assertFailsWith<FileNotFoundException> {
fileSystem.canonicalize("no-such-file".toPath())
}
}

@Test
fun canonicalizeFollowsSymlinkDirectories() {
if (!supportsSymlink()) return
Expand Down Expand Up @@ -2274,6 +2281,20 @@ abstract class AbstractFileSystemTest(
assertEquals(listOf(baseBA, baseBB), fileSystem.list(baseB))
}

@Test
fun listDotDirectory() {
if (!supportsSymlink()) return

println(fileSystem.metadata(".".toPath()))

val paths = fileSystem.list(".".toPath()).map(Path::toString)
assertTrue(paths.joinToString()) {
paths.contains("jvm") &&
paths.contains("src") &&
paths.contains("build.gradle.kts")
}
}

@Test
fun symlinkFileLastAccessedAt() {
if (!supportsSymlink()) return
Expand Down Expand Up @@ -2411,7 +2432,6 @@ abstract class AbstractFileSystemTest(
if (windowsLimitations) return false
return when (fileSystem::class.simpleName) {
"JvmSystemFileSystem",
"NioFileSystemWrappingFileSystem",
-> false
else -> true
}
Expand Down
28 changes: 26 additions & 2 deletions okio/src/jvmMain/kotlin/okio/NioFileSystemWrappingFileSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,31 @@ internal class NioFileSystemWrappingFileSystem(javaNioFileSystem: JavaNioFileSys
* nio file system in order for things to work properly.
*/
private fun Path.resolve(readSymlink: Boolean = false): NioPath {
val resolved = delegateRoot.resolve(toString())
val resolved = if (isRelative) {
delegateRoot.resolve(this.toFile().canonicalPath)
} else {
delegateRoot.resolve(toString())
}

return if (readSymlink && resolved.isSymbolicLink()) {
resolved.readSymbolicLink()
} else {
resolved
}
}

override fun canonicalize(path: Path): Path {
if (path.isAbsolute) {
try {
return path.resolve(readSymlink = true).toRealPath().toOkioPath()
} catch (e: NoSuchFileException) {
throw FileNotFoundException("no such file $path")
}
} else {
return super.canonicalize(path)
}
}

override fun metadataOrNull(path: Path): FileMetadata? {
return metadataOrNull(path.resolve())
}
Expand Down Expand Up @@ -195,7 +212,14 @@ internal class NioFileSystemWrappingFileSystem(javaNioFileSystem: JavaNioFileSys
}

override fun createSymlink(source: Path, target: Path) {
Files.createSymbolicLink(source.resolve(), target.resolve())
val sourceNioPath = source.resolve()
val targetNioPath =
if (source.isAbsolute && target.isRelative) {
sourceNioPath.parent.resolve(target.toString())
} else {
target.resolve()
}
Files.createSymbolicLink(sourceNioPath, targetNioPath)
}

override fun toString(): String = "NioFileSystemWrappingFileSystem"
Expand Down
11 changes: 11 additions & 0 deletions okio/src/jvmTest/kotlin/okio/JvmSystemFileSystemTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package okio
import com.google.common.jimfs.Configuration
import com.google.common.jimfs.Jimfs
import java.io.InterruptedIOException
import java.nio.file.FileSystems
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.fail
Expand Down Expand Up @@ -73,3 +74,13 @@ class NioFileSystemWrappingFileSystemTest : AbstractFileSystemTest(
allowAtomicMoveFromFileToDirectory = true,
temporaryDirectory = FileSystem.SYSTEM_TEMPORARY_DIRECTORY,
)

class NioDefaultFileSystemWrappingFileSystemTest : AbstractFileSystemTest(
clock = Clock.System,
fileSystem = FileSystems.getDefault().asOkioFileSystem(),
windowsLimitations = false,
allowClobberingEmptyDirectories = Path.DIRECTORY_SEPARATOR == "\\",
allowAtomicMoveFromFileToDirectory = false,
allowRenameWhenTargetIsOpen = Path.DIRECTORY_SEPARATOR != "\\",
temporaryDirectory = FileSystem.SYSTEM_TEMPORARY_DIRECTORY,
)

0 comments on commit 5750324

Please sign in to comment.