1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
14 type CustomFileSystem interface {
16 MountByID(mount string)
17 MountProject(mount, uuid string)
18 MountUsers(mount string)
19 ForwardSlashNameSubstitution(string)
22 type customFileSystem struct {
27 staleThreshold time.Time
30 forwardSlashNameSubstitution string
33 func (c *Client) CustomFileSystem(kc keepClient) CustomFileSystem {
35 fs := &customFileSystem{
37 fileSystem: fileSystem{
38 fsBackend: keepBackend{apiClient: c, keepClient: kc},
40 thr: newThrottle(concurrentWriters),
43 root.treenode = treenode{
48 mode: os.ModeDir | 0755,
51 inodes: make(map[string]inode),
56 func (fs *customFileSystem) MountByID(mount string) {
57 fs.root.treenode.Child(mount, func(inode) (inode, error) {
62 inodes: make(map[string]inode),
66 mode: 0755 | os.ModeDir,
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
80 func (fs *customFileSystem) MountUsers(mount string) {
81 fs.root.treenode.Child(mount, func(inode) (inode, error) {
84 loadOne: fs.usersLoadOne,
85 loadAll: fs.usersLoadAll,
89 inodes: make(map[string]inode),
93 mode: 0755 | os.ModeDir,
100 func (fs *customFileSystem) ForwardSlashNameSubstitution(repl string) {
101 fs.forwardSlashNameSubstitution = repl
104 // SiteFileSystem returns a FileSystem that maps collections and other
105 // Arvados objects onto a filesystem layout.
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")
117 func (fs *customFileSystem) Sync() error {
118 return fs.root.Sync()
121 // Stale returns true if information obtained at time t should be
123 func (fs *customFileSystem) Stale(t time.Time) bool {
125 defer fs.staleLock.Unlock()
126 return !fs.staleThreshold.Before(t)
129 func (fs *customFileSystem) newNode(name string, perm os.FileMode, modTime time.Time) (node inode, err error) {
130 return nil, ErrInvalidArgument
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)
143 func (fs *customFileSystem) mountCollection(parent inode, id string) inode {
145 err := fs.RequestAndDecode(&coll, "GET", "arvados/v1/collections/"+id, nil, nil)
149 newfs, err := coll.FileSystem(fs, fs)
153 cfs := newfs.(*collectionFileSystem)
154 cfs.SetParent(parent, id)
158 func (fs *customFileSystem) newProjectNode(root inode, name, uuid string) inode {
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) },
166 inodes: make(map[string]inode),
170 mode: 0755 | os.ModeDir,
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.
180 // create() can return either a new node, which will be added to the
181 // treenode, or nil for ENOENT.
182 type vdirnode struct {
184 create func(parent inode, name string) inode
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)
192 existing.SetParent(vn, name)
193 vn.treenode.fileinfo.modTime = time.Now()
198 } else if tryRepl, err := replace(existing); err != nil {
200 } else if tryRepl != existing {
201 return existing, ErrInvalidArgument