Merge branch 'master' of git.curoverse.com:arvados into 11876-r-sdk
[arvados.git] / services / keep-web / webdav.go
index 1a306e37e9d662ad62047dbb507f9e58e80d7698..af83681f9c4b435285ebe1e6f3b9c1a1f7a807a4 100644 (file)
@@ -8,6 +8,7 @@ import (
        "crypto/rand"
        "errors"
        "fmt"
+       "io"
        prand "math/rand"
        "os"
        "path"
@@ -35,8 +36,14 @@ var (
 // existence automatically so sequences like "mkcol foo; put foo/bar"
 // work as expected.
 type webdavFS struct {
-       collfs arvados.CollectionFileSystem
-       update func() error
+       collfs  arvados.CollectionFileSystem
+       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) {
@@ -50,67 +57,71 @@ func (fs *webdavFS) makeparents(name string) {
 }
 
 func (fs *webdavFS) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
-       if fs.update == nil {
+       if !fs.writing {
                return errReadOnly
        }
        name = strings.TrimRight(name, "/")
        fs.makeparents(name)
-       if err := fs.collfs.Mkdir(name, 0755); err != nil {
-               return err
-       }
-       return fs.update()
+       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 {
-               if fs.update == nil {
-                       return nil, errReadOnly
-               }
                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.
+               f = writeFailer{File: f, err: errReadOnly}
+       }
+       if fs.alwaysReadEOF {
+               f = readEOF{File: f}
        }
        return
 }
 
 func (fs *webdavFS) RemoveAll(ctx context.Context, name string) error {
-       if err := fs.collfs.RemoveAll(name); err != nil {
-               return err
-       }
-       return fs.update()
+       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
        }
        fs.makeparents(newName)
-       if err := fs.collfs.Rename(oldName, newName); err != nil {
-               return err
-       }
-       return fs.update()
+       return fs.collfs.Rename(oldName, newName)
 }
 
 func (fs *webdavFS) Stat(ctx context.Context, name string) (os.FileInfo, error) {
-       if fs.update != nil {
+       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 (f writingFile) Close() error {
-       if err := f.File.Close(); err != nil || f.update == nil {
-               return err
-       }
-       return f.update()
+func (wf writeFailer) Write([]byte) (int, error) {
+       return 0, wf.err
+}
+
+func (wf writeFailer) Close() error {
+       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