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

Function atomicWriteFile

internal/cli/session_arm_state.go:290–310  ·  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

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