fix: make webdav file copy atomic
This commit is contained in:
@@ -1256,21 +1256,43 @@ func (f *webdavStorageFS) Copy(ctx context.Context, name, dest string, opts *web
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
out, err := f.store.Create(dst)
|
||||
if err != nil {
|
||||
in.Close()
|
||||
defer in.Close()
|
||||
if err := copyFileToDestination(f.store, in, dst, dstExists); err != nil {
|
||||
return false, err
|
||||
}
|
||||
_, copyErr := io.Copy(out, in)
|
||||
in.Close()
|
||||
return !dstExists, nil
|
||||
}
|
||||
|
||||
func copyFileToDestination(st storage.Storage, src io.Reader, dst string, dstExists bool) error {
|
||||
tmp := fmt.Sprintf("%s.chfs-copy-%d", dst, time.Now().UnixNano())
|
||||
for i := 0; i < 16; i++ {
|
||||
if _, err := st.Stat(tmp); os.IsNotExist(err) {
|
||||
break
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
tmp = fmt.Sprintf("%s.chfs-copy-%d-%d", dst, time.Now().UnixNano(), i)
|
||||
}
|
||||
out, err := createExclusive(st, tmp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, copyErr := io.Copy(out, src)
|
||||
closeErr := out.Close()
|
||||
if copyErr != nil {
|
||||
return false, copyErr
|
||||
_ = st.Remove(tmp)
|
||||
return copyErr
|
||||
}
|
||||
if closeErr != nil {
|
||||
return false, closeErr
|
||||
_ = st.Remove(tmp)
|
||||
return closeErr
|
||||
}
|
||||
return !dstExists, nil
|
||||
if err := st.Rename(tmp, dst); err != nil {
|
||||
_ = st.Remove(tmp)
|
||||
return err
|
||||
}
|
||||
_ = dstExists
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *webdavStorageFS) Move(ctx context.Context, name, dest string, opts *webdav.MoveOptions) (bool, error) {
|
||||
|
||||
Reference in New Issue
Block a user