-
Notifications
You must be signed in to change notification settings - Fork 413
/
dir.go
413 lines (345 loc) · 9.97 KB
/
dir.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package inode
import (
"fmt"
"os"
"path"
"github.com/jacobsa/fuse"
"github.com/jacobsa/fuse/fuseops"
"github.com/jacobsa/fuse/fuseutil"
"github.com/jacobsa/gcloud/gcs"
"github.com/jacobsa/gcloud/syncutil"
"golang.org/x/net/context"
"google.golang.org/cloud/storage"
)
type DirInode struct {
/////////////////////////
// Dependencies
/////////////////////////
bucket gcs.Bucket
/////////////////////////
// Constant data
/////////////////////////
id fuseops.InodeID
implicitDirs bool
// The the GCS object backing the inode. The object's name is used as a
// prefix when listing. Special case: the empty string means this is the root
// inode.
//
// INVARIANT: src != nil
// INVARIANT: src.Name == "" || src.Name[len(name)-1] == '/'
src storage.Object
}
var _ Inode = &DirInode{}
// Set notes on NewRootInode.
const RootGen int64 = 0
// Create a directory inode for the root of the file system. For this inode,
// the result of SourceGeneration() is guaranteed to be RootGen.
func NewRootInode(
bucket gcs.Bucket,
implicitDirs bool) (d *DirInode) {
dummy := &storage.Object{
Name: "",
Generation: RootGen,
}
d = NewDirInode(bucket, fuseops.RootInodeID, dummy, implicitDirs)
return
}
// Create a directory inode for the supplied source object. The object's name
// must end with a slash unless this is the root directory, in which case it
// must be empty.
//
// If implicitDirs is set, LookUpChild will use ListObjects to find child
// directories that are "implicitly" defined by the existence of their own
// descendents. For example, if there is an object named "foo/bar/baz" and this
// is the directory "foo", a child directory named "bar" will be implied.
//
// REQUIRES: o != nil
// REQUIRES: o.Name == "" || o.Name[len(o.Name)-1] == '/'
func NewDirInode(
bucket gcs.Bucket,
id fuseops.InodeID,
o *storage.Object,
implicitDirs bool) (d *DirInode) {
if o.Name != "" && o.Name[len(o.Name)-1] != '/' {
panic(fmt.Sprintf("Unexpected name: %s", o.Name))
}
// Set up the struct.
d = &DirInode{
bucket: bucket,
id: id,
implicitDirs: implicitDirs,
src: *o,
}
return
}
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
func (d *DirInode) checkInvariants() {
// INVARIANT: src.Name == "" || src.Name[len(name)-1] == '/'
if !(d.src.Name == "" || d.src.Name[len(d.src.Name)-1] == '/') {
panic(fmt.Sprintf("Unexpected name: %s", d.src.Name))
}
}
func (d *DirInode) clobbered(ctx context.Context) (clobbered bool, err error) {
// Special case: the root is never clobbered.
if d.ID() == fuseops.RootInodeID {
return
}
// Stat the backing object.
req := &gcs.StatObjectRequest{
Name: d.Name(),
}
o, err := d.bucket.StatObject(ctx, req)
// "Not found" means clobbered.
if _, ok := err.(*gcs.NotFoundError); ok {
clobbered = true
err = nil
return
}
// Propagate other errors.
if err != nil {
err = fmt.Errorf("StatObject: %v", err)
return
}
// We are clobbered if the generation number has changed.
clobbered = o.Generation != d.SourceGeneration()
return
}
func (d *DirInode) lookUpChildFile(
ctx context.Context,
name string) (o *storage.Object, err error) {
o, err = statObjectMayNotExist(ctx, d.bucket, d.Name()+name)
if err != nil {
err = fmt.Errorf("statObjectMayNotExist: %v", err)
return
}
return
}
func (d *DirInode) lookUpChildDir(
ctx context.Context,
name string) (o *storage.Object, err error) {
b := syncutil.NewBundle(ctx)
// Stat the placeholder object.
b.Add(func(ctx context.Context) (err error) {
o, err = statObjectMayNotExist(ctx, d.bucket, d.Name()+name+"/")
if err != nil {
err = fmt.Errorf("statObjectMayNotExist: %v", err)
return
}
return
})
// If implicit directories are enabled, find out whether the child name is
// implicitly defined.
var implicitlyDefined bool
if d.implicitDirs {
b.Add(func(ctx context.Context) (err error) {
implicitlyDefined, err = objectNamePrefixNonEmpty(
ctx,
d.bucket,
d.Name()+name+"/")
if err != nil {
err = fmt.Errorf("objectNamePrefixNonEmpty: %v", err)
return
}
return
})
}
// Wait for both.
err = b.Join()
if err != nil {
return
}
// If statting failed by the directory is implicitly defined, fake a source
// object.
if o == nil && implicitlyDefined {
o = &storage.Object{
Name: d.Name() + name + "/",
Generation: ImplicitDirGen,
}
}
return
}
// List the supplied object name prefix to find out whether it is non-empty.
func objectNamePrefixNonEmpty(
ctx context.Context,
bucket gcs.Bucket,
prefix string) (nonEmpty bool, err error) {
query := &storage.Query{
Prefix: prefix,
MaxResults: 1,
}
listing, err := bucket.ListObjects(ctx, query)
if err != nil {
err = fmt.Errorf("ListObjects: %v", err)
return
}
nonEmpty = len(listing.Results) != 0
return
}
// Stat the object with the given name, returning (nil, nil) if the object
// doesn't exist rather than failing.
func statObjectMayNotExist(
ctx context.Context,
bucket gcs.Bucket,
name string) (o *storage.Object, err error) {
// Call the bucket.
req := &gcs.StatObjectRequest{
Name: name,
}
o, err = bucket.StatObject(ctx, req)
// Suppress "not found" errors.
if _, ok := err.(*gcs.NotFoundError); ok {
err = nil
}
// Annotate others.
if err != nil {
err = fmt.Errorf("StatObject: %v", err)
return
}
return
}
////////////////////////////////////////////////////////////////////////
// Public interface
////////////////////////////////////////////////////////////////////////
func (d *DirInode) Lock() {
// We don't require locks.
}
func (d *DirInode) Unlock() {
// We don't require locks.
}
// Return the ID of this inode.
func (d *DirInode) ID() fuseops.InodeID {
return d.id
}
// Return the full name of the directory object in GCS, including the trailing
// slash (e.g. "foo/bar/").
func (d *DirInode) Name() string {
return d.src.Name
}
// Return the generation number from which this inode was branched.
func (d *DirInode) SourceGeneration() int64 {
return d.src.Generation
}
func (d *DirInode) Attributes(
ctx context.Context) (attrs fuseops.InodeAttributes, err error) {
// Find out whether the backing object has been clobbered in GCS.
clobbered, err := d.clobbered(ctx)
if err != nil {
err = fmt.Errorf("clobbered: %v", err)
return
}
// Set up basic attributes.
attrs = fuseops.InodeAttributes{
Mode: 0700 | os.ModeDir,
}
// Modify Nlink as appropriate.
if !clobbered {
attrs.Nlink = 1
}
return
}
// See notes on DirInode.LookUpChild.
const ImplicitDirGen int64 = -1
// Look up the direct child with the given relative name, returning a record
// for the current object of that name in the GCS bucket. If both a file and a
// directory with the given name exist, be consistent from call to call about
// which is preferred. Return fuse.ENOENT if neither is found.
//
// If this inode was created with implicitDirs is set, this method will use
// ListObjects to find child directories that are "implicitly" defined by the
// existence of their own descendents. For example, if there is an object named
// "foo/bar/baz" and this is the directory "foo", a child directory named "bar"
// will be implied. In this case, the child's generation will be the sentinel
// value ImplicitDirGen.
func (d *DirInode) LookUpChild(
ctx context.Context,
name string) (o *storage.Object, err error) {
b := syncutil.NewBundle(ctx)
// Stat the child as a file.
var fileRecord *storage.Object
b.Add(func(ctx context.Context) (err error) {
fileRecord, err = d.lookUpChildFile(ctx, name)
return
})
// Stat the child as a directory.
var dirRecord *storage.Object
b.Add(func(ctx context.Context) (err error) {
dirRecord, err = d.lookUpChildDir(ctx, name)
return
})
// Wait for both.
err = b.Join()
if err != nil {
return
}
// Prefer directories over files.
switch {
case dirRecord != nil:
o = dirRecord
case fileRecord != nil:
o = fileRecord
default:
err = fuse.ENOENT
}
return
}
// Read some number of entries from the directory, returning a continuation
// token that can be used to pick up the read operation where it left off.
// Supply the empty token on the first call.
//
// At the end of the directory, the returned continuation token will be empty.
// Otherwise it will be non-empty. There is no guarantee about the number of
// entries returned; it may be zero even with a non-empty continuation token.
//
// The contents of the Offset and Inode fields for returned entries is
// undefined.
func (d *DirInode) ReadEntries(
ctx context.Context,
tok string) (entries []fuseutil.Dirent, newTok string, err error) {
// Ask the bucket to list some objects.
query := &storage.Query{
Delimiter: "/",
Prefix: d.Name(),
Cursor: tok,
}
listing, err := d.bucket.ListObjects(ctx, query)
if err != nil {
err = fmt.Errorf("ListObjects: %v", err)
return
}
// Convert objects to entries for files.
for _, o := range listing.Results {
e := fuseutil.Dirent{
Name: path.Base(o.Name),
Type: fuseutil.DT_File,
}
entries = append(entries, e)
}
// Convert prefixes to entries for directories.
for _, p := range listing.Prefixes {
e := fuseutil.Dirent{
Name: path.Base(p),
Type: fuseutil.DT_Directory,
}
entries = append(entries, e)
}
// Return an appropriate continuation token, if any.
if listing.Next != nil {
newTok = listing.Next.Cursor
}
return
}