Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling on nix systems #108

Merged
merged 3 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/), and this
project adheres to [Semantic Versioning](https://semver.org/).

## [0.2.3] - TBD
### Changed
- Improve error handling on nix systems (#108)

## [0.2.2] - 2020-08-27
### Added
- Support for watchOS and tvOS (#98)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import org.jetbrains.kotlin.konan.target.HostManager

plugins {
kotlin("multiplatform") version "1.4.0"
kotlin("multiplatform") version "1.4.10"
id("org.jetbrains.dokka") version "0.9.18"
id("maven-publish")
id("signing")
Expand Down
24 changes: 24 additions & 0 deletions src/nativeMain/kotlin/urandom.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.benasher44.uuid

import kotlinx.cinterop.ByteVar
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.usePinned
import platform.posix.O_RDONLY
import platform.posix.close
import platform.posix.errno
import platform.posix.open

internal fun bytesWithURandomFd(fdLambda: (Int, CPointer<ByteVar>) -> Unit): ByteArray {
return ByteArray(UUID_BYTES).also { bytes ->
val fd = open("/dev/urandom", O_RDONLY)
check(fd != -1) { "Failed to access /dev/urandom: $errno" }
try {
bytes.usePinned {
fdLambda(fd, it.addressOf(0))
}
} finally {
close(fd)
}
}
}
13 changes: 2 additions & 11 deletions src/nix32Main/kotlin/platform.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
package com.benasher44.uuid

import kotlinx.cinterop.addressOf
import kotlinx.cinterop.usePinned
import platform.posix.O_RDONLY
import platform.posix.close
import platform.posix.open
import platform.posix.read

internal actual fun getRandomUuidBytes(): ByteArray {
val bytes = ByteArray(UUID_BYTES)
val fd = open("/dev/urandom", O_RDONLY)
bytes.usePinned {
read(fd, it.addressOf(0), UUID_BYTES.toUInt())
return bytesWithURandomFd { fd, bytePtr ->
read(fd, bytePtr, UUID_BYTES.toUInt())
}
close(fd)
return bytes
}
13 changes: 2 additions & 11 deletions src/nix64Main/kotlin/platform.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
package com.benasher44.uuid

import kotlinx.cinterop.addressOf
import kotlinx.cinterop.convert
import kotlinx.cinterop.usePinned
import platform.posix.O_RDONLY
import platform.posix.close
import platform.posix.open
import platform.posix.read

internal actual fun getRandomUuidBytes(): ByteArray {
val bytes = ByteArray(UUID_BYTES)
val fd = open("/dev/urandom", O_RDONLY)
bytes.usePinned {
read(fd, it.addressOf(0), UUID_BYTES.convert())
return bytesWithURandomFd { fd, bytePtr ->
read(fd, bytePtr, UUID_BYTES.convert())
}
close(fd)
return bytes
}