X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/e789dc0f82a18f7e5830bc1a3647b353035814db..42c20b25e1325124b88e3b9b285544dc41122b56:/services/keep-web/webdav.go diff --git a/services/keep-web/webdav.go b/services/keep-web/webdav.go index 4a9476dc99..501c355a73 100644 --- a/services/keep-web/webdav.go +++ b/services/keep-web/webdav.go @@ -2,18 +2,21 @@ // // SPDX-License-Identifier: AGPL-3.0 -package main +package keepweb import ( "crypto/rand" "errors" "fmt" + "io" prand "math/rand" "os" + "path" + "strings" "sync/atomic" "time" - "git.curoverse.com/arvados.git/sdk/go/arvados" + "git.arvados.org/arvados.git/sdk/go/arvados" "golang.org/x/net/context" "golang.org/x/net/webdav" @@ -27,52 +30,116 @@ var ( // webdavFS implements a webdav.FileSystem by wrapping an // arvados.CollectionFilesystem. +// +// Collections don't preserve empty directories, so Mkdir is +// effectively a no-op, and we need to make parent dirs spring into +// existence automatically so sequences like "mkcol foo; put foo/bar" +// work as expected. type webdavFS struct { - collfs arvados.CollectionFileSystem - update func() error + collfs arvados.FileSystem + writing bool + // webdav PROPFIND reads the first few bytes of each file + // whose filename extension isn't recognized, which is + // prohibitively expensive: we end up fetching multiple 64MiB + // blocks. Avoid this by returning EOF on all reads when + // handling a PROPFIND. + alwaysReadEOF bool +} + +func (fs *webdavFS) makeparents(name string) { + if !fs.writing { + return + } + dir, _ := path.Split(name) + if dir == "" || dir == "/" { + return + } + dir = dir[:len(dir)-1] + fs.makeparents(dir) + fs.collfs.Mkdir(dir, 0755) } func (fs *webdavFS) Mkdir(ctx context.Context, name string, perm os.FileMode) error { + if !fs.writing { + return errReadOnly + } + name = strings.TrimRight(name, "/") + fs.makeparents(name) return fs.collfs.Mkdir(name, 0755) } func (fs *webdavFS) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (f webdav.File, err error) { - writing := flag&(os.O_WRONLY|os.O_RDWR) != 0 - if writing && fs.update == nil { - return nil, errReadOnly + writing := flag&(os.O_WRONLY|os.O_RDWR|os.O_TRUNC) != 0 + if writing { + fs.makeparents(name) } f, err = fs.collfs.OpenFile(name, flag, perm) - if writing && err == nil { - f = writingFile{File: f, update: fs.update} + if !fs.writing { + // webdav module returns 404 on all OpenFile errors, + // but returns 405 Method Not Allowed if OpenFile() + // succeeds but Write() or Close() fails. We'd rather + // have 405. writeFailer ensures Close() fails if the + // file is opened for writing *or* Write() is called. + var err error + if writing { + err = errReadOnly + } + f = writeFailer{File: f, err: err} + } + if fs.alwaysReadEOF { + f = readEOF{File: f} } return } func (fs *webdavFS) RemoveAll(ctx context.Context, name string) error { - return errReadOnly + return fs.collfs.RemoveAll(name) } func (fs *webdavFS) Rename(ctx context.Context, oldName, newName string) error { - if fs.update == nil { + if !fs.writing { return errReadOnly } - return fs.Rename(oldName, newName) + if strings.HasSuffix(oldName, "/") { + // WebDAV "MOVE foo/ bar/" means rename foo to bar. + oldName = oldName[:len(oldName)-1] + newName = strings.TrimSuffix(newName, "/") + } + fs.makeparents(newName) + return fs.collfs.Rename(oldName, newName) } func (fs *webdavFS) Stat(ctx context.Context, name string) (os.FileInfo, error) { + if fs.writing { + fs.makeparents(name) + } return fs.collfs.Stat(name) } -type writingFile struct { +type writeFailer struct { webdav.File - update func() error + err error +} + +func (wf writeFailer) Write([]byte) (int, error) { + wf.err = errReadOnly + return 0, wf.err } -func (f writingFile) Close() error { - if err := f.File.Close(); err != nil || f.update == nil { - return err +func (wf writeFailer) Close() error { + err := wf.File.Close() + if err != nil { + wf.err = err } - return f.update() + return wf.err +} + +type readEOF struct { + webdav.File +} + +func (readEOF) Read(p []byte) (int, error) { + return 0, io.EOF } // noLockSystem implements webdav.LockSystem by returning success for