mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-21 15:44:44 +00:00
clipboard: Reject duplicate stream outputs for the same MIME type
Stream outputs write data as it arrives, so with fan-out to all outputs sharing a remote MIME type, two outputs to the same stream would interleave their data chunk by chunk, or encode two images into the stream concurrently. Reject such duplicates with an error instead. Also add a changelog entry for saving all outputs sharing a MIME type.
This commit is contained in:
parent
f6eb49a47c
commit
3723c447aa
3 changed files with 50 additions and 6 deletions
|
|
@ -389,6 +389,10 @@ Detailed list of changes
|
|||
- Clipboard kitten: Fix saving the list of available MIME types to a file
|
||||
with :code:`kitten clipboard -g -m . formats.txt` silently doing nothing
|
||||
|
||||
- Clipboard kitten: Fix only the last of several outputs that use the same
|
||||
clipboard MIME type being saved, for example with
|
||||
:code:`kitten clipboard -g picture.png picture.jpg` (:pull:`10744`)
|
||||
|
||||
- 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`)
|
||||
|
|
|
|||
|
|
@ -281,8 +281,18 @@ func parse_aliases(raw []string) (map[string][]string, error) {
|
|||
|
||||
type read_outputs map[string][]*Output
|
||||
|
||||
func (self read_outputs) add(o *Output) {
|
||||
func (self read_outputs) add(o *Output) error {
|
||||
if o.arg_is_stream {
|
||||
// Stream outputs write data as it arrives, so two of them sharing a
|
||||
// remote MIME type would interleave their data in the same stream.
|
||||
for _, x := range self[o.remote_mime_type] {
|
||||
if x.arg_is_stream && x.arg == o.arg {
|
||||
return fmt.Errorf("Cannot write the clipboard MIME type %s to %s more than once", o.remote_mime_type, o.arg)
|
||||
}
|
||||
}
|
||||
}
|
||||
self[o.remote_mime_type] = append(self[o.remote_mime_type], o)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self read_outputs) add_data(mime_type string, data []byte) {
|
||||
|
|
@ -382,8 +392,8 @@ func run_get_loop(opts *Options, args []string) (err error) {
|
|||
}
|
||||
if o.remote_mime_type == "." {
|
||||
o.write_mime_list(available_mimes)
|
||||
} else {
|
||||
requested_mimes.add(o)
|
||||
} else if err = requested_mimes.add(o); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(requested_mimes) > 0 {
|
||||
|
|
|
|||
|
|
@ -52,7 +52,9 @@ func TestClipboardReadMultipleOutputs(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
outputs = append(outputs, o)
|
||||
requested.add(o)
|
||||
if err := requested.add(o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if len(requested) != len(tc.available_mimes) {
|
||||
t.Fatalf("Expected one request per remote MIME type, got %d", len(requested))
|
||||
|
|
@ -117,8 +119,11 @@ func TestClipboardReadOutputFailure(t *testing.T) {
|
|||
defer failed.cleanup()
|
||||
defer succeeded.cleanup()
|
||||
requested := make(read_outputs)
|
||||
requested.add(failed)
|
||||
requested.add(succeeded)
|
||||
for _, o := range []*Output{failed, succeeded} {
|
||||
if err := requested.add(o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
requested.add_data("text/plain", []byte("clipboard contents"))
|
||||
var wg sync.WaitGroup
|
||||
requested.commit("text/plain", &wg)
|
||||
|
|
@ -134,6 +139,31 @@ func TestClipboardReadOutputFailure(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestClipboardReadDuplicateStreamOutputs(t *testing.T) {
|
||||
stream := func(arg, remote_mime_type string) *Output {
|
||||
return &Output{arg: arg, arg_is_stream: true, remote_mime_type: remote_mime_type}
|
||||
}
|
||||
requested := make(read_outputs)
|
||||
for _, o := range []*Output{
|
||||
stream("/dev/stdout", "text/plain"),
|
||||
stream("/dev/stderr", "text/plain"),
|
||||
stream("/dev/stdout", "text/html"),
|
||||
{arg: filepath.Join(t.TempDir(), "output"), remote_mime_type: "text/plain"},
|
||||
} {
|
||||
if err := requested.add(o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, o := range []*Output{stream("/dev/stdout", "text/plain"), stream("/dev/stderr", "text/plain")} {
|
||||
if err := requested.add(o); err == nil {
|
||||
t.Fatalf("Duplicate stream output to %s for %s was not rejected", o.arg, o.remote_mime_type)
|
||||
}
|
||||
}
|
||||
if len(requested["text/plain"]) != 3 || len(requested["text/html"]) != 1 {
|
||||
t.Fatalf("Rejected outputs were added: %d %d", len(requested["text/plain"]), len(requested["text/html"]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestClipboardOutputCommitAtomic(t *testing.T) {
|
||||
dest := filepath.Join(t.TempDir(), "output")
|
||||
old_data := bytes.Repeat([]byte("old contents\n"), 1024)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue