1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
13 type CustomFileSystem interface {
15 MountByID(mount string)
16 MountProject(mount, uuid string)
17 MountUsers(mount string)
20 type customFileSystem struct {
24 staleThreshold time.Time
28 func (c *Client) CustomFileSystem(kc keepClient) CustomFileSystem {
30 fs := &customFileSystem{
32 fileSystem: fileSystem{
33 fsBackend: keepBackend{apiClient: c, keepClient: kc},
37 root.inode = &treenode{
42 mode: os.ModeDir | 0755,
45 inodes: make(map[string]inode),
50 func (fs *customFileSystem) MountByID(mount string) {
51 fs.root.inode.Child(mount, func(inode) (inode, error) {
56 inodes: make(map[string]inode),
60 mode: 0755 | os.ModeDir,
63 create: fs.mountCollection,
68 func (fs *customFileSystem) MountProject(mount, uuid string) {
69 fs.root.inode.Child(mount, func(inode) (inode, error) {
70 return fs.newProjectNode(fs.root, mount, uuid), nil
74 func (fs *customFileSystem) MountUsers(mount string) {
75 fs.root.inode.Child(mount, func(inode) (inode, error) {
78 loadOne: fs.usersLoadOne,
79 loadAll: fs.usersLoadAll,
83 inodes: make(map[string]inode),
87 mode: 0755 | os.ModeDir,
94 // SiteFileSystem returns a FileSystem that maps collections and other
95 // Arvados objects onto a filesystem layout.
97 // This is experimental: the filesystem layout is not stable, and
98 // there are significant known bugs and shortcomings. For example,
99 // writes are not persisted until Sync() is called.
100 func (c *Client) SiteFileSystem(kc keepClient) CustomFileSystem {
101 fs := c.CustomFileSystem(kc)
102 fs.MountByID("by_id")
103 fs.MountUsers("users")
107 func (fs *customFileSystem) Sync() error {
109 defer fs.staleLock.Unlock()
110 fs.staleThreshold = time.Now()
114 // Stale returns true if information obtained at time t should be
116 func (fs *customFileSystem) Stale(t time.Time) bool {
118 defer fs.staleLock.Unlock()
119 return !fs.staleThreshold.Before(t)
122 func (fs *customFileSystem) newNode(name string, perm os.FileMode, modTime time.Time) (node inode, err error) {
123 return nil, ErrInvalidOperation
126 func (fs *customFileSystem) mountCollection(parent inode, id string) inode {
128 err := fs.RequestAndDecode(&coll, "GET", "arvados/v1/collections/"+id, nil, nil)
132 cfs, err := coll.FileSystem(fs, fs)
136 root := cfs.rootnode()
137 root.SetParent(parent, id)
141 func (fs *customFileSystem) newProjectNode(root inode, name, uuid string) inode {
144 loadOne: func(parent inode, name string) (inode, error) { return fs.projectsLoadOne(parent, uuid, name) },
145 loadAll: func(parent inode) ([]inode, error) { return fs.projectsLoadAll(parent, uuid) },
149 inodes: make(map[string]inode),
153 mode: 0755 | os.ModeDir,
159 // vdirnode wraps an inode by ignoring any requests to add/replace
160 // children, and calling a create() func when a non-existing child is
163 // create() can return either a new node, which will be added to the
164 // treenode, or nil for ENOENT.
165 type vdirnode struct {
167 create func(parent inode, name string) inode
170 func (vn *vdirnode) Child(name string, replace func(inode) (inode, error)) (inode, error) {
171 return vn.inode.Child(name, func(existing inode) (inode, error) {
172 if existing == nil && vn.create != nil {
173 existing = vn.create(vn, name)
175 existing.SetParent(vn, name)
176 vn.inode.(*treenode).fileinfo.modTime = time.Now()
181 } else if tryRepl, err := replace(existing); err != nil {
183 } else if tryRepl != existing {
184 return existing, ErrInvalidArgument