12483: Enable rename 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         if fs.update == nil {
57                 return errReadOnly
58         }
59         return fs.Rename(oldName, newName)
60 }
61
62 func (fs *webdavFS) Stat(ctx context.Context, name string) (os.FileInfo, error) {
63         return fs.collfs.Stat(name)
64 }
65
66 type writingFile struct {
67         webdav.File
68         update func() error
69 }
70
71 func (f writingFile) Close() error {
72         if err := f.File.Close(); err != nil || f.update == nil {
73                 return err
74         }
75         return f.update()
76 }
77
78 // noLockSystem implements webdav.LockSystem by returning success for
79 // every possible locking operation, even though it has no side
80 // effects such as actually locking anything. This works for a
81 // read-only webdav filesystem because webdav locks only apply to
82 // writes.
83 //
84 // This is more suitable than webdav.NewMemLS() for two reasons:
85 // First, it allows keep-web to use one locker for all collections
86 // even though coll1.vhost/foo and coll2.vhost/foo have the same path
87 // but represent different resources. Additionally, it returns valid
88 // tokens (rfc2518 specifies that tokens are represented as URIs and
89 // are unique across all resources for all time), which might improve
90 // client compatibility.
91 //
92 // However, it does also permit impossible operations, like acquiring
93 // conflicting locks and releasing non-existent locks.  This might
94 // confuse some clients if they try to probe for correctness.
95 //
96 // Currently this is a moot point: the LOCK and UNLOCK methods are not
97 // accepted by keep-web, so it suffices to implement the
98 // webdav.LockSystem interface.
99 type noLockSystem struct{}
100
101 func (*noLockSystem) Confirm(time.Time, string, string, ...webdav.Condition) (func(), error) {
102         return noop, nil
103 }
104
105 func (*noLockSystem) Create(now time.Time, details webdav.LockDetails) (token string, err error) {
106         return fmt.Sprintf("opaquelocktoken:%s-%x", lockPrefix, atomic.AddInt64(&nextLockSuffix, 1)), nil
107 }
108
109 func (*noLockSystem) Refresh(now time.Time, token string, duration time.Duration) (webdav.LockDetails, error) {
110         return webdav.LockDetails{}, nil
111 }
112
113 func (*noLockSystem) Unlock(now time.Time, token string) error {
114         return nil
115 }
116
117 func noop() {}
118
119 // Return a version 1 variant 4 UUID, meaning all bits are random
120 // except the ones indicating the version and variant.
121 func uuid() string {
122         var data [16]byte
123         if _, err := rand.Read(data[:]); err != nil {
124                 panic(err)
125         }
126         // variant 1: N=10xx
127         data[8] = data[8]&0x3f | 0x80
128         // version 4: M=0100
129         data[6] = data[6]&0x0f | 0x40
130         return fmt.Sprintf("%x-%x-%x-%x-%x", data[0:4], data[4:6], data[6:8], data[8:10], data[10:])
131 }