webdav: Fix free/used display for rclone about/df for certain backends - fixes #4348

Before this change if the server sent us xml like this

```
<D:propstat>
<D:prop>
<g0:quota-available-bytes/>
<g0:quota-used-bytes/>
</D:prop>
<D:status>HTTP/1.1 404 Not Found</D:status>
</D:propstat>
```

Rclone would read the empty XML items as containing 0

After this fix we make sure that we have a value before using it.
This commit is contained in:
Nick Craig-Wood 2020-06-14 12:11:19 +01:00
parent 5c5ad62208
commit a55d882b7b
2 changed files with 10 additions and 12 deletions

View File

@ -226,6 +226,6 @@ func (t *Time) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
// </d:response> // </d:response>
// </d:multistatus> // </d:multistatus>
type Quota struct { type Quota struct {
Available int64 `xml:"DAV: response>propstat>prop>quota-available-bytes"` Available string `xml:"DAV: response>propstat>prop>quota-available-bytes"`
Used int64 `xml:"DAV: response>propstat>prop>quota-used-bytes"` Used string `xml:"DAV: response>propstat>prop>quota-used-bytes"`
} }

View File

@ -17,6 +17,7 @@ import (
"net/url" "net/url"
"os/exec" "os/exec"
"path" "path"
"strconv"
"strings" "strings"
"time" "time"
@ -975,10 +976,7 @@ func (f *Fs) About(ctx context.Context) (*fs.Usage, error) {
</D:prop> </D:prop>
</D:propfind> </D:propfind>
`)) `))
var q = api.Quota{ var q api.Quota
Available: -1,
Used: -1,
}
var resp *http.Response var resp *http.Response
var err error var err error
err = f.pacer.Call(func() (bool, error) { err = f.pacer.Call(func() (bool, error) {
@ -989,14 +987,14 @@ func (f *Fs) About(ctx context.Context) (*fs.Usage, error) {
return nil, errors.Wrap(err, "about call failed") return nil, errors.Wrap(err, "about call failed")
} }
usage := &fs.Usage{} usage := &fs.Usage{}
if q.Used >= 0 { if i, err := strconv.ParseInt(q.Used, 10, 64); err == nil && i >= 0 {
usage.Used = fs.NewUsageValue(q.Used) usage.Used = fs.NewUsageValue(i)
} }
if q.Available >= 0 { if i, err := strconv.ParseInt(q.Available, 10, 64); err == nil && i >= 0 {
usage.Free = fs.NewUsageValue(q.Available) usage.Free = fs.NewUsageValue(i)
} }
if q.Available >= 0 && q.Used >= 0 { if usage.Used != nil && usage.Free != nil {
usage.Total = fs.NewUsageValue(q.Available + q.Used) usage.Total = fs.NewUsageValue(*usage.Used + *usage.Free)
} }
return usage, nil return usage, nil
} }