add Stat()

This commit is contained in:
Christoph Polcin 2014-10-27 17:02:28 +01:00
parent 328e74fe93
commit a3cc4ebe01
2 changed files with 55 additions and 1 deletions

View File

@ -125,6 +125,53 @@ func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
return files, err
}
func (c *Client) Stat(path string) (os.FileInfo, error) {
var f *File = nil
parse := func(resp interface{}) error {
r := resp.(*response)
if p := getProps(r, "200"); p != nil && f == nil {
f = new(File)
f.name = p.Name
f.path = path
if p.Type.Local == "collection" {
if !strings.HasSuffix(f.path, "/") {
f.path += "/"
}
f.size = 0
f.modified = time.Unix(0, 0)
f.isdir = true
} else {
f.size = parseInt64(&p.Size)
f.modified = parseModified(&p.Modified)
f.isdir = false
}
}
r.Props = nil
return nil
}
err := c.propfind(path, true,
`<d:propfind xmlns:d='DAV:'>
<d:prop>
<d:displayname/>
<d:resourcetype/>
<d:getcontentlength/>
<d:getlastmodified/>
</d:prop>
</d:propfind>`,
&response{},
parse)
if err != nil {
if _, ok := err.(*os.PathError); !ok {
err = &os.PathError{"ReadDir", path, err}
}
}
return f, err
}
func (c *Client) Remove(path string) error {
return c.RemoveAll(path)
}

View File

@ -26,7 +26,7 @@ func Fail(err interface{}) {
fmt.Println(" CP | COPY <OLD_PATH> <NEW_PATH>")
fmt.Println(" GET | PULL | READ <PATH>")
fmt.Println(" PUT | PUSH | WRITE <PATH> <FILE>")
fmt.Println(" STAT <PATH>")
}
os.Exit(-1)
}
@ -73,6 +73,13 @@ func main() {
fmt.Println(err)
}
case "STAT":
if file, err := c.Stat(path); err == nil {
fmt.Println(file)
} else {
fmt.Println(err)
}
case "GET", "PULL", "READ":
if bytes, err := c.Read(path); err == nil {
if lidx := strings.LastIndex(path, "/"); lidx != -1 {