Scoop API

scoop:scoop.os.macos:0.1.0

src/watch.scoop

Selected production source · src/watch.scoop

1package scoop.os.macos.fsevents
3import scoop.platform.fs.*
4import scoop.platform.system.*
6/** An FSEvents item with its native event ID and unmodified flag bits. */
7public 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. */
10public class DirectoryWatch private constructor(private val root: Path) : Closeable {
11 private var state: Ptr<Unit>? = None
13 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 = root
19 .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.watch
26 }
27 return watch
28 }
29 }
31 // The control block is owned until GC. A close can race with a read, and a release
32 // 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: Boolean
46 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 None
61 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.flags
69 )
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 }
100private 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)
107@CLayout
108private struct OpenResult(val watch: Ptr<Unit>?, val error: Int)
110@CLayout
111private struct ReadResult(val batch: Ptr<Unit>?, val count: Long, val error: Int)
113@CLayout
114private struct NativeEvent(val path: Ptr<Byte>, val id: ULong, val flags: UInt)
116@Extern(name = "scoop_os_macos_watch_open")
117private fun nativeOpen(path: Ptr<Byte>): OpenResult
119@Extern(name = "scoop_os_macos_watch_closed")
120private fun nativeClosed(watch: Ptr<Unit>): Boolean
122@Extern(name = "scoop_os_macos_next_batch")
123private fun nativeNext(watch: Ptr<Unit>): ReadResult
125@GCLeaf
126@Extern(name = "scoop_os_macos_batch_event")
127private fun nativeEvent(batch: Ptr<Unit>, index: Long): NativeEvent
129@Extern(name = "scoop_os_macos_finish_read")
130private fun nativeFinish(watch: Ptr<Unit>, batch: Ptr<Unit>)
132@Extern(name = "scoop_os_macos_watch_close")
133private fun nativeClose(watch: Ptr<Unit>)
135@Extern(name = "scoop_os_macos_watch_release")
136private fun nativeRelease(watch: Ptr<Unit>)