Debian Codegraph @

copy/copy_windows_test.go

raw symbols SCIP
1 //go:build windows
2 // +build windows
3
4 package fs
5
6 import (
7 "context"
8 "os"
9 "path/filepath"
10 "sync"
11 "testing"
12
13 "github.com/stretchr/testify/require"
14 )
15
16 func readUidGid(_ os.FileInfo) (uid, gid int, ok bool) {
17 return 0, 0, false
18 }
19
20 // Test folder and file names
21 const (
22 normalFolder = "normal"
23 normalFile = "file.txt"
24 excludedFolder = "excluded"
25 systemVolumeInfo = "System Volume Information"
26 wcSandboxState = "WcSandboxState"
27 dirFolder = "dir"
28 includedFile = "included.txt"
29 excludedLogFile = "excluded.log"
30 logsFolder = "logs"
31 debugLogFile = "debug.log"
32 importantLogFile = "important.log"
33 )
34
35 // createLstatTracker creates a test hook that tracks os.Lstat() calls in a thread-safe map.
36 // Returns the tracking function and the map for verification.
37 func createLstatTracker() (func(string), map[string]int, *sync.Mutex) {
38 var mu sync.Mutex
39 lstatCalls := make(map[string]int)
40
41 trackFunc := func(path string) {
42 mu.Lock()
43 lstatCalls[path]++
44 mu.Unlock()
45 }
46
47 return trackFunc, lstatCalls, &mu
48 }
49
50 // TestCopyWithExcludeSkipsLstat verifies that excluded paths skip os.Lstat() when safe.
51 // This uses a test hook to directly verify Lstat is NOT called on excluded paths.
52 func TestCopyWithExcludeSkipsLstat(t *testing.T) {
53 t1 := t.TempDir()
54 t2 := t.TempDir()
55
56 // Create test structure
57 require.NoError(t, os.MkdirAll(filepath.Join(t1, normalFolder), 0755))
58 require.NoError(t, os.WriteFile(filepath.Join(t1, normalFolder, normalFile), []byte("data"), 0644))
59
60 require.NoError(t, os.MkdirAll(filepath.Join(t1, excludedFolder), 0755))
61 require.NoError(t, os.WriteFile(filepath.Join(t1, excludedFolder, normalFile), []byte("excluded"), 0644))
62
63 // Track which paths have Lstat called
64 testHookLstat, lstatCalls, mu := createLstatTracker()
65
66 // Copy with exclude pattern and test hook
67 ci := CopyInfo{
68 ExcludePatterns: []string{excludedFolder},
69 XAttrErrorHandler: func(string, string, string, error) error { return nil },
70 testHookLstat: testHookLstat,
71 }
72
73 err := Copy(context.Background(), t1, "/", t2, "/", WithCopyInfo(ci))
74 require.NoError(t, err)
75
76 // Verify normal folder was copied and Lstat WAS called
77 _, err = os.Stat(filepath.Join(t2, normalFolder, normalFile))
78 require.NoError(t, err)
79
80 normalPath := filepath.Join(t1, normalFolder)
81 mu.Lock()
82 normalCalls := lstatCalls[normalPath]
83 mu.Unlock()
84 require.Greater(t, normalCalls, 0, "Lstat should be called on normal folder")
85
86 // Verify excluded folder was NOT copied and Lstat was NOT called
87 _, err = os.Stat(filepath.Join(t2, excludedFolder))
88 require.True(t, os.IsNotExist(err))
89
90 excludedPath := filepath.Join(t1, excludedFolder)
91 mu.Lock()
92 excludedCalls := lstatCalls[excludedPath]
93 mu.Unlock()
94 require.Equal(t, 0, excludedCalls, "Lstat should NOT be called on excluded folder (optimization working)")
95 }
96
97 // TestCopyMultipleExcludesSkipLstat verifies multiple exclude patterns work correctly.
98 // This simulates the real scenario of excluding multiple Windows protected folders.
99 func TestCopyMultipleExcludesSkipLstat(t *testing.T) {
100 t1 := t.TempDir()
101 t2 := t.TempDir()
102
103 // Create structure mimicking container mount with protected folders
104 require.NoError(t, os.MkdirAll(filepath.Join(t1, normalFolder), 0755))
105 require.NoError(t, os.WriteFile(filepath.Join(t1, normalFolder, normalFile), []byte("data"), 0644))
106
107 require.NoError(t, os.MkdirAll(filepath.Join(t1, systemVolumeInfo), 0755))
108 require.NoError(t, os.WriteFile(filepath.Join(t1, systemVolumeInfo, normalFile), []byte("protected1"), 0644))
109
110 require.NoError(t, os.MkdirAll(filepath.Join(t1, wcSandboxState), 0755))
111 require.NoError(t, os.WriteFile(filepath.Join(t1, wcSandboxState, normalFile), []byte("protected2"), 0644))
112
113 // Track which paths have Lstat called
114 testHookLstat, lstatCalls, mu := createLstatTracker()
115
116 // Copy excluding both protected folders with test hook
117 ci := CopyInfo{
118 ExcludePatterns: []string{systemVolumeInfo, wcSandboxState},
119 XAttrErrorHandler: func(string, string, string, error) error { return nil },
120 testHookLstat: testHookLstat,
121 }
122
123 err := Copy(context.Background(), t1, "/", t2, "/", WithCopyInfo(ci))
124 require.NoError(t, err)
125
126 // Verify normal folder was copied and Lstat WAS called
127 _, err = os.Stat(filepath.Join(t2, normalFolder, normalFile))
128 require.NoError(t, err)
129
130 normalPath := filepath.Join(t1, normalFolder)
131 mu.Lock()
132 normalCalls := lstatCalls[normalPath]
133 mu.Unlock()
134 require.Greater(t, normalCalls, 0, "Lstat should be called on normal folder")
135
136 // Verify both protected folders were NOT copied and Lstat was NOT called
137 _, err = os.Stat(filepath.Join(t2, systemVolumeInfo))
138 require.True(t, os.IsNotExist(err))
139
140 sviPath := filepath.Join(t1, systemVolumeInfo)
141 mu.Lock()
142 sviCalls := lstatCalls[sviPath]
143 mu.Unlock()
144 require.Equal(t, 0, sviCalls, "Lstat should NOT be called on System Volume Information (optimization working)")
145
146 _, err = os.Stat(filepath.Join(t2, wcSandboxState))
147 require.True(t, os.IsNotExist(err))
148
149 wcsPath := filepath.Join(t1, wcSandboxState)
150 mu.Lock()
151 wcsCalls := lstatCalls[wcsPath]
152 mu.Unlock()
153 require.Equal(t, 0, wcsCalls, "Lstat should NOT be called on WcSandboxState (optimization working)")
154 }
155
156 // TestCopyWithIncludeAndExcludeMustCallLstat tests that when BOTH include and exclude
157 // patterns are used, os.Lstat() MUST be called even for excluded paths.
158 // This is necessary because include patterns might match files inside excluded directories.
159 func TestCopyWithIncludeAndExcludeMustCallLstat(t *testing.T) {
160 t1 := t.TempDir()
161 t2 := t.TempDir()
162
163 // Create directory structure
164 require.NoError(t, os.MkdirAll(filepath.Join(t1, dirFolder), 0755))
165 require.NoError(t, os.WriteFile(filepath.Join(t1, dirFolder, includedFile), []byte("include me"), 0644))
166 require.NoError(t, os.WriteFile(filepath.Join(t1, dirFolder, excludedLogFile), []byte("exclude me"), 0644))
167
168 // Track which paths have Lstat called
169 testHookLstat, lstatCalls, mu := createLstatTracker()
170
171 // Copy with BOTH include and exclude patterns with test hook
172 // Use **/*.txt to match files in subdirectories
173 ci := CopyInfo{
174 IncludePatterns: []string{"**/*.txt"},
175 ExcludePatterns: []string{"**/*.log"},
176 XAttrErrorHandler: func(string, string, string, error) error { return nil },
177 testHookLstat: testHookLstat,
178 }
179
180 err := Copy(context.Background(), t1, "/", t2, "/", WithCopyInfo(ci))
181 require.NoError(t, err)
182
183 // Verify included.txt was copied
184 _, err = os.Stat(filepath.Join(t2, dirFolder, includedFile))
185 require.NoError(t, err, "included.txt should be copied")
186
187 // Verify excluded.log was NOT copied
188 _, err = os.Stat(filepath.Join(t2, dirFolder, excludedLogFile))
189 require.True(t, os.IsNotExist(err), "excluded.log should not be copied")
190
191 // When include patterns exist, Lstat MUST be called even for excluded paths
192 // to properly evaluate the pattern matching
193 dirPath := filepath.Join(t1, dirFolder)
194 mu.Lock()
195 dirCalls := lstatCalls[dirPath]
196 mu.Unlock()
197 require.Greater(t, dirCalls, 0, "Lstat MUST be called when include patterns exist")
198 }
199
200 // TestCopyWithNegationPatternsMustCallLstat tests that when exclude patterns
201 // contain negations (!pattern), os.Lstat() MUST be called.
202 // Negations create exceptions to exclusions, requiring file stats to evaluate properly.
203 func TestCopyWithNegationPatternsMustCallLstat(t *testing.T) {
204 t1 := t.TempDir()
205 t2 := t.TempDir()
206
207 // Create directory structure
208 require.NoError(t, os.MkdirAll(filepath.Join(t1, logsFolder), 0755))
209 require.NoError(t, os.WriteFile(filepath.Join(t1, logsFolder, debugLogFile), []byte("debug"), 0644))
210 require.NoError(t, os.WriteFile(filepath.Join(t1, logsFolder, importantLogFile), []byte("important"), 0644))
211
212 // Track which paths have Lstat called
213 testHookLstat, lstatCalls, mu := createLstatTracker()
214
215 // Copy with negation pattern: exclude all logs EXCEPT important.log
216 ci := CopyInfo{
217 ExcludePatterns: []string{"logs/*.log", "!logs/important.log"},
218 XAttrErrorHandler: func(string, string, string, error) error { return nil },
219 testHookLstat: testHookLstat,
220 }
221
222 err := Copy(context.Background(), t1, "/", t2, "/", WithCopyInfo(ci))
223 require.NoError(t, err)
224
225 // Verify important.log was copied (negation exception)
226 _, err = os.Stat(filepath.Join(t2, logsFolder, importantLogFile))
227 require.NoError(t, err, "important.log should be copied due to negation")
228
229 // Verify debug.log was NOT copied
230 _, err = os.Stat(filepath.Join(t2, logsFolder, debugLogFile))
231 require.True(t, os.IsNotExist(err), "debug.log should be excluded")
232
233 // When negation patterns exist, Lstat MUST be called to evaluate exceptions
234 logsPath := filepath.Join(t1, logsFolder)
235 mu.Lock()
236 logsCalls := lstatCalls[logsPath]
237 mu.Unlock()
238 require.Greater(t, logsCalls, 0, "Lstat MUST be called when negation patterns exist")
239 }
240
241 // TestCopyFromRootWithProtectedFolders simulates copying from a root directory
242 // containing protected folders, which is the real-world buildkit scenario.
243 func TestCopyFromRootWithProtectedFolders(t *testing.T) {
244 t1 := t.TempDir()
245 t2 := t.TempDir()
246
247 // Create a structure that simulates a Windows root with protected folders
248 require.NoError(t, os.MkdirAll(filepath.Join(t1, normalFolder), 0755))
249 require.NoError(t, os.WriteFile(filepath.Join(t1, normalFolder, normalFile), []byte("data"), 0644))
250
251 require.NoError(t, os.MkdirAll(filepath.Join(t1, systemVolumeInfo), 0755))
252 require.NoError(t, os.WriteFile(filepath.Join(t1, systemVolumeInfo, normalFile), []byte("protected1"), 0644))
253
254 require.NoError(t, os.MkdirAll(filepath.Join(t1, wcSandboxState), 0755))
255 require.NoError(t, os.WriteFile(filepath.Join(t1, wcSandboxState, normalFile), []byte("protected2"), 0644))
256
257 // Track operations
258 testHookLstat, lstatCalls, mu := createLstatTracker()
259
260 // Copy from root "/" with exclude patterns (like buildkit does)
261 ci := CopyInfo{
262 ExcludePatterns: []string{systemVolumeInfo, wcSandboxState},
263 XAttrErrorHandler: func(string, string, string, error) error { return nil },
264 testHookLstat: testHookLstat,
265 }
266
267 err := Copy(context.Background(), t1, "/", t2, "/", WithCopyInfo(ci))
268 require.NoError(t, err)
269
270 // Verify normal folder was copied
271 _, err = os.Stat(filepath.Join(t2, normalFolder, normalFile))
272 require.NoError(t, err, "normal folder should be copied")
273
274 // Verify protected folders were NOT copied
275 _, err = os.Stat(filepath.Join(t2, systemVolumeInfo))
276 require.True(t, os.IsNotExist(err), "System Volume Information should not be copied")
277
278 _, err = os.Stat(filepath.Join(t2, wcSandboxState))
279 require.True(t, os.IsNotExist(err), "WcSandboxState should not be copied")
280
281 // Check the operations log
282 mu.Lock()
283 defer mu.Unlock()
284
285 // Verify Lstat was NOT called for protected folders
286 sviPath := filepath.Join(t1, systemVolumeInfo)
287 wcsPath := filepath.Join(t1, wcSandboxState)
288
289 require.NotContains(t, lstatCalls, sviPath, "Lstat should NOT be called on System Volume Information")
290 require.NotContains(t, lstatCalls, wcsPath, "Lstat should NOT be called on WcSandboxState")
291 }