Scoop API

scoop:scoop.os.nix:0.1.0

src/files.scoop

Selected production source · src/files.scoop

1package scoop.os.nix.fs
3import scoop.platform.fs.FileMetadata
4import scoop.platform.fs.Path
5import scoop.platform.fs.withNativePath
6import scoop.platform.native.NativeFileMetadata
7import scoop.platform.native.decodeFileMetadata
8import scoop.platform.native.requireFileMode
9import scoop.platform.system.*
11/** File metadata, ownership and identity from a single Unix stat or lstat call. */
12public 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: Long
21/** Queries the path, optionally observing the final symbolic link itself. */
22public fun Path.unixMetadata(followLinks: Boolean = true): UnixFileMetadata {
23 @Unsafe {
24 return this
25 .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, data
32 .linkCount
33 .toLong()
34 )
35 }
36 }
37 }
40/** Creates a named pipe with the given permission bits, subject to the process umask. */
41public fun Path.createFifo(mode: Int) {
42 requireFileMode(mode)
43 @Unsafe {
44 this
45 .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 }
62@CLayout
63private 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: ULong
72@Extern(name = "scoop_os_nix_stat", captureErrno = true)
73private fun nativeStat(path: Ptr<Byte>, follow: Boolean): (NativeUnixMetadata, Int)
75@Extern(name = "scoop_os_nix_mkfifo", captureErrno = true)
76private fun nativeCreateFifo(path: Ptr<Byte>, mode: Int): (Int, Int)