clipboard: Cleanup atomic replacement of output files

Follow up to the merge of #10738. Use utils.AtomicUpdateFile to move
downloaded data into place instead of hand rolling the stat/chmod/rename
sequence. This fixes:

- Permissions of existing destination files not being preserved, as the
  chmod was applied to an already closed file
- An empty file being left at the destination if the rename failed, as
  the permissions probe opened the destination with O_CREATE
- Symlinks at the destination being replaced by regular files
- No fsync before the rename

Also encode converted images into memory and write them atomically, so
a failed encode no longer truncates and then deletes an existing
destination file.
This commit is contained in:
Kovid Goyal 2026-10-25 20:33:25 +05:30
parent 41ca405b98
commit 68f242b7d9
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 117 additions and 49 deletions

View file

@ -383,7 +383,8 @@ Detailed list of changes
transfer no longer leaves a truncated file behind (:pull:`10713`)
- Clipboard kitten: Fix a race when replacing an existing output file and
losing the original file if renaming the temporary file fails
losing the original file if writing it fails. Also preserve the permissions
of and follow symlinks at existing output files (:pull:`10738`)
- Kittens: Fix laggy mouse selection and failures comparing remote
directories in the diff kitten, Unicode quotes not being removed from around

View file

@ -84,25 +84,20 @@ func (self *Output) add_data(data []byte) {
}
func (self *Output) write_image(img image.Image) (err error) {
var output *os.File
if self.arg_is_stream {
output = os.Stdout
output := os.Stdout
if self.arg == "/dev/stderr" {
output = os.Stderr
}
} else {
output, err = os.Create(self.arg)
if err != nil {
return err
}
return images.Encode(output, img, self.mime_type)
}
defer func() {
output.Close()
if err != nil && !self.arg_is_stream {
os.Remove(output.Name())
}
}()
return images.Encode(output, img, self.mime_type)
// Encode into memory first so that an encoding failure leaves any
// existing destination file untouched.
var buf bytes.Buffer
if err = images.Encode(&buf, img, self.mime_type); err != nil {
return err
}
return utils.AtomicUpdateFile(self.arg, &buf)
}
func (self *Output) commit() {
@ -112,30 +107,23 @@ func (self *Output) commit() {
if self.image_needs_conversion {
self.dest.Seek(0, io.SeekStart)
img, _, err := image.Decode(self.dest)
self.dest.Close()
os.Remove(self.dest.Name())
self.cleanup()
if err == nil {
err = self.write_image(img)
}
if err != nil {
self.err = fmt.Errorf("Failed to encode image data to %s with error: %w", self.mime_type, err)
}
} else {
self.dest.Close()
if !self.is_stream {
f, err := os.OpenFile(self.arg, os.O_CREATE|os.O_RDONLY, 0666)
if err == nil {
fi, err := f.Stat()
if err == nil {
self.dest.Chmod(fi.Mode().Perm())
}
f.Close()
}
self.err = os.Rename(self.dest.Name(), self.arg)
if self.err != nil {
os.Remove(self.dest.Name())
self.err = fmt.Errorf("Failed to rename temporary file used for downloading to destination: %s with error: %w", self.arg, self.err)
}
} else if !self.is_stream {
// Copy the temporary file into place atomically, preserving the
// permissions of and following symlinks at the destination.
_, err := self.dest.Seek(0, io.SeekStart)
if err == nil {
err = utils.AtomicUpdateFile(self.arg, self.dest)
}
self.cleanup()
if err != nil {
self.err = fmt.Errorf("Failed to write downloaded data to destination: %s with error: %w", self.arg, err)
}
}
self.dest = nil

View file

@ -4,10 +4,12 @@ package clipboard
import (
"bytes"
"errors"
"fmt"
"image"
"image/png"
"os"
"path/filepath"
"slices"
"testing"
)
@ -15,7 +17,7 @@ func TestClipboardOutputCommitAtomic(t *testing.T) {
dest := filepath.Join(t.TempDir(), "output")
old_data := bytes.Repeat([]byte("old contents\n"), 1024)
new_data := bytes.Repeat([]byte("new contents\n"), 1024)
if err := os.WriteFile(dest, old_data, 0o600); err != nil {
if err := os.WriteFile(dest, old_data, 0o640); err != nil {
t.Fatal(err)
}
@ -72,17 +74,33 @@ func TestClipboardOutputCommitAtomic(t *testing.T) {
if !bytes.Equal(actual, data) {
t.Fatal("Committed file has incorrect contents")
}
if st, err := os.Stat(dest); err != nil {
t.Fatal(err)
} else if st.Mode().Perm() != 0o640 {
t.Fatalf("Committed file has incorrect permissions: %o", st.Mode().Perm())
}
if _, err := os.Stat(temp_name); !os.IsNotExist(err) {
t.Fatalf("Temporary file still exists after commit: %v", err)
}
}
}
func TestClipboardOutputCommitRenameFailure(t *testing.T) {
func list_dir(t *testing.T, dir string) (ans []string) {
entries, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
for _, e := range entries {
ans = append(ans, e.Name())
}
return
}
func TestClipboardOutputCommitFailure(t *testing.T) {
dir := t.TempDir()
// Renaming a file over a non-empty directory fails even when running as root
dest := filepath.Join(dir, "output")
old_data := []byte("old contents")
if err := os.WriteFile(dest, old_data, 0o600); err != nil {
if err := os.MkdirAll(filepath.Join(dest, "child"), 0o755); err != nil {
t.Fatal(err)
}
output := Output{arg: dest}
@ -91,19 +109,80 @@ func TestClipboardOutputCommitRenameFailure(t *testing.T) {
if output.err != nil {
t.Fatal(output.err)
}
// Removing the source forces rename to fail even when running as root.
if err := os.Remove(output.dest.Name()); err != nil {
t.Fatal(err)
}
output.commit()
if !errors.Is(output.err, os.ErrNotExist) {
t.Fatalf("Expected rename to fail with a missing source, got: %v", output.err)
if output.err == nil {
t.Fatal("Expected commit to fail when destination is a non-empty directory")
}
actual, err := os.ReadFile(dest)
if err != nil {
t.Fatalf("Original destination lost after failed rename: %v", err)
if st, err := os.Stat(filepath.Join(dest, "child")); err != nil || !st.IsDir() {
t.Fatalf("Original destination changed after failed commit: %v", err)
}
if !bytes.Equal(actual, old_data) {
t.Fatalf("Original destination changed after failed rename: %q", actual)
if names := list_dir(t, dir); !slices.Equal(names, []string{"output"}) {
t.Fatalf("Stray files left behind after failed commit: %v", names)
}
}
func TestClipboardOutputCommitNewFile(t *testing.T) {
dir := t.TempDir()
dest := filepath.Join(dir, "output")
output := Output{arg: dest}
output.add_data([]byte("new contents"))
output.commit()
if output.err != nil {
t.Fatal(output.err)
}
if actual, err := os.ReadFile(dest); err != nil || string(actual) != "new contents" {
t.Fatalf("Incorrect contents in new file: %q %v", actual, err)
}
if names := list_dir(t, dir); !slices.Equal(names, []string{"output"}) {
t.Fatalf("Stray files left behind after commit: %v", names)
}
}
func TestClipboardOutputCommitSymlink(t *testing.T) {
dir := t.TempDir()
target := filepath.Join(dir, "target")
dest := filepath.Join(dir, "link")
if err := os.WriteFile(target, []byte("old contents"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.Symlink(target, dest); err != nil {
t.Fatal(err)
}
output := Output{arg: dest}
output.add_data([]byte("new contents"))
output.commit()
if output.err != nil {
t.Fatal(output.err)
}
if st, err := os.Lstat(dest); err != nil || st.Mode()&os.ModeSymlink == 0 {
t.Fatalf("Symlink at destination was replaced: %v", err)
}
if actual, err := os.ReadFile(target); err != nil || string(actual) != "new contents" {
t.Fatalf("Symlink target not updated: %q %v", actual, err)
}
}
func TestClipboardOutputImageEncodeFailure(t *testing.T) {
dest := filepath.Join(t.TempDir(), "output")
old_data := []byte("old contents")
if err := os.WriteFile(dest, old_data, 0o600); err != nil {
t.Fatal(err)
}
var png_data bytes.Buffer
if err := png.Encode(&png_data, image.NewRGBA(image.Rect(0, 0, 1, 1))); err != nil {
t.Fatal(err)
}
output := Output{arg: dest, image_needs_conversion: true, mime_type: "image/x-unsupported"}
defer output.cleanup()
output.add_data(png_data.Bytes())
if output.err != nil {
t.Fatal(output.err)
}
output.commit()
if output.err == nil {
t.Fatal("Expected commit to fail with an unsupported image type")
}
if actual, err := os.ReadFile(dest); err != nil || !bytes.Equal(actual, old_data) {
t.Fatalf("Original destination changed after failed image encode: %q %v", actual, err)
}
}