Skip to content

Commit b761de0

Browse files
committed
fix
1 parent 617957c commit b761de0

File tree

5 files changed

+62
-51
lines changed

5 files changed

+62
-51
lines changed

build-logic/src/main/kotlin/kotlinx/io/conventions/kotlinx-io-multiplatform.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ kotlin {
101101
group("apple")
102102
group("linux")
103103
}
104+
group("posix") {
105+
group("apple")
106+
group("unix")
107+
}
104108
}
105109
group("nodeFilesystemShared") {
106110
withJs()

core/mingw/src/files/FileSystemMingw.kt

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,10 @@
77

88
package kotlinx.io.files
99

10-
import kotlinx.cinterop.Arena
11-
import kotlinx.cinterop.CFunction
12-
import kotlinx.cinterop.CPointed
13-
import kotlinx.cinterop.CPointer
14-
import kotlinx.cinterop.ExperimentalForeignApi
15-
import kotlinx.cinterop.alloc
16-
import kotlinx.cinterop.allocArray
17-
import kotlinx.cinterop.convert
18-
import kotlinx.cinterop.invoke
19-
import kotlinx.cinterop.memScoped
20-
import kotlinx.cinterop.ptr
21-
import kotlinx.cinterop.reinterpret
22-
import kotlinx.cinterop.toKString
23-
import kotlinx.cinterop.wcstr
10+
import kotlinx.cinterop.*
2411
import kotlinx.io.IOException
2512
import platform.posix.size_t
26-
import platform.windows.CloseHandle
27-
import platform.windows.CreateDirectoryW
28-
import platform.windows.ERROR_FILE_NOT_FOUND
29-
import platform.windows.ERROR_NO_MORE_FILES
30-
import platform.windows.FALSE
31-
import platform.windows.FindClose
32-
import platform.windows.FindFirstFileW
33-
import platform.windows.FindNextFileW
34-
import platform.windows.GetFullPathNameW
35-
import platform.windows.GetLastError
36-
import platform.windows.GetProcAddress
37-
import platform.windows.HANDLE
38-
import platform.windows.HMODULE
39-
import platform.windows.HRESULT
40-
import platform.windows.INVALID_HANDLE_VALUE
41-
import platform.windows.LoadLibraryW
42-
import platform.windows.MAX_PATH
43-
import platform.windows.MOVEFILE_REPLACE_EXISTING
44-
import platform.windows.MoveFileExW
45-
import platform.windows.PWSTR
46-
import platform.windows.PathFindFileNameW
47-
import platform.windows.PathIsRelativeW
48-
import platform.windows.PathIsRootW
49-
import platform.windows.TRUE
50-
import platform.windows.WCHARVar
51-
import platform.windows.WIN32_FIND_DATAW
13+
import platform.windows.*
5214
import kotlin.experimental.ExperimentalNativeApi
5315

5416
private typealias PathCchRemoveFileSpecFunc = CPointer<CFunction<(PWSTR, size_t) -> HRESULT>>
@@ -86,7 +48,7 @@ internal actual fun dirnameImpl(path: String): String {
8648
}
8749

8850
internal actual fun basenameImpl(path: String): String {
89-
if (PathIsRootW(path) == TRUE) return ""
51+
if (PathIsRootW(path) == TRUE) return ""
9052
return PathFindFileNameW(path)?.toKString() ?: ""
9153
}
9254

@@ -179,3 +141,18 @@ internal actual class OpaqueDirEntry(private val directory: String) : AutoClosea
179141
}
180142

181143
internal actual fun opendir(path: String): OpaqueDirEntry = OpaqueDirEntry(path)
144+
145+
internal actual fun existsImpl(path: String): Boolean = PathFileExistsW(path) == TRUE
146+
147+
internal actual fun deleteNoCheckImpl(path: String) {
148+
if (DeleteFileW(path) != FALSE) return
149+
var e = GetLastError()
150+
if (e == ERROR_FILE_NOT_FOUND.toUInt()) return // ignore it
151+
if (e == ERROR_ACCESS_DENIED.toUInt()) {
152+
// might be a directory
153+
if (RemoveDirectoryW(path) != FALSE) return
154+
e = GetLastError()
155+
if (e == ERROR_FILE_NOT_FOUND.toUInt()) return // ignore it
156+
}
157+
throw IOException("Delete failed for $path: ${formatWin32ErrorMessage(e)}")
158+
}

