scoop:scoop.asyncio:0.1.0
src/path_modify.scoop
Selected production source · src/path_modify.scoop
1
package scoop.asyncio3
import scoop.platform.fs.*4
import scoop.platform.system.OsString6
internal suspend fun moveFileOnLoop(7
loop: LoopOwner,8
source: Path,9
destination: Path,10
replace: Boolean11
) {12
pathModification(13
loop,14
"move",15
destination.toOsString(),16
{17
native,18
id -> @Unsafe19
{20
return@pathModification source.withIoPath {21
from -> @Unsafe22
{23
return@withIoPath destination.withIoPath {24
to -> @Unsafe25
{26
return@withIoPath nativePathMove(native, id, from, to, replace)27
}28
}29
}30
}31
}32
}33
)34
}36
internal suspend fun deleteFileOnLoop(loop: LoopOwner, path: Path) {37
pathModification(38
loop,39
"delete",40
path.toOsString(),41
{ native, id -> deletePathNative(path, native, id) }42
)43
}45
internal 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)60
}62
private fun deletePathNative(path: Path, native: Ptr<Unit>, id: Long): Int {63
@Unsafe64
{65
return path.withIoPath {66
name -> @Unsafe67
{68
return@withIoPath nativePathDelete(native, id, name)69
}70
}71
}72
}74
internal suspend fun createDirectoryOnLoop(loop: LoopOwner, path: Path, mode: Int) {75
pathModification(76
loop,77
"create directory",78
path.toOsString(),79
{80
native,81
id -> @Unsafe82
{83
return@pathModification path.withIoPath {84
name -> @Unsafe85
{86
return@withIoPath nativePathMkdir(native, id, name, mode)87
}88
}89
}90
}91
)92
}94
private suspend fun pathModification(95
loop: LoopOwner,96
name: String,97
context: OsString,98
start: (Ptr<Unit>, Long) -> Int99
) {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 -> @Unsafe111
{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
}126
}128
@Extern(name = "scoop_io_path_move")129
private fun nativePathMove(130
loop: Ptr<Unit>,131
id: Long,132
source: Ptr<Byte>,133
destination: Ptr<Byte>,134
replace: Boolean135
): Int137
@Extern(name = "scoop_io_path_delete")138
private fun nativePathDelete(loop: Ptr<Unit>, id: Long, path: Ptr<Byte>): Int140
@Extern(name = "scoop_io_path_mkdir")141
private fun nativePathMkdir(loop: Ptr<Unit>, id: Long, path: Ptr<Byte>, mode: Int): Int