2023-01-03 09:18:30 +00:00
|
|
|
// Copyright (C) 2019-2023 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
package webdavd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-11 17:20:58 +00:00
|
|
|
"encoding/xml"
|
2020-08-11 21:56:10 +00:00
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"mime"
|
2022-10-11 17:20:58 +00:00
|
|
|
"net/http"
|
2020-08-11 21:56:10 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2022-11-03 07:31:40 +00:00
|
|
|
"github.com/drakkan/webdav"
|
2020-08-11 21:56:10 +00:00
|
|
|
"github.com/eikenb/pipeat"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
2022-10-11 17:20:58 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/vfs"
|
2020-08-11 21:56:10 +00:00
|
|
|
)
|
|
|
|
|
2022-10-11 17:20:58 +00:00
|
|
|
var (
|
|
|
|
errTransferAborted = errors.New("transfer aborted")
|
|
|
|
lastModifiedProps = []string{"Win32LastModifiedTime", "getlastmodified"}
|
|
|
|
)
|
2020-08-11 21:56:10 +00:00
|
|
|
|
|
|
|
type webDavFile struct {
|
|
|
|
*common.BaseTransfer
|
2020-08-22 08:12:00 +00:00
|
|
|
writer io.WriteCloser
|
|
|
|
reader io.ReadCloser
|
|
|
|
info os.FileInfo
|
2020-11-04 18:11:40 +00:00
|
|
|
startOffset int64
|
|
|
|
isFinished bool
|
2023-05-12 16:34:59 +00:00
|
|
|
readTried atomic.Bool
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 18:11:40 +00:00
|
|
|
func newWebDavFile(baseTransfer *common.BaseTransfer, pipeWriter *vfs.PipeWriter, pipeReader *pipeat.PipeReaderAt) *webDavFile {
|
2020-08-11 21:56:10 +00:00
|
|
|
var writer io.WriteCloser
|
|
|
|
var reader io.ReadCloser
|
|
|
|
if baseTransfer.File != nil {
|
|
|
|
writer = baseTransfer.File
|
|
|
|
reader = baseTransfer.File
|
|
|
|
} else if pipeWriter != nil {
|
|
|
|
writer = pipeWriter
|
|
|
|
} else if pipeReader != nil {
|
|
|
|
reader = pipeReader
|
|
|
|
}
|
2022-08-30 13:47:41 +00:00
|
|
|
f := &webDavFile{
|
2020-08-11 21:56:10 +00:00
|
|
|
BaseTransfer: baseTransfer,
|
|
|
|
writer: writer,
|
|
|
|
reader: reader,
|
|
|
|
isFinished: false,
|
|
|
|
startOffset: 0,
|
2020-11-04 18:11:40 +00:00
|
|
|
info: nil,
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
2023-05-12 16:34:59 +00:00
|
|
|
f.readTried.Store(false)
|
2022-08-30 13:47:41 +00:00
|
|
|
return f
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type webDavFileInfo struct {
|
|
|
|
os.FileInfo
|
2020-11-04 18:11:40 +00:00
|
|
|
Fs vfs.Fs
|
|
|
|
virtualPath string
|
|
|
|
fsPath string
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ContentType implements webdav.ContentTyper interface
|
2023-03-22 18:02:54 +00:00
|
|
|
func (fi *webDavFileInfo) ContentType(_ context.Context) (string, error) {
|
2020-11-04 18:11:40 +00:00
|
|
|
extension := path.Ext(fi.virtualPath)
|
2023-01-23 17:43:25 +00:00
|
|
|
if ctype, ok := customMimeTypeMapping[extension]; ok {
|
|
|
|
return ctype, nil
|
|
|
|
}
|
2022-11-03 07:31:40 +00:00
|
|
|
if extension == "" || extension == ".dat" {
|
|
|
|
return "application/octet-stream", nil
|
|
|
|
}
|
2020-11-04 18:11:40 +00:00
|
|
|
contentType := mime.TypeByExtension(extension)
|
2020-09-28 20:12:46 +00:00
|
|
|
if contentType != "" {
|
2020-08-11 21:56:10 +00:00
|
|
|
return contentType, nil
|
|
|
|
}
|
2020-11-04 18:11:40 +00:00
|
|
|
contentType = mimeTypeCache.getMimeFromCache(extension)
|
|
|
|
if contentType != "" {
|
|
|
|
return contentType, nil
|
|
|
|
}
|
|
|
|
contentType, err := fi.Fs.GetMimeType(fi.fsPath)
|
|
|
|
if contentType != "" {
|
2020-11-04 21:29:25 +00:00
|
|
|
mimeTypeCache.addMimeToCache(extension, contentType)
|
2020-08-11 21:56:10 +00:00
|
|
|
return contentType, err
|
|
|
|
}
|
2020-11-04 18:11:40 +00:00
|
|
|
return "", webdav.ErrNotImplemented
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Readdir reads directory entries from the handle
|
2023-03-22 18:02:54 +00:00
|
|
|
func (f *webDavFile) Readdir(_ int) ([]os.FileInfo, error) {
|
2020-11-04 18:11:40 +00:00
|
|
|
if !f.Connection.User.HasPerm(dataprovider.PermListItems, f.GetVirtualPath()) {
|
|
|
|
return nil, f.Connection.GetPermissionDeniedError()
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
2022-11-03 07:31:40 +00:00
|
|
|
entries, err := f.Connection.ListDir(f.GetVirtualPath())
|
2020-11-04 18:11:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-03 07:31:40 +00:00
|
|
|
for idx, info := range entries {
|
|
|
|
entries[idx] = &webDavFileInfo{
|
|
|
|
FileInfo: info,
|
2020-11-04 18:11:40 +00:00
|
|
|
Fs: f.Fs,
|
2022-11-03 07:31:40 +00:00
|
|
|
virtualPath: path.Join(f.GetVirtualPath(), info.Name()),
|
|
|
|
fsPath: f.Fs.Join(f.GetFsPath(), info.Name()),
|
|
|
|
}
|
2020-11-04 18:11:40 +00:00
|
|
|
}
|
2022-11-03 07:31:40 +00:00
|
|
|
return entries, nil
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stat the handle
|
|
|
|
func (f *webDavFile) Stat() (os.FileInfo, error) {
|
2020-11-04 18:11:40 +00:00
|
|
|
if f.GetType() == common.TransferDownload && !f.Connection.User.HasPerm(dataprovider.PermListItems, path.Dir(f.GetVirtualPath())) {
|
|
|
|
return nil, f.Connection.GetPermissionDeniedError()
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
f.Lock()
|
|
|
|
errUpload := f.ErrTransfer
|
|
|
|
f.Unlock()
|
2020-11-04 18:11:40 +00:00
|
|
|
if f.GetType() == common.TransferUpload && errUpload == nil {
|
|
|
|
info := &webDavFileInfo{
|
2022-11-03 07:31:40 +00:00
|
|
|
FileInfo: vfs.NewFileInfo(f.GetFsPath(), false, f.BytesReceived.Load(), time.Now(), false),
|
2020-11-04 18:11:40 +00:00
|
|
|
Fs: f.Fs,
|
|
|
|
virtualPath: f.GetVirtualPath(),
|
|
|
|
fsPath: f.GetFsPath(),
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
return info, nil
|
|
|
|
}
|
2020-08-22 08:12:00 +00:00
|
|
|
info, err := f.Fs.Stat(f.GetFsPath())
|
2020-08-11 21:56:10 +00:00
|
|
|
if err != nil {
|
2023-04-08 12:55:04 +00:00
|
|
|
return nil, f.Connection.GetFsError(f.Fs, err)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
2020-12-05 12:48:13 +00:00
|
|
|
if vfs.IsCryptOsFs(f.Fs) {
|
|
|
|
info = f.Fs.(*vfs.CryptFs).ConvertFileInfo(info)
|
|
|
|
}
|
2020-11-04 18:11:40 +00:00
|
|
|
fi := &webDavFileInfo{
|
|
|
|
FileInfo: info,
|
|
|
|
Fs: f.Fs,
|
|
|
|
virtualPath: f.GetVirtualPath(),
|
|
|
|
fsPath: f.GetFsPath(),
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
2020-11-04 18:11:40 +00:00
|
|
|
return fi, nil
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 07:31:40 +00:00
|
|
|
func (f *webDavFile) checkFirstRead() error {
|
|
|
|
if !f.Connection.User.HasPerm(dataprovider.PermDownload, path.Dir(f.GetVirtualPath())) {
|
|
|
|
return f.Connection.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
transferQuota := f.BaseTransfer.GetTransferQuota()
|
|
|
|
if !transferQuota.HasDownloadSpace() {
|
|
|
|
f.Connection.Log(logger.LevelInfo, "denying file read due to quota limits")
|
|
|
|
return f.Connection.GetReadQuotaExceededError()
|
|
|
|
}
|
|
|
|
if ok, policy := f.Connection.User.IsFileAllowed(f.GetVirtualPath()); !ok {
|
2023-02-27 18:02:43 +00:00
|
|
|
f.Connection.Log(logger.LevelWarn, "reading file %q is not allowed", f.GetVirtualPath())
|
2022-11-03 07:31:40 +00:00
|
|
|
return f.Connection.GetErrorForDeniedFile(policy)
|
|
|
|
}
|
2023-01-02 14:59:00 +00:00
|
|
|
_, err := common.ExecutePreAction(f.Connection, common.OperationPreDownload, f.GetFsPath(), f.GetVirtualPath(), 0, 0)
|
2022-11-03 07:31:40 +00:00
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
f.Connection.Log(logger.LevelDebug, "download for file %q denied by pre action: %v", f.GetVirtualPath(), err)
|
2022-11-03 07:31:40 +00:00
|
|
|
return f.Connection.GetPermissionDeniedError()
|
|
|
|
}
|
2023-05-12 16:34:59 +00:00
|
|
|
f.readTried.Store(true)
|
2022-11-03 07:31:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
// Read reads the contents to downloads.
|
|
|
|
func (f *webDavFile) Read(p []byte) (n int, err error) {
|
2022-08-30 13:47:41 +00:00
|
|
|
if f.AbortTransfer.Load() {
|
2020-08-11 21:56:10 +00:00
|
|
|
return 0, errTransferAborted
|
|
|
|
}
|
2023-05-12 16:34:59 +00:00
|
|
|
if !f.readTried.Load() {
|
2022-11-03 07:31:40 +00:00
|
|
|
if err := f.checkFirstRead(); err != nil {
|
|
|
|
return 0, err
|
2020-11-04 18:11:40 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
f.Connection.UpdateLastActivity()
|
|
|
|
|
|
|
|
// the file is read sequentially we don't need to check for concurrent reads and so
|
|
|
|
// lock the transfer while opening the remote file
|
|
|
|
if f.reader == nil {
|
|
|
|
if f.GetType() != common.TransferDownload {
|
|
|
|
f.TransferError(common.ErrOpUnsupported)
|
|
|
|
return 0, common.ErrOpUnsupported
|
|
|
|
}
|
2022-11-03 07:31:40 +00:00
|
|
|
file, r, cancelFn, e := f.Fs.Open(f.GetFsPath(), 0)
|
2020-08-11 21:56:10 +00:00
|
|
|
f.Lock()
|
2020-11-05 08:30:38 +00:00
|
|
|
if e == nil {
|
2022-11-03 07:31:40 +00:00
|
|
|
if file != nil {
|
|
|
|
f.File = file
|
|
|
|
f.writer = f.File
|
|
|
|
f.reader = f.File
|
|
|
|
} else if r != nil {
|
|
|
|
f.reader = r
|
|
|
|
}
|
2020-11-05 08:30:38 +00:00
|
|
|
f.BaseTransfer.SetCancelFn(cancelFn)
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
f.ErrTransfer = e
|
2020-08-11 21:56:10 +00:00
|
|
|
f.startOffset = 0
|
|
|
|
f.Unlock()
|
2020-10-25 07:18:48 +00:00
|
|
|
if e != nil {
|
2023-04-08 12:55:04 +00:00
|
|
|
return 0, f.Connection.GetFsError(f.Fs, e)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 07:18:48 +00:00
|
|
|
n, err = f.reader.Read(p)
|
2022-08-30 13:47:41 +00:00
|
|
|
f.BytesSent.Add(int64(n))
|
2022-01-30 10:42:36 +00:00
|
|
|
if err == nil {
|
|
|
|
err = f.CheckRead()
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
f.TransferError(err)
|
2023-04-08 12:55:04 +00:00
|
|
|
err = f.ConvertError(err)
|
2020-10-25 07:18:48 +00:00
|
|
|
return
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
f.HandleThrottle()
|
2020-10-25 07:18:48 +00:00
|
|
|
return
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes the uploaded contents.
|
|
|
|
func (f *webDavFile) Write(p []byte) (n int, err error) {
|
2022-08-30 13:47:41 +00:00
|
|
|
if f.AbortTransfer.Load() {
|
2020-08-11 21:56:10 +00:00
|
|
|
return 0, errTransferAborted
|
|
|
|
}
|
|
|
|
|
|
|
|
f.Connection.UpdateLastActivity()
|
|
|
|
|
2020-10-25 07:18:48 +00:00
|
|
|
n, err = f.writer.Write(p)
|
2022-08-30 13:47:41 +00:00
|
|
|
f.BytesReceived.Add(int64(n))
|
2020-08-11 21:56:10 +00:00
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
if err == nil {
|
|
|
|
err = f.CheckWrite()
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
if err != nil {
|
|
|
|
f.TransferError(err)
|
2023-04-08 12:55:04 +00:00
|
|
|
err = f.ConvertError(err)
|
2020-10-25 07:18:48 +00:00
|
|
|
return
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
f.HandleThrottle()
|
2020-10-25 07:18:48 +00:00
|
|
|
return
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 18:11:40 +00:00
|
|
|
func (f *webDavFile) updateStatInfo() error {
|
|
|
|
if f.info != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
info, err := f.Fs.Stat(f.GetFsPath())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-05 12:48:13 +00:00
|
|
|
if vfs.IsCryptOsFs(f.Fs) {
|
|
|
|
info = f.Fs.(*vfs.CryptFs).ConvertFileInfo(info)
|
|
|
|
}
|
2020-11-04 18:11:40 +00:00
|
|
|
f.info = info
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-31 18:30:25 +00:00
|
|
|
func (f *webDavFile) updateTransferQuotaOnSeek() {
|
|
|
|
transferQuota := f.GetTransferQuota()
|
|
|
|
if transferQuota.HasSizeLimits() {
|
|
|
|
go func(ulSize, dlSize int64, user dataprovider.User) {
|
|
|
|
dataprovider.UpdateUserTransferQuota(&user, ulSize, dlSize, false) //nolint:errcheck
|
2022-08-30 13:47:41 +00:00
|
|
|
}(f.BytesReceived.Load(), f.BytesSent.Load(), f.Connection.User)
|
2022-01-31 18:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 07:31:40 +00:00
|
|
|
func (f *webDavFile) checkFile() error {
|
2023-05-16 16:08:14 +00:00
|
|
|
if f.File == nil && vfs.FsOpenReturnsFile(f.Fs) {
|
2022-11-03 07:31:40 +00:00
|
|
|
file, _, _, err := f.Fs.Open(f.GetFsPath(), 0)
|
|
|
|
if err != nil {
|
|
|
|
f.Connection.Log(logger.LevelWarn, "could not open file %q for seeking: %v",
|
|
|
|
f.GetFsPath(), err)
|
|
|
|
f.TransferError(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.File = file
|
|
|
|
f.reader = file
|
|
|
|
f.writer = file
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *webDavFile) seekFile(offset int64, whence int) (int64, error) {
|
|
|
|
ret, err := f.File.Seek(offset, whence)
|
|
|
|
if err != nil {
|
|
|
|
f.TransferError(err)
|
|
|
|
}
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
// Seek sets the offset for the next Read or Write on the writer to offset,
|
|
|
|
// interpreted according to whence: 0 means relative to the origin of the file,
|
|
|
|
// 1 means relative to the current offset, and 2 means relative to the end.
|
|
|
|
// It returns the new offset and an error, if any.
|
|
|
|
func (f *webDavFile) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
f.Connection.UpdateLastActivity()
|
2022-11-03 07:31:40 +00:00
|
|
|
if err := f.checkFile(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
if f.File != nil {
|
2022-11-03 07:31:40 +00:00
|
|
|
return f.seekFile(offset, whence)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
if f.GetType() == common.TransferDownload {
|
2022-08-30 13:47:41 +00:00
|
|
|
readOffset := f.startOffset + f.BytesSent.Load()
|
2020-08-11 21:56:10 +00:00
|
|
|
if offset == 0 && readOffset == 0 {
|
|
|
|
if whence == io.SeekStart {
|
|
|
|
return 0, nil
|
2020-11-04 18:11:40 +00:00
|
|
|
} else if whence == io.SeekEnd {
|
|
|
|
if err := f.updateStatInfo(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
return f.info.Size(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// close the reader and create a new one at startByte
|
|
|
|
if f.reader != nil {
|
|
|
|
f.reader.Close() //nolint:errcheck
|
2020-11-05 08:30:38 +00:00
|
|
|
f.reader = nil
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
startByte := int64(0)
|
2022-08-30 13:47:41 +00:00
|
|
|
f.BytesReceived.Store(0)
|
|
|
|
f.BytesSent.Store(0)
|
2022-01-31 18:30:25 +00:00
|
|
|
f.updateTransferQuotaOnSeek()
|
2020-08-11 21:56:10 +00:00
|
|
|
|
|
|
|
switch whence {
|
|
|
|
case io.SeekStart:
|
|
|
|
startByte = offset
|
|
|
|
case io.SeekCurrent:
|
|
|
|
startByte = readOffset + offset
|
|
|
|
case io.SeekEnd:
|
2020-11-04 18:11:40 +00:00
|
|
|
if err := f.updateStatInfo(); err != nil {
|
2020-08-11 21:56:10 +00:00
|
|
|
f.TransferError(err)
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-11-04 18:11:40 +00:00
|
|
|
startByte = f.info.Size() - offset
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 08:12:00 +00:00
|
|
|
_, r, cancelFn, err := f.Fs.Open(f.GetFsPath(), startByte)
|
2020-08-11 21:56:10 +00:00
|
|
|
|
|
|
|
f.Lock()
|
|
|
|
if err == nil {
|
|
|
|
f.startOffset = startByte
|
|
|
|
f.reader = r
|
|
|
|
}
|
|
|
|
f.ErrTransfer = err
|
|
|
|
f.BaseTransfer.SetCancelFn(cancelFn)
|
|
|
|
f.Unlock()
|
|
|
|
|
|
|
|
return startByte, err
|
|
|
|
}
|
|
|
|
return 0, common.ErrOpUnsupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the open directory or the current transfer
|
|
|
|
func (f *webDavFile) Close() error {
|
|
|
|
if err := f.setFinished(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err := f.closeIO()
|
|
|
|
if f.isTransfer() {
|
|
|
|
errBaseClose := f.BaseTransfer.Close()
|
|
|
|
if errBaseClose != nil {
|
|
|
|
err = errBaseClose
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
f.Connection.RemoveTransfer(f.BaseTransfer)
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
return f.Connection.GetFsError(f.Fs, err)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *webDavFile) closeIO() error {
|
|
|
|
var err error
|
|
|
|
if f.File != nil {
|
|
|
|
err = f.File.Close()
|
|
|
|
} else if f.writer != nil {
|
|
|
|
err = f.writer.Close()
|
|
|
|
f.Lock()
|
|
|
|
// we set ErrTransfer here so quota is not updated, in this case the uploads are atomic
|
|
|
|
if err != nil && f.ErrTransfer == nil {
|
|
|
|
f.ErrTransfer = err
|
|
|
|
}
|
|
|
|
f.Unlock()
|
|
|
|
} else if f.reader != nil {
|
|
|
|
err = f.reader.Close()
|
2023-08-12 16:51:47 +00:00
|
|
|
if metadater, ok := f.reader.(vfs.Metadater); ok {
|
|
|
|
f.BaseTransfer.SetMetadata(metadater.Metadata())
|
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *webDavFile) setFinished() error {
|
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
2021-05-06 19:35:43 +00:00
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
if f.isFinished {
|
|
|
|
return common.ErrTransferClosed
|
|
|
|
}
|
|
|
|
f.isFinished = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *webDavFile) isTransfer() bool {
|
|
|
|
if f.GetType() == common.TransferDownload {
|
2023-05-12 16:34:59 +00:00
|
|
|
return f.readTried.Load()
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2022-10-11 17:20:58 +00:00
|
|
|
|
|
|
|
// DeadProps returns a copy of the dead properties held.
|
|
|
|
// We always return nil for now, we only support the last modification time
|
|
|
|
// and it is already included in "live" properties
|
|
|
|
func (f *webDavFile) DeadProps() (map[xml.Name]webdav.Property, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Patch patches the dead properties held.
|
|
|
|
// In our minimal implementation we just support Win32LastModifiedTime and
|
|
|
|
// getlastmodified to set the the modification time.
|
|
|
|
// We ignore any other property and just return an OK response if the patch sets
|
|
|
|
// the modification time, otherwise a Forbidden response
|
|
|
|
func (f *webDavFile) Patch(patches []webdav.Proppatch) ([]webdav.Propstat, error) {
|
|
|
|
resp := make([]webdav.Propstat, 0, len(patches))
|
|
|
|
hasError := false
|
|
|
|
for _, patch := range patches {
|
|
|
|
status := http.StatusForbidden
|
|
|
|
pstat := webdav.Propstat{}
|
|
|
|
for _, p := range patch.Props {
|
|
|
|
if status == http.StatusForbidden && !hasError {
|
|
|
|
if !patch.Remove && util.Contains(lastModifiedProps, p.XMLName.Local) {
|
2023-05-16 16:51:42 +00:00
|
|
|
parsed, err := parseTime(string(p.InnerXML))
|
2022-10-11 17:20:58 +00:00
|
|
|
if err != nil {
|
|
|
|
f.Connection.Log(logger.LevelWarn, "unsupported last modification time: %q, err: %v",
|
|
|
|
string(p.InnerXML), err)
|
|
|
|
hasError = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
attrs := &common.StatAttributes{
|
|
|
|
Flags: common.StatAttrTimes,
|
|
|
|
Atime: parsed,
|
|
|
|
Mtime: parsed,
|
|
|
|
}
|
|
|
|
if err := f.Connection.SetStat(f.GetVirtualPath(), attrs); err != nil {
|
|
|
|
f.Connection.Log(logger.LevelWarn, "unable to set modification time for %q, err :%v",
|
|
|
|
f.GetVirtualPath(), err)
|
|
|
|
hasError = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
status = http.StatusOK
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pstat.Props = append(pstat.Props, webdav.Property{XMLName: p.XMLName})
|
|
|
|
}
|
|
|
|
pstat.Status = status
|
|
|
|
resp = append(resp, pstat)
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|