ReadDir will fail on non collections

This commit is contained in:
Christoph Polcin 2014-10-24 12:39:35 +02:00
parent 4f95c70fb3
commit f728828956
3 changed files with 18 additions and 10 deletions

View File

@ -72,13 +72,16 @@ func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
path = FixSlashes(path) path = FixSlashes(path)
files := make([]os.FileInfo, 0) files := make([]os.FileInfo, 0)
skipSelf := true skipSelf := true
parse := func(resp interface{}) { parse := func(resp interface{}) error {
r := resp.(*response) r := resp.(*response)
if skipSelf { if skipSelf {
skipSelf = false skipSelf = false
r.Props = nil if p := getProps(r, "200"); p != nil && p.Type.Local == "collection" {
return r.Props = nil
return nil
}
return newPathError("ReadDir", path, 405)
} }
if p := getProps(r, "200"); p != nil { if p := getProps(r, "200"); p != nil {
@ -101,6 +104,7 @@ func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
} }
r.Props = nil r.Props = nil
return nil
} }
err := c.propfind(path, false, err := c.propfind(path, false,
@ -114,8 +118,11 @@ func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
</d:propfind>`, </d:propfind>`,
&response{}, &response{},
parse) parse)
if err != nil { if err != nil {
err = &os.PathError{"ReadDir", path, err} if _, ok := err.(*os.PathError); !ok {
err = &os.PathError{"ReadDir", path, err}
}
} }
return files, err return files, err
} }

View File

@ -55,7 +55,7 @@ func (c *Client) options(path string) (*http.Response, error) {
return c.c.Do(rq) return c.c.Do(rq)
} }
func (c *Client) propfind(path string, self bool, body string, resp interface{}, parse func(resp interface{})) error { func (c *Client) propfind(path string, self bool, body string, resp interface{}, parse func(resp interface{}) error) error {
rq, err := c.req("PROPFIND", path, strings.NewReader(body)) rq, err := c.req("PROPFIND", path, strings.NewReader(body))
if err != nil { if err != nil {
return err return err
@ -82,7 +82,5 @@ func (c *Client) propfind(path string, self bool, body string, resp interface{},
return errors.New(fmt.Sprintf("%s - %s %s", rs.Status, rq.Method, rq.URL.String())) return errors.New(fmt.Sprintf("%s - %s %s", rs.Status, rq.Method, rq.URL.String()))
} }
parseXML(rs.Body, resp, parse) return parseXML(rs.Body, resp, parse)
return nil
} }

View File

@ -65,16 +65,19 @@ func parseModified(s *string) time.Time {
return time.Unix(0, 0) return time.Unix(0, 0)
} }
func parseXML(data io.Reader, resp interface{}, parse func(resp interface{})) { func parseXML(data io.Reader, resp interface{}, parse func(resp interface{}) error) error {
decoder := xml.NewDecoder(data) decoder := xml.NewDecoder(data)
for t, _ := decoder.Token(); t != nil; t, _ = decoder.Token() { for t, _ := decoder.Token(); t != nil; t, _ = decoder.Token() {
switch se := t.(type) { switch se := t.(type) {
case xml.StartElement: case xml.StartElement:
if se.Name.Local == "response" { if se.Name.Local == "response" {
if e := decoder.DecodeElement(resp, &se); e == nil { if e := decoder.DecodeElement(resp, &se); e == nil {
parse(resp) if err := parse(resp); err != nil {
return err
}
} }
} }
} }
} }
return nil
} }