12216: Use no-op locking system.
[arvados.git] / services / keep-web / webdav.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "crypto/rand"
9         "errors"
10         "fmt"
11         prand "math/rand"
12         "net/http"
13         "os"
14         "sync/atomic"
15         "time"
16
17         "golang.org/x/net/context"
18         "golang.org/x/net/webdav"
19 )
20
21 var (
22         lockPrefix     string = uuid()
23         nextLockSuffix int64  = prand.Int63()
24         errReadOnly           = errors.New("read-only filesystem")
25 )
26
27 // webdavFS implements a read-only webdav.FileSystem by wrapping
28 // http.Filesystem.
29 type webdavFS struct {
30         httpfs http.FileSystem
31 }
32
33 var _ webdav.FileSystem = &webdavFS{}
34
35 func (fs *webdavFS) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
36         return errReadOnly
37 }
38
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)
41         if err != nil {
42                 return nil, err
43         }
44         return &webdavFile{File: f}, nil
45 }
46
47 func (fs *webdavFS) RemoveAll(ctx context.Context, name string) error {
48         return errReadOnly
49 }
50
51 func (fs *webdavFS) Rename(ctx context.Context, oldName, newName string) error {
52         return errReadOnly
53 }
54
55 func (fs *webdavFS) Stat(ctx context.Context, name string) (os.FileInfo, error) {
56         if f, err := fs.httpfs.Open(name); err != nil {
57                 return nil, err
58         } else {
59                 return f.Stat()
60         }
61 }
62
63 // webdavFile implements a read-only webdav.File by wrapping
64 // http.File. Writes fail.
65 type webdavFile struct {
66         http.File
67 }
68
69 func (f *webdavFile) Write([]byte) (int, error) {
70         return 0, errReadOnly
71 }
72
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
77 // writes.
78 //
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.
86 //
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{}
91
92 func (*noLockSystem) Confirm(time.Time, string, string, ...webdav.Condition) (func(), error) {
93         return noop, nil
94 }
95
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
98 }
99
100 func (*noLockSystem) Refresh(now time.Time, token string, duration time.Duration) (webdav.LockDetails, error) {
101         return webdav.LockDetails{}, nil
102 }
103
104 func (*noLockSystem) Unlock(now time.Time, token string) error {
105         return nil
106 }
107
108 func noop() {}
109
110 // Return a version 1 variant 4 UUID, meaning all bits are random
111 // except the ones indicating the version and variant.
112 func uuid() string {
113         var data [16]byte
114         if _, err := rand.Read(data[:]); err != nil {
115                 panic(err)
116         }
117         // variant 1: N=10xx
118         data[8] = data[8]&0x3f | 0x80
119         // version 4: M=0100
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:])
122 }