返回 DeepSeek-Reasonix
atomicwrite_test.go
根目录 / internal / fileutil / atomicwrite_test.go
1 package fileutil
2
3 import (
4 "errors"
5 "os"
6 "path/filepath"
7 "runtime"
8 "syscall"
9 "testing"
10 "time"
11 )
12
13 func TestReplaceFileNoRetryWhenTmpMissing(t *testing.T) {
14 oldBase := replaceRetryBase
15 replaceRetryBase = 10 * time.Second
16 t.Cleanup(func() { replaceRetryBase = oldBase })
17
18 dir := t.TempDir()
19 start := time.Now()
20 err := ReplaceFile(filepath.Join(dir, "missing.tmp"), filepath.Join(dir, "x.txt"))
21 if err == nil {
22 t.Fatal("want error when tmp source is missing")
23 }
24 if elapsed := time.Since(start); elapsed > time.Second {
25 t.Errorf("missing tmp should fail fast, took %v — it retried", elapsed)
26 }
27 }
28
29 func TestReplaceFileRetriesThenReturnsError(t *testing.T) {
30 oldBase, oldMax := replaceRetryBase, maxReplaceRetries
31 replaceRetryBase, maxReplaceRetries = 0, 3
32 t.Cleanup(func() { replaceRetryBase, maxReplaceRetries = oldBase, oldMax })
33
34 dir := t.TempDir()
35 tmp := filepath.Join(dir, "x.tmp")
36 if err := os.WriteFile(tmp, []byte("payload"), 0o644); err != nil {
37 t.Fatal(err)
38 }
39 dest := filepath.Join(dir, "blocked")
40 if err := os.Mkdir(dest, 0o755); err != nil {
41 t.Fatal(err)
42 }
43 if err := ReplaceFile(tmp, dest); err == nil {
44 t.Fatal("want error when dest can never be replaced")
45 }
46 if !fileExists(tmp) {
47 t.Error("tmp should survive a failed replace so the next launch can retry")
48 }
49 }
50
51 func TestReplaceFileRenamesInPlace(t *testing.T) {
52 dir := t.TempDir()
53 tmp := filepath.Join(dir, "x.tmp")
54 dest := filepath.Join(dir, "x.txt")
55 if err := os.WriteFile(tmp, []byte("hello"), 0o644); err != nil {
56 t.Fatal(err)
57 }
58 if err := ReplaceFile(tmp, dest); err != nil {
59 t.Fatal(err)
60 }
61 if b, _ := os.ReadFile(dest); string(b) != "hello" {
62 t.Errorf("dest = %q, want hello", b)
63 }
64 if _, err := os.Stat(tmp); !os.IsNotExist(err) {
65 t.Error("tmp should be gone after ReplaceFile")
66 }
67 }
68
69 func TestReplaceFileTransientFailureNeverTruncatesDest(t *testing.T) {
70 // A rename blocked by a transient lock must surface the error, never fall
71 // back to the in-place copy: the copy truncates dest first, so a reader
72 // racing it can observe an empty or half-written file — the torn state
73 // AtomicWriteFile promises its callers (session leases, credentials,
74 // plugin state) can never happen.
75 oldBase, oldMax, oldRename := replaceRetryBase, maxReplaceRetries, renameFile
76 replaceRetryBase, maxReplaceRetries = 0, 2
77 renameCalls := 0
78 renameFile = func(oldpath, newpath string) error {
79 renameCalls++
80 return &os.LinkError{Op: "rename", Old: oldpath, New: newpath, Err: errors.New("transient sharing violation")}
81 }
82 t.Cleanup(func() { replaceRetryBase, maxReplaceRetries, renameFile = oldBase, oldMax, oldRename })
83
84 dir := t.TempDir()
85 tmp := filepath.Join(dir, "x.tmp")
86 dest := filepath.Join(dir, "x.txt")
87 if err := os.WriteFile(tmp, []byte("new"), 0o644); err != nil {
88 t.Fatal(err)
89 }
90 if err := os.WriteFile(dest, []byte("old"), 0o644); err != nil {
91 t.Fatal(err)
92 }
93 if err := ReplaceFile(tmp, dest); err == nil {
94 t.Fatal("want the rename error to surface once retries are exhausted")
95 }
96 if want := maxReplaceRetries + 1; renameCalls != want {
97 t.Errorf("rename attempts = %d, want %d (initial try plus retries)", renameCalls, want)
98 }
99 if b, _ := os.ReadFile(dest); string(b) != "old" {
100 t.Fatalf("dest = %q, want the old content intact — anything else means the non-atomic copy ran", b)
101 }
102 if !fileExists(tmp) {
103 t.Error("tmp should survive a failed replace so the caller can clean up")
104 }
105 }
106
107 func TestReplaceFileCrossDeviceCopiesImmediately(t *testing.T) {
108 // The cross-device class (Windows encryption filter drivers, #2696) fails
109 // identically on every retry, so ReplaceFile must take the copy fallback
110 // straight away instead of sleeping through the retry ladder.
111 oldBase, oldMax, oldRename := replaceRetryBase, maxReplaceRetries, renameFile
112 // Any retry sleep would trip the elapsed-time check below.
113 replaceRetryBase, maxReplaceRetries = 10*time.Second, 8
114 renameCalls := 0
115 renameFile = func(oldpath, newpath string) error {
116 renameCalls++
117 return &os.LinkError{Op: "rename", Old: oldpath, New: newpath, Err: syscall.EXDEV}
118 }
119 t.Cleanup(func() { replaceRetryBase, maxReplaceRetries, renameFile = oldBase, oldMax, oldRename })
120
121 dir := t.TempDir()
122 tmp := filepath.Join(dir, "x.tmp")
123 dest := filepath.Join(dir, "x.txt")
124 if err := os.WriteFile(tmp, []byte("new"), 0o644); err != nil {
125 t.Fatal(err)
126 }
127 if err := os.WriteFile(dest, []byte("old"), 0o644); err != nil {
128 t.Fatal(err)
129 }
130 start := time.Now()
131 if err := ReplaceFile(tmp, dest); err != nil {
132 t.Fatalf("ReplaceFile should succeed via the copy fallback: %v", err)
133 }
134 if renameCalls != 1 {
135 t.Errorf("rename attempts = %d, want 1 — a structurally impossible rename must not be retried", renameCalls)
136 }
137 if elapsed := time.Since(start); elapsed > time.Second {
138 t.Errorf("cross-device fallback took %v — it slept through the retry ladder", elapsed)
139 }
140 if b, _ := os.ReadFile(dest); string(b) != "new" {
141 t.Errorf("dest = %q, want the new content from the copy fallback", b)
142 }
143 if fileExists(tmp) {
144 t.Error("tmp should be consumed by the copy fallback")
145 }
146 }
147
148 func TestAtomicWriteFileStrictSyncsParentDir(t *testing.T) {
149 calls := 0
150 restore := SetSyncParentDirForTest(func(path string) error {
151 calls++
152 return syncParentDir(path)
153 })
154 t.Cleanup(restore)
155
156 dir := t.TempDir()
157 dest := filepath.Join(dir, "pointer.json")
158 if err := AtomicWriteFileStrict(dest, []byte(`{"ok":true}`), 0o644); err != nil {
159 t.Fatalf("AtomicWriteFileStrict: %v", err)
160 }
161 if calls != 1 {
162 t.Fatalf("parent dir sync calls = %d, want 1", calls)
163 }
164 if got, err := os.ReadFile(dest); err != nil || string(got) != `{"ok":true}` {
165 t.Fatalf("dest = %q, err=%v", got, err)
166 }
167 }
168
169 func TestAtomicWriteFileStrictDirSyncFailureStillPublishes(t *testing.T) {
170 // Rename already committed the new file; dir fsync failure must not look
171 // like a pre-publish failure or callers will fork memory from disk.
172 restore := SetSyncParentDirForTest(func(string) error {
173 return errors.New("injected parent dir fsync failure")
174 })
175 t.Cleanup(restore)
176
177 dir := t.TempDir()
178 dest := filepath.Join(dir, "pointer.json")
179 if err := os.WriteFile(dest, []byte("old"), 0o644); err != nil {
180 t.Fatal(err)
181 }
182 if err := AtomicWriteFileStrict(dest, []byte("new"), 0o644); err != nil {
183 t.Fatalf("post-publish dir sync must not fail the write: %v", err)
184 }
185 if got, err := os.ReadFile(dest); err != nil || string(got) != "new" {
186 t.Fatalf("dest = %q, err=%v, want published new content", got, err)
187 }
188 }
189
190 func TestAtomicWriteFileStrictSyncsRelativeParentDir(t *testing.T) {
191 // Relative destinations resolve to "."; that parent must still be synced.
192 cwd, err := os.Getwd()
193 if err != nil {
194 t.Fatal(err)
195 }
196 dir := t.TempDir()
197 if err := os.Chdir(dir); err != nil {
198 t.Fatal(err)
199 }
200 t.Cleanup(func() { _ = os.Chdir(cwd) })
201
202 synced := ""
203 restore := SetSyncParentDirForTest(func(path string) error {
204 synced = filepath.Dir(path)
205 return syncParentDir(path)
206 })
207 t.Cleanup(restore)
208
209 if err := AtomicWriteFileStrict("pointer.json", []byte(`{"ok":true}`), 0o644); err != nil {
210 t.Fatal(err)
211 }
212 if synced != "." {
213 t.Fatalf("synced parent = %q, want \".\"", synced)
214 }
215 if got, err := os.ReadFile("pointer.json"); err != nil || string(got) != `{"ok":true}` {
216 t.Fatalf("dest = %q, err=%v", got, err)
217 }
218 }
219
220 func TestAtomicWriteFileDoesNotRequireParentDirSync(t *testing.T) {
221 // Non-strict path keeps the historical file-only durability contract.
222 restore := SetSyncParentDirForTest(func(string) error {
223 t.Fatal("AtomicWriteFile must not require parent-dir sync")
224 return nil
225 })
226 t.Cleanup(restore)
227
228 path := filepath.Join(t.TempDir(), "config.toml")
229 if err := AtomicWriteFile(path, []byte("ok"), 0o644); err != nil {
230 t.Fatal(err)
231 }
232 }
233
234 func TestAtomicWriteFileStrictCrossDeviceKeepsExistingDestination(t *testing.T) {
235 testStrictCrossDeviceKeepsExistingDestination(t, AtomicWriteFileStrict)
236 }
237
238 func TestAtomicOverwriteFileStrictCrossDeviceKeepsExistingDestination(t *testing.T) {
239 testStrictCrossDeviceKeepsExistingDestination(t, AtomicOverwriteFileStrict)
240 }
241
242 func testStrictCrossDeviceKeepsExistingDestination(t *testing.T, write func(string, []byte, os.FileMode) error) {
243 t.Helper()
244 oldRename := renameFile
245 renameFile = func(oldpath, newpath string) error {
246 return &os.LinkError{Op: "rename", Old: oldpath, New: newpath, Err: syscall.EXDEV}
247 }
248 t.Cleanup(func() { renameFile = oldRename })
249
250 dir := t.TempDir()
251 dest := filepath.Join(dir, "current.json")
252 if err := os.WriteFile(dest, []byte("old-pointer"), 0o644); err != nil {
253 t.Fatal(err)
254 }
255 if err := write(dest, []byte("new-pointer"), 0o644); err == nil {
256 t.Fatal("strict atomic write accepted a cross-device rename")
257 }
258 if got, err := os.ReadFile(dest); err != nil || string(got) != "old-pointer" {
259 t.Fatalf("destination changed after strict replace failure: %q, %v", got, err)
260 }
261 entries, err := os.ReadDir(dir)
262 if err != nil {
263 t.Fatal(err)
264 }
265 if len(entries) != 1 || entries[0].Name() != "current.json" {
266 t.Fatalf("strict write left temporary files: %v", entries)
267 }
268 }
269
270 func TestCopyOntoOverwritesAndPreservesMode(t *testing.T) {
271 dir := t.TempDir()
272 tmp := filepath.Join(dir, "x.tmp")
273 dest := filepath.Join(dir, "x.txt")
274 if err := os.WriteFile(tmp, []byte("new"), 0o600); err != nil {
275 t.Fatal(err)
276 }
277 if err := os.WriteFile(dest, []byte("old-and-longer"), 0o644); err != nil {
278 t.Fatal(err)
279 }
280 if err := copyOnto(tmp, dest); err != nil {
281 t.Fatal(err)
282 }
283 if b, _ := os.ReadFile(dest); string(b) != "new" {
284 t.Errorf("dest = %q, want new (fully overwritten)", b)
285 }
286 if _, err := os.Stat(tmp); !os.IsNotExist(err) {
287 t.Error("tmp should be removed after copyOnto")
288 }
289 // Mode preservation is meaningful on Unix; Windows only tracks the read-only bit.
290 if info, err := os.Stat(dest); err == nil && info.Mode().Perm() != 0o600 {
291 t.Logf("dest mode = %o (want 0600 on Unix)", info.Mode().Perm())
292 }
293 }
294
295 func TestAtomicWriteFileReplacesExisting(t *testing.T) {
296 dir := t.TempDir()
297 path := filepath.Join(dir, "config.toml")
298 if err := os.WriteFile(path, []byte("old"), 0o644); err != nil {
299 t.Fatal(err)
300 }
301 if err := AtomicWriteFile(path, []byte("new-content"), 0o600); err != nil {
302 t.Fatalf("AtomicWriteFile: %v", err)
303 }
304 got, err := os.ReadFile(path)
305 if err != nil {
306 t.Fatal(err)
307 }
308 if string(got) != "new-content" {
309 t.Fatalf("content = %q, want %q", got, "new-content")
310 }
311 info, err := os.Stat(path)
312 if err != nil {
313 t.Fatal(err)
314 }
315 if perm := info.Mode().Perm(); runtime.GOOS != "windows" && perm != 0o600 {
316 t.Fatalf("perm = %o, want 600", perm)
317 }
318 // No leftover tmp files in the directory.
319 entries, _ := os.ReadDir(dir)
320 for _, e := range entries {
321 if e.Name() != "config.toml" {
322 t.Fatalf("unexpected leftover file: %s", e.Name())
323 }
324 }
325 }
326
327 func TestAtomicWriteFileCreatesParentDir(t *testing.T) {
328 dir := t.TempDir()
329 path := filepath.Join(dir, "nested", "deep", "creds")
330 if err := AtomicWriteFile(path, []byte("x"), 0o600); err != nil {
331 t.Fatalf("AtomicWriteFile into missing dir: %v", err)
332 }
333 if _, err := os.Stat(path); err != nil {
334 t.Fatalf("file not created: %v", err)
335 }
336 }
337
338 func TestAtomicCreateFileNeverOverwritesExisting(t *testing.T) {
339 dir := t.TempDir()
340 path := filepath.Join(dir, "config.toml")
341 if err := os.WriteFile(path, []byte("concurrent"), 0o600); err != nil {
342 t.Fatal(err)
343 }
344 if err := AtomicCreateFile(path, []byte("confirmed"), 0o600); err == nil {
345 t.Fatal("AtomicCreateFile overwrote an existing target")
346 }
347 if got, err := os.ReadFile(path); err != nil || string(got) != "concurrent" {
348 t.Fatalf("existing target changed: %q, %v", got, err)
349 }
350 entries, err := os.ReadDir(dir)
351 if err != nil {
352 t.Fatal(err)
353 }
354 if len(entries) != 1 || entries[0].Name() != "config.toml" {
355 t.Fatalf("temporary files leaked: %v", entries)
356 }
357 }
358
359 func TestAtomicCreateFilePublishesCompleteContent(t *testing.T) {
360 path := filepath.Join(t.TempDir(), "nested", "config.toml")
361 if err := AtomicCreateFile(path, []byte("confirmed"), 0o600); err != nil {
362 t.Fatal(err)
363 }
364 if got, err := os.ReadFile(path); err != nil || string(got) != "confirmed" {
365 t.Fatalf("created target = %q, %v", got, err)
366 }
367 }
368
369 func TestAtomicOverwriteFileKeepsExecutableBit(t *testing.T) {
370 if runtime.GOOS == "windows" {
371 t.Skip("windows does not carry a POSIX executable bit")
372 }
373 path := filepath.Join(t.TempDir(), "build.sh")
374 if err := os.WriteFile(path, []byte("#!/bin/sh\nold\n"), 0o755); err != nil {
375 t.Fatal(err)
376 }
377 if err := AtomicOverwriteFile(path, []byte("#!/bin/sh\nnew\n"), 0o644); err != nil {
378 t.Fatalf("AtomicOverwriteFile: %v", err)
379 }
380 info, err := os.Stat(path)
381 if err != nil {
382 t.Fatal(err)
383 }
384 if perm := info.Mode().Perm(); perm != 0o755 {
385 t.Fatalf("perm = %o, want 755 — the script lost its executable bit", perm)
386 }
387 }
388
389 func TestAtomicOverwriteFileWritesThroughSymlink(t *testing.T) {
390 dir := t.TempDir()
391 target := filepath.Join(dir, "real.txt")
392 link := filepath.Join(dir, "link.txt")
393 if err := os.WriteFile(target, []byte("old"), 0o644); err != nil {
394 t.Fatal(err)
395 }
396 if err := os.Symlink(target, link); err != nil {
397 t.Skipf("symlinks unavailable: %v", err)
398 }
399
400 if err := AtomicOverwriteFile(link, []byte("new"), 0o644); err != nil {
401 t.Fatalf("AtomicOverwriteFile: %v", err)
402 }
403 if info, err := os.Lstat(link); err != nil || info.Mode()&os.ModeSymlink == 0 {
404 t.Fatalf("link was replaced by a regular file: mode=%v err=%v", info.Mode(), err)
405 }
406 if got, err := os.ReadFile(target); err != nil || string(got) != "new" {
407 t.Fatalf("target content = %q, %v — the write did not reach the link target", got, err)
408 }
409 }
410
411 func TestAtomicOverwriteFileUsesDefaultPermForNewFile(t *testing.T) {
412 path := filepath.Join(t.TempDir(), "fresh.txt")
413 if err := AtomicOverwriteFile(path, []byte("x"), 0o600); err != nil {
414 t.Fatal(err)
415 }
416 info, err := os.Stat(path)
417 if err != nil {
418 t.Fatal(err)
419 }
420 if perm := info.Mode().Perm(); runtime.GOOS != "windows" && perm != 0o600 {
421 t.Fatalf("perm = %o, want 600", perm)
422 }
423 }
424
425 // The claim's whole value is that exactly one caller can win it, so the copy
426 // fallback ReplaceFile takes for undoable renames must never apply here: a copy
427 // would leave both claimants holding the record.
428 func TestClaimRenameNeverFallsBackToCopy(t *testing.T) {
429 oldBase, oldMax := replaceRetryBase, maxReplaceRetries
430 replaceRetryBase, maxReplaceRetries = 0, 2
431 t.Cleanup(func() { replaceRetryBase, maxReplaceRetries = oldBase, oldMax })
432
433 dir := t.TempDir()
434 src := filepath.Join(dir, "record.json")
435 if err := os.WriteFile(src, []byte("payload"), 0o644); err != nil {
436 t.Fatal(err)
437 }
438 dst := filepath.Join(dir, "blocked")
439 if err := os.Mkdir(dst, 0o755); err != nil {
440 t.Fatal(err)
441 }
442 if err := ClaimRename(src, dst); err == nil {
443 t.Fatal("want an error when the claim cannot rename")
444 }
445 if entries, err := os.ReadDir(dst); err != nil || len(entries) != 0 {
446 t.Fatalf("destination directory = %v, %v — the claim copied into it", entries, err)
447 }
448 if got, err := os.ReadFile(src); err != nil || string(got) != "payload" {
449 t.Fatalf("source = %q, %v — a failed claim must leave the record in place", got, err)
450 }
451 }
452
453 // A source that has already been claimed by someone else is the loser of a
454 // race, not a lock worth waiting out.
455 func TestClaimRenameDoesNotRetryWhenSourceIsGone(t *testing.T) {
456 oldBase := replaceRetryBase
457 replaceRetryBase = 10 * time.Second
458 t.Cleanup(func() { replaceRetryBase = oldBase })
459
460 dir := t.TempDir()
461 start := time.Now()
462 err := ClaimRename(filepath.Join(dir, "taken.json"), filepath.Join(dir, "taken.json.claimed"))
463 if err == nil {
464 t.Fatal("want an error when the record is already claimed")
465 }
466 if elapsed := time.Since(start); elapsed > time.Second {
467 t.Errorf("lost claim took %v — it retried a race it had already lost", elapsed)
468 }
469 }
470
470 lines GO