Skip to content

Commit

Permalink
Use @ExperimentalFilesystem on Filesystem, Path, and FileMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
squarejesse committed Dec 22, 2020
1 parent a176e8d commit 5fa3ea4
Show file tree
Hide file tree
Showing 28 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions okio-testing/src/commonMain/kotlin/okio/FakeFilesystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import okio.Path.Companion.toPath
* [windowsLimitations] to true to throw an [IOException] when asked to delete or rename an open
* file.
*/
@ExperimentalFilesystem
class FakeFilesystem(
val clock: Clock = Clock.System,
private val windowsLimitations: Boolean = false,
Expand Down
1 change: 1 addition & 0 deletions okio-testing/src/commonMain/kotlin/okio/time.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import kotlinx.datetime.Instant
import kotlin.jvm.JvmName

@JvmName("newFileMetadata")
@ExperimentalFilesystem
internal fun FileMetadata(
isRegularFile: Boolean = false,
isDirectory: Boolean = false,
Expand Down
5 changes: 5 additions & 0 deletions okio/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ kotlin {
mingwX64()
}
sourceSets {
all {
languageSettings {
useExperimentalAnnotation('kotlin.RequiresOptIn')
}
}
commonMain {
dependencies {
api deps.kotlin.stdLib.common
Expand Down
1 change: 1 addition & 0 deletions okio/src/appleMain/kotlin/okio/posixVariant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import platform.posix.S_IFREG
import platform.posix.errno
import platform.posix.stat

@ExperimentalFilesystem
internal actual fun PosixSystemFilesystem.variantMetadata(path: Path): FileMetadata {
return memScoped {
val stat = alloc<stat>()
Expand Down
27 changes: 27 additions & 0 deletions okio/src/commonMain/kotlin/okio/ExperimentalFilesystem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2020 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okio

import kotlin.RequiresOptIn.Level.ERROR
import kotlin.annotation.AnnotationRetention.BINARY
import kotlin.annotation.AnnotationTarget.CLASS
import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.PROPERTY

@RequiresOptIn(level = ERROR, message = "okio's Filesystem is unstable and subject to change")
@Retention(BINARY)
@Target(CLASS, FUNCTION, PROPERTY)
annotation class ExperimentalFilesystem
1 change: 1 addition & 0 deletions okio/src/commonMain/kotlin/okio/FileMetadata.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ package okio
* File metadata is subject to change, and code that operates on filesystems should defend against
* changes to the file that occur between reading metadata and subsequent operations.
*/
@ExperimentalFilesystem
class FileMetadata(
/** True if this file is a container of bytes. If this is true, then [size] is non-null. */
val isRegularFile: Boolean,
Expand Down
1 change: 1 addition & 0 deletions okio/src/commonMain/kotlin/okio/Filesystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ package okio
*
* [s3]: https://aws.amazon.com/s3/
*/
@ExperimentalFilesystem
abstract class Filesystem {
/**
* Resolves [path] against the current working directory and symlinks in this filesystem. The
Expand Down
1 change: 1 addition & 0 deletions okio/src/commonMain/kotlin/okio/Path.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import okio.Path.Companion.toPath
* | `\\server` | null | `server` | UNC server (Windows) |
* | `\\server\project\notes.txt` | `\\server\project` | `notes.txt` | UNC absolute path (Windows) |
*/
@ExperimentalFilesystem
class Path private constructor(
private val slash: ByteString,
private val bytes: ByteString
Expand Down
2 changes: 2 additions & 0 deletions okio/src/commonMain/kotlin/okio/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
package okio

@ExperimentalFilesystem
internal expect val PLATFORM_FILESYSTEM: Filesystem

@ExperimentalFilesystem
internal expect val PLATFORM_TEMPORARY_DIRECTORY: Path

internal expect val DIRECTORY_SEPARATOR: String
1 change: 1 addition & 0 deletions okio/src/commonTest/kotlin/okio/AbstractFilesystemTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import kotlin.time.seconds

/** This test assumes that okio-files/ is the current working directory when executed. */
@ExperimentalTime
@ExperimentalFilesystem
abstract class AbstractFilesystemTest(
val clock: Clock,
val filesystem: Filesystem,
Expand Down
3 changes: 3 additions & 0 deletions okio/src/commonTest/kotlin/okio/FakeFilesystemTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,23 @@ import kotlin.time.ExperimentalTime
import kotlin.time.minutes

@ExperimentalTime
@ExperimentalFilesystem
class FakeWindowsFilesystemTest : FakeFilesystemTest(
clock = FakeClock(),
windowsLimitations = true,
temporaryDirectory = "C:\\".toPath(),
)

@ExperimentalTime
@ExperimentalFilesystem
class FakeUnixFilesystemTest : FakeFilesystemTest(
clock = FakeClock(),
windowsLimitations = false,
temporaryDirectory = "/".toPath(),
)

@ExperimentalTime
@ExperimentalFilesystem
abstract class FakeFilesystemTest internal constructor(
clock: FakeClock,
windowsLimitations: Boolean,
Expand Down
1 change: 1 addition & 0 deletions okio/src/commonTest/kotlin/okio/PathTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import kotlin.test.assertFalse
import kotlin.test.assertNull
import kotlin.test.assertTrue

@ExperimentalFilesystem
class PathTest {
@Test
fun unixRoot() {
Expand Down
1 change: 1 addition & 0 deletions okio/src/commonTest/kotlin/okio/SystemFilesystemTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import kotlinx.datetime.Clock
import kotlin.time.ExperimentalTime

@ExperimentalTime
@ExperimentalFilesystem
class SystemFilesystemTest : AbstractFilesystemTest(
clock = Clock.System,
filesystem = Filesystem.SYSTEM,
Expand Down
3 changes: 3 additions & 0 deletions okio/src/commonTest/kotlin/okio/time.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ package okio

import kotlinx.datetime.Instant

@ExperimentalFilesystem
internal val FileMetadata.createdAt: Instant?
get() {
val createdAt = createdAtMillis ?: return null
return Instant.fromEpochMilliseconds(createdAt)
}

@ExperimentalFilesystem
internal val FileMetadata.lastModifiedAt: Instant?
get() {
val lastModifiedAt = lastModifiedAtMillis ?: return null
return Instant.fromEpochMilliseconds(lastModifiedAt)
}

@ExperimentalFilesystem
internal val FileMetadata.lastAccessedAt: Instant?
get() {
val lastAccessedAt = lastAccessedAtMillis ?: return null
Expand Down
1 change: 1 addition & 0 deletions okio/src/jsMain/kotlin/okio/NodeJsSystemFilesystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import okio.Path.Companion.toPath
*
* [node_fs]: https://nodejs.org/dist/latest-v14.x/docs/api/fs.html
*/
@ExperimentalFilesystem
internal object NodeJsSystemFilesystem : Filesystem() {
private var S_IFMT = 0xf000 // fs.constants.S_IFMT
private var S_IFREG = 0x8000 // fs.constants.S_IFREG
Expand Down
2 changes: 2 additions & 0 deletions okio/src/jsMain/kotlin/okio/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package okio

import okio.Path.Companion.toPath

@ExperimentalFilesystem
internal actual val PLATFORM_FILESYSTEM: Filesystem = NodeJsSystemFilesystem

@ExperimentalFilesystem
internal actual val PLATFORM_TEMPORARY_DIRECTORY: Path
get() = os.tmpdir().toPath()

Expand Down
1 change: 1 addition & 0 deletions okio/src/jvmMain/kotlin/okio/JvmSystemFilesystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import java.nio.file.StandardCopyOption.REPLACE_EXISTING
import java.nio.file.attribute.BasicFileAttributes
import java.nio.file.attribute.FileTime

@ExperimentalFilesystem
internal object JvmSystemFilesystem : Filesystem() {
override fun canonicalize(path: Path): Path {
val canonicalFile = path.toFile().canonicalFile
Expand Down
4 changes: 4 additions & 0 deletions okio/src/jvmMain/kotlin/okio/Paths.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ import java.io.File
import java.nio.file.Paths
import java.nio.file.Path as NioPath

@ExperimentalFilesystem
fun Path.toFile(): File = File(toString())

@IgnoreJRERequirement // Can only be invoked on platforms that have java.nio.file.
@ExperimentalFilesystem
fun Path.toNioPath(): NioPath = Paths.get(toString())

@ExperimentalFilesystem
fun File.toOkioPath(): Path = toString().toPath()

@IgnoreJRERequirement // Can only be invoked on platforms that have java.nio.file.
@ExperimentalFilesystem
fun NioPath.toOkioPath(): Path = toString().toPath()
2 changes: 2 additions & 0 deletions okio/src/jvmMain/kotlin/okio/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package okio
import okio.Path.Companion.toPath
import java.io.File

@ExperimentalFilesystem
internal actual val PLATFORM_FILESYSTEM: Filesystem = JvmSystemFilesystem

@ExperimentalFilesystem
internal actual val PLATFORM_TEMPORARY_DIRECTORY: Path
get() = System.getProperty("java.io.tmpdir").toPath()

Expand Down
1 change: 1 addition & 0 deletions okio/src/jvmTest/kotlin/okio/JvmTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import java.io.File
import java.nio.file.Paths
import kotlin.test.Test

@ExperimentalFilesystem
class JvmTest {
@Test
fun `base directory consistent with java io File`() {
Expand Down
1 change: 1 addition & 0 deletions okio/src/linuxX64Main/kotlin/okio/posixVariant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import platform.posix.S_IFREG
import platform.posix.errno
import platform.posix.stat

@ExperimentalFilesystem
internal actual fun PosixSystemFilesystem.variantMetadata(path: Path): FileMetadata {
return memScoped {
val stat = alloc<stat>()
Expand Down
1 change: 1 addition & 0 deletions okio/src/mingwX64Main/kotlin/okio/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import kotlinx.cinterop.toKString
import okio.Path.Companion.toPath
import platform.posix.getenv

@ExperimentalFilesystem
internal actual val PLATFORM_TEMPORARY_DIRECTORY: Path
get() {
// Windows' built-in APIs check the TEMP, TMP, and USERPROFILE environment variables in order.
Expand Down
5 changes: 5 additions & 0 deletions okio/src/mingwX64Main/kotlin/okio/posixVariant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import platform.windows.MoveFileExA

internal actual val VARIANT_DIRECTORY_SEPARATOR = "\\"

@ExperimentalFilesystem
internal actual fun PosixSystemFilesystem.variantDelete(path: Path) {
val pathString = path.toString()

Expand All @@ -50,10 +51,12 @@ internal actual fun PosixSystemFilesystem.variantDelete(path: Path) {
throw IOException(errnoString(EACCES))
}

@ExperimentalFilesystem
internal actual fun PosixSystemFilesystem.variantMkdir(dir: Path): Int {
return mkdir(dir.toString())
}

@ExperimentalFilesystem
internal actual fun PosixSystemFilesystem.variantCanonicalize(path: Path): Path {
// Note that _fullpath() returns normally if the file doesn't exist.
val fullpath = _fullpath(null, path.toString(), PATH_MAX)
Expand All @@ -67,6 +70,7 @@ internal actual fun PosixSystemFilesystem.variantCanonicalize(path: Path): Path
}
}

@ExperimentalFilesystem
internal actual fun PosixSystemFilesystem.variantMetadata(path: Path): FileMetadata {
return memScoped {
val stat = alloc<_stat64>()
Expand All @@ -84,6 +88,7 @@ internal actual fun PosixSystemFilesystem.variantMetadata(path: Path): FileMetad
}
}

@ExperimentalFilesystem
internal actual fun PosixSystemFilesystem.variantMove(source: Path, target: Path) {
if (MoveFileExA(source.toString(), target.toString(), MOVEFILE_REPLACE_EXISTING) == 0) {
throw IOException(lastErrorString())
Expand Down
2 changes: 2 additions & 0 deletions okio/src/nativeMain/kotlin/okio/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package okio

@ExperimentalFilesystem
internal actual val PLATFORM_FILESYSTEM: Filesystem = PosixSystemFilesystem

internal actual val DIRECTORY_SEPARATOR
get() = VARIANT_DIRECTORY_SEPARATOR
1 change: 1 addition & 0 deletions okio/src/nativeMain/kotlin/okio/PosixSystemFilesystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import platform.posix.opendir
import platform.posix.readdir
import platform.posix.set_posix_errno

@ExperimentalFilesystem
internal object PosixSystemFilesystem : Filesystem() {
private val SELF_DIRECTORY_ENTRY = ".".toPath()
private val PARENT_DIRECTORY_ENTRY = "..".toPath()
Expand Down
5 changes: 5 additions & 0 deletions okio/src/nativeMain/kotlin/okio/posixVariant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ package okio

internal expect val VARIANT_DIRECTORY_SEPARATOR: String

@ExperimentalFilesystem
internal expect fun PosixSystemFilesystem.variantDelete(path: Path)

@ExperimentalFilesystem
internal expect fun PosixSystemFilesystem.variantMkdir(dir: Path): Int

@ExperimentalFilesystem
internal expect fun PosixSystemFilesystem.variantCanonicalize(path: Path): Path

@ExperimentalFilesystem
internal expect fun PosixSystemFilesystem.variantMetadata(path: Path): FileMetadata

@ExperimentalFilesystem
internal expect fun PosixSystemFilesystem.variantMove(source: Path, target: Path)
1 change: 1 addition & 0 deletions okio/src/unixMain/kotlin/okio/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import kotlinx.cinterop.toKString
import okio.Path.Companion.toPath
import platform.posix.getenv

@ExperimentalFilesystem
internal actual val PLATFORM_TEMPORARY_DIRECTORY: Path
get() {
val tmpdir = getenv("TMPDIR")
Expand Down
4 changes: 4 additions & 0 deletions okio/src/unixMain/kotlin/okio/posixVariant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ import platform.posix.timespec

internal actual val VARIANT_DIRECTORY_SEPARATOR = "/"

@ExperimentalFilesystem
internal actual fun PosixSystemFilesystem.variantDelete(path: Path) {
val result = remove(path.toString())
if (result != 0) {
throw IOException(errnoString(errno))
}
}

@ExperimentalFilesystem
internal actual fun PosixSystemFilesystem.variantMkdir(dir: Path): Int {
return mkdir(dir.toString(), 0b111111111 /* octal 777 */)
}

@ExperimentalFilesystem
internal actual fun PosixSystemFilesystem.variantCanonicalize(path: Path): Path {
// Note that realpath() fails if the file doesn't exist.
val fullpath = realpath(path.toString(), null)
Expand All @@ -48,6 +51,7 @@ internal actual fun PosixSystemFilesystem.variantCanonicalize(path: Path): Path
}
}

@ExperimentalFilesystem
internal actual fun PosixSystemFilesystem.variantMove(
source: Path,
target: Path
Expand Down

0 comments on commit 5fa3ea4

Please sign in to comment.