github.com/gofunct/common@v0.0.0-20190131174352-fd058c7fbf22/pkg/fs/watcher/fswatch/watch_item.go (about) 1 package fswatch 2 3 import "os" 4 5 type watchItem struct { 6 Path string 7 StatInfo os.FileInfo 8 LastEvent int 9 } 10 11 func watchPath(path string) (wi *watchItem) { 12 wi = new(watchItem) 13 wi.Path = path 14 wi.LastEvent = NONE 15 16 fi, err := os.Stat(path) 17 if err == nil { 18 wi.StatInfo = fi 19 } else if os.IsNotExist(err) { 20 wi.LastEvent = NOEXIST 21 } else if os.IsPermission(err) { 22 wi.LastEvent = NOPERM 23 } else { 24 wi.LastEvent = INVALID 25 } 26 return 27 } 28 29 func (wi *watchItem) Update() bool { 30 fi, err := os.Stat(wi.Path) 31 32 if err != nil { 33 if os.IsNotExist(err) { 34 if wi.LastEvent == NOEXIST { 35 return false 36 } else if wi.LastEvent == DELETED { 37 wi.LastEvent = NOEXIST 38 return false 39 } else { 40 wi.LastEvent = DELETED 41 return true 42 } 43 } else if os.IsPermission(err) { 44 if wi.LastEvent == NOPERM { 45 return false 46 } 47 wi.LastEvent = NOPERM 48 return true 49 } else { 50 wi.LastEvent = INVALID 51 return false 52 } 53 } 54 55 if wi.LastEvent == NOEXIST { 56 wi.LastEvent = CREATED 57 wi.StatInfo = fi 58 return true 59 } else if fi.ModTime().After(wi.StatInfo.ModTime()) { 60 wi.StatInfo = fi 61 switch wi.LastEvent { 62 case NONE, CREATED, NOPERM, INVALID: 63 wi.LastEvent = MODIFIED 64 case DELETED, NOEXIST: 65 wi.LastEvent = CREATED 66 } 67 return true 68 } else if fi.Mode() != wi.StatInfo.Mode() { 69 wi.LastEvent = PERM 70 wi.StatInfo = fi 71 return true 72 } 73 return false 74 } 75 76 // Notification represents a file state change. The Path field indicates 77 // the file that was changed, while last event corresponds to one of the 78 // event type constants. 79 type Notification struct { 80 Path string 81 Event int 82 } 83 84 // Notification returns a notification from a watchItem. 85 func (wi *watchItem) Notification() *Notification { 86 return &Notification{wi.Path, wi.LastEvent} 87 }