change the atomic write functions to work with readers

This commit is contained in:
Kovid Goyal
2024-07-22 15:01:19 +05:30
parent d31c48092a
commit c906314974
7 changed files with 15 additions and 11 deletions

View File

@@ -5,6 +5,7 @@ package utils
import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
@@ -39,7 +40,7 @@ func AtomicCreateSymlink(oldname, newname string) (err error) {
}
}
func AtomicWriteFile(path string, data []byte, perm os.FileMode) (err error) {
func AtomicWriteFile(path string, data io.Reader, perm os.FileMode) (err error) {
npath, err := filepath.EvalSymlinks(path)
if errors.Is(err, fs.ErrNotExist) {
err = nil
@@ -60,7 +61,7 @@ func AtomicWriteFile(path string, data []byte, perm os.FileMode) (err error) {
removed = true
}
}()
_, err = f.Write(data)
_, err = io.Copy(f, data)
if err == nil {
err = f.Chmod(perm)
if err == nil {
@@ -76,7 +77,7 @@ func AtomicWriteFile(path string, data []byte, perm os.FileMode) (err error) {
return
}
func AtomicUpdateFile(path string, data []byte, perms ...fs.FileMode) (err error) {
func AtomicUpdateFile(path string, data io.Reader, perms ...fs.FileMode) (err error) {
perm := fs.FileMode(0o644)
if len(perms) > 0 {
perm = perms[0]

View File

@@ -3,6 +3,7 @@
package utils
import (
"bytes"
"encoding/json"
"fmt"
"os"
@@ -31,7 +32,7 @@ func (self *CachedValues[T]) Load() T {
func (self *CachedValues[T]) Save() {
raw, err := json.Marshal(self.Opts)
if err == nil {
AtomicUpdateFile(self.Path(), raw, 0o600)
AtomicUpdateFile(self.Path(), bytes.NewReader(raw), 0o600)
}
}