mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-19 06:35:15 +00:00
Merge branch 'codex/clipboard-case-alias-outputs' of https://github.com/hezhizhen/kitty
This commit is contained in:
commit
feeb87b1c4
3 changed files with 91 additions and 0 deletions
|
|
@ -399,6 +399,9 @@ Detailed list of changes
|
|||
a symlink followed by ``..``, avoiding overwrites of the same target and
|
||||
erroneous rejection of distinct output files
|
||||
|
||||
- Clipboard kitten: Reject duplicate output files that differ only in case on
|
||||
case-insensitive filesystems, preventing one output from overwriting another
|
||||
|
||||
- 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`)
|
||||
|
|
|
|||
|
|
@ -440,6 +440,7 @@ func resolved_output_path(path string) (string, error) {
|
|||
|
||||
func check_for_duplicate_output_files(outputs []*Output) error {
|
||||
seen := make(map[string]string, len(outputs))
|
||||
probe_prefix := "." + utils.RandomFilename() + "-"
|
||||
for _, o := range outputs {
|
||||
if o.arg_is_stream {
|
||||
continue
|
||||
|
|
@ -452,6 +453,21 @@ func check_for_duplicate_output_files(outputs []*Output) error {
|
|||
return fmt.Errorf("The output files %s and %s are the same file", prev, o.arg)
|
||||
}
|
||||
seen[path] = o.arg
|
||||
|
||||
// Let the destination filesystem compare the names, including case
|
||||
// and Unicode normalization, without touching the output files.
|
||||
probe := filepath.Join(filepath.Dir(path), probe_prefix+filepath.Base(path))
|
||||
f, err := os.OpenFile(probe, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
|
||||
if err != nil {
|
||||
if os.IsExist(err) {
|
||||
return fmt.Errorf("The output file %s refers to the same file as another output", o.arg)
|
||||
}
|
||||
// If no temporary file can be created, let this output report
|
||||
// its write error without preventing other outputs.
|
||||
continue
|
||||
}
|
||||
f.Close()
|
||||
defer os.Remove(probe)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -119,6 +119,9 @@ func TestClipboardReadOutputFailure(t *testing.T) {
|
|||
succeeded := &Output{arg: filepath.Join(dir, "output"), remote_mime_type: "text/plain"}
|
||||
defer failed.cleanup()
|
||||
defer succeeded.cleanup()
|
||||
if err := check_for_duplicate_output_files([]*Output{failed, succeeded}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
requested := make(read_outputs)
|
||||
for _, o := range []*Output{failed, succeeded} {
|
||||
if err := requested.add(o); err != nil {
|
||||
|
|
@ -260,6 +263,75 @@ func TestClipboardReadDuplicateOutputFiles(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestClipboardReadCaseAliasedOutputFiles(t *testing.T) {
|
||||
for _, existing := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("existing=%t", existing), func(t *testing.T) {
|
||||
t.Chdir(t.TempDir())
|
||||
for _, dir := range []string{"destination", "DESTINATION"} {
|
||||
if err := os.Mkdir(dir, 0o755); err != nil && !os.IsExist(err) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
lower, err := os.Stat("destination")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
upper, err := os.Stat("DESTINATION")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case_insensitive := os.SameFile(lower, upper)
|
||||
if err := os.Symlink("destination/OUTPUT.TXT", "link"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
paths := []string{"destination/output.txt", "destination/OUTPUT.TXT", "DESTINATION/output.txt", "DESTINATION/OUTPUT.TXT"}
|
||||
if existing {
|
||||
for _, path := range paths {
|
||||
if err := os.WriteFile(path, []byte("original contents"), 0o640); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
before := list_dir(t, "destination")
|
||||
for _, path := range append(paths[1:], "link") {
|
||||
err := check_for_duplicate_output_files([]*Output{{arg: paths[0]}, {arg: path}})
|
||||
if (err != nil) != case_insensitive {
|
||||
t.Errorf("Incorrect duplicate detection for %s and %s: case_insensitive=%t error=%v", paths[0], path, case_insensitive, err)
|
||||
}
|
||||
}
|
||||
if actual := list_dir(t, "destination"); !slices.Equal(actual, before) {
|
||||
t.Errorf("Duplicate detection left files behind: %v", actual)
|
||||
}
|
||||
for _, path := range paths {
|
||||
data, err := os.ReadFile(path)
|
||||
if existing {
|
||||
if err != nil || string(data) != "original contents" {
|
||||
t.Errorf("Duplicate detection changed %s: %q %v", path, data, err)
|
||||
}
|
||||
} else if !os.IsNotExist(err) {
|
||||
t.Errorf("Duplicate detection created %s: %v", path, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClipboardReadHardLinkedOutputFiles(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
a, b := filepath.Join(dir, "output.txt"), filepath.Join(dir, "hardlink.txt")
|
||||
if err := os.WriteFile(a, []byte("original contents"), 0o640); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Link(a, b); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Atomic replacement writes to distinct directory entries even when the
|
||||
// original files share an inode.
|
||||
if err := check_for_duplicate_output_files([]*Output{{arg: a}, {arg: b}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClipboardReadDuplicateOutputFilesSymlinkParent(t *testing.T) {
|
||||
for _, existing := range []bool{false, true} {
|
||||
for _, absolute_link := range []bool{false, true} {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue