1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "golang.org/x/net/context"
18 "golang.org/x/net/webdav"
22 lockPrefix string = uuid()
23 nextLockSuffix int64 = prand.Int63()
24 errReadOnly = errors.New("read-only filesystem")
27 // webdavFS implements a read-only webdav.FileSystem by wrapping
29 type webdavFS struct {
30 httpfs http.FileSystem
33 var _ webdav.FileSystem = &webdavFS{}
35 func (fs *webdavFS) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
39 func (fs *webdavFS) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) {
40 f, err := fs.httpfs.Open(name)
44 return &webdavFile{File: f}, nil
47 func (fs *webdavFS) RemoveAll(ctx context.Context, name string) error {
51 func (fs *webdavFS) Rename(ctx context.Context, oldName, newName string) error {
55 func (fs *webdavFS) Stat(ctx context.Context, name string) (os.FileInfo, error) {
56 if f, err := fs.httpfs.Open(name); err != nil {
63 // webdavFile implements a read-only webdav.File by wrapping
64 // http.File. Writes fail.
65 type webdavFile struct {
69 func (f *webdavFile) Write([]byte) (int, error) {
73 // noLockSystem implements webdav.LockSystem by returning success for
74 // every possible locking operation, even though it has no side
75 // effects such as actually locking anything. This works for a
76 // read-only webdav filesystem because webdav locks only apply to
79 // This is more suitable than webdav.NewMemLS() for two reasons:
80 // First, it allows keep-web to use one locker for all collections
81 // even though coll1.vhost/foo and coll2.vhost/foo have the same path
82 // but represent different resources. Additionally, it returns valid
83 // tokens (rfc2518 specifies that tokens are represented as URIs and
84 // are unique across all resources for all time), which might improve
85 // client compatibility.
87 // However, it does also permit impossible operations, like acquiring
88 // conflicting locks and releasing non-existent locks. This might
89 // confuse some clients if they try to probe for correctness.
90 type noLockSystem struct{}
92 func (*noLockSystem) Confirm(time.Time, string, string, ...webdav.Condition) (func(), error) {
96 func (*noLockSystem) Create(now time.Time, details webdav.LockDetails) (token string, err error) {
97 return fmt.Sprintf("opaquelocktoken:%s-%x", lockPrefix, atomic.AddInt64(&nextLockSuffix, 1)), nil
100 func (*noLockSystem) Refresh(now time.Time, token string, duration time.Duration) (webdav.LockDetails, error) {
101 return webdav.LockDetails{}, nil
104 func (*noLockSystem) Unlock(now time.Time, token string) error {
110 // Return a version 1 variant 4 UUID, meaning all bits are random
111 // except the ones indicating the version and variant.
114 if _, err := rand.Read(data[:]); err != nil {
118 data[8] = data[8]&0x3f | 0x80
120 data[6] = data[6]&0x0f | 0x40
121 return fmt.Sprintf("%x-%x-%x-%x-%x", data[0:4], data[4:6], data[6:8], data[8:10], data[10:])