X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/70111d82653ca633d2397c74eb994a8a9d718e80..79bce4a71a58118a9003882e0ca9bbfb9d2957a9:/services/keep-web/webdav.go diff --git a/services/keep-web/webdav.go b/services/keep-web/webdav.go index 3e12f27e0d..a7b7980995 100644 --- a/services/keep-web/webdav.go +++ b/services/keep-web/webdav.go @@ -8,6 +8,7 @@ import ( "crypto/rand" "errors" "fmt" + "io" prand "math/rand" "os" "path" @@ -15,7 +16,7 @@ import ( "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" @@ -35,12 +36,21 @@ var ( // existence automatically so sequences like "mkcol foo; put foo/bar" // work as expected. type webdavFS struct { - collfs arvados.CollectionFileSystem + 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) { - dir, name := path.Split(name) + if !fs.writing { + return + } + dir, _ := path.Split(name) if dir == "" || dir == "/" { return } @@ -59,14 +69,26 @@ func (fs *webdavFS) Mkdir(ctx context.Context, name string, perm os.FileMode) er } 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 + writing := flag&(os.O_WRONLY|os.O_RDWR|os.O_TRUNC) != 0 if writing { - if !fs.writing { - return nil, errReadOnly - } fs.makeparents(name) } f, err = fs.collfs.OpenFile(name, flag, perm) + 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 } @@ -78,6 +100,11 @@ func (fs *webdavFS) Rename(ctx context.Context, oldName, newName string) error { if !fs.writing { return errReadOnly } + 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) } @@ -89,6 +116,32 @@ func (fs *webdavFS) Stat(ctx context.Context, name string) (os.FileInfo, error) return fs.collfs.Stat(name) } +type writeFailer struct { + webdav.File + err error +} + +func (wf writeFailer) Write([]byte) (int, error) { + wf.err = errReadOnly + return 0, wf.err +} + +func (wf writeFailer) Close() error { + err := wf.File.Close() + if err != nil { + wf.err = err + } + 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 // every possible locking operation, even though it has no side // effects such as actually locking anything. This works for a