diff --git a/client.go b/client.go index 5944b69..d864753 100644 --- a/client.go +++ b/client.go @@ -176,6 +176,14 @@ func (c *Client) MkdirAll(path string, _ os.FileMode) error { return newPathError("MkdirAll", path, status) } +func (c *Client) Rename(oldpath string, newpath string, overwrite bool) error { + return c.copymove("MOVE", oldpath, newpath, overwrite) +} + +func (c *Client) Copy(oldpath string, newpath string, overwrite bool) error { + return c.copymove("COPY", oldpath, newpath, overwrite) +} + func (c *Client) Read(path string) { fmt.Println("Read " + path) } diff --git a/main/client.go b/main/client.go index 660825d..ddb93f7 100644 --- a/main/client.go +++ b/main/client.go @@ -20,6 +20,8 @@ func Fail(err interface{}) { fmt.Println(" RM | DELETE | DEL ") fmt.Println(" MKDIR | MKCOL ") fmt.Println(" MKDIRALL | MKCOLALL ") + fmt.Println(" MV | MOVE | RENAME ") + fmt.Println(" CP | COPY ") } os.Exit(-1) } @@ -86,6 +88,20 @@ func main() { a0 := flag.Args()[0] a1 := flag.Args()[1] switch *m { + case "RENAME", "MV", "MOVE": + if err := c.Rename(a0, a1, true); err != nil { + fmt.Println(err) + } else { + fmt.Println("Rename: " + a0 + " -> " + a1) + } + + case "COPY", "CP": + if err := c.Copy(a0, a1, true); err != nil { + fmt.Println(err) + } else { + fmt.Println("Copy: " + a0 + " -> " + a1) + } + default: Fail(nil) } diff --git a/requests.go b/requests.go index 882ce39..e2730e4 100644 --- a/requests.go +++ b/requests.go @@ -84,3 +84,30 @@ func (c *Client) propfind(path string, self bool, body string, resp interface{}, return parseXML(rs.Body, resp, parse) } + +func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) error { + rq, err := c.req(method, oldpath, nil) + if err != nil { + return newPathErrorErr(method, oldpath, err) + } + + rq.Header.Add("Destination", Join(c.root, newpath)) + if overwrite { + rq.Header.Add("Overwrite", "T") + } else { + rq.Header.Add("Overwrite", "F") + } + + rs, err := c.c.Do(rq) + if err != nil { + return newPathErrorErr(method, oldpath, err) + } + defer rs.Body.Close() + + switch rs.StatusCode { + case 201, 204: + return nil + } + + return newPathError(method, oldpath, rs.StatusCode) +}