scoop:scoop.os.nix:0.1.0
src/files.scoop
Selected production source · src/files.scoop
1
package scoop.os.nix.fs3
import scoop.platform.fs.FileMetadata4
import scoop.platform.fs.Path5
import scoop.platform.fs.withNativePath6
import scoop.platform.native.NativeFileMetadata7
import scoop.platform.native.decodeFileMetadata8
import scoop.platform.native.requireFileMode9
import scoop.platform.system.*11
/** File metadata, ownership and identity from a single Unix stat or lstat call. */12
public struct UnixFileMetadata(13
val basic: FileMetadata,14
val userId: UInt,15
val groupId: UInt,16
val deviceId: ULong,17
val inode: ULong,18
val linkCount: Long19
)21
/** Queries the path, optionally observing the final symbolic link itself. */22
public fun Path.unixMetadata(followLinks: Boolean = true): UnixFileMetadata {23
@Unsafe {24
return this25
.withNativePath { pointer ->26
@Unsafe {27
val(data, error) = nativeStat(pointer, followLinks)28
val basic = decodeFileMetadata(data.basic, error, Some(this.toOsString()))29
if (data.linkCount > Long.MAX_VALUE.toULong()) throw ArithmeticException()30
return@withNativePath UnixFileMetadata(31
basic, data.userId, data.groupId, data.deviceId, data.inode, data32
.linkCount33
.toLong()34
)35
}36
}37
}38
}40
/** Creates a named pipe with the given permission bits, subject to the process umask. */41
public fun Path.createFifo(mode: Int) {42
requireFileMode(mode)43
@Unsafe {44
this45
.withNativePath { pointer ->46
@Unsafe {47
val(status, code) = nativeCreateFifo(pointer, mode)48
if (status < 0) {49
val error = NativeError(NativeErrorDomain.PosixErrno, code)50
throw PlatformException(51
"mkfifo",52
error.kind,53
Some(error),54
Some(this.toOsString())55
)56
}57
}58
}59
}60
}62
@CLayout63
private struct NativeUnixMetadata(64
val basic: NativeFileMetadata,65
val userId: UInt,66
val groupId: UInt,67
val deviceId: ULong,68
val inode: ULong,69
val linkCount: ULong70
)72
@Extern(name = "scoop_os_nix_stat", captureErrno = true)73
private fun nativeStat(path: Ptr<Byte>, follow: Boolean): (NativeUnixMetadata, Int)75
@Extern(name = "scoop_os_nix_mkfifo", captureErrno = true)76
private fun nativeCreateFifo(path: Ptr<Byte>, mode: Int): (Int, Int)