Files
seaweedfs/weed/server/webdav_oc.go
T
484e16e30c webdav: keep X-OC-Mtime and report oc:checksums (#13226)
* webdav: keep X-OC-Mtime and report oc:checksums

ownCloud and Nextcloud sync clients, and rclone's webdav backend in those
vendor modes, send a file's modification time in X-OC-Mtime and read its
hash from the oc:checksums property. Without them every upload gets the
upload time as mtime and clients can only compare sizes, so syncs re-upload
unchanged files.

- PUT stores X-OC-Mtime (Unix seconds, fractions dropped) as the entry's
  mtime, at create and at commit, and answers "X-OC-Mtime: accepted"
- a file created and written front to back stores its content MD5; any
  other write clears it, so no stale MD5 outlives a partial rewrite
- PROPFIND reports oc:checksums ("MD5:<hex>") for files whose MD5 is known;
  PROPPATCH is still refused

Checked with rclone (vendor=owncloud): sync keeps mtimes, md5sum matches
local files, including an 8-chunk file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* webdav: only report an inline MD5 for whole inline content; test commits

- contentMd5 hashes inline content only when it is the whole file
  (filer.ETagOfInlineContent): content next to chunks, or shorter than the
  file size, is only part of it and must not be advertised as its checksum
- test a PUT that crosses a buffer flush end to end: the committed entry
  carries the X-OC-Mtime and the MD5 of the whole body
- test that writing into an existing file clears its stored MD5

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* webdav: ignore a NaN X-OC-Mtime

int64 of NaN is implementation-specific (the most negative value on amd64,
the largest on riscv64), so a NaN header could store an arbitrary mtime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* webdav: group the oc test imports

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Denis Maier <denis@dAir.lab>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-12-01 13:16:48 -08:00

91 lines
2.8 KiB
Go

package weed_server
import (
"encoding/hex"
"encoding/xml"
"net/http"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
"golang.org/x/net/webdav"
)
// ownCloud's namespace; ownCloud and Nextcloud clients (and rclone's webdav
// backend in those vendor modes) read file hashes from oc:checksums
const ownCloudNS = "http://owncloud.org/ns"
var ocChecksums = xml.Name{Space: ownCloudNS, Local: "checksums"}
// contentMd5 is the MD5 of the whole content when the filer knows it: stored
// for files written in one pass, or computed from content stored inline. The ETag
// of a chunked file is not a content MD5, so such files report none.
func contentMd5(entry *filer_pb.Entry) []byte {
if entry.IsDirectory {
return nil
}
if entry.Attributes != nil && len(entry.Attributes.Md5) > 0 {
return entry.Attributes.Md5
}
// only when the whole file is inline: content next to chunks, or short
// of the file size, is just part of it
if etag, ok := filer.ETagOfInlineContent(entry); ok {
if sum, err := hex.DecodeString(etag); err == nil {
return sum
}
}
return nil
}
// DeadProps publishes oc:checksums ("MD5:<hex>") for files whose MD5 is
// known, so ownCloud-style clients can verify transfers without downloading.
func (f *WebDavFile) DeadProps() (map[xml.Name]webdav.Property, error) {
if f.info == nil || f.info.isDirectory || len(f.info.md5) == 0 {
return nil, nil
}
return map[xml.Name]webdav.Property{
ocChecksums: {
XMLName: ocChecksums,
InnerXML: []byte(`<checksum xmlns="` + ownCloudNS + `">MD5:` + hex.EncodeToString(f.info.md5) + `</checksum>`),
},
}, nil
}
// Patch refuses every change: the only dead property is computed, and
// PROPPATCH was refused the same way before files held dead properties.
func (f *WebDavFile) Patch(patches []webdav.Proppatch) ([]webdav.Propstat, error) {
pstat := webdav.Propstat{Status: http.StatusForbidden}
for _, patch := range patches {
for _, p := range patch.Props {
pstat.Props = append(pstat.Props, webdav.Property{XMLName: p.XMLName})
}
}
return []webdav.Propstat{pstat}, nil
}
// hashWrite feeds buf to the content hash while the file is being written
// front to back from empty; a write anywhere else gives the hash up
func (f *WebDavFile) hashWrite(buf []byte) {
if !f.created || f.hashBroken {
return
}
if f.off != f.hashed {
f.hashBroken = true
return
}
if f.hasher == nil {
f.hasher = util.NewMd5()
}
f.hasher.Write(buf)
f.hashed += int64(len(buf))
}
// contentMd5 is the MD5 of what this open wrote, when it wrote the whole
// file in order; nil otherwise, so no stale MD5 outlives a partial rewrite
func (f *WebDavFile) contentMd5() []byte {
if !f.created || f.hashBroken || f.hasher == nil || f.hashed != int64(f.entry.Attributes.FileSize) {
return nil
}
return f.hasher.Sum(nil)
}