Skip to content

Commit

Permalink
Simplify how the base log folder is created and evaluated as a return…
Browse files Browse the repository at this point in the history
… value.
  • Loading branch information
airxnoor committed Mar 26, 2024
1 parent 542d763 commit cea541c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ internal actual class PlatformFileInputOutputImpl : PlatformFileInputOutput {

override val pathSeparator: Char = File.separatorChar

override suspend fun isDirectory(path: String): Boolean = File(path).isDirectory
override suspend fun mkdirs(path: String): Boolean {
val pathFile = File(path)

override suspend fun mkdirs(path: String): Boolean = File(path).mkdirs()
pathFile.mkdirs()

return pathFile.isDirectory
}

override suspend fun size(path: String): Long = File(path).length()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class FileLoggerFacility(

init {
coroutineScope.launch {
if (!io.isDirectory(baseFolder) && !io.mkdirs(baseFolder)) {
if (!io.mkdirs(baseFolder)) {
throw IllegalArgumentException("Base log folder is invalid: $baseFolder")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class JsonLoggerFacility(

init {
coroutineScope.launch {
if (!io.isDirectory(baseFolder) && !io.mkdirs(baseFolder)) {
if (!io.mkdirs(baseFolder)) {
throw IllegalArgumentException("Base log folder is invalid: $baseFolder")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,12 @@ internal interface PlatformFileInputOutput : PlatformDirectoryListing {
*/
val pathSeparator: Char

/**
* Returns true if the provided path points to a directory, false otherwise.
*
* @param path The location of the directory.
*/
suspend fun isDirectory(path: String): Boolean

/**
* Creates the provided [directory path][path], including any intermediary ones, and returns true on success,
* false otherwise.
*
* If the directory already exists, then this method does nothing and returns true immediately.
*
* @param path The location of the directory.
*/
suspend fun mkdirs(path: String): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,21 @@ private typealias NSERROR_CPOINTER = CPointer<ObjCObjectVar<NSError?>>
internal actual class PlatformFileInputOutputImpl : PlatformFileInputOutput {
override val pathSeparator: Char = '/'

override suspend fun isDirectory(path: String): Boolean = memScoped {
val cValue = alloc<BooleanVar>()
cValue.value = true
NSFileManager.defaultManager.fileExistsAtPath(
path = path,
isDirectory = cValue.ptr,
)
}

override suspend fun size(path: String): Long = nsErrorWrapper(0L) {
sizeImpl(path)
}

override suspend fun mkdirs(path: String): Boolean = nsErrorWrapper(false) {
NSFileManager.defaultManager.createDirectoryAtPath(
path = path,
withIntermediateDirectories = true,
attributes = null,
error = this,
)
override suspend fun mkdirs(path: String): Boolean {
nsErrorWrapper(false) {
NSFileManager.defaultManager.createDirectoryAtPath(
path = path,
withIntermediateDirectories = true,
attributes = null,
error = this,
)
}

return isDirectory(path)
}

override suspend fun write(path: String, position: Long, contents: String) {
Expand Down Expand Up @@ -141,6 +136,15 @@ internal actual class PlatformFileInputOutputImpl : PlatformFileInputOutput {

override fun toString(): String = PLATFORM_IOS

private fun isDirectory(path: String): Boolean = memScoped {
val cValue = alloc<BooleanVar>()
cValue.value = true
NSFileManager.defaultManager.fileExistsAtPath(
path = path,
isDirectory = cValue.ptr,
)
}

private fun NSERROR_CPOINTER.sizeImpl(path: String): Long = NSFileManager.defaultManager
.attributesOfFileSystemForPath(path, this)
?.get(NSFileSize)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ internal actual class PlatformFileInputOutputImpl : PlatformFileInputOutput {

override val pathSeparator: Char = File.separatorChar

override suspend fun isDirectory(path: String): Boolean = File(path).isDirectory
override suspend fun mkdirs(path: String): Boolean {
val pathFile = File(path)

override suspend fun mkdirs(path: String): Boolean = File(path).mkdirs()
pathFile.mkdirs()

return pathFile.isDirectory
}

override suspend fun size(path: String): Long = File(path).length()

Expand Down

0 comments on commit cea541c

Please sign in to comment.