-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfs.go
247 lines (215 loc) · 5.39 KB
/
fs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package ghvfs
import (
"log"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/google/go-github/github"
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/hanwen/go-fuse/fuse/pathfs"
lru "github.com/hashicorp/golang-lru"
"golang.org/x/net/context"
"golang.org/x/oauth2"
)
// Opt is an option for filesystem construction.
type Opt func(*config)
// WithGHEndpoint allows specifying an optional GitHub base URL.
// This can be used to point to an internal GitHub Enterprise endpoint.
func WithGHEndpoint(base string) Opt {
return func(cfg *config) {
v3URL := base + "/api/v3/"
u, err := url.Parse(v3URL)
if err != nil {
log.Fatal("unable to parse github url")
}
cfg.v3 = u
}
}
// WithToken provides a GitHub token for authentication.
// The token must provide read access to contents for the orgs and repos being
// accessed.
func WithToken(t string) Opt {
return func(cfg *config) {
cfg.token = t
}
}
// WithCacheSize provides a cache size for internal attribute caching.
func WithCacheSize(size int) Opt {
return func(cfg *config) {
cfg.cacheSize = size
}
}
type config struct {
v3 *url.URL
token string
cacheSize int
}
type gfs struct {
pathfs.FileSystem
client *github.Client
// cache maps full path to *stat.
// Since contents are tied to a specific ref, they are immutable and can
// be cached indefinitely to speed up access.
// This can cache both positive and negative results.
cache *lru.Cache
}
type stat struct {
attr *fuse.Attr
status fuse.Status
entries []fuse.DirEntry // for directories
}
// NewFS creates a new filesystem.
func NewFS(opts ...Opt) pathfs.FileSystem {
cfg := config{
cacheSize: 4096,
}
for _, o := range opts {
o(&cfg)
}
cache, err := lru.New(cfg.cacheSize)
if err != nil {
log.Fatal("unable to construct cache:", err)
}
fs := &gfs{
FileSystem: pathfs.NewDefaultFileSystem(),
cache: cache,
}
// Auth config.
var ts oauth2.TokenSource
if cfg.token != "" {
ts = oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: cfg.token},
)
}
tc := oauth2.NewClient(context.Background(), ts)
// Initialize V3 API client.
fs.client = github.NewClient(tc)
if cfg.v3 != nil {
Debug.Printf("connecting to %v", cfg.v3)
fs.client.BaseURL = cfg.v3
fs.client.UploadURL = nil
}
return fs
}
func (fs *gfs) StatFs(name string) *fuse.StatfsOut {
return &fuse.StatfsOut{}
}
func (fs *gfs) GetAttr(name string, fctx *fuse.Context) (*fuse.Attr, fuse.Status) {
Debug.Printf("getattr %q", name)
if cached, ok := fs.cache.Get(name); ok {
c := cached.(*stat)
return c.attr, c.status
}
parts := strings.SplitN(name, string(os.PathSeparator), 4)
if len(parts) <= 3 {
dirAttr := newDirInfo(0111)
return &dirAttr, fuse.OK
}
attr, status := fs.getContent(parts[0], parts[1], parts[2], parts[3], fctx)
fs.cache.Add(name, &stat{
attr: attr,
status: status,
})
return attr, status
}
func (fs *gfs) OpenDir(name string, fctx *fuse.Context) (entries []fuse.DirEntry, status fuse.Status) {
Debug.Printf("opendir %q", name)
if cached, ok := fs.cache.Get(name); ok {
c := cached.(*stat)
if c.entries != nil {
return c.entries, c.status
}
}
parts := strings.SplitN(name, string(os.PathSeparator), 4)
if len(parts) < 3 {
return nil, fuse.EACCES
}
ctx := context.Background()
org := parts[0]
repo := parts[1]
var path string
if len(parts) == 4 {
path = parts[3]
}
opt := github.RepositoryContentGetOptions{
Ref: parts[2],
}
// TODO: iterate on too many results?
_, ds, _, err := fs.client.Repositories.GetContents(ctx, org, repo, path, &opt)
if err != nil {
// TODO: distinguish error values
return nil, fuse.ENOENT
}
if ds == nil {
return nil, fuse.ENOTDIR
}
for _, ent := range ds {
entries = append(entries, fuse.DirEntry{
Name: ent.GetName(),
Mode: 0444,
})
var attr fuse.Attr
if ent.GetType() == "file" {
attr = newFileInfo()
} else {
attr = newDirInfo(0555)
}
attr.Size = uint64(ent.GetSize())
fs.cache.Add(filepath.Join(name, ent.GetName()), &stat{
attr: &attr,
status: fuse.OK,
})
}
attr := newDirInfo(0555)
fs.cache.Add(name, &stat{
attr: &attr,
status: status,
entries: entries,
})
return entries, fuse.OK
}
func (fs *gfs) getContent(org, repo, id, path string, fctx *fuse.Context) (*fuse.Attr, fuse.Status) {
Debug.Printf("get content %q / %q / %q/ %q", org, repo, id, path)
ctx := context.Background()
opt := github.RepositoryContentGetOptions{
Ref: id,
}
fc, ds, _, err := fs.client.Repositories.GetContents(ctx, org, repo, path, &opt)
if err != nil {
Debug.Printf("err: %v", err)
// TODO: distinguish different error types
return nil, fuse.ENOENT
}
var attr fuse.Attr
if ds == nil {
attr = newFileInfo()
attr.Size = uint64(fc.GetSize())
} else {
attr = newDirInfo(0555)
}
return &attr, fuse.OK
}
func newDirInfo(mask uint32) fuse.Attr {
var info fuse.Attr
now := time.Now()
info.SetTimes(&now, &now, &now)
info.Mode = fuse.S_IFDIR | mask
return info
}
func newFileInfo() fuse.Attr {
var info fuse.Attr
now := time.Now()
info.SetTimes(&now, &now, &now)
info.Mode = fuse.S_IFREG | 0444
return info
}
func (fs *gfs) Open(name string, flags uint32, fctx *fuse.Context) (nodefs.File, fuse.Status) {
parts := strings.SplitN(name, string(os.PathSeparator), 4)
if len(parts) != 4 {
return nil, fuse.ENOENT
}
return newContentNode(fs, flags, parts[0], parts[1], parts[2], parts[3])
}