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 read-only webdav.FileSystem by wrapping an
31 // arvados.CollectionFilesystem.
32 type webdavFS struct {
33 collfs arvados.CollectionFileSystem
36 var _ webdav.FileSystem = &webdavFS{}
38 func (fs *webdavFS) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
42 func (fs *webdavFS) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) {
43 fi, err := fs.collfs.Stat(name)
47 return &webdavFile{collfs: fs.collfs, fileInfo: fi, name: name}, nil
50 func (fs *webdavFS) RemoveAll(ctx context.Context, name string) error {
54 func (fs *webdavFS) Rename(ctx context.Context, oldName, newName string) error {
58 func (fs *webdavFS) Stat(ctx context.Context, name string) (os.FileInfo, error) {
59 return fs.collfs.Stat(name)
62 // webdavFile implements a read-only webdav.File by wrapping
65 // The http.File is opened from an arvados.CollectionFileSystem, but
66 // not until Seek, Read, or Readdir is called. This deferred-open
67 // strategy makes webdav's OpenFile-Stat-Close cycle fast even though
68 // the collfs's Open method is slow. This is relevant because webdav
69 // does OpenFile-Stat-Close on each file when preparing directory
72 // Writes to a webdavFile always fail.
73 type webdavFile struct {
74 // fields populated by (*webdavFS).OpenFile()
75 collfs http.FileSystem
85 func (f *webdavFile) load() {
86 f.file, f.err = f.collfs.Open(f.name)
89 func (f *webdavFile) Write([]byte) (int, error) {
93 func (f *webdavFile) Seek(offset int64, whence int) (int64, error) {
98 return f.file.Seek(offset, whence)
101 func (f *webdavFile) Read(buf []byte) (int, error) {
102 f.loadOnce.Do(f.load)
106 return f.file.Read(buf)
109 func (f *webdavFile) Close() error {
111 // We never called load(), or load() failed
114 return f.file.Close()
117 func (f *webdavFile) Readdir(n int) ([]os.FileInfo, error) {
118 f.loadOnce.Do(f.load)
122 return f.file.Readdir(n)
125 func (f *webdavFile) Stat() (os.FileInfo, error) {
126 return f.fileInfo, nil
129 // noLockSystem implements webdav.LockSystem by returning success for
130 // every possible locking operation, even though it has no side
131 // effects such as actually locking anything. This works for a
132 // read-only webdav filesystem because webdav locks only apply to
135 // This is more suitable than webdav.NewMemLS() for two reasons:
136 // First, it allows keep-web to use one locker for all collections
137 // even though coll1.vhost/foo and coll2.vhost/foo have the same path
138 // but represent different resources. Additionally, it returns valid
139 // tokens (rfc2518 specifies that tokens are represented as URIs and
140 // are unique across all resources for all time), which might improve
141 // client compatibility.
143 // However, it does also permit impossible operations, like acquiring
144 // conflicting locks and releasing non-existent locks. This might
145 // confuse some clients if they try to probe for correctness.
147 // Currently this is a moot point: the LOCK and UNLOCK methods are not
148 // accepted by keep-web, so it suffices to implement the
149 // webdav.LockSystem interface.
150 type noLockSystem struct{}
152 func (*noLockSystem) Confirm(time.Time, string, string, ...webdav.Condition) (func(), error) {
156 func (*noLockSystem) Create(now time.Time, details webdav.LockDetails) (token string, err error) {
157 return fmt.Sprintf("opaquelocktoken:%s-%x", lockPrefix, atomic.AddInt64(&nextLockSuffix, 1)), nil
160 func (*noLockSystem) Refresh(now time.Time, token string, duration time.Duration) (webdav.LockDetails, error) {
161 return webdav.LockDetails{}, nil
164 func (*noLockSystem) Unlock(now time.Time, token string) error {
170 // Return a version 1 variant 4 UUID, meaning all bits are random
171 // except the ones indicating the version and variant.
174 if _, err := rand.Read(data[:]); err != nil {
178 data[8] = data[8]&0x3f | 0x80
180 data[6] = data[6]&0x0f | 0x40
181 return fmt.Sprintf("%x-%x-%x-%x-%x", data[0:4], data[4:6], data[6:8], data[8:10], data[10:])