12483: Support file create/write via webdav.
[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         "os"
13         "sync/atomic"
14         "time"
15
16         "git.curoverse.com/arvados.git/sdk/go/arvados"
17
18         "golang.org/x/net/context"
19         "golang.org/x/net/webdav"
20 )
21
22 var (
23         lockPrefix     string = uuid()
24         nextLockSuffix int64  = prand.Int63()
25         errReadOnly           = errors.New("read-only filesystem")
26 )
27
28 // webdavFS implements a webdav.FileSystem by wrapping an
29 // arvados.CollectionFilesystem.
30 type webdavFS struct {
31         collfs arvados.CollectionFileSystem
32         update func() error
33 }
34
35 func (fs *webdavFS) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
36         return fs.collfs.Mkdir(name, 0755)
37 }
38
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
43         }
44         f, err = fs.collfs.OpenFile(name, flag, perm)
45         if writing && err == nil {
46                 f = writingFile{File: f, update: fs.update}
47         }
48         return
49 }
50
51 func (fs *webdavFS) RemoveAll(ctx context.Context, name string) error {
52         return errReadOnly
53 }
54
55 func (fs *webdavFS) Rename(ctx context.Context, oldName, newName string) error {
56         return errReadOnly
57 }
58
59 func (fs *webdavFS) Stat(ctx context.Context, name string) (os.FileInfo, error) {
60         return fs.collfs.Stat(name)
61 }
62
63 type writingFile struct {
64         webdav.File
65         update func() error
66 }
67
68 func (f writingFile) Close() error {
69         if err := f.File.Close(); err != nil || f.update == nil {
70                 return err
71         }
72         return f.update()
73 }
74
75 // noLockSystem implements webdav.LockSystem by returning success for
76 // every possible locking operation, even though it has no side
77 // effects such as actually locking anything. This works for a
78 // read-only webdav filesystem because webdav locks only apply to
79 // writes.
80 //
81 // This is more suitable than webdav.NewMemLS() for two reasons:
82 // First, it allows keep-web to use one locker for all collections
83 // even though coll1.vhost/foo and coll2.vhost/foo have the same path
84 // but represent different resources. Additionally, it returns valid
85 // tokens (rfc2518 specifies that tokens are represented as URIs and
86 // are unique across all resources for all time), which might improve
87 // client compatibility.
88 //
89 // However, it does also permit impossible operations, like acquiring
90 // conflicting locks and releasing non-existent locks.  This might
91 // confuse some clients if they try to probe for correctness.
92 //
93 // Currently this is a moot point: the LOCK and UNLOCK methods are not
94 // accepted by keep-web, so it suffices to implement the
95 // webdav.LockSystem interface.
96 type noLockSystem struct{}
97
98 func (*noLockSystem) Confirm(time.Time, string, string, ...webdav.Condition) (func(), error) {
99         return noop, nil
100 }
101
102 func (*noLockSystem) Create(now time.Time, details webdav.LockDetails) (token string, err error) {
103         return fmt.Sprintf("opaquelocktoken:%s-%x", lockPrefix, atomic.AddInt64(&nextLockSuffix, 1)), nil
104 }
105
106 func (*noLockSystem) Refresh(now time.Time, token string, duration time.Duration) (webdav.LockDetails, error) {
107         return webdav.LockDetails{}, nil
108 }
109
110 func (*noLockSystem) Unlock(now time.Time, token string) error {
111         return nil
112 }
113
114 func noop() {}
115
116 // Return a version 1 variant 4 UUID, meaning all bits are random
117 // except the ones indicating the version and variant.
118 func uuid() string {
119         var data [16]byte
120         if _, err := rand.Read(data[:]); err != nil {
121                 panic(err)
122         }
123         // variant 1: N=10xx
124         data[8] = data[8]&0x3f | 0x80
125         // version 4: M=0100
126         data[6] = data[6]&0x0f | 0x40
127         return fmt.Sprintf("%x-%x-%x-%x-%x", data[0:4], data[4:6], data[6:8], data[8:10], data[10:])
128 }