| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package notify_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"log" |
| 11 |
|
| 12 |
"github.com/syncthing/notify" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
func ExampleWatch_darwin() { |
| 17 |
|
| 18 |
|
| 19 |
c := make(chan notify.EventInfo, 1) |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
if err := notify.Watch(".", c, notify.FSEventsChangeOwner, notify.FSEventsMount); err != nil { |
| 25 |
log.Fatal(err) |
| 26 |
} |
| 27 |
defer notify.Stop(c) |
| 28 |
|
| 29 |
|
| 30 |
switch ei := <-c; ei.Event() { |
| 31 |
case notify.FSEventsChangeOwner: |
| 32 |
log.Println("The owner of", ei.Path(), "has changed.") |
| 33 |
case notify.FSEventsMount: |
| 34 |
log.Println("The path", ei.Path(), "has been mounted.") |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
func ExampleWatch_darwinDirFileSymlink() { |
| 42 |
var must = func(err error) { |
| 43 |
if err != nil { |
| 44 |
log.Fatal(err) |
| 45 |
} |
| 46 |
} |
| 47 |
var stop = func(c ...chan<- notify.EventInfo) { |
| 48 |
for _, c := range c { |
| 49 |
notify.Stop(c) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
dir := make(chan notify.EventInfo, 1) |
| 56 |
file := make(chan notify.EventInfo, 1) |
| 57 |
symlink := make(chan notify.EventInfo, 1) |
| 58 |
all := make(chan notify.EventInfo, 1) |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
must(notify.Watch(".", dir, notify.FSEventsIsDir)) |
| 63 |
must(notify.Watch(".", file, notify.FSEventsIsFile)) |
| 64 |
must(notify.Watch(".", symlink, notify.FSEventsIsSymlink)) |
| 65 |
must(notify.Watch(".", all, notify.All)) |
| 66 |
defer stop(dir, file, symlink, all) |
| 67 |
|
| 68 |
|
| 69 |
select { |
| 70 |
case ei := <-dir: |
| 71 |
log.Println("The directory", ei.Path(), "has changed") |
| 72 |
case ei := <-file: |
| 73 |
log.Println("The file", ei.Path(), "has changed") |
| 74 |
case ei := <-symlink: |
| 75 |
log.Println("The symlink", ei.Path(), "has changed") |
| 76 |
case ei := <-all: |
| 77 |
var kind string |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
switch flags := ei.Sys().(*notify.FSEvent).Flags; { |
| 82 |
case flags¬ify.FSEventsIsFile != 0: |
| 83 |
kind = "file" |
| 84 |
case flags¬ify.FSEventsIsDir != 0: |
| 85 |
kind = "dir" |
| 86 |
case flags¬ify.FSEventsIsSymlink != 0: |
| 87 |
kind = "symlink" |
| 88 |
} |
| 89 |
|
| 90 |
log.Printf("The %s under path %s has been %sd\n", kind, ei.Path(), ei.Event()) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
func ExampleWatch_darwinCoalesce() { |
| 101 |
|
| 102 |
|
| 103 |
c := make(chan notify.EventInfo, 4) |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
if err := notify.Watch(".", c, notify.All); err != nil { |
| 108 |
log.Fatal(err) |
| 109 |
} |
| 110 |
defer notify.Stop(c) |
| 111 |
|
| 112 |
var id uint64 |
| 113 |
var coalesced []notify.EventInfo |
| 114 |
|
| 115 |
for ei := range c { |
| 116 |
switch n := ei.Sys().(*notify.FSEvent).ID; { |
| 117 |
case id == 0: |
| 118 |
id = n |
| 119 |
coalesced = []notify.EventInfo{ei} |
| 120 |
case id == n: |
| 121 |
coalesced = append(coalesced, ei) |
| 122 |
default: |
| 123 |
log.Printf("FSEvents reported a filesystem action with the following"+ |
| 124 |
" coalesced events %v groupped by %d ID\n", coalesced, id) |
| 125 |
return |
| 126 |
} |
| 127 |
} |
| 128 |
} |