fix: make webdav put atomic

This commit is contained in:
OpenClaw Agent
2026-09-22 00:58:38 +08:00
parent 098d79c253
commit 0b57e776d3
2 changed files with 57 additions and 23 deletions
+32
View File
@@ -3,6 +3,7 @@ package api
import (
"context"
"errors"
"io"
"net/http/httptest"
"os"
"path/filepath"
@@ -147,6 +148,10 @@ type failingReader struct{}
func (failingReader) Read([]byte) (int, error) { return 0, errors.New("source read failed") }
type readCloser struct{ io.Reader }
func (r readCloser) Close() error { return nil }
func TestWebDAVStorageFSCopyFailureDoesNotCorruptDestination(t *testing.T) {
root := t.TempDir()
store := storage.Local{Root: root}
@@ -172,6 +177,33 @@ func TestWebDAVStorageFSCopyFailureDoesNotCorruptDestination(t *testing.T) {
}
}
func TestWebDAVStorageFSCreateFailureDoesNotCorruptDestination(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)
}
fs := &webdavStorageFS{store: store}
_, _, err := fs.Create(context.Background(), "/dst.txt", readCloser{Reader: failingReader{}}, nil)
if err == nil {
t.Fatal("expected PUT body 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 PUT files leaked: %v", matches)
}
}
func TestWebDAVStorageFSCopyAndMove(t *testing.T) {
root1 := t.TempDir()
root2 := t.TempDir()