Skip to content

Commit 5d8982c

Browse files
authored
Correctly extract parent path on Windows (#227)
* Delegated path filtration to dirnameImpl Closes #221
1 parent e9a90bc commit 5d8982c

File tree

6 files changed

+24
-4
lines changed

6 files changed

+24
-4
lines changed

core/android/src/files/FileSystemAndroid.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import platform.posix.dirname
1212

1313
@OptIn(ExperimentalForeignApi::class)
1414
internal actual fun dirnameImpl(path: String): String {
15+
if (!path.contains(SystemPathSeparator)) {
16+
return ""
17+
}
1518
return dirname(path)?.toKString() ?: ""
1619
}
1720

core/apple/src/files/FileSystemApple.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public actual val SystemTemporaryDirectory: Path
2525
get() = Path(NSTemporaryDirectory())
2626

2727
internal actual fun dirnameImpl(path: String): String {
28+
if (!path.contains(SystemPathSeparator)) {
29+
return ""
30+
}
2831
memScoped {
2932
return dirname(path.cstr.ptr)?.toKString() ?: ""
3033
}

core/linux/src/files/FileSystemLinux.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import platform.posix.dirname
1414

1515
@OptIn(ExperimentalForeignApi::class)
1616
internal actual fun dirnameImpl(path: String): String {
17+
if (!path.contains(SystemPathSeparator)) {
18+
return ""
19+
}
1720
memScoped {
1821
return dirname(path.cstr.ptr)?.toKString() ?: ""
1922
}

core/mingw/src/files/FileSystemMingw.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import platform.windows.MOVEFILE_REPLACE_EXISTING
1515
import platform.windows.MoveFileExA
1616
import platform.windows.PathIsRelativeA
1717

18+
private const val WindowsPathSeparator: Char = '\\'
19+
1820
internal actual fun atomicMoveImpl(source: Path, destination: Path) {
1921
if (MoveFileExA(source.path, destination.path, MOVEFILE_REPLACE_EXISTING.convert()) == 0) {
2022
// TODO: get formatted error message
@@ -23,6 +25,9 @@ internal actual fun atomicMoveImpl(source: Path, destination: Path) {
2325
}
2426

2527
internal actual fun dirnameImpl(path: String): String {
28+
if (!path.contains(SystemPathSeparator) && !path.contains(WindowsPathSeparator)) {
29+
return ""
30+
}
2631
memScoped {
2732
return dirname(path.cstr.ptr)?.toKString() ?: ""
2833
}

core/mingw/test/files/SmokeFileTestWindows.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66
package kotlinx.io.files
77

8-
import kotlin.test.Test
9-
import kotlin.test.assertFalse
10-
import kotlin.test.assertTrue
8+
import kotlin.test.*
119

1210
class SmokeFileTestWindows {
1311
@Test
@@ -20,4 +18,13 @@ class SmokeFileTestWindows {
2018
assertFalse(Path("bla\\bla\\bla").isAbsolute)
2119
assertTrue(Path("\\\\server\\share").isAbsolute)
2220
}
21+
22+
@Test
23+
fun getParent() {
24+
assertNull(Path("C:\\").parent)
25+
assertNull(Path("a\\b").parent?.parent)
26+
assertEquals(Path("\\\\server"), Path("\\\\server\\share").parent)
27+
assertEquals(Path("C:\\"), Path("C:\\Program Files").parent)
28+
assertEquals(Path("C:\\Program Files"), Path("C:\\Program Files/Java").parent)
29+
}
2330
}

core/native/src/files/PathsNative.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public actual class Path internal constructor(
2323
get() {
2424
when {
2525
path.isBlank() -> return null
26-
!path.contains(SystemPathSeparator) -> return null
2726
}
2827
val parentName = dirnameImpl(path)
2928
return when {

0 commit comments

Comments
 (0)