|
4 | 4 | */ |
5 | 5 | package aws.smithy.kotlin.runtime.util |
6 | 6 |
|
| 7 | +import aws.smithy.kotlin.runtime.io.IOException |
| 8 | +import kotlinx.cinterop.* |
| 9 | +import platform.posix.* |
| 10 | + |
7 | 11 | internal actual object SystemDefaultProvider : PlatformProvider { |
8 | | - actual override fun getAllEnvVars(): Map<String, String> { |
9 | | - TODO("Not yet implemented") |
10 | | - } |
| 12 | + // FIXME How to get all environment variables on Native? |
| 13 | + // See if it's possible to get extern char **environ loaded... is not available by default in platform.posix.* |
| 14 | + // https://man7.org/linux/man-pages/man7/environ.7.html |
| 15 | + actual override fun getAllEnvVars(): Map<String, String> = mapOf<String, String>() |
11 | 16 |
|
12 | | - actual override fun getenv(key: String): String? { |
13 | | - TODO("Not yet implemented") |
14 | | - } |
| 17 | + actual override fun getenv(key: String): String? = platform.posix.getenv(key)?.toKString() |
15 | 18 |
|
16 | 19 | actual override val filePathSeparator: String |
17 | | - get() = TODO("Not yet implemented") |
| 20 | + get() = "/" // Unix-style separator is used in Native |
18 | 21 |
|
19 | 22 | actual override suspend fun readFileOrNull(path: String): ByteArray? { |
20 | | - TODO("Not yet implemented") |
| 23 | + return try { |
| 24 | + val file = fopen(path, "rb") ?: return null |
| 25 | + |
| 26 | + try { |
| 27 | + // Get file size |
| 28 | + fseek(file, 0L, SEEK_END) |
| 29 | + val size = ftell(file) |
| 30 | + fseek(file, 0L, SEEK_SET) |
| 31 | + |
| 32 | + // Read file content |
| 33 | + val buffer = ByteArray(size.toInt()).pin() |
| 34 | + val rc = fread(buffer.addressOf(0), 1.toULong(), size.toULong(), file) |
| 35 | + if (rc == size.toULong()) buffer.get() else null |
| 36 | + } finally { |
| 37 | + fclose(file) |
| 38 | + } |
| 39 | + } catch (e: Exception) { |
| 40 | + null |
| 41 | + } |
21 | 42 | } |
22 | 43 |
|
23 | 44 | actual override suspend fun writeFile(path: String, data: ByteArray) { |
24 | | - TODO("Not yet implemented") |
| 45 | + val file = fopen(path, "wb") ?: throw IOException("Cannot open file for writing: $path") |
| 46 | + try { |
| 47 | + val wc = fwrite(data.refTo(0), 1.toULong(), data.size.toULong(), file) |
| 48 | + if (wc != data.size.toULong()) { |
| 49 | + throw IOException("Failed to write all bytes to file $path, expected ${data.size.toLong()}, wrote $wc") |
| 50 | + } |
| 51 | + } finally { |
| 52 | + fclose(file) |
| 53 | + } |
25 | 54 | } |
26 | 55 |
|
27 | | - actual override fun fileExists(path: String): Boolean { |
28 | | - TODO("Not yet implemented") |
29 | | - } |
| 56 | + actual override suspend fun deleteFile(path: String) { remove(path) } |
30 | 57 |
|
31 | | - actual override fun osInfo(): OperatingSystem { |
32 | | - TODO("Not yet implemented") |
33 | | - } |
| 58 | + actual override fun fileExists(path: String): Boolean = access(path, F_OK) == 0 |
34 | 59 |
|
35 | | - actual override val isJvm: Boolean |
36 | | - get() = TODO("Not yet implemented") |
37 | | - actual override val isAndroid: Boolean |
38 | | - get() = TODO("Not yet implemented") |
39 | | - actual override val isBrowser: Boolean |
40 | | - get() = TODO("Not yet implemented") |
41 | | - actual override val isNode: Boolean |
42 | | - get() = TODO("Not yet implemented") |
43 | | - actual override val isNative: Boolean |
44 | | - get() = TODO("Not yet implemented") |
45 | | - |
46 | | - actual override fun getAllProperties(): Map<String, String> { |
47 | | - TODO("Not yet implemented") |
48 | | - } |
| 60 | + actual override fun osInfo(): OperatingSystem = memScoped { |
| 61 | + val utsname = alloc<utsname>() |
| 62 | + uname(utsname.ptr) |
| 63 | + |
| 64 | + val sysName = utsname.sysname.toKString().lowercase() |
| 65 | + val version = utsname.release.toKString() |
| 66 | + val machine = utsname.machine.toKString() // Helps differentiate iOS/macOS |
49 | 67 |
|
50 | | - actual override fun getProperty(key: String): String? { |
51 | | - TODO("Not yet implemented") |
| 68 | + val family = when { |
| 69 | + sysName.contains("darwin") -> { |
| 70 | + if (machine.startsWith("iPhone") || machine.startsWith("iPad")) { |
| 71 | + OsFamily.Ios |
| 72 | + } else { |
| 73 | + OsFamily.MacOs |
| 74 | + } |
| 75 | + } |
| 76 | + sysName.contains("linux") -> OsFamily.Linux |
| 77 | + sysName.contains("windows") -> OsFamily.Windows |
| 78 | + else -> OsFamily.Unknown |
| 79 | + } |
| 80 | + |
| 81 | + return OperatingSystem(family, version) |
52 | 82 | } |
| 83 | + |
| 84 | + actual override val isJvm: Boolean = false |
| 85 | + actual override val isAndroid: Boolean = false |
| 86 | + actual override val isBrowser: Boolean = false |
| 87 | + actual override val isNode: Boolean = false |
| 88 | + actual override val isNative: Boolean = true |
| 89 | + |
| 90 | + // Kotlin/Native doesn't have system properties |
| 91 | + actual override fun getAllProperties(): Map<String, String> = emptyMap() |
| 92 | + actual override fun getProperty(key: String): String? = null |
53 | 93 | } |
0 commit comments