feat(platform): add WriteUserFile atomic user-space writer

This commit is contained in:
MDietermann
2026-04-14 09:53:03 +02:00
parent b9f9f0306d
commit adfc9232ac
2 changed files with 79 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
package platform
import (
"os"
"path/filepath"
)
// WriteUserFile writes data to path atomically (tmp + rename) with the
// given mode, creating any missing parent directories (mode 0755).
// Intended for user-owned destinations under $HOME — no sudo.
func WriteUserFile(path string, data []byte, mode os.FileMode) error {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
tmp := path + ".tmp"
if err := os.WriteFile(tmp, data, mode); err != nil {
os.Remove(tmp)
return err
}
if err := os.Rename(tmp, path); err != nil {
os.Remove(tmp)
return err
}
return nil
}