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
+25 -23
View File
@@ -1110,32 +1110,34 @@ func (f *webdavStorageFS) Create(ctx context.Context, name string, body io.ReadC
return nil, false, statErr
}
exclusive := opts != nil && opts.IfNoneMatch.IsWildcard()
var file *os.File
if exclusive {
file, err = createExclusive(f.store, p)
file, createErr := createExclusive(f.store, p)
if createErr != nil {
if os.IsExist(createErr) {
return nil, false, webdav.NewHTTPError(http.StatusPreconditionFailed, fmt.Errorf("destination exists"))
}
return nil, false, createErr
}
_, copyErr := io.Copy(file, body)
closeErr := file.Close()
body.Close()
if copyErr != nil {
_ = f.store.Remove(p)
return nil, false, copyErr
}
if closeErr != nil {
_ = f.store.Remove(p)
return nil, false, closeErr
}
} else {
file, err = f.store.Create(p)
}
if err != nil {
if exclusive && os.IsExist(err) {
return nil, false, webdav.NewHTTPError(http.StatusPreconditionFailed, fmt.Errorf("destination exists"))
// Write into a sibling temporary file and atomically replace the
// destination only after the complete request body has been received.
// A failed PUT therefore cannot truncate or corrupt the old file.
if err := copyFileToDestination(f.store, body, p); err != nil {
body.Close()
return nil, false, err
}
return nil, false, err
}
_, copyErr := io.Copy(file, body)
closeErr := file.Close()
body.Close()
if copyErr != nil {
if exclusive {
_ = f.store.Remove(p)
}
return nil, false, copyErr
}
if closeErr != nil {
if exclusive {
_ = f.store.Remove(p)
}
return nil, false, closeErr
body.Close()
}
fi, err := f.info(p)
return fi, true, err