Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly extract parent path on Windows #227

Merged
merged 4 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/android/src/files/FileSystemAndroid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() ?: ""
}

Expand Down
3 changes: 3 additions & 0 deletions core/apple/src/files/FileSystemApple.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() ?: ""
}
Expand Down
3 changes: 3 additions & 0 deletions core/linux/src/files/FileSystemLinux.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() ?: ""
}
Expand Down
5 changes: 5 additions & 0 deletions core/mingw/src/files/FileSystemMingw.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() ?: ""
}
Expand Down
13 changes: 10 additions & 3 deletions core/mingw/test/files/SmokeFileTestWindows.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,4 +18,13 @@ class SmokeFileTestWindows {
assertFalse(Path("bla\\bla\\bla").isAbsolute)
assertTrue(Path("\\\\server\\share").isAbsolute)
}

@Test
fun getParent() {
assertNull(Path("C:\\").parent)
shanshin marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}
1 change: 0 additions & 1 deletion core/native/src/files/PathsNative.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down