Scoop API

scoop:scoop.asyncio:0.1.0

src/directory_next.scoop

Selected production source · src/directory_next.scoop

1package scoop.asyncio
3import scoop.platform.system.OsString
5internal fun takeDirectoryName(pointer: Ptr<Unit>): OsString {
6 @Unsafe
7 {
8 val name = pointer.cast<Byte>()
9 try {
10 return copyIoString(name)
11 }
12 finally {
13 nativeDirectoryNameFree(name)
14 }
15 }
18internal suspend fun nextDirectoryName(owner: OwnedDirectory, job: Job): OsString? =
19 suspendCoroutine<OsString?> {
20 continuation ->
21 val operation =
22 CallbackOperation(
23 owner.loop,
24 owner.loop.nextOperation(),
25 {
26 _,
27 id -> @Unsafe
28 {
29 return@CallbackOperation nativeStatus(
30 id,
31 nativeDirectoryNext(owner.pointer, id)
32 )
33 }
34 },
35 {
36 native,
37 id -> @Unsafe
38 {
39 nativeWorkCancel(native, id)
40 }
41 },
42 {
43 event,
44 selected -> when (
45 completionFailure(
46 "read directory",
47 event.result,
48 selected,
49 Some(owner.context)
50 )
51 ) {
52 case Some(val error) -> continuation.resumeWithException(error)
53 case None -> when (event.result) {
54 case DirectoryName(val name) -> continuation.resume(Some(name))
55 case Eof -> {
56 owner.reachedEnd();
57 continuation.resume(None)
58 }
59 case _ -> continuation.resumeWithException(
60 IllegalStateException(
61 Some("directory read returned no name or EOF")
62 )
63 )
64 }
65 }
66 }
67 )
68 operation.subscribe(job)
69 owner.attach(operation)
70 owner.loop.submit(operation)
71 }
73@Extern(name = "scoop_io_directory_next")
74private fun nativeDirectoryNext(directory: Ptr<Unit>, id: Long): Int
76@Extern(name = "scoop_io_directory_name_free")
77private fun nativeDirectoryNameFree(name: Ptr<Byte>)