File: closewrite.go

package info (click to toggle)
golang-fsnotify 1.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 728 kB
  • sloc: ansic: 98; makefile: 4
file content (89 lines) | stat: -rw-r--r-- 1,527 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main

/*
func closeWrite(paths ...string) {
	if len(paths) < 1 {
		exit("must specify at least one path to watch")
	}

	w, err := fsnotify.NewWatcher()
	if err != nil {
		exit("creating a new watcher: %s", err)
	}
	defer w.Close()

	var (
		op fsnotify.Op
		cw = w.Supports(fsnotify.UnportableCloseWrite)
	)
	if cw {
		op |= fsnotify.UnportableCloseWrite
	} else {
		op |= fsnotify.Create | fsnotify.Write
	}

	go closeWriteLoop(w, cw)

	for _, p := range paths {
		err := w.AddWith(p, fsnotify.WithOps(op))
		if err != nil {
			exit("%q: %s", p, err)
		}
	}

	printTime("ready; press ^C to exit")
	<-make(chan struct{})
}

func closeWriteLoop(w *fsnotify.Watcher, cw bool) {
	var (
		waitFor = 100 * time.Millisecond
		mu      sync.Mutex
		timers  = make(map[string]*time.Timer)
	)
	for {
		select {
		case err, ok := <-w.Errors:
			if !ok {
				return
			}
			panic(err)
		case e, ok := <-w.Events:
			if !ok {
				return
			}

			// CloseWrite is supported: easy case.
			if cw {
				if e.Has(fsnotify.UnportableCloseWrite) {
					printTime(e.String())
				}
				continue
			}

			// Get timer.
			mu.Lock()
			t, ok := timers[e.Name]
			mu.Unlock()

			// No timer yet, so create one.
			if !ok {
				t = time.AfterFunc(math.MaxInt64, func() {
					printTime(e.String())
					mu.Lock()
					delete(timers, e.Name)
					mu.Unlock()
				})
				t.Stop()

				mu.Lock()
				timers[e.Name] = t
				mu.Unlock()
			}

			// Reset the timer for this path, so it will start from 100ms again.
			t.Reset(waitFor)
		}
	}
}
*/