mirror of
https://github.com/kovidgoyal/kitty
synced 2026-06-06 01:05:48 +02:00
change the atomic write functions to work with readers
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
package edit_in_kitty
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -212,7 +213,7 @@ func edit_in_kitty(path string, opts *Options) (err error) {
|
||||
add_encoded("file_data", utils.UnsafeBytesToString(file_data))
|
||||
fmt.Println("Waiting for editing to be completed, press Esc to abort...")
|
||||
write_data := func(data_type string, rdata []byte) (err error) {
|
||||
err = utils.AtomicWriteFile(path, rdata, fs.FileMode(s.Mode).Perm())
|
||||
err = utils.AtomicWriteFile(path, bytes.NewReader(rdata), fs.FileMode(s.Mode).Perm())
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Failed to write data to %s with error: %w", path, err)
|
||||
}
|
||||
|
||||
@@ -344,7 +344,7 @@ func (self Patcher) Patch(path, sentinel, content string, settings_to_comment_ou
|
||||
_ = os.WriteFile(backup_path+".bak", raw, self.Mode)
|
||||
}
|
||||
|
||||
return true, utils.AtomicUpdateFile(path, nraw, self.Mode)
|
||||
return true, utils.AtomicUpdateFile(path, bytes.NewReader(nraw), self.Mode)
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
@@ -344,7 +344,7 @@ func set_comment_in_zip_file(path string, comment string) error {
|
||||
}
|
||||
}
|
||||
dest.Close()
|
||||
return utils.AtomicUpdateFile(path, buf.Bytes(), 0o644)
|
||||
return utils.AtomicUpdateFile(path, bytes.NewReader(buf.Bytes()), 0o644)
|
||||
}
|
||||
|
||||
func fetch_cached(name, url, cache_path string, max_cache_age time.Duration) (string, error) {
|
||||
@@ -584,7 +584,7 @@ func (self *Theme) SaveInDir(dirpath string) (err error) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return utils.AtomicUpdateFile(path, utils.UnsafeStringToBytes(code), 0o644)
|
||||
return utils.AtomicUpdateFile(path, bytes.NewReader(utils.UnsafeStringToBytes(code)), 0o644)
|
||||
}
|
||||
|
||||
func (self *Theme) SaveInConf(config_dir, reload_in, config_file_name string) (err error) {
|
||||
@@ -594,7 +594,7 @@ func (self *Theme) SaveInConf(config_dir, reload_in, config_file_name string) (e
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = utils.AtomicUpdateFile(path, utils.UnsafeStringToBytes(code), 0o644)
|
||||
err = utils.AtomicUpdateFile(path, bytes.NewReader(utils.UnsafeStringToBytes(code)), 0o644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ func extract_files(match, dest_dir string) (err error) {
|
||||
if existing, rerr := os.ReadFile(dest); rerr == nil && bytes.Equal(existing, entry.Data) {
|
||||
continue
|
||||
}
|
||||
if err = utils.AtomicWriteFile(dest, entry.Data, 0o644); err != nil {
|
||||
if err = utils.AtomicWriteFile(dest, bytes.NewReader(entry.Data), 0o644); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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