add Rename and Copy

This commit is contained in:
Christoph Polcin 2014-10-24 14:08:42 +02:00
parent e02560544b
commit 47076d6487
3 changed files with 51 additions and 0 deletions

View File

@ -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)
}

View File

@ -20,6 +20,8 @@ func Fail(err interface{}) {
fmt.Println(" RM | DELETE | DEL <PATH>")
fmt.Println(" MKDIR | MKCOL <PATH>")
fmt.Println(" MKDIRALL | MKCOLALL <PATH>")
fmt.Println(" MV | MOVE | RENAME <OLD_PATH> <NEW_PATH>")
fmt.Println(" CP | COPY <OLD_PATH> <NEW_PATH>")
}
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)
}

View File

@ -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)
}