fix(clipboard): preserve active CF_HDROP staging batch during expiry prune

This commit is contained in:
staytomorrow committed 2026-12-03 00:23:51 +08:00
1 parent 8c823d98fb
commit 0dae99c356
2 files changed
+81 -1

No files matched your search

+48 -1
View File
@@ -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)
}
@@ -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)
}
}