13111: Rearrange source files.
[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         "time"
10 )
11
12 // SiteFileSystem returns a FileSystem that maps collections and other
13 // Arvados objects onto a filesystem layout.
14 //
15 // This is experimental: the filesystem layout is not stable, and
16 // there are significant known bugs and shortcomings. For example,
17 // although the FileSystem allows files to be added and modified in
18 // collections, these changes are not persistent or visible to other
19 // Arvados clients.
20 func (c *Client) SiteFileSystem(kc keepClient) FileSystem {
21         root := &treenode{
22                 fileinfo: fileinfo{
23                         name:    "/",
24                         mode:    os.ModeDir | 0755,
25                         modTime: time.Now(),
26                 },
27                 inodes: make(map[string]inode),
28         }
29         root.parent = root
30         root.Child("by_id", func(inode) inode {
31                 return &vdirnode{
32                         treenode: treenode{
33                                 parent: root,
34                                 inodes: make(map[string]inode),
35                                 fileinfo: fileinfo{
36                                         name:    "by_id",
37                                         modTime: time.Now(),
38                                         mode:    0755 | os.ModeDir,
39                                 },
40                         },
41                         create: func(name string) inode {
42                                 return newEntByID(c, kc, name)
43                         },
44                 }
45         })
46         return &fileSystem{inode: root}
47 }
48
49 func newEntByID(c *Client, kc keepClient, id string) inode {
50         var coll Collection
51         err := c.RequestAndDecode(&coll, "GET", "arvados/v1/collections/"+id, nil, nil)
52         if err != nil {
53                 return nil
54         }
55         fs, err := coll.FileSystem(c, kc)
56         fs.(*collectionFileSystem).inode.(*dirnode).fileinfo.name = id
57         if err != nil {
58                 return nil
59         }
60         return fs
61 }
62
63 type vdirnode struct {
64         treenode
65         create func(string) inode
66 }
67
68 func (vn *vdirnode) Child(name string, _ func(inode) inode) inode {
69         return vn.treenode.Child(name, func(existing inode) inode {
70                 if existing != nil {
71                         return existing
72                 } else {
73                         return vn.create(name)
74                 }
75         })
76 }