aboutsummaryrefslogtreecommitdiffstats
path: root/watcher_test_helper.go
blob: 519d3f62ff7e1d47e532d207a24682199d56b05b (plain) (blame)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//go:build !windows

package main

import (
	"context"
	"os"
	"path/filepath"
	"sync"
	"time"

	"github.com/fsnotify/fsnotify"
)

// watchForTests uses inotify directly for integration testing.
// This is only used in tests - production uses socket-based events from Windows watcher.
func watchForTests(ctx context.Context, hostDir, dataDir string, manifest *Manifest, stack *GitignoreStack, dryRun bool) error {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return err
	}
	defer watcher.Close()

	var mu sync.Mutex
	debounceTimers := make(map[string]*time.Timer)
	debounceOps := make(map[string]fsnotify.Op)

	if err := addWatchersForTests(watcher, hostDir, stack); err != nil {
		return err
	}

	for {
		select {
		case <-ctx.Done():
			return nil

		case event, ok := <-watcher.Events:
			if !ok {
				return nil
			}

			relPath, err := filepath.Rel(hostDir, event.Name)
			if err != nil {
				continue
			}

			if isGitDir(relPath) {
				continue
			}

			if stack.IsIgnored(relPath) {
				continue
			}

			if event.Op&fsnotify.Create != 0 {
				srcPath := filepath.Join(hostDir, relPath)
				info, err := os.Lstat(srcPath)
				if err == nil && info.IsDir() {
					if hasGitignore(srcPath) {
						stack.Push(srcPath)
					}
					watcher.Add(srcPath)
					filepath.Walk(srcPath, func(path string, info os.FileInfo, err error) error {
						if err != nil || !info.IsDir() || path == srcPath {
							return nil
						}
						watcher.Add(path)
						return nil
					})
				}
			}

			mu.Lock()
			if existing, exists := debounceOps[relPath]; exists {
				debounceTimers[relPath].Stop()
				debounceOps[relPath] = existing | event.Op
			} else {
				debounceOps[relPath] = event.Op
			}
			path := relPath
			debounceTimers[relPath] = time.AfterFunc(200*time.Millisecond, func() {
				mu.Lock()
				op := debounceOps[path]
				delete(debounceOps, path)
				delete(debounceTimers, path)
				mu.Unlock()

				destPath := filepath.Join(dataDir, path)
				srcPath := filepath.Join(hostDir, path)

				switch {
				case op&fsnotify.Create != 0:
					syncCreate(srcPath, destPath, path, manifest, stack, dryRun, watcher)
				case op&fsnotify.Write != 0:
					syncWrite(srcPath, destPath, path, manifest, dryRun)
				case op&fsnotify.Remove != 0:
					syncRemove(destPath, path, manifest, dryRun)
				case op&fsnotify.Rename != 0:
					syncRemove(destPath, path, manifest, dryRun)
				}
			})
			mu.Unlock()

		case err, ok := <-watcher.Errors:
			if !ok {
				return nil
			}
			_ = err
		}
	}
}

func addWatchersForTests(watcher *fsnotify.Watcher, root string, stack *GitignoreStack) error {
	return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		relPath, err := filepath.Rel(root, path)
		if err != nil {
			return err
		}

		if isGitDir(relPath) {
			if info.IsDir() {
				return filepath.SkipDir
			}
			return nil
		}

		if info.IsDir() {
			if hasGitignore(path) {
				stack.Push(path)
			}
			if !stack.IsIgnored(relPath) {
				return watcher.Add(path)
			}
		}

		return nil
	})
}