From 0dae99c3566a9771ea19e783cfc31f97fe11ddb9 Mon Sep 17 00:00:00 2001 From: staytomorrow Date: Thu, 3 Dec 2026 00:23:51 +0800 Subject: [PATCH] fix(clipboard): preserve active CF_HDROP staging batch during expiry prune --- internal/guesttools/clipboard_files.go | 49 ++++++++++++++++++++- internal/guesttools/clipboard_files_test.go | 33 ++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/internal/guesttools/clipboard_files.go b/internal/guesttools/clipboard_files.go index b4b1684..f2214ab 100644 --- a/internal/guesttools/clipboard_files.go +++ b/internal/guesttools/clipboard_files.go @@ -14,6 +14,7 @@ import ( "os" "path" "path/filepath" + "runtime" "slices" "strconv" "strings" @@ -135,13 +136,59 @@ func (s *Server) clearVMClipFiles(vmKey string) { } } +func batchDirOwnsAnyClipFile(batchDir string, clipFiles []string) bool { + if batchDir == "" || len(clipFiles) == 0 { + return false + } + cleanDir := filepath.Clean(batchDir) + prefix := cleanDir + string(filepath.Separator) + for _, f := range clipFiles { + cf := filepath.Clean(f) + if runtime.GOOS == "windows" { + if strings.EqualFold(cf, cleanDir) || (len(cf) > len(prefix) && strings.EqualFold(cf[:len(prefix)], prefix)) { + return true + } + } else if cf == cleanDir || strings.HasPrefix(cf, prefix) { + return true + } + } + return false +} + +func (s *Server) hasExpiredClipStaging(now time.Time) bool { + s.mu.RLock() + defer s.mu.RUnlock() + for _, list := range s.clipGuestBatches { + for _, b := range list { + if now.Sub(b.createdAt) > clipStagingMaxAge { + return true + } + } + } + return false +} + func (s *Server) pruneExpiredClipStaging(now time.Time) { + if !s.hasExpiredClipStaging(now) { + return + } + curPayload, _, err := s.readClipboardState(false, true) + if err != nil { + // If the host clipboard is transiently locked, defer pruning so we never + // delete a batch that is still referenced on CF_HDROP. + return + } + var activeFiles []string + if curPayload.Kind == ClipFiles { + activeFiles = curPayload.Files + } + var toRemove []string s.mu.Lock() for vmKey, list := range s.clipGuestBatches { kept := list[:0] for _, b := range list { - if now.Sub(b.createdAt) > clipStagingMaxAge { + if now.Sub(b.createdAt) > clipStagingMaxAge && !batchDirOwnsAnyClipFile(b.dir, activeFiles) { if b.dir != "" { toRemove = append(toRemove, b.dir) } diff --git a/internal/guesttools/clipboard_files_test.go b/internal/guesttools/clipboard_files_test.go index 1917982..abfda93 100644 --- a/internal/guesttools/clipboard_files_test.go +++ b/internal/guesttools/clipboard_files_test.go @@ -14,6 +14,7 @@ import ( "slices" "strings" "testing" + "time" ) func TestHDropEncodeDecodeRoundTrip(t *testing.T) { @@ -399,3 +400,35 @@ func TestClipboardGuestToHostTarUploadSecurityAndCleanup(t *testing.T) { t.Fatalf("expected staging dir %s removed on disable, stat err=%v", staging, err) } } + +func TestPruneExpiredClipStagingPreservesActiveClipboardBatch(t *testing.T) { + staging := filepath.Join(t.TempDir(), "tmp", "clip-files") + clip := &memClipboard{seq: 1} + srv := newClipboardTestServer(clip) + srv.SetClipStagingDir(staging) + + tarData := buildTestTar(t, []tarTestEntry{{name: "keep-me.txt", body: "still-on-clipboard"}}) + post := clipboardRequest(t, srv, http.MethodPost, "", "application/x-tar", "", tarData) + if post.Code != http.StatusOK { + t.Fatalf("tar POST = %d: %s", post.Code, post.Body.String()) + } + cur, _ := clip.Read() + if cur.Kind != ClipFiles || len(cur.Files) != 1 { + t.Fatalf("clipboard = %+v", cur) + } + activeFile := cur.Files[0] + + // Even past clipStagingMaxAge, pruning must keep the batch while it backs CF_HDROP. + future := time.Now().Add(clipStagingMaxAge + 5*time.Minute) + srv.pruneExpiredClipStaging(future) + if data, err := os.ReadFile(activeFile); err != nil || string(data) != "still-on-clipboard" { + t.Fatalf("active clipboard file removed prematurely: %q, err=%v", data, err) + } + + // Once the user copies something else, the expired batch is reclaimed cleanly. + _ = clip.WriteText("replaced") + srv.pruneExpiredClipStaging(future) + if _, err := os.Stat(filepath.Dir(activeFile)); !os.IsNotExist(err) { + t.Fatalf("expected expired inactive batch directory removed, stat err=%v", err) + } +}