Пример #1
0
func Watch(path string, eventHandler func(id uint64, path string, flags []string)) {
	dev, _ := fsevents.DeviceForPath(path)
	fsevents.EventIDForDeviceBeforeTime(dev, time.Now())

	es := &fsevents.EventStream{
		Paths:   []string{path},
		Latency: 50 * time.Millisecond,
		Device:  dev,
		Flags:   fsevents.FileEvents | fsevents.WatchRoot}
	es.Start()
	ec := es.Events

	for {
		select {
		case event := <-ec:
			flags := make([]string, 0)
			for bit, description := range noteDescription {
				if event.Flags&bit == bit {
					flags = append(flags, description)
				}
			}
			sort.Sort(sort.StringSlice(flags))
			go eventHandler(event.ID, event.Path, flags)

			es.Flush(false)
		}
	}
}
Пример #2
0
func main() {
	dev, _ := fsevents.DeviceForPath("/tmp")
	log.Print(dev)
	log.Println(fsevents.EventIDForDeviceBeforeTime(dev, time.Now()))

	es := &fsevents.EventStream{
		Paths:   []string{"/tmp"},
		Latency: 500 * time.Millisecond,
		// Device:  dev,
		Flags: fsevents.FileEvents | fsevents.WatchRoot}
	es.Start()
	ec := es.Events

	go func() {
		for {
			select {
			case event := <-ec:
				logEvent(event)
			}
		}
	}()

	in := bufio.NewReader(os.Stdin)

	if false {
		log.Print("Started, press enter to GC")
		in.ReadString('\n')
		runtime.GC()
		log.Print("GC'd, press enter to quit")
		in.ReadString('\n')
	} else {
		log.Print("Started, press enter to stop")
		in.ReadString('\n')
		es.Stop()

		log.Print("Stopped, press enter to restart")
		in.ReadString('\n')
		es.Resume = true
		es.Start()

		log.Print("Restarted, press enter to quit")
		in.ReadString('\n')
		es.Stop()
	}
}
Пример #3
0
func Watch(path string, ignored []string, poll bool, callback func(id uint64, path string, flags []string)) {
	dev, _ := fsevents.DeviceForPath(path)
	fsevents.EventIDForDeviceBeforeTime(dev, time.Now())

	es := &fsevents.EventStream{
		Paths:   []string{path},
		Latency: 50 * time.Millisecond,
		Device:  dev,
		Flags:   fsevents.FileEvents | fsevents.WatchRoot}
	es.Start()
	ec := es.Events

	poller := time.NewTicker(WATCH_POLL_INTERVAL * time.Second)

	// Disable the poller if polling isn't requested.
	if !poll {
		poller.Stop()
	}

	for {
		select {
		case event := <-ec:
			if FileIsIgnored(event.Path, ignored) {
				continue
			}

			flags := make([]string, 0)
			for bit, description := range noteDescription {
				if event.Flags&bit == bit {
					flags = append(flags, description)
				}
			}
			sort.Sort(sort.StringSlice(flags))
			go callback(event.ID, event.Path, flags)
			es.Flush(false)

		case <-poller.C:
			go callback(0, "", []string{})
		}
	}
}