MCPcopy Create free account
hub / github.com/diillson/chatcli / atomicWriteFile

Function atomicWriteFile

cli/workspace/memory/persist.go:30–58  ·  view source on GitHub ↗

atomicWriteFile writes data to path via a same-directory temp file and an atomic rename, so readers (and crashes) only ever observe the old or the new content — never a torn write.

(path string, data []byte, perm os.FileMode)

Source from the content-addressed store, hash-verified

28// atomic rename, so readers (and crashes) only ever observe the old or the new
29// content — never a torn write.
30func atomicWriteFile(path string, data []byte, perm os.FileMode) error {
31 dir := filepath.Dir(path)
32 tmp, err := os.CreateTemp(dir, filepath.Base(path)+".*.tmp")
33 if err != nil {
34 return err
35 }
36 tmpName := tmp.Name()
37 cleanup := func() { _ = os.Remove(tmpName) }
38
39 if _, err := tmp.Write(data); err != nil {
40 _ = tmp.Close()
41 cleanup()
42 return err
43 }
44 if err := tmp.Chmod(perm); err != nil {
45 _ = tmp.Close()
46 cleanup()
47 return err
48 }
49 if err := tmp.Close(); err != nil {
50 cleanup()
51 return err
52 }
53 if err := os.Rename(tmpName, path); err != nil {
54 cleanup()
55 return err
56 }
57 return nil
58}
59
60// quarantineCorrupt moves an unparseable store file aside as
61// "<path>.corrupt" (or a timestamped variant when a previous quarantine

Callers 11

appendFactsToArchiveFunction · 0.85
persistLockedMethod · 0.85
markCompactedMethod · 0.85
writeMemoryMDMethod · 0.85
persistMethod · 0.85
persistMethod · 0.85
persistMethod · 0.85
writeDigestMethod · 0.85
persistMethod · 0.85

Calls 5

NameMethod · 0.65
RemoveMethod · 0.65
WriteMethod · 0.65
CloseMethod · 0.65
DirMethod · 0.45

Tested by 1