"crypto/rand"
"errors"
"fmt"
+ "io"
prand "math/rand"
- "net/http"
"os"
+ "path"
+ "strings"
"sync/atomic"
"time"
+ "git.curoverse.com/arvados.git/sdk/go/arvados"
+
"golang.org/x/net/context"
"golang.org/x/net/webdav"
)
errReadOnly = errors.New("read-only filesystem")
)
-// webdavFS implements a read-only webdav.FileSystem by wrapping
-// http.Filesystem.
+// 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 {
- httpfs http.FileSystem
+ 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
}
-var _ webdav.FileSystem = &webdavFS{}
+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 {
- return errReadOnly
+ 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) (webdav.File, error) {
- f, err := fs.httpfs.Open(name)
- if err != nil {
- return nil, err
+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|os.O_TRUNC) != 0
+ if writing {
+ 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}
}
- return &webdavFile{File: f}, nil
+ 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 {
- return errReadOnly
+ if !fs.writing {
+ return errReadOnly
+ }
+ fs.makeparents(newName)
+ return fs.collfs.Rename(oldName, newName)
}
func (fs *webdavFS) Stat(ctx context.Context, name string) (os.FileInfo, error) {
- if f, err := fs.httpfs.Open(name); err != nil {
- return nil, err
- } else {
- return f.Stat()
+ if fs.writing {
+ fs.makeparents(name)
}
+ return fs.collfs.Stat(name)
}
-// webdavFile implements a read-only webdav.File by wrapping
-// http.File. Writes fail.
-type webdavFile struct {
- http.File
+type writeFailer struct {
+ webdav.File
+ err error
}
-func (f *webdavFile) Write([]byte) (int, error) {
- return 0, errReadOnly
+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
// However, it does also permit impossible operations, like acquiring
// conflicting locks and releasing non-existent locks. This might
// confuse some clients if they try to probe for correctness.
+//
+// Currently this is a moot point: the LOCK and UNLOCK methods are not
+// accepted by keep-web, so it suffices to implement the
+// webdav.LockSystem interface.
type noLockSystem struct{}
func (*noLockSystem) Confirm(time.Time, string, string, ...webdav.Condition) (func(), error) {