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:
parent
bf63772263
commit
2b106fb99b
15
fs_local.go
15
fs_local.go
@ -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
|
// TODO: "Note that an infinite-depth COPY of /A/ into /A/B/ could lead to
|
||||||
// infinite recursion if not handled correctly"
|
// infinite recursion if not handled correctly"
|
||||||
|
|
||||||
srcInfo, err := os.Stat(srcPath)
|
if _, err := os.Stat(srcPath); err != nil {
|
||||||
if err != nil {
|
|
||||||
return false, errFromOS(err)
|
return false, errFromOS(err)
|
||||||
}
|
}
|
||||||
srcPerm := srcInfo.Mode() & os.ModePerm
|
|
||||||
|
|
||||||
if _, err := os.Stat(dstPath); err != nil {
|
if _, err := os.Stat(dstPath); err != nil {
|
||||||
if !os.IsNotExist(err) {
|
if !os.IsNotExist(err) {
|
||||||
@ -277,12 +275,19 @@ func (fs LocalFileSystem) Copy(ctx context.Context, src, dst string, options *Co
|
|||||||
return err
|
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 fi.IsDir() {
|
||||||
if err := os.Mkdir(dstPath, srcPerm); err != nil {
|
if err := os.Mkdir(target, perm); err != nil {
|
||||||
return errFromOS(err)
|
return errFromOS(err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err := copyRegularFile(srcPath, dstPath, srcPerm); err != nil {
|
if err := copyRegularFile(p, target, perm); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
36
fs_local_test.go
Normal file
36
fs_local_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user