| 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. |
| 4 | |
| 5 | // +build linux |
| 6 | |
| 7 | package notify_test |
| 8 | |
| 9 | import ( |
| 10 | "log" |
| 11 | |
| 12 | "golang.org/x/sys/unix" |
| 13 | |
| 14 | "github.com/syncthing/notify" |
| 15 | ) |
| 16 | |
| 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) |
| 24 | |
| 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 { |
| 29 | log.Fatal(err) |
| 30 | } |
| 31 | defer notify.Stop(c) |
| 32 | |
| 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.") |
| 39 | } |
| 40 | } |
| 41 | |
| 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) |
| 48 | |
| 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 { |
| 53 | log.Fatal(err) |
| 54 | } |
| 55 | defer notify.Stop(c) |
| 56 | |
| 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 { |
| 62 | From string |
| 63 | To string |
| 64 | }) |
| 65 | |
| 66 | // Wait for moves. |
| 67 | for ei := range c { |
| 68 | cookie := ei.Sys().(*unix.InotifyEvent).Cookie |
| 69 | |
| 70 | info := moves[cookie] |
| 71 | switch ei.Event() { |
| 72 | case notify.InMovedFrom: |
| 73 | info.From = ei.Path() |
| 74 | case notify.InMovedTo: |
| 75 | info.To = ei.Path() |
| 76 | } |
| 77 | moves[cookie] = info |
| 78 | |
| 79 | if cookie != 0 && info.From != "" && info.To != "" { |
| 80 | log.Println("File:", info.From, "was renamed to", info.To) |
| 81 | delete(moves, cookie) |
| 82 | } |
| 83 | } |
| 84 | } |