fix: make webdav file copy atomic

This commit is contained in:
OpenClaw Agent
2026-09-22 00:45:28 +08:00
parent f3d80e939e
commit a7b111234d
2 changed files with 89 additions and 8 deletions
+59
View File
@@ -2,6 +2,7 @@ package api
import (
"context"
"errors"
"net/http/httptest"
"os"
"path/filepath"
@@ -113,6 +114,64 @@ func TestWebDAVAuthFSMoveMissingSourcePrecedence(t *testing.T) {
}
}
func TestWebDAVStorageFSCopyPreservesExistingDestinationOnSourceReadFailure(t *testing.T) {
root := t.TempDir()
if err := os.WriteFile(filepath.Join(root, "dst.txt"), []byte("original"), 0o644); err != nil {
t.Fatal(err)
}
store := storage.Local{Root: root}
if err := os.WriteFile(filepath.Join(root, "src.txt"), []byte("replacement"), 0o644); err != nil {
t.Fatal(err)
}
fs := &webdavStorageFS{store: store}
if _, err := fs.Copy(context.Background(), "/src.txt", "/dst.txt", &webdav.CopyOptions{}); err != nil {
t.Fatalf("copy: %v", err)
}
got, err := os.ReadFile(filepath.Join(root, "dst.txt"))
if err != nil {
t.Fatal(err)
}
if string(got) != "replacement" {
t.Fatalf("destination content=%q", got)
}
matches, err := filepath.Glob(filepath.Join(root, "dst.txt.chfs-copy-*"))
if err != nil {
t.Fatal(err)
}
if len(matches) != 0 {
t.Fatalf("temporary copy files leaked: %v", matches)
}
}
type failingReader struct{}
func (failingReader) Read([]byte) (int, error) { return 0, errors.New("source read failed") }
func TestWebDAVStorageFSCopyFailureDoesNotCorruptDestination(t *testing.T) {
root := t.TempDir()
store := storage.Local{Root: root}
if err := os.WriteFile(filepath.Join(root, "dst.txt"), []byte("original"), 0o644); err != nil {
t.Fatal(err)
}
if err := copyFileToDestination(store, failingReader{}, "dst.txt", true); err == nil {
t.Fatal("expected source read failure")
}
got, err := os.ReadFile(filepath.Join(root, "dst.txt"))
if err != nil {
t.Fatal(err)
}
if string(got) != "original" {
t.Fatalf("destination was corrupted: %q", got)
}
matches, err := filepath.Glob(filepath.Join(root, "dst.txt.chfs-copy-*"))
if err != nil {
t.Fatal(err)
}
if len(matches) != 0 {
t.Fatalf("temporary copy files leaked: %v", matches)
}
}
func TestWebDAVStorageFSCopyAndMove(t *testing.T) {
root1 := t.TempDir()
root2 := t.TempDir()