From 94e034f2256a7b911508458836ef0a994731e202 Mon Sep 17 00:00:00 2001 From: Benoit Quenaudon Date: Thu, 28 Oct 2021 12:25:57 -0400 Subject: [PATCH] support for ubuntu and nodejs --- .../src/main/kotlin/okio/NodeJsFileSystem.kt | 14 ++++++++++++-- .../kotlin/okio/-WindowsPosixVariant.kt | 2 +- okio/src/unixMain/kotlin/okio/-UnixPosixVariant.kt | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/okio-nodefilesystem/src/main/kotlin/okio/NodeJsFileSystem.kt b/okio-nodefilesystem/src/main/kotlin/okio/NodeJsFileSystem.kt index de95fc5cca..f2fa51a8bb 100644 --- a/okio-nodefilesystem/src/main/kotlin/okio/NodeJsFileSystem.kt +++ b/okio-nodefilesystem/src/main/kotlin/okio/NodeJsFileSystem.kt @@ -167,10 +167,16 @@ object NodeJsFileSystem : FileSystem() { } } - override fun createDirectory(dir: Path) { + override fun createDirectory(dir: Path, mustCreate: Boolean) { try { mkdirSync(dir.toString()) } catch (e: Throwable) { + val alreadyExist = metadataOrNull(dir)?.isDirectory == true + if (alreadyExist) { + if (mustCreate) throw IOException("$dir already exist.") + else return + } + throw e.toIOException() } } @@ -189,7 +195,7 @@ object NodeJsFileSystem : FileSystem() { * * TODO(jwilson): switch to fs.rmSync() when our minimum requirements are Node 14.14.0. */ - override fun delete(path: Path) { + override fun delete(path: Path, mustExist: Boolean) { try { unlinkSync(path.toString()) return @@ -198,6 +204,10 @@ object NodeJsFileSystem : FileSystem() { try { rmdirSync(path.toString()) } catch (e: Throwable) { + if (e.errorCode == "ENOENT") { + if (mustExist) throw FileNotFoundException("no such file: $path") + else return + } throw e.toIOException() } } diff --git a/okio/src/mingwX64Main/kotlin/okio/-WindowsPosixVariant.kt b/okio/src/mingwX64Main/kotlin/okio/-WindowsPosixVariant.kt index e837d0c03e..d4a1cadc2e 100644 --- a/okio/src/mingwX64Main/kotlin/okio/-WindowsPosixVariant.kt +++ b/okio/src/mingwX64Main/kotlin/okio/-WindowsPosixVariant.kt @@ -81,7 +81,7 @@ internal actual fun PosixFileSystem.variantDelete(path: Path, mustExist: Boolean if (rmdir(pathString) == 0) return } if (errno == ENOENT) { - if (mustExist) throw FileNotFoundException("$path doesn't exist.") + if (mustExist) throw FileNotFoundException("no such file: $path") else return } diff --git a/okio/src/unixMain/kotlin/okio/-UnixPosixVariant.kt b/okio/src/unixMain/kotlin/okio/-UnixPosixVariant.kt index e8b6c56bc1..7e252215aa 100644 --- a/okio/src/unixMain/kotlin/okio/-UnixPosixVariant.kt +++ b/okio/src/unixMain/kotlin/okio/-UnixPosixVariant.kt @@ -61,7 +61,7 @@ internal actual fun PosixFileSystem.variantDelete(path: Path, mustExist: Boolean val result = remove(path.toString()) if (result != 0) { if (errno == ENOENT) { - if (mustExist) throw FileNotFoundException("$path doesn't exist.") + if (mustExist) throw FileNotFoundException("no such file: $path") else return } throw errnoToIOException(errno)