github.com/DiversionCompany/notify@v0.9.9/example_inotify_test.go (about)

     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  //go:build linux
     6  // +build linux
     7  
     8  package notify_test
     9  
    10  import (
    11  	"log"
    12  
    13  	"golang.org/x/sys/unix"
    14  
    15  	"github.com/DiversionCompany/notify"
    16  )
    17  
    18  // This example shows how to watch changes made on file-system by text editor
    19  // when saving a file. Usually, either InCloseWrite or InMovedTo (when swapping
    20  // with a temporary file) event is created.
    21  func ExampleWatch_linux() {
    22  	// Make the channel buffered to ensure no event is dropped. Notify will drop
    23  	// an event if the receiver is not able to keep up the sending pace.
    24  	c := make(chan notify.EventInfo, 1)
    25  
    26  	// Set up a watchpoint listening for inotify-specific events within a
    27  	// current working directory. Dispatch each InCloseWrite and InMovedTo
    28  	// events separately to c.
    29  	if err := notify.Watch(".", c, notify.InCloseWrite, notify.InMovedTo); err != nil {
    30  		log.Fatal(err)
    31  	}
    32  	defer notify.Stop(c)
    33  
    34  	// Block until an event is received.
    35  	switch ei := <-c; ei.Event() {
    36  	case notify.InCloseWrite:
    37  		log.Println("Editing of", ei.Path(), "file is done.")
    38  	case notify.InMovedTo:
    39  		log.Println("File", ei.Path(), "was swapped/moved into the watched directory.")
    40  	}
    41  }
    42  
    43  // This example shows how to use Sys() method from EventInfo interface to tie
    44  // two separate events generated by rename(2) function.
    45  func ExampleWatch_linuxMove() {
    46  	// Make the channel buffered to ensure no event is dropped. Notify will drop
    47  	// an event if the receiver is not able to keep up the sending pace.
    48  	c := make(chan notify.EventInfo, 2)
    49  
    50  	// Set up a watchpoint listening for inotify-specific events within a
    51  	// current working directory. Dispatch each InMovedFrom and InMovedTo
    52  	// events separately to c.
    53  	if err := notify.Watch(".", c, notify.InMovedFrom, notify.InMovedTo); err != nil {
    54  		log.Fatal(err)
    55  	}
    56  	defer notify.Stop(c)
    57  
    58  	// Inotify reports move filesystem action by sending two events tied with
    59  	// unique cookie value (uint32): one of the events is of InMovedFrom type
    60  	// carrying move source path, while the second one is of InMoveTo type
    61  	// carrying move destination path.
    62  	moves := make(map[uint32]struct {
    63  		From string
    64  		To   string
    65  	})
    66  
    67  	// Wait for moves.
    68  	for ei := range c {
    69  		cookie := ei.Sys().(*unix.InotifyEvent).Cookie
    70  
    71  		info := moves[cookie]
    72  		switch ei.Event() {
    73  		case notify.InMovedFrom:
    74  			info.From = ei.Path()
    75  		case notify.InMovedTo:
    76  			info.To = ei.Path()
    77  		}
    78  		moves[cookie] = info
    79  
    80  		if cookie != 0 && info.From != "" && info.To != "" {
    81  			log.Println("File:", info.From, "was renamed to", info.To)
    82  			delete(moves, cookie)
    83  		}
    84  	}
    85  }