mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-23 08:47:47 +02:00
change the atomic write functions to work with readers
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user