Mining/pkg/mining/atomic_write_file.go
Virgil aea1fc1d03
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run
AX: clarify helper names and path examples
2026-04-04 06:55:27 +00:00

49 lines
1.2 KiB
Go

package mining
import (
"os"
"path/filepath"
)
// AtomicWriteFile("/home/alice/.config/lethean-desktop/miners/installed-miners.json", data, 0600)
func AtomicWriteFile(filePath string, data []byte, perm os.FileMode) error {
directoryPath := filepath.Dir(filePath)
temporaryFile, err := os.CreateTemp(directoryPath, ".tmp-*")
if err != nil {
return ErrInternal("create temp file").WithCause(err)
}
temporaryFilePath := temporaryFile.Name()
success := false
defer func() {
if !success {
os.Remove(temporaryFilePath)
}
}()
if _, err := temporaryFile.Write(data); err != nil {
temporaryFile.Close()
return ErrInternal("write temp file").WithCause(err)
}
if err := temporaryFile.Sync(); err != nil {
temporaryFile.Close()
return ErrInternal("sync temp file").WithCause(err)
}
if err := temporaryFile.Close(); err != nil {
return ErrInternal("close temp file").WithCause(err)
}
if err := os.Chmod(temporaryFilePath, perm); err != nil {
return ErrInternal("set file permissions").WithCause(err)
}
if err := os.Rename(temporaryFilePath, filePath); err != nil {
return ErrInternal("rename temp file").WithCause(err)
}
success = true
return nil
}