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:") 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(`MD5:` + hex.EncodeToString(f.info.md5) + ``), }, }, 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) }