Merge branch '15964-fix-docs' refs #15964
[arvados.git] / sdk / go / arvados / fs_site.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package arvados
6
7 import (
8         "os"
9         "strings"
10         "sync"
11         "time"
12 )
13
14 type CustomFileSystem interface {
15         FileSystem
16         MountByID(mount string)
17         MountProject(mount, uuid string)
18         MountUsers(mount string)
19         ForwardSlashNameSubstitution(string)
20 }
21
22 type customFileSystem struct {
23         fileSystem
24         root *vdirnode
25         thr  *throttle
26
27         staleThreshold time.Time
28         staleLock      sync.Mutex
29
30         forwardSlashNameSubstitution string
31 }
32
33 func (c *Client) CustomFileSystem(kc keepClient) CustomFileSystem {
34         root := &vdirnode{}
35         fs := &customFileSystem{
36                 root: root,
37                 fileSystem: fileSystem{
38                         fsBackend: keepBackend{apiClient: c, keepClient: kc},
39                         root:      root,
40                         thr:       newThrottle(concurrentWriters),
41                 },
42         }
43         root.treenode = treenode{
44                 fs:     fs,
45                 parent: root,
46                 fileinfo: fileinfo{
47                         name:    "/",
48                         mode:    os.ModeDir | 0755,
49                         modTime: time.Now(),
50                 },
51                 inodes: make(map[string]inode),
52         }
53         return fs
54 }
55
56 func (fs *customFileSystem) MountByID(mount string) {
57         fs.root.treenode.Child(mount, func(inode) (inode, error) {
58                 return &vdirnode{
59                         treenode: treenode{
60                                 fs:     fs,
61                                 parent: fs.root,
62                                 inodes: make(map[string]inode),
63                                 fileinfo: fileinfo{
64                                         name:    mount,
65                                         modTime: time.Now(),
66                                         mode:    0755 | os.ModeDir,
67                                 },
68                         },
69                         create: fs.mountByID,
70                 }, nil
71         })
72 }
73
74 func (fs *customFileSystem) MountProject(mount, uuid string) {
75         fs.root.treenode.Child(mount, func(inode) (inode, error) {
76                 return fs.newProjectNode(fs.root, mount, uuid), nil
77         })
78 }
79
80 func (fs *customFileSystem) MountUsers(mount string) {
81         fs.root.treenode.Child(mount, func(inode) (inode, error) {
82                 return &lookupnode{
83                         stale:   fs.Stale,
84                         loadOne: fs.usersLoadOne,
85                         loadAll: fs.usersLoadAll,
86                         treenode: treenode{
87                                 fs:     fs,
88                                 parent: fs.root,
89                                 inodes: make(map[string]inode),
90                                 fileinfo: fileinfo{
91                                         name:    mount,
92                                         modTime: time.Now(),
93                                         mode:    0755 | os.ModeDir,
94                                 },
95                         },
96                 }, nil
97         })
98 }
99
100 func (fs *customFileSystem) ForwardSlashNameSubstitution(repl string) {
101         fs.forwardSlashNameSubstitution = repl
102 }
103
104 // SiteFileSystem returns a FileSystem that maps collections and other
105 // Arvados objects onto a filesystem layout.
106 //
107 // This is experimental: the filesystem layout is not stable, and
108 // there are significant known bugs and shortcomings. For example,
109 // writes are not persisted until Sync() is called.
110 func (c *Client) SiteFileSystem(kc keepClient) CustomFileSystem {
111         fs := c.CustomFileSystem(kc)
112         fs.MountByID("by_id")
113         fs.MountUsers("users")
114         return fs
115 }
116
117 func (fs *customFileSystem) Sync() error {
118         return fs.root.Sync()
119 }
120
121 // Stale returns true if information obtained at time t should be
122 // considered stale.
123 func (fs *customFileSystem) Stale(t time.Time) bool {
124         fs.staleLock.Lock()
125         defer fs.staleLock.Unlock()
126         return !fs.staleThreshold.Before(t)
127 }
128
129 func (fs *customFileSystem) newNode(name string, perm os.FileMode, modTime time.Time) (node inode, err error) {
130         return nil, ErrInvalidArgument
131 }
132
133 func (fs *customFileSystem) mountByID(parent inode, id string) inode {
134         if strings.Contains(id, "-4zz18-") || pdhRegexp.MatchString(id) {
135                 return fs.mountCollection(parent, id)
136         } else if strings.Contains(id, "-j7d0g-") {
137                 return fs.newProjectNode(fs.root, id, id)
138         } else {
139                 return nil
140         }
141 }
142
143 func (fs *customFileSystem) mountCollection(parent inode, id string) inode {
144         var coll Collection
145         err := fs.RequestAndDecode(&coll, "GET", "arvados/v1/collections/"+id, nil, nil)
146         if err != nil {
147                 return nil
148         }
149         newfs, err := coll.FileSystem(fs, fs)
150         if err != nil {
151                 return nil
152         }
153         cfs := newfs.(*collectionFileSystem)
154         cfs.SetParent(parent, id)
155         return cfs
156 }
157
158 func (fs *customFileSystem) newProjectNode(root inode, name, uuid string) inode {
159         return &lookupnode{
160                 stale:   fs.Stale,
161                 loadOne: func(parent inode, name string) (inode, error) { return fs.projectsLoadOne(parent, uuid, name) },
162                 loadAll: func(parent inode) ([]inode, error) { return fs.projectsLoadAll(parent, uuid) },
163                 treenode: treenode{
164                         fs:     fs,
165                         parent: root,
166                         inodes: make(map[string]inode),
167                         fileinfo: fileinfo{
168                                 name:    name,
169                                 modTime: time.Now(),
170                                 mode:    0755 | os.ModeDir,
171                         },
172                 },
173         }
174 }
175
176 // vdirnode wraps an inode by rejecting (with ErrInvalidArgument)
177 // calls that add/replace children directly, instead calling a
178 // create() func when a non-existing child is looked up.
179 //
180 // create() can return either a new node, which will be added to the
181 // treenode, or nil for ENOENT.
182 type vdirnode struct {
183         treenode
184         create func(parent inode, name string) inode
185 }
186
187 func (vn *vdirnode) Child(name string, replace func(inode) (inode, error)) (inode, error) {
188         return vn.treenode.Child(name, func(existing inode) (inode, error) {
189                 if existing == nil && vn.create != nil {
190                         existing = vn.create(vn, name)
191                         if existing != nil {
192                                 existing.SetParent(vn, name)
193                                 vn.treenode.fileinfo.modTime = time.Now()
194                         }
195                 }
196                 if replace == nil {
197                         return existing, nil
198                 } else if tryRepl, err := replace(existing); err != nil {
199                         return existing, err
200                 } else if tryRepl != existing {
201                         return existing, ErrInvalidArgument
202                 } else {
203                         return existing, nil
204                 }
205         })
206 }