示例#1
0
func initWatcher(watcher *fsnotify.Watcher, skipFolders []string) *Watcher {
	event := make(chan *fsnotify.FileEvent)
	watcherError := make(chan error)
	monitorWatcher := &Watcher{Event: event, Error: watcherError, watcher: watcher, SkipFolders: skipFolders}
	go func() {
		for {
			select {
			case ev := <-watcher.Event:
				event <- ev
				if ev.IsCreate() {
					go func() {
						if f, err := os.Stat(ev.Name); err == nil {
							if f.IsDir() {
								monitorWatcher.watchAllFolders(ev.Name)
							}
						}

					}()
				}
				if ev.IsDelete() {
					go func() {
						watcher.RemoveWatch(ev.Name)
					}()
				}
			case e := <-watcher.Error:
				watcherError <- e
			}
		}
	}()
	return monitorWatcher
}
func EventHandler(eventChan chan Event, watcher *fsnotify.Watcher) {
	fmt.Println("In event handler")
	for {
		event := <-eventChan
		switch {
		case event.EventType == fsnotify.FSN_CREATE:
			watcher.Watch(event.FilePath)
			if event.IsDir {
				allPaths := CollectPaths([]string{event.FilePath})
				//fmt.Println(allPaths[1:])
				//time.Sleep(1 * time.Millisecond)
				for _, path := range allPaths[1:] {
					err := watcher.Watch(path)
					if err != nil {
						fmt.Println("Error: ", err, "  establishing watch on: ", path)
					}
				}
			}
			fmt.Printf("EVENT %s: %s \t%v\n", "CREATE", event.FilePath, event.EventTime)

		case event.EventType == fsnotify.FSN_MODIFY:
			fmt.Printf("EVENT %s: %s \t%v\n", "MODIFY", event.FilePath, event.EventTime)

		case event.EventType == fsnotify.FSN_DELETE:
			if event.IsDir {
				err := watcher.RemoveWatch(event.FilePath)
				if err != nil {
					fmt.Println("Error removing watchg (delete): ", err)
				}
			}
			fmt.Printf("EVENT %s: %s \t%v\n", "DELETE", event.FilePath, event.EventTime)

		case event.EventType == fsnotify.FSN_RENAME:
			if event.IsDir {
				err := watcher.RemoveWatch(event.FilePath)
				if err != nil {
					fmt.Println("Error removing watch (rename): ", err)
				}
			}
			fmt.Printf("EVENT %s: %s \t%v\n", "RENAME", event.FilePath, event.EventTime)

		}
	}
}
示例#3
0
func EventHandler(watcher *fsnotify.Watcher, manager chan *Command) {
	for {
		select {
		case ev := <-watcher.Event:
			//encrypt() upload()
			switch {
			case ev.IsCreate():
				watcher.Watch(ev.Name)
				logEvent(ev.Name, "create")

			case ev.IsDelete():
				watcher.RemoveWatch(ev.Name)
				logEvent(ev.Name, "delete")

			case ev.IsModify():
				logEvent(ev.Name, "modify")

			case ev.IsAttrib():
				logEvent(ev.Name, "modify attrib")

			case ev.IsRename():
				watcher.RemoveWatch(ev.Name)
				logEvent(ev.Name, "rename")

			default:
				fmt.Println("Something is weird. Event but not type?")
			}
		case err := <-watcher.Error:
			// TODO: handle errors and see why reading from this can cause a block.
			fmt.Println("error reading error in fsmonitor: ", err)
		case com := <-manager:
			// TODO: Add in ability to add/remove watches from a recieved command
			if com.exitP {
				err := watcher.Close()
				fmt.Println("Returning EventHandler")
				if err != nil {
					fmt.Println("Error on close of watch: ", err)
				}
				return
			}
		}
	}
}