From 109f7cee59784f0e0833961300d499f914783dd7 Mon Sep 17 00:00:00 2001 From: Filipp Zhinkin Date: Wed, 11 Oct 2023 14:18:56 +0200 Subject: [PATCH 1/4] Added Windows-specific Path.parent test --- core/mingw/test/files/SmokeFileTestWindows.kt | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/core/mingw/test/files/SmokeFileTestWindows.kt b/core/mingw/test/files/SmokeFileTestWindows.kt index 1a5c7a930..b42e9b0f3 100644 --- a/core/mingw/test/files/SmokeFileTestWindows.kt +++ b/core/mingw/test/files/SmokeFileTestWindows.kt @@ -5,9 +5,7 @@ package kotlinx.io.files -import kotlin.test.Test -import kotlin.test.assertFalse -import kotlin.test.assertTrue +import kotlin.test.* class SmokeFileTestWindows { @Test @@ -20,4 +18,12 @@ class SmokeFileTestWindows { assertFalse(Path("bla\\bla\\bla").isAbsolute) assertTrue(Path("\\\\server\\share").isAbsolute) } + + @Test + fun getParent() { + assertNull(Path("C:\\").parent) + assertNull(Path("a\\b").parent?.parent) + assertEquals(Path("\\\\server"), Path("\\\\server\\share").parent) + assertEquals(Path("C:\\"), Path("C:\\Program Files").parent) + } } From f412bea4467d30136ae92d2dc29bc8b5dea5dfc3 Mon Sep 17 00:00:00 2001 From: Filipp Zhinkin Date: Wed, 11 Oct 2023 14:50:52 +0200 Subject: [PATCH 2/4] Delegate path filtration to dirnameImpl --- core/android/src/files/FileSystemAndroid.kt | 3 +++ core/apple/src/files/FileSystemApple.kt | 3 +++ core/common/test/files/SmokeFileTest.kt | 1 + core/linux/src/files/FileSystemLinux.kt | 3 +++ core/mingw/src/files/FileSystemMingw.kt | 5 +++++ core/native/src/files/PathsNative.kt | 1 - 6 files changed, 15 insertions(+), 1 deletion(-) diff --git a/core/android/src/files/FileSystemAndroid.kt b/core/android/src/files/FileSystemAndroid.kt index 1b1b5804b..8b29a1440 100644 --- a/core/android/src/files/FileSystemAndroid.kt +++ b/core/android/src/files/FileSystemAndroid.kt @@ -12,6 +12,9 @@ import platform.posix.dirname @OptIn(ExperimentalForeignApi::class) internal actual fun dirnameImpl(path: String): String { + if (!path.contains(SystemPathSeparator)) { + return "" + } return dirname(path)?.toKString() ?: "" } diff --git a/core/apple/src/files/FileSystemApple.kt b/core/apple/src/files/FileSystemApple.kt index 1208d84b1..869ea7c53 100644 --- a/core/apple/src/files/FileSystemApple.kt +++ b/core/apple/src/files/FileSystemApple.kt @@ -25,6 +25,9 @@ public actual val SystemTemporaryDirectory: Path get() = Path(NSTemporaryDirectory()) internal actual fun dirnameImpl(path: String): String { + if (!path.contains(SystemPathSeparator)) { + return "" + } memScoped { return dirname(path.cstr.ptr)?.toKString() ?: "" } diff --git a/core/common/test/files/SmokeFileTest.kt b/core/common/test/files/SmokeFileTest.kt index fe046a2ff..0180a7457 100644 --- a/core/common/test/files/SmokeFileTest.kt +++ b/core/common/test/files/SmokeFileTest.kt @@ -176,6 +176,7 @@ class SmokeFileTest { val p1 = Path("home", "..", "lib") assertEquals(constructRelativePath("home", ".."), p1.parent?.toString()) assertEquals("home", p1.parent?.parent?.toString()) + println(p1) assertNull(p1.parent?.parent?.parent) assertNull(Path("").parent) diff --git a/core/linux/src/files/FileSystemLinux.kt b/core/linux/src/files/FileSystemLinux.kt index 22cecb4f8..5c46e71f4 100644 --- a/core/linux/src/files/FileSystemLinux.kt +++ b/core/linux/src/files/FileSystemLinux.kt @@ -14,6 +14,9 @@ import platform.posix.dirname @OptIn(ExperimentalForeignApi::class) internal actual fun dirnameImpl(path: String): String { + if (!path.contains(SystemPathSeparator)) { + return "" + } memScoped { return dirname(path.cstr.ptr)?.toKString() ?: "" } diff --git a/core/mingw/src/files/FileSystemMingw.kt b/core/mingw/src/files/FileSystemMingw.kt index 21000df0a..5c027ba24 100644 --- a/core/mingw/src/files/FileSystemMingw.kt +++ b/core/mingw/src/files/FileSystemMingw.kt @@ -15,6 +15,8 @@ import platform.windows.MOVEFILE_REPLACE_EXISTING import platform.windows.MoveFileExA import platform.windows.PathIsRelativeA +private const val WindowsPathSeparator: Char = '\\' + internal actual fun atomicMoveImpl(source: Path, destination: Path) { if (MoveFileExA(source.path, destination.path, MOVEFILE_REPLACE_EXISTING.convert()) == 0) { // TODO: get formatted error message @@ -23,6 +25,9 @@ internal actual fun atomicMoveImpl(source: Path, destination: Path) { } internal actual fun dirnameImpl(path: String): String { + if (!path.contains(SystemPathSeparator) && !path.contains(WindowsPathSeparator)) { + return "" + } memScoped { return dirname(path.cstr.ptr)?.toKString() ?: "" } diff --git a/core/native/src/files/PathsNative.kt b/core/native/src/files/PathsNative.kt index a7f31e2b7..244a21219 100644 --- a/core/native/src/files/PathsNative.kt +++ b/core/native/src/files/PathsNative.kt @@ -23,7 +23,6 @@ public actual class Path internal constructor( get() { when { path.isBlank() -> return null - !path.contains(SystemPathSeparator) -> return null } val parentName = dirnameImpl(path) return when { From e43137cf8205908aeb1e45580002d558616186b2 Mon Sep 17 00:00:00 2001 From: Filipp Zhinkin Date: Wed, 11 Oct 2023 15:54:45 +0200 Subject: [PATCH 3/4] Cleanup --- core/common/test/files/SmokeFileTest.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/core/common/test/files/SmokeFileTest.kt b/core/common/test/files/SmokeFileTest.kt index 0180a7457..fe046a2ff 100644 --- a/core/common/test/files/SmokeFileTest.kt +++ b/core/common/test/files/SmokeFileTest.kt @@ -176,7 +176,6 @@ class SmokeFileTest { val p1 = Path("home", "..", "lib") assertEquals(constructRelativePath("home", ".."), p1.parent?.toString()) assertEquals("home", p1.parent?.parent?.toString()) - println(p1) assertNull(p1.parent?.parent?.parent) assertNull(Path("").parent) From dcd1db182608178cdfe79499bdbf5c7c7231f791 Mon Sep 17 00:00:00 2001 From: Filipp Zhinkin Date: Wed, 11 Oct 2023 15:55:35 +0200 Subject: [PATCH 4/4] Add test with mixed path separators --- core/mingw/test/files/SmokeFileTestWindows.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/core/mingw/test/files/SmokeFileTestWindows.kt b/core/mingw/test/files/SmokeFileTestWindows.kt index b42e9b0f3..0ff7df62c 100644 --- a/core/mingw/test/files/SmokeFileTestWindows.kt +++ b/core/mingw/test/files/SmokeFileTestWindows.kt @@ -25,5 +25,6 @@ class SmokeFileTestWindows { assertNull(Path("a\\b").parent?.parent) assertEquals(Path("\\\\server"), Path("\\\\server\\share").parent) assertEquals(Path("C:\\"), Path("C:\\Program Files").parent) + assertEquals(Path("C:\\Program Files"), Path("C:\\Program Files/Java").parent) } }