| 1 |
|
| 2 |
|
| 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 |
|
| 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 |
|
| 36 |
|
| 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 |
|
| 51 |
|
| 52 |
func TestCopyWithExcludeSkipsLstat(t *testing.T) { |
| 53 |
t1 := t.TempDir() |
| 54 |
t2 := t.TempDir() |
| 55 |
|
| 56 |
|
| 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 |
|
| 64 |
testHookLstat, lstatCalls, mu := createLstatTracker() |
| 65 |
|
| 66 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 98 |
|
| 99 |
func TestCopyMultipleExcludesSkipLstat(t *testing.T) { |
| 100 |
t1 := t.TempDir() |
| 101 |
t2 := t.TempDir() |
| 102 |
|
| 103 |
|
| 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 |
|
| 114 |
testHookLstat, lstatCalls, mu := createLstatTracker() |
| 115 |
|
| 116 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 157 |
|
| 158 |
|
| 159 |
func TestCopyWithIncludeAndExcludeMustCallLstat(t *testing.T) { |
| 160 |
t1 := t.TempDir() |
| 161 |
t2 := t.TempDir() |
| 162 |
|
| 163 |
|
| 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 |
|
| 169 |
testHookLstat, lstatCalls, mu := createLstatTracker() |
| 170 |
|
| 171 |
|
| 172 |
|
| 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 |
|
| 184 |
_, err = os.Stat(filepath.Join(t2, dirFolder, includedFile)) |
| 185 |
require.NoError(t, err, "included.txt should be copied") |
| 186 |
|
| 187 |
|
| 188 |
_, err = os.Stat(filepath.Join(t2, dirFolder, excludedLogFile)) |
| 189 |
require.True(t, os.IsNotExist(err), "excluded.log should not be copied") |
| 190 |
|
| 191 |
|
| 192 |
|
| 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 |
|
| 201 |
|
| 202 |
|
| 203 |
func TestCopyWithNegationPatternsMustCallLstat(t *testing.T) { |
| 204 |
t1 := t.TempDir() |
| 205 |
t2 := t.TempDir() |
| 206 |
|
| 207 |
|
| 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 |
|
| 213 |
testHookLstat, lstatCalls, mu := createLstatTracker() |
| 214 |
|
| 215 |
|
| 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 |
|
| 226 |
_, err = os.Stat(filepath.Join(t2, logsFolder, importantLogFile)) |
| 227 |
require.NoError(t, err, "important.log should be copied due to negation") |
| 228 |
|
| 229 |
|
| 230 |
_, err = os.Stat(filepath.Join(t2, logsFolder, debugLogFile)) |
| 231 |
require.True(t, os.IsNotExist(err), "debug.log should be excluded") |
| 232 |
|
| 233 |
|
| 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 |
|
| 242 |
|
| 243 |
func TestCopyFromRootWithProtectedFolders(t *testing.T) { |
| 244 |
t1 := t.TempDir() |
| 245 |
t2 := t.TempDir() |
| 246 |
|
| 247 |
|
| 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 |
|
| 258 |
testHookLstat, lstatCalls, mu := createLstatTracker() |
| 259 |
|
| 260 |
|
| 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 |
|
| 271 |
_, err = os.Stat(filepath.Join(t2, normalFolder, normalFile)) |
| 272 |
require.NoError(t, err, "normal folder should be copied") |
| 273 |
|
| 274 |
|
| 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 |
|
| 282 |
mu.Lock() |
| 283 |
defer mu.Unlock() |
| 284 |
|
| 285 |
|
| 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 |
} |