Scoop API

scoop:scoop.asyncio:0.1.0

src/file.scoop

Selected production source · src/file.scoop

1package scoop.asyncio
3import scoop.platform.fs.*
4import scoop.platform.native.fileOpenFlags
5import scoop.platform.memory.NativeByteBuffer
6import scoop.platform.system.*
8/**
9 * An owned asynchronous file with a shared logical position, explicit seek, truncation, and
10 * durability operations.
11 */
12public interface AsyncFile: AsyncByteSource, AsyncByteSink, AsyncCloseable {
13 /**
14 * Obtains metadata for the open file.
15 */
16 public suspend fun metadata(): FileMetadata
17 /**
18 * Returns the current file size in bytes.
19 */
20 public suspend fun size(): Long
21 /**
22 * Returns the current logical byte position.
23 */
24 public suspend fun position(): Long
25 /**
26 * Sets the logical byte position; negative positions are invalid.
27 */
28 public suspend fun seek(position: Long)
29 /**
30 * Sets the file length in bytes according to the open file's write permissions.
31 */
32 public suspend fun truncate(size: Long)
33 /**
34 * Requests persistence of file data and metadata needed to retrieve it.
35 */
36 public suspend fun syncData()
37 /**
38 * Requests persistence of file data and full associated metadata.
39 */
40 public suspend fun syncAll()
43internal class WorkerFile(private val owner: OwnedFile): AsyncFile {
44 private val inputTransfer = FileTransfer(owner, false)
45 private val outputTransfer = FileTransfer(owner, true)
46 init {
47 owner.installTransfers(inputTransfer, outputTransfer)
48 }
49 public override suspend fun readInto(buffer: NativeByteBuffer, count: Long): Long =
50 suspendCoroutine(inputTransfer.prepare(buffer, count, owner.loop.requireJob()))
51 public override suspend fun writeFrom(buffer: NativeByteBuffer, count: Long): Long =
52 suspendCoroutine(outputTransfer.prepare(buffer, count, owner.loop.requireJob()))
53 public override suspend fun close() {
54 owner.close()
55 }
56 public override suspend fun flush() {
57 owner.loop.requireJob();
58 owner.reserve().dispose()
59 }
60 public override suspend fun metadata(): FileMetadata = fileMetadata(owner)
61 public override suspend fun size(): Long = metadata().size
62 public override suspend fun position(): Long = filePosition(owner, 0L, true)
63 public override suspend fun seek(position: Long) {
64 filePosition(owner, position, false)
65 }
66 public override suspend fun truncate(size: Long) {
67 truncateFile(owner, size)
68 }
69 public override suspend fun syncData() {
70 syncFile(owner, true)
71 }
72 public override suspend fun syncAll() {
73 syncFile(owner, false)
74 }
77internal suspend fun openFileOnLoop(loop: LoopOwner, path: Path, options: OpenOptions): AsyncFile {
78 val job = loop.requireJob()
79 val flags = fileOpenFlags(options)
80 val context = path.toOsString()
81 return suspendCoroutine<AsyncFile> {
82 continuation ->
83 val operation =
84 CallbackOperation(
85 loop,
86 loop.nextOperation(),
87 {
88 native,
89 id -> @Unsafe
90 {
91 return@CallbackOperation path.withIoPath {
92 pointer -> @Unsafe
93 {
94 return@withIoPath nativeStatus(
95 id,
96 nativeFileOpen(native, id, pointer, flags, options.mode)
97 )
98 }
99 }
100 }
101 },
102 {
103 native,
104 id -> @Unsafe
105 {
106 nativeWorkCancel(native, id)
107 }
108 },
109 {
110 event,
111 selected -> when (event.result) {
112 case Created(val pointer) -> {
113 val owner = OwnedFile(loop, pointer, context)
114 loop.retain(owner)
115 when (selected) {
116 case None -> continuation.resume(WorkerFile(owner))
117 case Some(val error) -> owner.closeOnLoop {
118 cleanup -> continuation.resumeWithException(
119 when (cleanup) {
120 case Some(val value) -> ResourceUseException(
121 error,
122 value
123 )
124 case None -> error
125 }
126 )
127 }
128 }
129 }
130 case _ -> continuation.resumeWithException(
131 completionFailure("file open", event.result, selected, Some(context)) ?:
132 IllegalStateException(Some("open returned no file"))
133 )
134 }
135 }
136 )
137 operation.subscribe(job)
138 loop.submit(operation)
139 }