webdav: fix LocalFileSystem.Copy for directories

The walk ignored the path of each entry: it created dstPath for every directory
and copied srcPath to dstPath for every file, so copying a non-empty directory
failed with "open: is a directory". With Overwrite the destination had already
been removed, so its files were lost. Copy each entry to its own path under the
destination.
This commit is contained in:
Jay Hemnani 2026-11-18 18:34:25 +05:30 committed by Simon Ser
parent bf63772263
commit 2b106fb99b
2 changed files with 46 additions and 5 deletions

View File

@ -252,11 +252,9 @@ func (fs LocalFileSystem) Copy(ctx context.Context, src, dst string, options *Co
// TODO: "Note that an infinite-depth COPY of /A/ into /A/B/ could lead to
// infinite recursion if not handled correctly"
srcInfo, err := os.Stat(srcPath)
if err != nil {
if _, err := os.Stat(srcPath); err != nil {
return false, errFromOS(err)
}
srcPerm := srcInfo.Mode() & os.ModePerm
if _, err := os.Stat(dstPath); err != nil {
if !os.IsNotExist(err) {
@ -277,12 +275,19 @@ func (fs LocalFileSystem) Copy(ctx context.Context, src, dst string, options *Co
return err
}
rel, err := filepath.Rel(srcPath, p)
if err != nil {
return err
}
target := filepath.Join(dstPath, rel)
perm := fi.Mode() & os.ModePerm
if fi.IsDir() {
if err := os.Mkdir(dstPath, srcPerm); err != nil {
if err := os.Mkdir(target, perm); err != nil {
return errFromOS(err)
}
} else {
if err := copyRegularFile(srcPath, dstPath, srcPerm); err != nil {
if err := copyRegularFile(p, target, perm); err != nil {
return err
}
}

36
fs_local_test.go Normal file
View File

@ -0,0 +1,36 @@
package webdav
import (
"context"
"os"
"path/filepath"
"testing"
)
func TestLocalFileSystemCopyDirectory(t *testing.T) {
dir := t.TempDir()
for name, data := range map[string]string{"src/a.txt": "a", "src/sub/b.txt": "b", "dst/old.txt": "old"} {
p := filepath.Join(dir, name)
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(data), 0o644); err != nil {
t.Fatal(err)
}
}
fs := LocalFileSystem(dir)
if _, err := fs.Copy(context.Background(), "/src", "/dst", &CopyOptions{}); err != nil {
t.Fatalf("Copy() = %v", err)
}
for name, want := range map[string]string{"dst/a.txt": "a", "dst/sub/b.txt": "b"} {
got, err := os.ReadFile(filepath.Join(dir, name))
if err != nil || string(got) != want {
t.Errorf("%s = %q, %v; want %q", name, got, err, want)
}
}
if _, err := os.Stat(filepath.Join(dir, "dst/old.txt")); !os.IsNotExist(err) {
t.Errorf("dst/old.txt still exists after overwrite: %v", err)
}
}