Scoop API

scoop:scoop.asyncio:0.1.0

src/path_modify.scoop

Selected production source · src/path_modify.scoop

1package scoop.asyncio
3import scoop.platform.fs.*
4import scoop.platform.system.OsString
6internal suspend fun moveFileOnLoop(
7 loop: LoopOwner,
8 source: Path,
9 destination: Path,
10 replace: Boolean
11) {
12 pathModification(
13 loop,
14 "move",
15 destination.toOsString(),
16 {
17 native,
18 id -> @Unsafe
19 {
20 return@pathModification source.withIoPath {
21 from -> @Unsafe
22 {
23 return@withIoPath destination.withIoPath {
24 to -> @Unsafe
25 {
26 return@withIoPath nativePathMove(native, id, from, to, replace)
27 }
28 }
29 }
30 }
31 }
32 }
33 )
36internal suspend fun deleteFileOnLoop(loop: LoopOwner, path: Path) {
37 pathModification(
38 loop,
39 "delete",
40 path.toOsString(),
41 { native, id -> deletePathNative(path, native, id) }
42 )
45internal fun deleteFileForCleanup(loop: LoopOwner, path: Path, complete: (Throwable?) -> Unit) {
46 val operation =
47 CallbackOperation(
48 loop,
49 loop.nextOperation(),
50 { native, id -> nativeStatus(id, deletePathNative(path, native, id)) },
51 { _, _ -> },
52 {
53 event,
54 selected -> complete(
55 completionFailure("delete", event.result, selected, Some(path.toOsString()))
56 )
57 }
58 )
59 loop.submit(operation)
62private fun deletePathNative(path: Path, native: Ptr<Unit>, id: Long): Int {
63 @Unsafe
64 {
65 return path.withIoPath {
66 name -> @Unsafe
67 {
68 return@withIoPath nativePathDelete(native, id, name)
69 }
70 }
71 }
74internal suspend fun createDirectoryOnLoop(loop: LoopOwner, path: Path, mode: Int) {
75 pathModification(
76 loop,
77 "create directory",
78 path.toOsString(),
79 {
80 native,
81 id -> @Unsafe
82 {
83 return@pathModification path.withIoPath {
84 name -> @Unsafe
85 {
86 return@withIoPath nativePathMkdir(native, id, name, mode)
87 }
88 }
89 }
90 }
91 )
94private suspend fun pathModification(
95 loop: LoopOwner,
96 name: String,
97 context: OsString,
98 start: (Ptr<Unit>, Long) -> Int
99) {
100 val job = loop.requireJob()
101 suspendCoroutine<Unit> {
102 continuation ->
103 val operation =
104 CallbackOperation(
105 loop,
106 loop.nextOperation(),
107 { native, id -> nativeStatus(id, start(native, id)) },
108 {
109 native,
110 id -> @Unsafe
111 {
112 nativeWorkCancel(native, id)
113 }
114 },
115 {
116 event,
117 selected -> finishUnit(
118 continuation,
119 completionFailure(name, event.result, selected, Some(context))
120 )
121 }
122 )
123 operation.subscribe(job)
124 loop.submit(operation)
125 }
128@Extern(name = "scoop_io_path_move")
129private fun nativePathMove(
130 loop: Ptr<Unit>,
131 id: Long,
132 source: Ptr<Byte>,
133 destination: Ptr<Byte>,
134 replace: Boolean
135): Int
137@Extern(name = "scoop_io_path_delete")
138private fun nativePathDelete(loop: Ptr<Unit>, id: Long, path: Ptr<Byte>): Int
140@Extern(name = "scoop_io_path_mkdir")
141private fun nativePathMkdir(loop: Ptr<Unit>, id: Long, path: Ptr<Byte>, mode: Int): Int