func (watch *Watch) Run() { notify, err := inotify.NewWatcher() if err != nil { log.Fatal(err) } watch.Files = make(chan string) go func() { for { select { case ev := <-notify.Event: if watch.Delay > 0 { go func() { time.Sleep(time.Duration(watch.Delay) * time.Millisecond) watch.Files <- ev.Name }() } else { watch.Files <- ev.Name } case err := <-notify.Error: log.Print(err) } } }() err = notify.AddWatch(watch.Directory, inotify.IN_CLOSE_WRITE|inotify.IN_MOVED_TO) if err != nil { log.Fatal(err) } else { log.Print(watch) } }
func main() { root_folder := "watch_folder" dirname := "." + string(filepath.Separator) + root_folder + string(filepath.Separator) f, err := os.Open("FST_watch") defer f.Close() if err != nil { log.Println("Error opening file:", err) } var fst1 FStree decoder := gob.NewDecoder(f) decoder.Decode(&fst1) watcher, err := inotify.NewWatcher() if err != nil { log.Fatal(err) } FST_parse_watch(&fst1, dirname, watcher) for { select { case ev := <-watcher.Event: log.Println("event:", ev) case err := <-watcher.Error: log.Println("error:", err) } } }
func SetupWatch(paths []string, excludes []string) (int, *inotify.Watcher) { // How many directories are being watched var watchedCount int // Collect all subdirs of the watch and exclude roots paths = CollectPaths(paths) excludes = CollectPaths(excludes) watcher, err := inotify.NewWatcher() if err != nil { fmt.Println("Error establishing watcher: ", err) } // establish watches for _, path := range paths { if IndexOf(path, excludes) == -1 { err = watcher.Watch(path) if err != nil { fmt.Println("Error: ", err, " establishing watch on: ", path) } watchedCount++ } } return watchedCount, watcher }
// Watch a directory for new files that are finished writing and files // moved into the directory. Returns a read-only channel of absolute filenames func WatchDirectory(directory string) <-chan string { if !path.IsAbs(directory) { return nil } watcher, err := inotify.NewWatcher() if err != nil { return nil } err = watcher.AddWatch(w.Path, inotify.IN_CLOSE_WRITE|inotify.IN_MOVED_TO) if err != nil { return nil } newFiles := make(chan string, 1) go func() { defer watcher.Close() for { select { case ev := <-watcher.Event: // inotify.Event.Name is a full path, huzzah! newFiles <- ev.Name case err := <-watcher.Error: log.Println(err) break } } }() return newFiles }
func NewInotifyWatcher() (w *InotifyWatcher, err error) { i, err := inotify.NewWatcher() if err != nil { return } w = &InotifyWatcher{i} return }
// Create new alfredwatcher, all the methods of alfredwatcher should been invoked by alfred, not by client directly. func newAlfredWatcher() *alfredWatcher { w, _ := inotify.NewWatcher() aw := &alfredWatcher{ watcher: w, list: make(map[string]uint32), } return aw }
func init() { var err error watcher, err = inotify.NewWatcher() if err != nil { log.Printf("unable to start watcher: %s", err.Error()) return } }
//This function watches all of the files in the background and takes action accordingly func (tnt *TnTServer) FST_watch_files(dirname string) { //First initialize the watch watcher, err := inotify.NewWatcher() if err != nil { log.Fatal(err) } fmt.Println(dirname) //Set watch on /tmp folder for transfers tnt.FST_set_watch(tnt.tmp, watcher) tnt.FST_set_watch(dirname, watcher) fst := tnt.Tree.MyTree for { select { case ev := <-watcher.Event: //fmt.Println("I see event: ", ev) //This if statement causes us to avoid taking into account swap files used to keep //track of file modifications if !strings.Contains(ev.Name, ".swp") && !strings.Contains(ev.Name, ".swx") && !strings.Contains(ev.Name, "~") && !strings.Contains(ev.Name, ".goutputstream") && !strings.Contains(ev.Name, strings.TrimSuffix(tnt.tmp, "/")) { if ev.Mask != IN_CLOSE && ev.Mask != IN_OPEN && ev.Mask != IN_OPEN_ISDIR && ev.Mask != IN_CLOSE_ISDIR { fmt.Println("event: ", ev) fi, err := os.Lstat(ev.Name) key_path := "./" + strings.TrimPrefix(ev.Name, tnt.root) //fmt.Println("ev: ", ev, key_path) update := true if err == nil { if fi.IsDir() { tnt.FST_set_watch(ev.Name, watcher) key_path = key_path + "/" } } else if fst[key_path+"/"] != nil { key_path = key_path + "/" } else if fst[key_path] != nil { //fmt.Println("this is a file") } else { //fmt.Println("what am i doing", err, fst[key_path]) update = false } if update { tnt.mu.Lock() tnt.UpdateTreeWrapper(key_path) tnt.mu.Unlock() } } } case err := <-watcher.Error: log.Println("error:", err) } } }
func main() { flag.Parse() args := flag.Args() if len(args) == 0 { log.Fatal("error: missing path") } runtime.GOMAXPROCS(*cpus) var err error watcher, err = inotify.NewWatcher() if err != nil { log.Fatal(err) } rules = &Rules{ m: new(sync.RWMutex), } loadRules(rules) scan := make(chan string) for i := 0; i < *cpus; i++ { go scanner(scan) } for _, v := range args { watchDir(v) } log.Println("Watcher preload done. Scanner ready.") for { select { case ev := <-watcher.Event: log.Println("event: ", ev) if ev.Mask&inotify.IN_ISDIR == 0 { scan <- ev.Name } /* If a directory is created and files are created in it before a watcher * is set up to monitor the directory, they are not scanned. */ if ev.Mask&inotify.IN_CREATE != 0 && ev.Mask&inotify.IN_ISDIR != 0 { log.Println("directory has been created, watching it too") watchDir(ev.Name) } case err := <-watcher.Error: log.Println("error: ", err) } } }
func NewDirwatch(path string) (result Dirwatch, err error) { d := new(baseDirwatch) if d.watcher, err = inotify.NewWatcher(); err != nil { return } mediaFiles := make(chan string, 10240) dirs := make(chan string, 1024) dirs <- path if err = initWatch(path, dirs, mediaFiles); err != nil { return } log.Printf("len files: %d\n", len(mediaFiles)) return d, nil }
func (self *rawContainerHandler) WatchSubcontainers(events chan container.SubcontainerEvent) error { // Lazily initialize the watcher so we don't use it when not asked to. if self.watcher == nil { w, err := inotify.NewWatcher() if err != nil { return err } self.watcher = w } // Watch this container (all its cgroups) and all subdirectories. for _, cgroupPath := range self.cgroupPaths { err := self.watchDirectory(cgroupPath, self.name) if err != nil { return err } } // Process the events received from the kernel. go func() { for { select { case event := <-self.watcher.Event: err := self.processEvent(event, events) if err != nil { glog.Warningf("Error while processing event (%+v): %v", event, err) } case err := <-self.watcher.Error: glog.Warningf("Error while watching %q:", self.name, err) case <-self.stopWatcher: err := self.watcher.Close() if err == nil { self.stopWatcher <- err self.watcher = nil return } } } }() return nil }
func TestWatchFolder(t *testing.T) { tnts, clean := SetupServers("getfile") defer clean() root_folder := "/home/zek/fss/roots/root" watcher, err := inotify.NewWatcher() if err != nil { log.Fatal(err) } test_server := 0 dirname := root_folder + strconv.Itoa(test_server) tnts[test_server].FST_set_watch(dirname, watcher) dirname = root_folder + strconv.Itoa(test_server) + "/" tnts[test_server].FST_watch_files(dirname, watcher) for { } }
func main() { fmt.Printf("Starting imageserver %s\n", appVersion) setup() go periodically(10*time.Minute, createIndices) startWebserver() watcher, err := inotify.NewWatcher() if err != nil { log.Fatal(err) } watcher.AddWatch(wwwPath, inotify.IN_CLOSE_WRITE|inotify.IN_MOVED_TO) watcher.AddWatch(poolPath, inotify.IN_CLOSE_WRITE|inotify.IN_MOVED_TO) for { select { case ev := <-watcher.Event: fileChanged(ev.Name) case err := <-watcher.Error: log.Println(err) } } }
func file_watcher() { watcher, err := inotify.NewWatcher() if err != nil { log.Fatal(err) } err = watcher.Watch("./watch_folder") if err != nil { log.Fatal(err) } err = watcher.Watch("./watch_folder/nest1") if err != nil { log.Fatal(err) } for { select { case ev := <-watcher.Event: log.Println("event:", ev) case err := <-watcher.Error: log.Println("error:", err) } } }
//This function watches all of the files in the background and takes action accordingly func (tnt *TnTServer) FST_watch_files(dirname string) { //First initialize the watch watcher, err := inotify.NewWatcher() if err != nil { log.Fatal(err) } fmt.Println(dirname) //Set watch on /tmp folder for transfers tnt.FST_set_watch("../roots/tmp"+strconv.Itoa(tnt.me)+"/", watcher) tnt.FST_set_watch(dirname, watcher) fst := tnt.Tree.MyTree for { select { case ev := <-watcher.Event: //fmt.Println("I see event: ", ev) //This if statement causes us to avoid taking into account swap files used to keep //track of file modifications if !strings.Contains(ev.Name, ".swp") && !strings.Contains(ev.Name, ".swx") && !strings.Contains(ev.Name, "~") && !strings.Contains(ev.Name, ".goutputstream") && !strings.Contains(ev.Name, tnt.tmp) { if ev.Mask != IN_CLOSE && ev.Mask != IN_OPEN && ev.Mask != IN_OPEN_ISDIR && ev.Mask != IN_CLOSE_ISDIR { //fmt.Println("ev.Name: ", ev.Name) fi, err := os.Lstat(ev.Name) key_path := "./" + strings.TrimPrefix(ev.Name, tnt.root) //fmt.Println("ev: ", ev, key_path) if err == nil { if fi.IsDir() { tnt.FST_set_watch(ev.Name, watcher) key_path = key_path + "/" } } else if fst[key_path+"/"] != nil { key_path = key_path + "/" } else if fst[key_path] != nil { fmt.Println("this is a file") } else { fmt.Println("what am i doing", err, fst[key_path]) } //fmt.Println("key to update", key_path) //if(fst[key_path] != nil || err == nil){ tnt.mu.Lock() tnt.Tree.LogicalTime++ tnt.UpdateTree(key_path) if fst[key_path] != nil { tnt.PropagateUp(fst[key_path].VerVect, fst[key_path].SyncVect, fst[key_path].Parent) } tnt.mu.Unlock() //} } } case err := <-watcher.Error: log.Println("error:", err) } } }
//This function watches all of the files in the background and takes action accordingly func (tnt *TnTServer) FST_watch_files(dirname string) { //First initialize the watch watcher, err := inotify.NewWatcher() if err != nil { log.Fatal(err) } //Set watch on /tmp folder for transfers tnt.FST_set_watch("../roots/tmp"+strconv.Itoa(tnt.me)+"/", watcher) tnt.FST_set_watch(dirname, watcher) fst := tnt.Tree.MyTree //fmt.Println("in FST_watch_files", dirname) //fmt.Println(fst[dirname]) var cur_file string var seq_count int = 0 //This is for creation and mods from text editors var mod_count int = 0 //This is for tracking modifications var move_count int = 0 var old_path string var old_path_key string for { select { case ev := <-watcher.Event: //This if statement causes us to avoid taking into account swap files used to keep //track of file modifications if !strings.Contains(ev.Name, ".swp") && !strings.Contains(ev.Name, ".swx") && !strings.Contains(ev.Name, "~") && !strings.Contains(ev.Name, ".goutputstream") && !strings.Contains(ev.Name, "roots/tmp") { fmt.Println("ev: ", ev) //fmt.Println("ev.Name: ", ev.Name) fi, _ := os.Lstat(ev.Name) key_path := "./" + strings.TrimPrefix(ev.Name, tnt.root) tnt.Tree.LogicalTime++ tnt.UpdateTree(key_path) //fmt.Println("./"+key_path) //trim_name := strings.TrimPrefix(ev.Name, tnt.root) // 1) Create a file/folder - add it to tree //Folder only command is IN_CREATE with name as path /* if(ev.Mask == IN_CREATE_ISDIR && fst[key_path] == nil && !strings.Contains(ev.Name,"roots/tmp")){ fmt.Println("new folder", ev.Name) err := watcher.Watch(ev.Name) if err != nil { log.Fatal(err) } tnt.Tree.LogicalTime++ tnt.UpdateTree(key_path) /* fst[key_path] = new(FSnode) fst[key_path].Name = fi.Name() fst[key_path].Size = fi.Size() fst[key_path].IsDir = fi.IsDir() fst[key_path].LastModTime = fi.ModTime() fst[key_path].Creator = tnt.me fst[key_path].CreationTime = tnt.Tree.LogicalTime fst[key_path].Children = make(map[string]bool) fst[key_path].VerVect = make(map[int]int64) fst[key_path].SyncVect = make(map[int]int64) for i:=0; i<len(tnt.servers); i++ { fst[key_path].VerVect[i] = 0 fst[key_path].SyncVect[i] = 0 } fst[key_path].VerVect[tnt.me] = tnt.Tree.LogicalTime fst[key_path].SyncVect[tnt.me] = tnt.Tree.LogicalTime fst[key_path].Parent = parent(ev.Name) //fmt.Println("parent is ", fst[ev.Name].Parent) } //This is the sequence of commands when a file is created or modified in a text editor //fmt.Println(seq_count) if(ev.Mask == IN_CREATE && seq_count == 0 && !strings.Contains(ev.Name,"/tmp")){ cur_file = ev.Name seq_count = 1 }else if(ev.Mask == IN_OPEN && seq_count == 1){ seq_count = 2 } else if(ev.Mask == IN_MODIFY && seq_count == 2){ seq_count = 3 }else if(ev.Mask == IN_CLOSE && cur_file == ev.Name && seq_count == 3){ seq_count = 0 if(fst[key_path] == nil){ fmt.Println("new file was created", ev.Name) tnt.Tree.LogicalTime++ fst[key_path] = new(FSnode) fst[key_path].Name = fi.Name() fst[key_path].Size = fi.Size() fst[key_path].IsDir = fi.IsDir() fst[key_path].LastModTime = fi.ModTime() fst[key_path].Creator = tnt.me fst[key_path].CreationTime = tnt.Tree.LogicalTime fst[key_path].VerVect = make(map[int]int64) fst[key_path].SyncVect = make(map[int]int64) for i:=0; i<len(tnt.servers); i++ { fst[key_path].VerVect[i] = 0 fst[key_path].SyncVect[i] = 0 } fst[key_path].VerVect[tnt.me] = tnt.Tree.LogicalTime fst[key_path].SyncVect[tnt.me] = tnt.Tree.LogicalTime fst[key_path].Parent = parent(ev.Name) }else{ // 2) Modify a file - increment its modified vector by 1 fmt.Println("file has been modified", fst[key_path]) tnt.Tree.LogicalTime++ fst[key_path].LastModTime = fi.ModTime() fst[key_path].VerVect[tnt.me] = tnt.Tree.LogicalTime fst[key_path].SyncVect[tnt.me] = tnt.Tree.LogicalTime tnt.PropagateUp(fst[key_path].VerVect,fst[key_path].SyncVect,fst[key_path].Parent) } }else { seq_count = 0 } //This is the events that occur when files modified from the command line if(ev.Mask == IN_MODIFY && mod_count == 0 && !strings.Contains(ev.Name,"/tmp")){ cur_file = ev.Name mod_count = 1 }else if(ev.Mask == IN_OPEN && mod_count == 1){ mod_count = 2 } else if(ev.Mask == IN_MODIFY && mod_count == 2){ mod_count = 3 }else if(ev.Mask == IN_CLOSE && cur_file == ev.Name && mod_count == 3){ mod_count = 0 // 2) Modify a file - increment its modified vector by 1 //fmt.Println("file has been modified", fst[key_path]) tnt.Tree.LogicalTime++ fst[key_path].LastModTime = fi.ModTime() fst[key_path].VerVect[tnt.me] = tnt.Tree.LogicalTime fst[key_path].SyncVect[tnt.me] = tnt.Tree.LogicalTime tnt.PropagateUp(fst[key_path].VerVect,fst[key_path].SyncVect,fst[key_path].Parent) fmt.Println("file has been modified", fst[key_path]) }else { mod_count = 0 } // 3) Delete a file - indicate it has been removed, don't necessarily remove it from tree if(ev.Mask == IN_DELETE && fst[key_path] != nil){ fmt.Println("file has been deleted", fst[key_path]) tnt.Tree.LogicalTime++ fst[key_path].VerVect[tnt.me] = tnt.Tree.LogicalTime fst[key_path].SyncVect[tnt.me] = tnt.Tree.LogicalTime tnt.PropagateUp(fst[key_path].VerVect,fst[key_path].SyncVect,fst[key_path].Parent) tnt.DeleteTree(ev.Name) } // 6) Delete a directory, need to parse and remove children as well if(ev.Mask == IN_DELETE_ISDIR && fst[key_path] != nil){ fmt.Println("folder has been deleted", fst[key_path]) tnt.Tree.LogicalTime++ fst[key_path].VerVect[tnt.me] = tnt.Tree.LogicalTime fst[key_path].SyncVect[tnt.me] = tnt.Tree.LogicalTime tnt.PropagateUp(fst[key_path].VerVect,fst[key_path].SyncVect,fst[key_path].Parent) tnt.DeleteTree(ev.Name) } // 5) Do nothing when transferring files from tmp/ to the rest of the directory //fmt.Println(ev, move_count) if(ev.Mask == IN_MOVE_FROM && move_count == 0){ fmt.Println("This is a move") old_path = ev.Name old_path_key = "./"+strings.TrimPrefix(ev.Name,tnt.root) move_count = 1 }else if( ev.Mask == IN_MOVE_TO && move_count == 1){ fmt.Println("file has been moved", old_path) if(strings.Contains(old_path,"/tmp")){ fmt.Println("Moved thru transfer, do nothing") }else{ fmt.Println("Actual Move, do something") //Need to delete previous path and create new path tnt.Tree.LogicalTime++ fst[old_path_key].VerVect[tnt.me] = tnt.Tree.LogicalTime fst[old_path_key].SyncVect[tnt.me] = tnt.Tree.LogicalTime tnt.PropagateUp(fst[old_path_key].VerVect,fst[old_path_key].SyncVect,fst[old_path_key].Parent) tnt.DeleteTree(old_path) fst[key_path] = new(FSnode) fst[key_path].Name = fi.Name() fst[key_path].Size = fi.Size() fst[key_path].IsDir = fi.IsDir() fst[key_path].LastModTime = fi.ModTime() fst[key_path].Creator = tnt.me fst[key_path].CreationTime = tnt.Tree.LogicalTime fst[key_path].VerVect = make(map[int]int64) fst[key_path].SyncVect = make(map[int]int64) for i:=0; i<len(tnt.servers); i++ { fst[key_path].VerVect[i] = 0 fst[key_path].SyncVect[i] = 0 } fst[key_path].VerVect[tnt.me] = tnt.Tree.LogicalTime fst[key_path].SyncVect[tnt.me] = tnt.Tree.LogicalTime fst[key_path].Parent = parent(ev.Name) } move_count = 0 }else if(ev.Mask == IN_MOVE_TO && fst[key_path] != nil && move_count == 0){ //This is when a file has been modified fmt.Println("file has been modified") tnt.Tree.LogicalTime++ fst[key_path].LastModTime = fi.ModTime() fst[key_path].VerVect[tnt.me] = tnt.Tree.LogicalTime fst[key_path].SyncVect[tnt.me] = tnt.Tree.LogicalTime tnt.PropagateUp(fst[key_path].VerVect,fst[key_path].SyncVect,fst[key_path].Parent) }else if(ev.Mask == IN_MOVE_TO && fst[key_path] == nil && move_count == 0) { fmt.Println("file has been moved from outside directory, treat like new file") tnt.Tree.LogicalTime++ fst[key_path] = new(FSnode) fst[key_path].Name = fi.Name() fst[key_path].Size = fi.Size() fst[key_path].IsDir = fi.IsDir() fst[key_path].LastModTime = fi.ModTime() fst[key_path].Creator = tnt.me fst[key_path].CreationTime = tnt.Tree.LogicalTime fst[key_path].VerVect = make(map[int]int64) fst[key_path].SyncVect = make(map[int]int64) for i:=0; i<len(tnt.servers); i++ { fst[key_path].VerVect[i] = 0 fst[key_path].SyncVect[i] = 0 } fst[key_path].VerVect[tnt.me] = tnt.Tree.LogicalTime fst[key_path].SyncVect[tnt.me] = tnt.Tree.LogicalTime fst[key_path].Parent = parent(ev.Name) }else{ move_count = 0 } */ } case err := <-watcher.Error: log.Println("error:", err) } } }