1 // Copyright (c) 2014-2015 The Notify Authors. All rights reserved.
2 // Use of this source code is governed by the MIT license that can be
3 // found in the LICENSE file.
12 "golang.org/x/sys/unix"
14 "github.com/rjeczalik/notify"
17 // This example shows how to watch changes made on file-system by text editor
18 // when saving a file. Usually, either InCloseWrite or InMovedTo (when swapping
19 // with a temporary file) event is created.
20 func ExampleWatch_linux() {
21 // Make the channel buffered to ensure no event is dropped. Notify will drop
22 // an event if the receiver is not able to keep up the sending pace.
23 c := make(chan notify.EventInfo, 1)
25 // Set up a watchpoint listening for inotify-specific events within a
26 // current working directory. Dispatch each InCloseWrite and InMovedTo
27 // events separately to c.
28 if err := notify.Watch(".", c, notify.InCloseWrite, notify.InMovedTo); err != nil {
33 // Block until an event is received.
34 switch ei := <-c; ei.Event() {
35 case notify.InCloseWrite:
36 log.Println("Editing of", ei.Path(), "file is done.")
37 case notify.InMovedTo:
38 log.Println("File", ei.Path(), "was swapped/moved into the watched directory.")
42 // This example shows how to use Sys() method from EventInfo interface to tie
43 // two separate events generated by rename(2) function.
44 func ExampleWatch_linuxMove() {
45 // Make the channel buffered to ensure no event is dropped. Notify will drop
46 // an event if the receiver is not able to keep up the sending pace.
47 c := make(chan notify.EventInfo, 2)
49 // Set up a watchpoint listening for inotify-specific events within a
50 // current working directory. Dispatch each InMovedFrom and InMovedTo
51 // events separately to c.
52 if err := notify.Watch(".", c, notify.InMovedFrom, notify.InMovedTo); err != nil {
57 // Inotify reports move filesystem action by sending two events tied with
58 // unique cookie value (uint32): one of the events is of InMovedFrom type
59 // carrying move source path, while the second one is of InMoveTo type
60 // carrying move destination path.
61 moves := make(map[uint32]struct {
68 cookie := ei.Sys().(*unix.InotifyEvent).Cookie
72 case notify.InMovedFrom:
74 case notify.InMovedTo:
79 if cookie != 0 && info.From != "" && info.To != "" {
80 log.Println("File:", info.From, "was renamed to", info.To)