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)
21 type customFileSystem struct {
25 staleThreshold time.Time
29 func (c *Client) CustomFileSystem(kc keepClient) CustomFileSystem {
31 fs := &customFileSystem{
33 fileSystem: fileSystem{
34 fsBackend: keepBackend{apiClient: c, keepClient: kc},
38 root.inode = &treenode{
43 mode: os.ModeDir | 0755,
46 inodes: make(map[string]inode),
51 func (fs *customFileSystem) MountByID(mount string) {
52 fs.root.inode.Child(mount, func(inode) (inode, error) {
57 inodes: make(map[string]inode),
61 mode: 0755 | os.ModeDir,
69 func (fs *customFileSystem) MountProject(mount, uuid string) {
70 fs.root.inode.Child(mount, func(inode) (inode, error) {
71 return fs.newProjectNode(fs.root, mount, uuid), nil
75 func (fs *customFileSystem) MountUsers(mount string) {
76 fs.root.inode.Child(mount, func(inode) (inode, error) {
79 loadOne: fs.usersLoadOne,
80 loadAll: fs.usersLoadAll,
84 inodes: make(map[string]inode),
88 mode: 0755 | os.ModeDir,
95 // SiteFileSystem returns a FileSystem that maps collections and other
96 // Arvados objects onto a filesystem layout.
98 // This is experimental: the filesystem layout is not stable, and
99 // there are significant known bugs and shortcomings. For example,
100 // writes are not persisted until Sync() is called.
101 func (c *Client) SiteFileSystem(kc keepClient) CustomFileSystem {
102 fs := c.CustomFileSystem(kc)
103 fs.MountByID("by_id")
104 fs.MountUsers("users")
108 func (fs *customFileSystem) Sync() error {
110 defer fs.staleLock.Unlock()
111 fs.staleThreshold = time.Now()
115 // Stale returns true if information obtained at time t should be
117 func (fs *customFileSystem) Stale(t time.Time) bool {
119 defer fs.staleLock.Unlock()
120 return !fs.staleThreshold.Before(t)
123 func (fs *customFileSystem) newNode(name string, perm os.FileMode, modTime time.Time) (node inode, err error) {
124 return nil, ErrInvalidOperation
127 func (fs *customFileSystem) mountByID(parent inode, id string) inode {
128 if strings.Contains(id, "-4zz18-") || pdhRegexp.MatchString(id) {
129 return fs.mountCollection(parent, id)
130 } else if strings.Contains(id, "-j7d0g-") {
131 return fs.newProjectNode(fs.root, id, id)
137 func (fs *customFileSystem) mountCollection(parent inode, id string) inode {
139 err := fs.RequestAndDecode(&coll, "GET", "arvados/v1/collections/"+id, nil, nil)
143 cfs, err := coll.FileSystem(fs, fs)
147 root := cfs.rootnode()
148 root.SetParent(parent, id)
152 func (fs *customFileSystem) newProjectNode(root inode, name, uuid string) inode {
155 loadOne: func(parent inode, name string) (inode, error) { return fs.projectsLoadOne(parent, uuid, name) },
156 loadAll: func(parent inode) ([]inode, error) { return fs.projectsLoadAll(parent, uuid) },
160 inodes: make(map[string]inode),
164 mode: 0755 | os.ModeDir,
170 // vdirnode wraps an inode by ignoring any requests to add/replace
171 // children, and calling a create() func when a non-existing child is
174 // create() can return either a new node, which will be added to the
175 // treenode, or nil for ENOENT.
176 type vdirnode struct {
178 create func(parent inode, name string) inode
181 func (vn *vdirnode) Child(name string, replace func(inode) (inode, error)) (inode, error) {
182 return vn.inode.Child(name, func(existing inode) (inode, error) {
183 if existing == nil && vn.create != nil {
184 existing = vn.create(vn, name)
186 existing.SetParent(vn, name)
187 vn.inode.(*treenode).fileinfo.modTime = time.Now()
192 } else if tryRepl, err := replace(existing); err != nil {
194 } else if tryRepl != existing {
195 return existing, ErrInvalidArgument