1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 "git.curoverse.com/arvados.git/sdk/go/arvados"
18 "golang.org/x/net/context"
19 "golang.org/x/net/webdav"
23 lockPrefix string = uuid()
24 nextLockSuffix int64 = prand.Int63()
25 errReadOnly = errors.New("read-only filesystem")
28 // webdavFS implements a webdav.FileSystem by wrapping an
29 // arvados.CollectionFilesystem.
30 type webdavFS struct {
31 collfs arvados.CollectionFileSystem
35 func (fs *webdavFS) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
36 return fs.collfs.Mkdir(name, 0755)
39 func (fs *webdavFS) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (f webdav.File, err error) {
40 writing := flag&(os.O_WRONLY|os.O_RDWR) != 0
41 if writing && fs.update == nil {
42 return nil, errReadOnly
44 f, err = fs.collfs.OpenFile(name, flag, perm)
45 if writing && err == nil {
46 f = writingFile{File: f, update: fs.update}
51 func (fs *webdavFS) RemoveAll(ctx context.Context, name string) error {
52 return fs.collfs.RemoveAll(name)
55 func (fs *webdavFS) Rename(ctx context.Context, oldName, newName string) error {
59 return fs.collfs.Rename(oldName, newName)
62 func (fs *webdavFS) Stat(ctx context.Context, name string) (os.FileInfo, error) {
63 return fs.collfs.Stat(name)
66 type writingFile struct {
71 func (f writingFile) Close() error {
72 if err := f.File.Close(); err != nil || f.update == nil {
78 // noLockSystem implements webdav.LockSystem by returning success for
79 // every possible locking operation, even though it has no side
80 // effects such as actually locking anything. This works for a
81 // read-only webdav filesystem because webdav locks only apply to
84 // This is more suitable than webdav.NewMemLS() for two reasons:
85 // First, it allows keep-web to use one locker for all collections
86 // even though coll1.vhost/foo and coll2.vhost/foo have the same path
87 // but represent different resources. Additionally, it returns valid
88 // tokens (rfc2518 specifies that tokens are represented as URIs and
89 // are unique across all resources for all time), which might improve
90 // client compatibility.
92 // However, it does also permit impossible operations, like acquiring
93 // conflicting locks and releasing non-existent locks. This might
94 // confuse some clients if they try to probe for correctness.
96 // Currently this is a moot point: the LOCK and UNLOCK methods are not
97 // accepted by keep-web, so it suffices to implement the
98 // webdav.LockSystem interface.
99 type noLockSystem struct{}
101 func (*noLockSystem) Confirm(time.Time, string, string, ...webdav.Condition) (func(), error) {
105 func (*noLockSystem) Create(now time.Time, details webdav.LockDetails) (token string, err error) {
106 return fmt.Sprintf("opaquelocktoken:%s-%x", lockPrefix, atomic.AddInt64(&nextLockSuffix, 1)), nil
109 func (*noLockSystem) Refresh(now time.Time, token string, duration time.Duration) (webdav.LockDetails, error) {
110 return webdav.LockDetails{}, nil
113 func (*noLockSystem) Unlock(now time.Time, token string) error {
119 // Return a version 1 variant 4 UUID, meaning all bits are random
120 // except the ones indicating the version and variant.
123 if _, err := rand.Read(data[:]); err != nil {
127 data[8] = data[8]&0x3f | 0x80
129 data[6] = data[6]&0x0f | 0x40
130 return fmt.Sprintf("%x-%x-%x-%x-%x", data[0:4], data[4:6], data[6:8], data[8:10], data[10:])