1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 // Package webdavfs adds special behaviors to an arvados.FileSystem so
6 // it's suitable to use with a webdav server.
21 "git.arvados.org/arvados.git/sdk/go/arvados"
22 "golang.org/x/net/webdav"
26 lockPrefix string = uuid()
27 nextLockSuffix int64 = prand.Int63()
28 ErrReadOnly = errors.New("read-only filesystem")
31 // FS implements a webdav.FileSystem by wrapping an
32 // arvados.CollectionFilesystem.
34 FileSystem arvados.FileSystem
35 // Prefix works like fs.Sub: Stat(name) calls
36 // Stat(prefix+name) in the wrapped filesystem.
38 // If Writing is false, all write operations return errors.
39 // (Opening a file for writing succeeds -- otherwise webdav
40 // would return 404 -- but writing to it fails.)
42 // webdav PROPFIND reads the first few bytes of each file
43 // whose filename extension isn't recognized, which is
44 // prohibitively expensive: we end up fetching multiple 64MiB
45 // blocks. Avoid this by returning EOF on all reads when
46 // handling a PROPFIND.
50 func (fs *FS) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
54 name = strings.TrimRight(name, "/")
55 return fs.FileSystem.Mkdir(fs.Prefix+name, 0755)
58 func (fs *FS) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (f webdav.File, err error) {
59 writing := flag&(os.O_WRONLY|os.O_RDWR|os.O_TRUNC) != 0
60 f, err = fs.FileSystem.OpenFile(fs.Prefix+name, flag, perm)
62 // webdav module returns 404 on all OpenFile errors,
63 // but returns 405 Method Not Allowed if OpenFile()
64 // succeeds but Write() or Close() fails. We'd rather
65 // have 405. writeFailer ensures Close() fails if the
66 // file is opened for writing *or* Write() is called.
71 f = writeFailer{File: f, err: err}
79 func (fs *FS) RemoveAll(ctx context.Context, name string) error {
80 return fs.FileSystem.RemoveAll(fs.Prefix + name)
83 func (fs *FS) Rename(ctx context.Context, oldName, newName string) error {
87 if strings.HasSuffix(oldName, "/") {
88 // WebDAV "MOVE foo/ bar/" means rename foo to bar.
89 oldName = oldName[:len(oldName)-1]
90 newName = strings.TrimSuffix(newName, "/")
92 return fs.FileSystem.Rename(fs.Prefix+oldName, fs.Prefix+newName)
95 func (fs *FS) Stat(ctx context.Context, name string) (os.FileInfo, error) {
96 return fs.FileSystem.Stat(fs.Prefix + name)
99 type writeFailer struct {
104 func (wf writeFailer) Write([]byte) (int, error) {
109 func (wf writeFailer) Close() error {
110 err := wf.File.Close()
117 type readEOF struct {
121 func (readEOF) Read(p []byte) (int, error) {
125 // NoLockSystem implements webdav.LockSystem by returning success for
126 // every possible locking operation, even though it has no side
127 // effects such as actually locking anything. This works for a
128 // read-only webdav filesystem because webdav locks only apply to
131 // This is more suitable than webdav.NewMemLS() for two reasons:
132 // First, it allows keep-web to use one locker for all collections
133 // even though coll1.vhost/foo and coll2.vhost/foo have the same path
134 // but represent different resources. Additionally, it returns valid
135 // tokens (rfc2518 specifies that tokens are represented as URIs and
136 // are unique across all resources for all time), which might improve
137 // client compatibility.
139 // However, it does also permit impossible operations, like acquiring
140 // conflicting locks and releasing non-existent locks. This might
141 // confuse some clients if they try to probe for correctness.
143 // Currently this is a moot point: the LOCK and UNLOCK methods are not
144 // accepted by keep-web, so it suffices to implement the
145 // webdav.LockSystem interface.
146 var NoLockSystem = noLockSystem{}
148 type noLockSystem struct{}
150 func (noLockSystem) Confirm(time.Time, string, string, ...webdav.Condition) (func(), error) {
154 func (noLockSystem) Create(now time.Time, details webdav.LockDetails) (token string, err error) {
155 return fmt.Sprintf("opaquelocktoken:%s-%x", lockPrefix, atomic.AddInt64(&nextLockSuffix, 1)), nil
158 func (noLockSystem) Refresh(now time.Time, token string, duration time.Duration) (webdav.LockDetails, error) {
159 return webdav.LockDetails{}, nil
162 func (noLockSystem) Unlock(now time.Time, token string) error {
168 // Return a version 1 variant 4 UUID, meaning all bits are random
169 // except the ones indicating the version and variant.
172 if _, err := rand.Read(data[:]); err != nil {
176 data[8] = data[8]&0x3f | 0x80
178 data[6] = data[6]&0x0f | 0x40
179 return fmt.Sprintf("%x-%x-%x-%x-%x", data[0:4], data[4:6], data[6:8], data[8:10], data[10:])