core/mingw/test/files/SmokeFileTestWindowsMinGW.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import platform.windows.ERROR_TOO_MANY_OPEN_FILES
1313
import kotlin.test.Test
1414
import kotlin.test.assertEquals
1515
import kotlin.test.assertFails
16+
import kotlin.test.assertTrue
1617

1718
class SmokeFileTestWindowsMinGW {
19+
private val testDir = Path("""./mingw/testdir""")
1820
@OptIn(ExperimentalForeignApi::class)
1921
@Test
2022
fun mingwProblem() {
@@ -74,7 +76,14 @@ class SmokeFileTestWindowsMinGW {
7476
@Test
7577
fun testReadDir() {
7678
val expected = listOf("foo", "いろは歌", "天地玄黄")
77-
val actual = SystemFileSystem.list(Path("""./mingw/testdir""")).map { it.name }.sorted()
79+
val actual = SystemFileSystem.list(testDir).map { it.name }.sorted()
7880
assertEquals(expected, actual)
7981
}
82+
83+
@Test
84+
fun testExists() {
85+
for (path in SystemFileSystem.list(testDir)) {
86+
assertTrue(SystemFileSystem.exists(path), path.toString())
87+
}
88+
}
8089
}

core/native/src/files/FileSystemNative.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ import kotlin.experimental.ExperimentalNativeApi
1616

1717
@OptIn(ExperimentalForeignApi::class)
1818
public actual val SystemFileSystem: FileSystem = object : SystemFileSystemImpl() {
19-
override fun exists(path: Path): Boolean {
20-
return access(path.path, F_OK) == 0
21-
}
19+
override fun exists(path: Path): Boolean = existsImpl(path.path)
2220

2321
@OptIn(ExperimentalForeignApi::class)
2422
override fun delete(path: Path, mustExist: Boolean) {
@@ -28,12 +26,7 @@ public actual val SystemFileSystem: FileSystem = object : SystemFileSystemImpl()
2826
}
2927
return
3028
}
31-
if (remove(path.path) != 0) {
32-
if (errno == EACCES) {
33-
if (rmdir(path.path) == 0) return
34-
}
35-
throw IOException("Delete failed for $path: ${strerror(errno)?.toKString()}")
36-
}
29+
deleteNoCheckImpl(path.path)
3730
}
3831

3932
override fun createDirectories(path: Path, mustCreate: Boolean) {
@@ -114,6 +107,10 @@ internal expect fun mkdirImpl(path: String)
114107

115108
internal expect fun realpathImpl(path: String): String
116109

110+
internal expect fun existsImpl(path: String): Boolean
111+
112+
internal expect fun deleteNoCheckImpl(path: String)
113+
117114
public actual open class FileNotFoundException actual constructor(
118115
message: String?
119116
) : IOException(message)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package kotlinx.io.files
2+
3+
import kotlinx.cinterop.ExperimentalForeignApi
4+
import kotlinx.cinterop.toKString
5+
import kotlinx.io.IOException
6+
import platform.posix.EACCES
7+
import platform.posix.F_OK
8+
import platform.posix.access
9+
import platform.posix.errno
10+
import platform.posix.remove
11+
import platform.posix.rmdir
12+
import platform.posix.strerror
13+
14+
internal actual fun existsImpl(path: String): Boolean = access(path, F_OK) == 0
15+
16+
@OptIn(ExperimentalForeignApi::class)
17+
internal actual fun deleteNoCheckImpl(path: String) {
18+
if (remove(path) != 0) {
19+
if (errno == EACCES) {
20+
if (rmdir(path) == 0) return
21+
}
22+
throw IOException("Delete failed for $path: ${strerror(errno)?.toKString()}")
23+
}
24+
}

0 commit comments

Comments
 (0)