MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / atomicWriteFile

Function atomicWriteFile

internal/cli/session_arm_state.go:291–311  ·  view source on GitHub ↗

atomicWriteFile writes data to a temp file in path's directory and renames it into place, so a concurrent reader sees either the old file or the complete new one, never a partial write.

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

Source from the content-addressed store, hash-verified

289// renames it into place, so a concurrent reader sees either the old file
290// or the complete new one, never a partial write.
291func atomicWriteFile(path string, data []byte, perm os.FileMode) error {
292 dir := filepath.Dir(path)
293 tmp, err := os.CreateTemp(dir, ".arm-*.tmp")
294 if err != nil {
295 return err
296 }
297 tmpName := tmp.Name()
298 defer os.Remove(tmpName) // no-op after a successful rename
299 if err := tmp.Chmod(perm); err != nil {
300 tmp.Close()
301 return err
302 }
303 if _, err := tmp.Write(data); err != nil {
304 tmp.Close()
305 return err
306 }
307 if err := tmp.Close(); err != nil {
308 return err
309 }
310 return os.Rename(tmpName, path)
311}
312
313// RemoveArmState disarms the current session by removing its arm-state
314// file. It reports whether a file was actually present (so `pad session

Callers 5

writeArmStateFileFunction · 0.85
MarkFirstConnectFunction · 0.85
sessionWorkerIDFunction · 0.85
RegisterSessionFunction · 0.85
WriteChatDirectionFunction · 0.85

Calls 4

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

Tested by

no test coverage detected