1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.arvados.org/arvados.git/sdk/go/arvados"
21 "golang.org/x/net/context"
22 "golang.org/x/net/webdav"
26 lockPrefix string = uuid()
27 nextLockSuffix int64 = prand.Int63()
28 errReadOnly = errors.New("read-only filesystem")
31 // webdavFS implements a webdav.FileSystem by wrapping an
32 // arvados.CollectionFilesystem.
34 // Collections don't preserve empty directories, so Mkdir is
35 // effectively a no-op, and we need to make parent dirs spring into
36 // existence automatically so sequences like "mkcol foo; put foo/bar"
38 type webdavFS struct {
39 collfs arvados.FileSystem
40 // prefix works like fs.Sub: Stat(name) calls
41 // Stat(prefix+name) in the wrapped filesystem.
44 // webdav PROPFIND reads the first few bytes of each file
45 // whose filename extension isn't recognized, which is
46 // prohibitively expensive: we end up fetching multiple 64MiB
47 // blocks. Avoid this by returning EOF on all reads when
48 // handling a PROPFIND.
52 func (fs *webdavFS) makeparents(name string) {
56 dir, _ := path.Split(name)
57 if dir == "" || dir == "/" {
60 dir = dir[:len(dir)-1]
62 fs.collfs.Mkdir(fs.prefix+dir, 0755)
65 func (fs *webdavFS) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
69 name = strings.TrimRight(name, "/")
71 return fs.collfs.Mkdir(fs.prefix+name, 0755)
74 func (fs *webdavFS) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (f webdav.File, err error) {
75 writing := flag&(os.O_WRONLY|os.O_RDWR|os.O_TRUNC) != 0
79 f, err = fs.collfs.OpenFile(fs.prefix+name, flag, perm)
81 // webdav module returns 404 on all OpenFile errors,
82 // but returns 405 Method Not Allowed if OpenFile()
83 // succeeds but Write() or Close() fails. We'd rather
84 // have 405. writeFailer ensures Close() fails if the
85 // file is opened for writing *or* Write() is called.
90 f = writeFailer{File: f, err: err}
98 func (fs *webdavFS) RemoveAll(ctx context.Context, name string) error {
99 return fs.collfs.RemoveAll(fs.prefix + name)
102 func (fs *webdavFS) Rename(ctx context.Context, oldName, newName string) error {
106 if strings.HasSuffix(oldName, "/") {
107 // WebDAV "MOVE foo/ bar/" means rename foo to bar.
108 oldName = oldName[:len(oldName)-1]
109 newName = strings.TrimSuffix(newName, "/")
111 fs.makeparents(newName)
112 return fs.collfs.Rename(fs.prefix+oldName, fs.prefix+newName)
115 func (fs *webdavFS) Stat(ctx context.Context, name string) (os.FileInfo, error) {
119 return fs.collfs.Stat(fs.prefix + name)
122 type writeFailer struct {
127 func (wf writeFailer) Write([]byte) (int, error) {
132 func (wf writeFailer) Close() error {
133 err := wf.File.Close()
140 type readEOF struct {
144 func (readEOF) Read(p []byte) (int, error) {
148 // noLockSystem implements webdav.LockSystem by returning success for
149 // every possible locking operation, even though it has no side
150 // effects such as actually locking anything. This works for a
151 // read-only webdav filesystem because webdav locks only apply to
154 // This is more suitable than webdav.NewMemLS() for two reasons:
155 // First, it allows keep-web to use one locker for all collections
156 // even though coll1.vhost/foo and coll2.vhost/foo have the same path
157 // but represent different resources. Additionally, it returns valid
158 // tokens (rfc2518 specifies that tokens are represented as URIs and
159 // are unique across all resources for all time), which might improve
160 // client compatibility.
162 // However, it does also permit impossible operations, like acquiring
163 // conflicting locks and releasing non-existent locks. This might
164 // confuse some clients if they try to probe for correctness.
166 // Currently this is a moot point: the LOCK and UNLOCK methods are not
167 // accepted by keep-web, so it suffices to implement the
168 // webdav.LockSystem interface.
169 type noLockSystem struct{}
171 func (*noLockSystem) Confirm(time.Time, string, string, ...webdav.Condition) (func(), error) {
175 func (*noLockSystem) Create(now time.Time, details webdav.LockDetails) (token string, err error) {
176 return fmt.Sprintf("opaquelocktoken:%s-%x", lockPrefix, atomic.AddInt64(&nextLockSuffix, 1)), nil
179 func (*noLockSystem) Refresh(now time.Time, token string, duration time.Duration) (webdav.LockDetails, error) {
180 return webdav.LockDetails{}, nil
183 func (*noLockSystem) Unlock(now time.Time, token string) error {
189 // Return a version 1 variant 4 UUID, meaning all bits are random
190 // except the ones indicating the version and variant.
193 if _, err := rand.Read(data[:]); err != nil {
197 data[8] = data[8]&0x3f | 0x80
199 data[6] = data[6]&0x0f | 0x40
200 return fmt.Sprintf("%x-%x-%x-%x-%x", data[0:4], data[4:6], data[6:8], data[8:10], data[10:])