Merge branch 'codex/clipboard-symlink-parent-output' of https://github.com/hezhizhen/kitty

This commit is contained in:
Kovid Goyal 2026-10-26 15:08:31 +05:30
commit 4a4bcbc3c1
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 95 additions and 4 deletions

View file

@ -395,6 +395,10 @@ Detailed list of changes
downloaded only once and images are decoded only once for all such outputs.
Specifying the same output file twice is now an error (:pull:`10744`)
- Clipboard kitten: Correctly detect duplicate output files when a path contains
a symlink followed by ``..``, avoiding overwrites of the same target and
erroneous rejection of distinct output files
- Kittens: When saving files, such as themes, config files or clipboard
output, write to the target of a symlink even when the target does not yet
exist, instead of replacing the symlink with a regular file (:pull:`10740`)

View file

@ -424,11 +424,14 @@ func (self read_outputs) cleanup() {
}
}
// Return the path an output file will be written to, following symlinks the
// same way utils.AtomicUpdateFile does. The path is made absolute first so
// that symlinks in the working directory are resolved as well.
// Resolve the path in the same order as utils.AtomicUpdateFile, then resolve
// symlinks in the absolute working-directory prefix as well.
func resolved_output_path(path string) (string, error) {
path, err := filepath.Abs(path)
path, err := utils.EvalSymlinksThatExist(path)
if err != nil {
return "", err
}
path, err = filepath.Abs(path)
if err != nil {
return "", err
}

View file

@ -260,6 +260,90 @@ func TestClipboardReadDuplicateOutputFiles(t *testing.T) {
}
}
func TestClipboardReadDuplicateOutputFilesSymlinkParent(t *testing.T) {
for _, existing := range []bool{false, true} {
for _, absolute_link := range []bool{false, true} {
t.Run(fmt.Sprintf("existing=%t/absolute_link=%t", existing, absolute_link), func(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
if err := os.MkdirAll("real/sub", 0o755); err != nil {
t.Fatal(err)
}
link := "real/sub"
if absolute_link {
link = filepath.Join(dir, link)
}
if err := os.Symlink(link, "link"); err != nil {
t.Fatal(err)
}
if existing {
for _, path := range []string{"target", "real/target"} {
if err := os.WriteFile(path, []byte("old contents"), 0o600); err != nil {
t.Fatal(err)
}
}
}
// Concatenate to preserve link/.. in the absolute path as well.
for _, path := range []string{"link/../target", dir + "/link/../target"} {
outputs := []*Output{{arg: path}, {arg: "real/target"}}
if err := check_for_duplicate_output_files(outputs); err == nil {
t.Errorf("Duplicate output files not rejected: %s and real/target", path)
}
outputs[1].arg = "target"
if err := check_for_duplicate_output_files(outputs); err != nil {
t.Errorf("Distinct output files rejected: %s and target: %s", path, err)
}
}
})
}
}
}
func TestClipboardReadDuplicateOutputFilesSymlinkCwd(t *testing.T) {
for _, existing := range []bool{false, true} {
t.Run(fmt.Sprintf("existing=%t", existing), func(t *testing.T) {
dir := t.TempDir()
if err := os.MkdirAll(filepath.Join(dir, "real", "sub"), 0o755); err != nil {
t.Fatal(err)
}
link := filepath.Join(dir, "link")
if err := os.Symlink("real/sub", link); err != nil {
t.Fatal(err)
}
t.Chdir(link)
t.Setenv("PWD", link)
paths := []string{filepath.Join(dir, "target"), filepath.Join(dir, "real", "target")}
if existing {
for _, path := range paths {
if err := os.WriteFile(path, []byte("old contents"), 0o600); err != nil {
t.Fatal(err)
}
}
}
output := &Output{arg: "../target"}
defer output.cleanup()
output.add_data([]byte("clipboard contents"))
output.commit()
if output.err != nil {
t.Fatal(output.err)
}
// Compare detection with the actual write, including when PWD
// names a symlink rather than the physical working directory.
for _, path := range paths {
data, err := os.ReadFile(path)
if err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}
written := string(data) == "clipboard contents"
err = check_for_duplicate_output_files([]*Output{output, {arg: path}})
if (err != nil) != written {
t.Errorf("Duplicate detection disagrees with write to %s: written=%t error=%v", path, written, err)
}
}
})
}
}
func TestClipboardOutputCommitAtomic(t *testing.T) {
dest := filepath.Join(t.TempDir(), "output")
old_data := bytes.Repeat([]byte("old contents\n"), 1024)