Skip to content

Commit

Permalink
WIP: split out mkdir function
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrueden committed May 7, 2024
1 parent 32ecac7 commit def3fb5
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 47 deletions.
14 changes: 10 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,23 @@ kotlin {
val posixMain by creating {
dependsOn(commonMain.get())
}
val macosMain by creating {
dependsOn(posixMain)
}
val linuxMain by creating {
dependsOn(posixMain)
}
val macosArm64Main by getting {
dependsOn(posixMain)
dependsOn(macosMain)
}
val macosX64Main by getting {
dependsOn(posixMain)
dependsOn(macosMain)
}
val linuxArm64Main by getting {
dependsOn(posixMain)
dependsOn(linuxMain)
}
val linuxX64Main by getting {
dependsOn(posixMain)
dependsOn(linuxMain)
}
// HACK: Prevent "Variable is never used" warnings.
// Unfortunately, @Suppress("UNUSED_PARAMETER") does not do the trick.
Expand Down
9 changes: 8 additions & 1 deletion src/commonMain/kotlin/file.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ expect class File(rawPath: String) {
val length : Long
fun ls(): List<File>
fun lines(): List<String>
fun mkdir(): Boolean
fun mv(dest:File): Boolean
fun rm() : Boolean
fun rmdir() : Boolean
Expand Down Expand Up @@ -49,6 +48,14 @@ val File.base: File
return if (dot < lastSlash(path)) this else File(path.substring(0, dot))
}

fun File.mkdir(): Boolean {
if (exists && !isDirectory) {
warn("Error: '$path' already exists but is not a directory.")
return false
}
return mkdir(path)
}

operator fun File.div(p: String): File = File("$path$SLASH$p")

// -- File-related utility functions --
Expand Down
2 changes: 2 additions & 0 deletions src/commonMain/kotlin/platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ expect fun printlnErr(s: String = "")

expect fun stdinLines(): Array<String>

expect fun mkdir(path: String): Boolean

data class MemoryInfo(var total: Long? = null, var free: Long? = null)

expect fun memInfo(): MemoryInfo
Expand Down
13 changes: 13 additions & 0 deletions src/linuxMain/kotlin/platform.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.toKString
import platform.posix.*

@OptIn(ExperimentalForeignApi::class)
actual fun mkdir(path: String): Boolean {
val result = mkdir(path, S_IRWXU.toUInt())
if (result != 0) {
val errorCode = errno
platform.posix.warn("Error creating directory '$path': ${strerror(errorCode)?.toKString()}")
}
return result == 0
}
13 changes: 13 additions & 0 deletions src/macosMain/kotlin/platform.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.toKString
import platform.posix.*

@OptIn(ExperimentalForeignApi::class)
actual fun mkdir(path: String): Boolean {
val result = mkdir(path, S_IRWXU.toUShort())
if (result != 0) {
val errorCode = errno
platform.posix.warn("Error creating directory '$path': ${strerror(errorCode)?.toKString()}")
}
return result == 0
}
23 changes: 1 addition & 22 deletions src/posixMain/kotlin/file.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ actual class File actual constructor(private val rawPath: String) {
return statResult.st_size
}

@OptIn(ExperimentalForeignApi::class)
@OptIn(ExperimentalForeignApi::class, UnsafeNumber::class)
private fun isMode(modeBits: Int): Boolean {
val statMode = memScoped {
val statResult = alloc<stat>()
Expand Down Expand Up @@ -75,27 +75,6 @@ actual class File actual constructor(private val rawPath: String) {
return path
}

@OptIn(ExperimentalForeignApi::class)
actual fun mkdir(): Boolean {
if (exists && ! isDirectory) {
println("Error: '$path' already exists but is not a directory.")
return false
}
memScoped {
// Default permissions for new directories (read/write for owner)
val mode = S_IRWXU.toUInt() // User can read, write, execute

// Create the directory, returning false if it fails
if (mkdir(path, mode) != 0) {
val errorCode = errno
println("Error creating directory '$path': ${strerror(errorCode)?.toKString()}")
return false
}

return true // Successful directory creation
}
}

@OptIn(ExperimentalForeignApi::class)
actual fun mv(dest: File): Boolean {
memScoped {
Expand Down
20 changes: 0 additions & 20 deletions src/windowsMain/kotlin/file.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,6 @@ actual class File actual constructor(private val rawPath: String) {
return path
}

@OptIn(ExperimentalForeignApi::class)
actual fun mkdir(): Boolean {
memScoped {
// Create the directory, returning false if it fails
val result = CreateDirectoryW(path, null)

if (result == 0) {
val errorCode = GetLastError()
if (errorCode == ERROR_ALREADY_EXISTS.toUInt()) {
return true // The directory already exists
} else {
println("Error creating directory '$path': $errorCode")
return false
}
}

return true
}
}

@OptIn(ExperimentalForeignApi::class)
actual fun mv(dest: File): Boolean {
memScoped {
Expand Down
17 changes: 17 additions & 0 deletions src/windowsMain/kotlin/platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ actual fun stdinLines(): Array<String> {
return lines
}

@OptIn(ExperimentalForeignApi::class)
actual fun mkdir(path: String): Boolean {
memScoped {
val result = CreateDirectoryW(path, null)
if (result == 0) {
val errorCode = GetLastError()
if (errorCode == ERROR_ALREADY_EXISTS.toUInt()) {
return true
} else {
warn("Error creating directory '$path': $errorCode")
return false
}
}
return true
}
}

@OptIn(ExperimentalForeignApi::class)
actual fun memInfo(): MemoryInfo {
val memInfo = MemoryInfo()
Expand Down

0 comments on commit def3fb5

Please sign in to comment.