Commit 8b2d0ae1 authored by Nigel Tao's avatar Nigel Tao

webdav: add a FileSystem.Rename method.

It will be used by the WebDAV MOVE method.

Change-Id: Iacbcf58af4da0d169271661324537825d4ff887c
Reviewed-on: https://go-review.googlesource.com/2952Reviewed-by: 's avatarDave Cheney <dave@cheney.net>
parent 5a08939a
This diff is collapsed.
......@@ -9,12 +9,37 @@ import (
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"testing"
)
func TestSlashClean(t *testing.T) {
testCases := []string{
"",
".",
"/",
"/./",
"//",
"//.",
"//a",
"/a",
"/a/b/c",
"/a//b/./../c/d/",
"a",
"a/b/c",
}
for _, tc := range testCases {
got := slashClean(tc)
want := path.Clean("/" + tc)
if got != want {
t.Errorf("tc=%q: got %q, want %q", tc, got, want)
}
}
}
func TestDirResolve(t *testing.T) {
testCases := []struct {
dir, name, want string
......@@ -260,6 +285,41 @@ func testFS(t *testing.T, fs FileSystem) {
" stat /c want errNotExist",
" stat /d want dir",
" stat /d/m want dir",
"rename /b /b want ok",
" stat /b want 2",
" stat /c want errNotExist",
"rename /b /c want ok",
" stat /b want errNotExist",
" stat /c want 2",
" stat /d/m want dir",
" stat /d/n want errNotExist",
"rename /d/m /d/n want ok",
"create /d/n/q QQQQ want ok",
" stat /d/m want errNotExist",
" stat /d/n want dir",
" stat /d/n/q want 4",
"rename /d /d/n/x want err",
"rename /c /d/n/q want ok",
" stat /c want errNotExist",
" stat /d/n/q want 2",
"create /d/n/r RRRRR want ok",
"mk-dir /u want ok",
"mk-dir /u/v want ok",
"rename /d/n /u want err",
"create /t TTTTTT want ok",
"rename /d/n /t want err",
"rm-all /t want ok",
"rename /d/n /t want ok",
" stat /d want dir",
" stat /d/n want errNotExist",
" stat /d/n/r want errNotExist",
" stat /t want dir",
" stat /t/q want 2",
" stat /t/r want 5",
"rename /t / want err",
"rename /t /u/v want ok",
" stat /u/v/r want 5",
"rename / /x want err",
}
for i, tc := range testCases {
......@@ -292,9 +352,13 @@ func testFS(t *testing.T, fs FileSystem) {
}
}
case "mk-dir", "rm-all", "stat":
case "mk-dir", "rename", "rm-all", "stat":
nParts := 3
if op == "rename" {
nParts = 4
}
parts := strings.Split(arg, " ")
if len(parts) != 3 {
if len(parts) != nParts {
t.Fatalf("test case #%d %q: invalid %s", i, tc, op)
}
......@@ -302,6 +366,8 @@ func testFS(t *testing.T, fs FileSystem) {
switch op {
case "mk-dir":
opErr = fs.Mkdir(parts[0], 0777)
case "rename":
opErr = fs.Rename(parts[0], parts[1])
case "rm-all":
opErr = fs.RemoveAll(parts[0])
case "stat":
......@@ -318,10 +384,10 @@ func testFS(t *testing.T, fs FileSystem) {
got = errStr(opErr)
}
if parts[1] != "want" {
if parts[len(parts)-2] != "want" {
t.Fatalf("test case #%d %q: invalid %s", i, tc, op)
}
if want := parts[2]; got != want {
if want := parts[len(parts)-1]; got != want {
t.Fatalf("test case #%d %q: got %q (%v), want %q", i, tc, got, opErr, want)
}
}
......
......@@ -6,7 +6,6 @@ package webdav
import (
"errors"
"path"
"strconv"
"strings"
"sync"
......@@ -145,7 +144,7 @@ func (m *memLS) Confirm(now time.Time, name string, conditions ...Condition) (Re
m.mu.Lock()
defer m.mu.Unlock()
m.collectExpiredNodes(now)
name = path.Clean("/" + name)
name = slashClean(name)
// TODO: touch n.held.
panic("TODO")
......@@ -155,7 +154,7 @@ func (m *memLS) Create(now time.Time, details LockDetails) (string, error) {
m.mu.Lock()
defer m.mu.Unlock()
m.collectExpiredNodes(now)
name := path.Clean("/" + details.Root)
name := slashClean(details.Root)
if !m.canCreate(name, details.ZeroDepth) {
return "", ErrLocked
......
......@@ -354,11 +354,13 @@ func StatusText(code int) string {
}
var (
errDirectoryNotEmpty = errors.New("webdav: directory not empty")
errInvalidDepth = errors.New("webdav: invalid depth")
errInvalidIfHeader = errors.New("webdav: invalid If header")
errInvalidLockInfo = errors.New("webdav: invalid lock info")
errInvalidLockToken = errors.New("webdav: invalid lock token")
errNoFileSystem = errors.New("webdav: no file system")
errNoLockSystem = errors.New("webdav: no lock system")
errNotADirectory = errors.New("webdav: not a directory")
errUnsupportedLockInfo = errors.New("webdav: unsupported lock info")
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment