scoop:scoop.os.macos:0.1.0
src/watch.scoop
Selected production source · src/watch.scoop
1
package scoop.os.macos.fsevents3
import scoop.platform.fs.*4
import scoop.platform.system.*6
/** An FSEvents item with its native event ID and unmodified flag bits. */7
public struct DirectoryEvent(val path: Path, val id: ULong, val flags: UInt)9
/** A directory tree subscription. Close from any thread to wake the pending reader. */10
public class DirectoryWatch private constructor(private val root: Path) : Closeable {11
private var state: Ptr<Unit>? = None13
public companion object {14
/** Subscribes to events from now on. The system may coalesce nearby changes. */15
public fun open(root: Path): DirectoryWatch {16
val watch = DirectoryWatch(root)17
@Unsafe {18
val opened = root19
.withNativePath { path ->20
@Unsafe {21
return@withNativePath nativeOpen(path)22
}23
}24
if (opened.watch == None) failWatch("open FSEvents", opened.error, root)25
watch.state = opened.watch26
}27
return watch28
}29
}31
// The control block is owned until GC. A close can race with a read, and a release32
// hook must not free the block while a native call is still using its address.33
private fun <R> withState(block: (Ptr<Unit>) -> R): R {34
@Unsafe {35
val owner = getGcHandle(this)36
try {37
return block(state!!)38
} finally {39
releaseGcHandle(owner)40
}41
}42
}44
/** True once this subscription has started closing. */45
public val isClosed: Boolean46
get() = withState { watch ->47
@Unsafe {48
return@withState nativeClosed(watch)49
}50
}52
/** Waits for the next batch. A competing reader throws; a closed subscription returns None. */53
public fun nextBatch(): Array<DirectoryEvent>? {54
return withState { watch ->55
@Unsafe {56
val result = nativeNext(watch)57
if (result.error == -1)58
throw IllegalStateException(Some("an FSEvents reader is active"))59
if (result.error != 0) failWatch("read FSEvents", result.error, root)60
val batch = result.batch ?: return@withState None61
try {62
val events = Array<DirectoryEvent>(result.count) { index ->63
@Unsafe {64
val item = nativeEvent(batch, index)65
return@Array DirectoryEvent(66
Path.fromOsString(copyNativeString(item.path)),67
item.id,68
item.flags69
)70
}71
}72
return@withState Some(events)73
} finally {74
nativeFinish(watch, batch)75
}76
}77
}78
}80
/** Stops delivery, discards queued batches and waits for callbacks already in progress. */81
public override fun close() {82
withState { watch ->83
@Unsafe {84
nativeClose(watch)85
}86
}87
}89
release {90
when (state) {91
case Some(val watch) ->92
@Unsafe {93
nativeRelease(watch)94
}95
case None -> {}96
}97
}98
}100
private fun failWatch(operation: String, code: Int, root: Path): Nothing {101
val context = Some(root.toOsString())102
if (code == 0) throw PlatformException(operation, PlatformErrorKind.Other, None, context)103
val error = NativeError(NativeErrorDomain.PosixErrno, code)104
throw PlatformException(operation, error.kind, Some(error), context)105
}107
@CLayout108
private struct OpenResult(val watch: Ptr<Unit>?, val error: Int)110
@CLayout111
private struct ReadResult(val batch: Ptr<Unit>?, val count: Long, val error: Int)113
@CLayout114
private struct NativeEvent(val path: Ptr<Byte>, val id: ULong, val flags: UInt)116
@Extern(name = "scoop_os_macos_watch_open")117
private fun nativeOpen(path: Ptr<Byte>): OpenResult119
@Extern(name = "scoop_os_macos_watch_closed")120
private fun nativeClosed(watch: Ptr<Unit>): Boolean122
@Extern(name = "scoop_os_macos_next_batch")123
private fun nativeNext(watch: Ptr<Unit>): ReadResult125
@GCLeaf126
@Extern(name = "scoop_os_macos_batch_event")127
private fun nativeEvent(batch: Ptr<Unit>, index: Long): NativeEvent129
@Extern(name = "scoop_os_macos_finish_read")130
private fun nativeFinish(watch: Ptr<Unit>, batch: Ptr<Unit>)132
@Extern(name = "scoop_os_macos_watch_close")133
private fun nativeClose(watch: Ptr<Unit>)135
@Extern(name = "scoop_os_macos_watch_release")136
private fun nativeRelease(watch: Ptr<Unit>)