|
| 1 | +//go:build darwin |
| 2 | + |
| 3 | +// Package fsevents provides file system notifications on macOS. |
| 4 | +package fsevents |
| 5 | + |
| 6 | +import ( |
| 7 | + "syscall" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// Event represents a single file system notification. |
| 12 | +type Event struct { |
| 13 | + // Path holds the path to the item that's changed, relative |
| 14 | + // to its device's root. |
| 15 | + // Use DeviceForPath to determine the absolute path that's |
| 16 | + // being referred to. |
| 17 | + Path string |
| 18 | + |
| 19 | + // Flags holds details what has happened. |
| 20 | + Flags EventFlags |
| 21 | + |
| 22 | + // ID holds the event ID. |
| 23 | + // |
| 24 | + // Each event ID comes from the most recent event being reported |
| 25 | + // in the corresponding directory named in the EventStream.Paths field |
| 26 | + // Event IDs all come from a single global source. |
| 27 | + // They are guaranteed to always be increasing, usually in leaps |
| 28 | + // and bounds, even across system reboots and moving drives from |
| 29 | + // one machine to another. If you were to |
| 30 | + // stop processing events from this stream after this event |
| 31 | + // and resume processing them later from a newly-created |
| 32 | + // EventStream, this is the value you would pass for the |
| 33 | + // EventStream.EventID along with Resume=true. |
| 34 | + ID uint64 |
| 35 | +} |
| 36 | + |
| 37 | +// DeviceForPath returns the device ID for the specified volume. |
| 38 | +func DeviceForPath(path string) (int32, error) { |
| 39 | + stat := syscall.Stat_t{} |
| 40 | + if err := syscall.Lstat(path, &stat); err != nil { |
| 41 | + return 0, err |
| 42 | + } |
| 43 | + return stat.Dev, nil |
| 44 | +} |
| 45 | + |
| 46 | +// EventStream is the primary interface to FSEvents |
| 47 | +// You can provide your own event channel if you wish (or one will be |
| 48 | +// created on Start). |
| 49 | +// |
| 50 | +// es := &EventStream{Paths: []string{"/tmp"}, Flags: 0} |
| 51 | +// es.Start() |
| 52 | +// es.Stop() |
| 53 | +// ... |
| 54 | +type EventStream struct { |
| 55 | + // Events holds the channel on which events will be sent. |
| 56 | + // It's initialized by EventStream.Start if nil. |
| 57 | + Events chan []Event |
| 58 | + |
| 59 | + // Paths holds the set of paths to watch, each |
| 60 | + // specifying the root of a filesystem hierarchy to be |
| 61 | + // watched for modifications. |
| 62 | + Paths []string |
| 63 | + |
| 64 | + // Flags specifies what events to receive on the stream. |
| 65 | + Flags CreateFlags |
| 66 | + |
| 67 | + // Resume specifies that watching should resume from the event |
| 68 | + // specified by EventID. |
| 69 | + Resume bool |
| 70 | + |
| 71 | + // EventID holds the most recent event ID. |
| 72 | + // |
| 73 | + // NOTE: this is updated asynchronously by the |
| 74 | + // watcher and should not be accessed while |
| 75 | + // the stream has been started. |
| 76 | + EventID uint64 |
| 77 | + |
| 78 | + // Latency holds the number of seconds the service should wait after hearing |
| 79 | + // about an event from the kernel before passing it along to the |
| 80 | + // client via its callback. Specifying a larger value may result |
| 81 | + // in more effective temporal coalescing, resulting in fewer |
| 82 | + // callbacks and greater overall efficiency. |
| 83 | + Latency time.Duration |
| 84 | + |
| 85 | + // When Device is non-zero, the watcher will watch events on the |
| 86 | + // device with this ID, and the paths in the Paths field are |
| 87 | + // interpreted relative to the device's root. |
| 88 | + // |
| 89 | + // The device ID is the same as the st_dev field from a stat |
| 90 | + // structure of a file on that device or the f_fsid[0] field of |
| 91 | + // a statfs structure. |
| 92 | + Device int32 |
| 93 | +} |
| 94 | + |
| 95 | +// Start listening to an event stream. This creates es.Events if it's not already |
| 96 | +// a valid channel. |
| 97 | +func (es *EventStream) Start() error { |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +// Flush flushes events that have occurred but haven't been delivered. |
| 102 | +// If sync is true, it will block until all the events have been delivered, |
| 103 | +// otherwise it will return immediately. |
| 104 | +func (es *EventStream) Flush(sync bool) { |
| 105 | +} |
| 106 | + |
| 107 | +// Stop stops listening to the event stream. |
| 108 | +func (es *EventStream) Stop() { |
| 109 | +} |
| 110 | + |
| 111 | +// Restart restarts the event listener. This |
| 112 | +// can be used to change the current watch flags. |
| 113 | +func (es *EventStream) Restart() error { |
| 114 | + es.Stop() |
| 115 | + es.Resume = true |
| 116 | + return es.Start() |
| 117 | +} |
0 commit comments