1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
18 "git.curoverse.com/arvados.git/sdk/go/arvados"
20 "golang.org/x/net/context"
21 "golang.org/x/net/webdav"
25 lockPrefix string = uuid()
26 nextLockSuffix int64 = prand.Int63()
27 errReadOnly = errors.New("read-only filesystem")
30 // webdavFS implements a webdav.FileSystem by wrapping an
31 // arvados.CollectionFilesystem.
33 // Collections don't preserve empty directories, so Mkdir is
34 // effectively a no-op, and we need to make parent dirs spring into
35 // existence automatically so sequences like "mkcol foo; put foo/bar"
37 type webdavFS struct {
38 collfs arvados.CollectionFileSystem
42 func (fs *webdavFS) makeparents(name string) {
43 dir, name := path.Split(name)
44 if dir == "" || dir == "/" {
47 dir = dir[:len(dir)-1]
49 fs.collfs.Mkdir(dir, 0755)
52 func (fs *webdavFS) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
56 name = strings.TrimRight(name, "/")
58 if err := fs.collfs.Mkdir(name, 0755); err != nil {
64 func (fs *webdavFS) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (f webdav.File, err error) {
65 writing := flag&(os.O_WRONLY|os.O_RDWR) != 0
68 return nil, errReadOnly
72 f, err = fs.collfs.OpenFile(name, flag, perm)
73 if writing && err == nil {
74 f = writingFile{File: f, update: fs.update}
79 func (fs *webdavFS) RemoveAll(ctx context.Context, name string) error {
80 if err := fs.collfs.RemoveAll(name); err != nil {
86 func (fs *webdavFS) Rename(ctx context.Context, oldName, newName string) error {
90 fs.makeparents(newName)
91 if err := fs.collfs.Rename(oldName, newName); err != nil {
97 func (fs *webdavFS) Stat(ctx context.Context, name string) (os.FileInfo, error) {
101 return fs.collfs.Stat(name)
104 type writingFile struct {
109 func (f writingFile) Close() error {
110 if err := f.File.Close(); err != nil || f.update == nil {
116 // noLockSystem implements webdav.LockSystem by returning success for
117 // every possible locking operation, even though it has no side
118 // effects such as actually locking anything. This works for a
119 // read-only webdav filesystem because webdav locks only apply to
122 // This is more suitable than webdav.NewMemLS() for two reasons:
123 // First, it allows keep-web to use one locker for all collections
124 // even though coll1.vhost/foo and coll2.vhost/foo have the same path
125 // but represent different resources. Additionally, it returns valid
126 // tokens (rfc2518 specifies that tokens are represented as URIs and
127 // are unique across all resources for all time), which might improve
128 // client compatibility.
130 // However, it does also permit impossible operations, like acquiring
131 // conflicting locks and releasing non-existent locks. This might
132 // confuse some clients if they try to probe for correctness.
134 // Currently this is a moot point: the LOCK and UNLOCK methods are not
135 // accepted by keep-web, so it suffices to implement the
136 // webdav.LockSystem interface.
137 type noLockSystem struct{}
139 func (*noLockSystem) Confirm(time.Time, string, string, ...webdav.Condition) (func(), error) {
143 func (*noLockSystem) Create(now time.Time, details webdav.LockDetails) (token string, err error) {
144 return fmt.Sprintf("opaquelocktoken:%s-%x", lockPrefix, atomic.AddInt64(&nextLockSuffix, 1)), nil
147 func (*noLockSystem) Refresh(now time.Time, token string, duration time.Duration) (webdav.LockDetails, error) {
148 return webdav.LockDetails{}, nil
151 func (*noLockSystem) Unlock(now time.Time, token string) error {
157 // Return a version 1 variant 4 UUID, meaning all bits are random
158 // except the ones indicating the version and variant.
161 if _, err := rand.Read(data[:]); err != nil {
165 data[8] = data[8]&0x3f | 0x80
167 data[6] = data[6]&0x0f | 0x40
168 return fmt.Sprintf("%x-%x-%x-%x-%x", data[0:4], data[4:6], data[6:8], data[8:10], data[10:])