/
drive_db.go
1311 lines (1169 loc) · 35.3 KB
/
drive_db.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// drive_sync syncs Google Drive metadata to a local LevelDB database and provides methods to query it.
package drive_db
import (
"bytes"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"sync"
"time"
"github.com/asjoyner/fuse_gdrive/lru"
"github.com/golang/groupcache/singleflight"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/errors"
"github.com/syndtr/goleveldb/leveldb/filter"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/util"
gdrive "google.golang.org/api/drive/v2"
)
const (
downloadUrlLifetime = time.Duration(time.Hour * 12)
// https://developers.google.com/drive/web/folder
driveFolderMimeType string = "application/vnd.google-apps.folder"
checkpointVersion = 3
)
var (
debugDriveDB = flag.Bool("drivedb.debug", false, "print debug statements from the drive_db package and debug enable HTTP handlers which can leak all your data via HTTP.")
logChanges = flag.Bool("drivedb.logchanges", false, "Log json encoded metadata as it is fetched from Google Drive.")
continuousPrefetch = flag.Bool("drivedb.continuous_prefetch", false, "keep prefetching data as long as the file is open.")
driveCacheChunk = flag.Int64("drivedb.cachechunk", 256*1024, "Cache data in segments of this many bytes.")
driveCacheChunks = flag.Int64("drivedb.fetchsize", 32, "Chunks of --drivedb.cachechunk bytes to read from drive at a time (aka readahead size).")
cacheSize = flag.Int64("drivedb.maxcachesize", 32768, "Chunks to cache from drive at a time.")
inodeCacheSize = flag.Int("drivedb.inodecachesize", 50000, "number of cached inode entries (nb: larger than num files in the largest directory)")
)
type debugging bool
var debug debugging
func (d debugging) Printf(format string, args ...interface{}) {
if d {
log.Printf(format, args...)
}
}
// encode returns the item encoded into []byte.
func encode(item interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
err := enc.Encode(item)
return buf.Bytes(), err
}
// decode decodes the data buffer into the item.
func decode(data []byte, item interface{}) error {
dec := json.NewDecoder(bytes.NewBuffer(data))
return dec.Decode(item)
}
type File struct {
*gdrive.File
Inode uint64
Children []uint64 // inodes of children
LinkCount int
}
type CheckPoint struct {
LastChangeID int64
LastInode uint64
Version int
CacheBlock int64
}
type DownloadSpec struct {
file *File
chunk int64
filesize int64
}
type DownloadURL struct {
URL string
When int64 // epoch time
}
type DriveDB struct {
sync.Mutex
client *http.Client
service *gdrive.Service
db *leveldb.DB
data string // root of data cache directory
lruCache *lru.Cache // in-memory inode to *File cache
syncmu sync.Mutex
synced *sync.Cond
iters sync.WaitGroup
cpt CheckPoint
changes chan *gdrive.ChangeList
pollInterval time.Duration
sf singleflight.Group
dbpath string
rootId string
driveSize int64
cacheBlocks int64
pfetchq chan DownloadSpec
pfetchmap map[string]bool
openmap map[string]int
openmu sync.Mutex
}
func openLevelDB(filepath string) (*leveldb.DB, error) {
o := &opt.Options{
Filter: filter.NewBloomFilter(10),
Strict: opt.StrictAll,
}
db, err := leveldb.OpenFile(filepath, o)
if err == nil {
return db, nil
}
if _, ok := err.(*errors.ErrCorrupted); ok {
log.Printf("recovering leveldb: %v", err)
db, err = leveldb.RecoverFile(filepath, o)
if err != nil {
log.Printf("failed to recover leveldb: %v", err)
return nil, err
}
return db, nil
}
log.Printf("failed to open leveldb: %v", err)
return nil, err
}
// NewDriveDB creates a new DriveDB and starts syncing metadata.
func NewDriveDB(client *http.Client, dbPath, cachePath string, pollInterval time.Duration, rootId string) (*DriveDB, error) {
svc, _ := gdrive.New(client)
_, err := svc.About.Get().Do()
if err != nil {
log.Fatalf("drive.service.About.Get().Do: %v\n", err)
}
if *debugDriveDB {
debug = true
}
ldbPath := path.Join(dbPath, "meta")
log.Printf("using db path: %q", ldbPath)
err = os.MkdirAll(ldbPath, 0700)
if err != nil {
return nil, fmt.Errorf("could not create directory %q", ldbPath)
}
log.Printf("using cache path: %q", cachePath)
err = os.MkdirAll(cachePath, 0700)
if err != nil {
return nil, fmt.Errorf("could not create directory %q", cachePath)
}
db, err := openLevelDB(ldbPath)
if err != nil {
return nil, err
}
d := &DriveDB{
client: client,
service: svc,
db: db,
dbpath: ldbPath,
data: cachePath,
lruCache: lru.New(*inodeCacheSize),
changes: make(chan *gdrive.ChangeList, 200),
pollInterval: pollInterval,
rootId: rootId,
driveSize: (*driveCacheChunk) * (*driveCacheChunks), // ensure drive reads are always a multiple of cache size
cacheBlocks: (*cacheSize) * (*driveCacheChunks), // enough blocks for readahead
pfetchq: make(chan DownloadSpec, 200),
pfetchmap: make(map[string]bool),
openmap: make(map[string]int),
}
log.Printf("%d cache blocks of %d bytes each = %d GB", d.cacheBlocks, *driveCacheChunk, d.cacheBlocks*(*driveCacheChunk)/1024/1024/1024)
log.Printf("reading from drive %d bytes at a time", d.driveSize)
// Get saved checkpoint.
err = d.get(internalKey("checkpoint"), &d.cpt)
if err != nil {
log.Printf("error reading checkpoint: %v", err)
d.cpt = NewCheckpoint()
}
if d.cpt.Version != checkpointVersion {
log.Printf("checkpoint version invalid, require %v but found %v", checkpointVersion, d.cpt.Version)
err = d.reinit()
if err != nil {
log.Printf("Failed to reinitialize the database: %v", err)
log.Fatal("You should probably run: rm -rf %v", ldbPath)
}
log.Printf("reinitialized databse")
}
err = d.writeCheckpoint(nil)
if err != nil {
return nil, fmt.Errorf("could not write checkpoint: %v", err)
}
debug.Printf("Recovered from checkpoint: %+v", d.cpt)
if err := d.createRoot(); err != nil {
return nil, fmt.Errorf("could not create root inode entry: %v", err)
}
d.synced = sync.NewCond(&d.syncmu)
go d.sync()
go d.pollForChanges()
if debug {
registerDebugHandles(*d) // in http_handlers.go
}
go d.prefetcher()
return d, nil
}
func (d *DriveDB) Service() *gdrive.Service {
return d.service
}
func NewCheckpoint() CheckPoint {
return CheckPoint{
LastInode: 1000, // start high, to allow "special" inodes
Version: checkpointVersion,
CacheBlock: 0,
}
}
// createRoot synthesizes the root of the filesystem, based on the
// rootId provided at instantiation time.
func (d *DriveDB) createRoot() error {
launch, _ := time.Unix(1335225600, 0).MarshalText()
file := &gdrive.File{
Id: d.rootId,
Title: "/",
MimeType: driveFolderMimeType,
LastViewedByMeDate: string(launch),
ModifiedDate: string(launch),
CreatedDate: string(launch),
}
// Inode allocation special-cases the rootId, so we can let the usual
// code paths do all the work
_, err := d.UpdateFile(nil, file)
return err
}
// Delete all the stored metadata from Google Drive, preserving only the
// mappings of fileid to inodes. Cached chunks are left behind, but the
// blocks will be recycled.
func (d *DriveDB) reinit() error {
d.Lock()
defer d.Unlock()
i := d.cpt.LastInode // preserve the last Inode allocated
d.cpt = NewCheckpoint() // recreate the checkpoint
d.cpt.LastInode = i // restore the last Inode allocated
s := time.Now()
err := d.RemoveAllFiles() // blow away all of the metadata from Drive
debug.Printf("Removing all files took %v seconds.", time.Since(s))
return err
}
// LevelDB key helpers. Key prefixes are 3 chars and ":".
func internalKey(key string) []byte {
return []byte("int:" + key)
}
func fileIdToInodeKey(key string) []byte {
return []byte("f2i:" + key)
}
func inodeToFileIdKey(key uint64) []byte {
return []byte("i2f:" + fmt.Sprintf("%d", key))
}
func fileKey(key string) []byte {
return []byte("fid:" + key)
}
func downloadUrlKey(key string) []byte {
return []byte("url:" + key)
}
func childKey(parentId, childId string) []byte {
return []byte(string(childKeyPrefix(parentId)) + ":" + childId)
}
// childKeyPrefix returns a byte prefix that can be used to lookup all children
// of the given parent.
func childKeyPrefix(parentId string) []byte {
return []byte("kid:" + parentId)
}
func cacheMapKeyPrefix(fileId string) []byte {
return []byte(fmt.Sprintf("cky:%s\\0", fileId))
}
func cacheMapKey(fileId string, chunk int64) []byte {
return []byte(fmt.Sprintf("cky:%s\\0%d\\0%d", fileId, *driveCacheChunk, chunk))
}
func deKey(key string) string {
return key[4:]
}
// linkCount returns the link count of the passed gdrive.File given its subdirectory count.
func linkCount(f *gdrive.File, numSubDirs int) int {
links := len(f.Parents) + numSubDirs
// If this is a directory, the '.' counts as an additional link
if f.MimeType == driveFolderMimeType {
links++
}
return links
}
// get retrives a single key from the database.
func (d *DriveDB) get(key []byte, item interface{}) error {
data, err := d.db.Get(key, nil)
if err != nil {
return err
}
return decode(data, item)
}
// writeCheckpoint writes the checkpoint to the db, optionally using a batch.
func (d *DriveDB) writeCheckpoint(batch *leveldb.Batch) error {
d.Lock()
cpt := d.cpt
d.Unlock()
bytes, err := encode(cpt)
if err != nil {
log.Printf("error encoding checkpoint: %v", err)
return err
}
if batch != nil {
batch.Put(internalKey("checkpoint"), bytes)
return nil
}
return d.db.Put(internalKey("checkpoint"), bytes, nil)
}
// lastChangeId() returns the last changeID recorded in the checkpoint.
func (d *DriveDB) lastChangeId() int64 {
d.Lock()
defer d.Unlock()
return d.cpt.LastChangeID
}
// setLastChangeId sets the lastChangeId in the checkpoint.
// It does not commit to leveldb; use writeCheckpoint to do that.
func (d *DriveDB) setLastChangeId(id int64) {
d.Lock()
defer d.Unlock()
d.cpt.LastChangeID = id
}
// nextInode allocates a new inode number and updates the checkpoint, including writing to leveldb.
func (d *DriveDB) nextInode(batch *leveldb.Batch) (uint64, error) {
var inode uint64
d.Lock()
d.cpt.LastInode++
inode = d.cpt.LastInode
d.Unlock()
return inode, d.writeCheckpoint(batch)
}
// nextCacheBlock allocates a new cache block number and updates, including writing to leveldb.
func (d *DriveDB) nextCacheBlock(batch *leveldb.Batch) (int64, error) {
var block int64
d.Lock()
block = d.cpt.CacheBlock
d.cpt.CacheBlock = (d.cpt.CacheBlock + 1) % d.cacheBlocks
d.Unlock()
return block, d.writeCheckpoint(batch)
}
// InodeForFileId returns a File's inode number, given its ID.
// Allocates a new inode number if needed, and commits immediately
// to leveldb.
func (d *DriveDB) InodeForFileId(fileId string) (uint64, error) {
key := "inf:" + fileId
v, err := d.sf.Do(key, func() (interface{}, error) {
return d.inodeForFileIdImpl(fileId)
})
return v.(uint64), err
}
func (d *DriveDB) inodeForFileIdImpl(fileId string) (uint64, error) {
var inode uint64
batch := new(leveldb.Batch)
// Check if an inode has been allocated for this fileId
if fileId == d.rootId {
inode = 1
} else {
err := d.get(fileIdToInodeKey(fileId), &inode)
if err != nil {
// if not, allocate an inode number
inode, err = d.nextInode(batch)
if err != nil {
return 0, err
}
}
}
// Check the opposite mapping is present and correct
var currentId string
err := d.get(inodeToFileIdKey(inode), ¤tId)
if err == nil {
if currentId == fileId {
return inode, nil
} else {
debug.Printf("inodeToFileId mapping wrong for %v, expected %v got %v", inode, fileId, currentId)
}
}
encodedInode, err := encode(inode)
if err != nil {
return 0, err
}
encodedFileId, err := encode(fileId)
if err != nil {
return 0, err
}
// Create forward and reverse mappings.
batch.Put(fileIdToInodeKey(fileId), encodedInode)
batch.Put(inodeToFileIdKey(inode), encodedFileId)
err = d.db.Write(batch, nil)
if err != nil {
return 0, err
}
return inode, nil
}
// AllFileIds returns the IDs of all Google Drive file objects currently stored.
func (d *DriveDB) AllFileIds() ([]string, error) {
var ids []string
// We can't Close() until all iterators are released.
// TODO: this can still be racy with Close(), fix that.
d.iters.Add(1)
iter := d.db.NewIterator(util.BytesPrefix(fileKey("")), nil)
for iter.Next() {
ids = append(ids, deKey(string(iter.Key())))
}
iter.Release()
d.iters.Done()
return ids, iter.Error()
}
// childFileIds returns a map containing IDs of all Files that have parent
// refs to the given file. The returned map keys are IDs, and the map values
// indicate if the child is a directory.
func (d *DriveDB) childFileIds(fileId string) (map[string]bool, error) {
ids := make(map[string]bool)
d.iters.Add(1)
batch := new(leveldb.Batch)
iter := d.db.NewIterator(util.BytesPrefix(childKeyPrefix(fileId)), nil)
for iter.Next() {
pidcid := deKey(string(iter.Key()))
cid := pidcid[len(fileId)+1:]
if gdriveFile, err := d.FileById(cid); err != nil {
log.Printf("unknown fileId %v: %v", fileId, err)
batch.Delete(iter.Key())
} else {
ids[cid] = gdriveFile.MimeType == driveFolderMimeType
}
}
iter.Release()
d.iters.Done()
if batch.Len() > 0 {
err := d.db.Write(batch, nil)
if err != nil {
log.Printf("error writing to db: %v", err)
}
}
return ids, iter.Error()
}
// FileById returns a File, given its ID.
func (d *DriveDB) FileById(fileId string) (*gdrive.File, error) {
var res gdrive.File
err := d.get(fileKey(fileId), &res)
if err != nil {
return nil, err
}
return &res, nil
}
// FileIdForInode returns the FileId associated with a given inode.
func (d *DriveDB) FileIdForInode(inode uint64) (string, error) {
var fileId string
err := d.get(inodeToFileIdKey(inode), &fileId)
if err != nil {
log.Printf("FileIdForInode: %v: %v", inode, err)
return "", err
}
return fileId, nil
}
// FileByInode returns a *File given an inode number
func (d *DriveDB) FileByInode(inode uint64) (*File, error) {
if f, ok := d.lruCache.Get(inode); ok {
return f.(*File), nil
}
fileId, err := d.FileIdForInode(inode)
if err != nil {
return nil, err
}
file, err := d.FileByFileId(fileId)
if err != nil {
return nil, err
}
d.lruCache.Add(inode, file)
return file, nil
}
// FileByFileId returns a *File given a fileId
func (d *DriveDB) FileByFileId(fileId string) (*File, error) {
gdriveFile, err := d.FileById(fileId)
if err != nil {
return nil, fmt.Errorf("unknown fileId %v: %v", fileId, err)
}
file := File{File: gdriveFile}
file.Inode, err = d.InodeForFileId(fileId)
if err != nil {
return nil, fmt.Errorf("no inode for %v: %v", fileId, err)
}
childFileIds, err := d.childFileIds(fileId)
if err != nil {
return nil, fmt.Errorf("error getting children of fileId %v: %v", fileId, err)
}
var numSubDirs int
for fileId, isDir := range childFileIds {
inode, err := d.InodeForFileId(fileId)
if err != nil {
d.lruCache.Remove(inode)
return nil, fmt.Errorf("error getting inode of child %v: %v", fileId, err)
}
file.Children = append(file.Children, inode)
if isDir {
numSubDirs++
}
}
file.LinkCount = linkCount(gdriveFile, numSubDirs)
d.lruCache.Add(file.Inode, &file)
return &file, nil
}
// Refresh the file object of the given fileId
func (d *DriveDB) Refresh(fileId string) (*File, error) {
f, err := d.service.Files.Get(fileId).Do()
if err != nil {
return &File{}, err
}
return d.UpdateFile(nil, f)
}
// RemoveAllFiles removes all file entries and child references from leveldb.
// This also flushes the cache, but preserves the fileid->inode mapping
func (d *DriveDB) RemoveAllFiles() error {
af, err := d.AllFileIds()
if err != nil {
return fmt.Errorf("AllFileIds(): %v", err)
}
batch := new(leveldb.Batch)
for _, id := range af {
d.RemoveFileById(id, batch)
}
err = d.db.Write(batch, nil)
if err != nil {
return err
}
return nil
}
func (d *DriveDB) RemoveFile(f *gdrive.File) error {
if f == nil {
return nil
}
return d.RemoveFileById(f.Id, nil)
}
func (d *DriveDB) RemoveFileById(fileId string, batch *leveldb.Batch) error {
if batch == nil {
batch = new(leveldb.Batch)
}
staleFiles := []string{fileId}
// Grab a copy of the file object as it existed previously, if it did,
// remove this file from its parents, and remember to remove the stale
// inode.
of, err := d.FileById(fileId)
if err == nil && of != nil && of.Parents != nil {
for _, pr := range of.Parents {
batch.Delete(childKey(pr.Id, fileId))
staleFiles = append(staleFiles, pr.Id)
}
}
// delete the file itself.
batch.Delete(fileKey(fileId))
batch.Delete(downloadUrlKey(fileId))
// delete the inode to fileid mapping
// nota bene: fileid to inode mapping is preserved, in case we see this
// fileid again in the future; preserves mapping during re-init
// batch.Delete(inodeToFileIdKey(inode))
// also delete all of its child refs
d.iters.Add(1)
iter := d.db.NewIterator(util.BytesPrefix(childKeyPrefix(fileId)), nil)
for iter.Next() {
batch.Delete(iter.Key())
}
iter.Release()
d.iters.Done()
// commit
if err := d.db.Write(batch, nil); err != nil {
return err
}
// Clear the cached download url, inode cache and data cache
for _, id := range staleFiles {
d.FlushCachedInodeForFileId(id)
}
d.clearDataCache(fileId)
return nil
}
// UpdateFile commits a gdrive.File to levelDB, updating all mappings and allocating inodes if needed.
func (d *DriveDB) UpdateFile(batch *leveldb.Batch, f *gdrive.File) (*File, error) {
if f == nil {
return &File{}, fmt.Errorf("cannot update nil File")
}
fileId := f.Id
bytes, err := encode(f)
if err != nil {
return &File{}, fmt.Errorf("error encoding file %v: %v", fileId, err)
}
b := batch
if b == nil {
b = new(leveldb.Batch)
}
// Find its inode, allocate if necessary
inode, err := d.InodeForFileId(fileId)
if err != nil {
return &File{}, fmt.Errorf("error allocating inode for fileid %v: %v", fileId, err)
}
staleFiles := []string{fileId}
oldParents := make(map[string]bool)
// Grab a copy of the file object as it existed previously, if it did
of, err := d.FileById(fileId)
if err == nil && of != nil && of.Parents != nil {
for _, pr := range of.Parents {
oldParents[pr.Id] = true
}
}
// write the file itself.
b.Put(fileKey(fileId), bytes)
// Maintain child references
for _, pr := range f.Parents {
debug.Printf("Adding parent: %v", pr.Id)
b.Put(childKey(pr.Id, fileId), nil) // we care only about the key
delete(oldParents, pr.Id)
staleFiles = append(staleFiles, pr.Id)
}
for pId := range oldParents { // these parents were no longer present
debug.Printf("Removing parent: %v", pId)
b.Delete(childKey(pId, fileId))
staleFiles = append(staleFiles, pId)
}
// Clear the downloadURL
b.Delete(downloadUrlKey(fileId))
// Write now if no batch was supplied.
if batch == nil {
err := d.db.Write(b, nil)
if err != nil {
return &File{}, err
}
}
// Clear the cached download url and inode cache
for _, id := range staleFiles {
d.FlushCachedInodeForFileId(id)
}
file := File{
File: f,
Inode: inode,
}
return &file, nil
}
func (d *DriveDB) FlushCachedInode(inode uint64) {
d.lruCache.Remove(inode)
}
func (d *DriveDB) FlushCachedInodeForFileId(fileId string) {
inode, _ := d.InodeForFileId(fileId)
d.lruCache.Remove(inode)
}
// pollForChanges is a background goroutine to poll Drive for changes.
func (d *DriveDB) pollForChanges() {
poll := make(chan struct{})
pollTime := time.NewTicker(d.pollInterval).C
http.HandleFunc("/refresh", func(w http.ResponseWriter, r *http.Request) {
poll <- struct{}{}
fmt.Fprintf(w, "Refresh request accepted.")
})
// TODO: Allow full requery via http handler, invoke on leveldb corruption
// track lastChangeId outside of readChanges, just pass in 0 to rebuild
d.readChanges()
for {
select {
case <-pollTime:
d.readChanges()
case <-poll:
d.readChanges()
}
}
}
// readChanges is called by pollForChanges to grab all new metadata changes from Drive.
func (d *DriveDB) readChanges() {
l := d.service.Changes.List().IncludeDeleted(true).IncludeSubscribed(true).MaxResults(1000)
lastChangeId := d.lastChangeId()
if lastChangeId > 0 {
l.StartChangeId(lastChangeId + 1)
}
debug.Printf("Querying Google Drive for changes since %d.", lastChangeId)
var filenum int
for {
filenum++
c, err := l.Do()
if err != nil {
log.Printf("sync error: %v", err)
return
}
debug.Printf("Response from Drive contains %d changes of %d", len(c.Items), c.LargestChangeId)
if *logChanges {
filename := fmt.Sprintf("%s/changes.out.%d", d.dbpath, filenum)
data, _ := encode(c)
ioutil.WriteFile(filename, data, 0700)
}
// Process the changelist.
d.changes <- c
if len(c.Items) == 0 {
return
}
// Go to the next page, or next syncid.
if c.NextPageToken != "" {
l.PageToken(c.NextPageToken)
} else {
return
}
}
}
// processChange applies a ChangeList to the database.
func (d *DriveDB) processChange(c *gdrive.ChangeList) error {
if c == nil {
return nil
}
// If we read zero items, there's no work to do, and we're probably synced.
if len(c.Items) == 0 {
if d.lastChangeId() >= c.LargestChangeId {
d.synced.Broadcast()
}
return nil
}
log.Printf("processing %v/%v, %v changes", d.lastChangeId(), c.LargestChangeId, len(c.Items))
batch := new(leveldb.Batch)
for _, i := range c.Items {
if i.File == nil {
debug.Printf(" %s: deleted", i.FileId)
} else {
debug.Printf(" %s: %q size:%v version:%v labels:%#v", i.FileId, i.File.Title, i.File.FileSize, i.File.Version, i.File.Labels)
}
batch.Reset()
// Update leveldb.
inode, _ := d.InodeForFileId(i.FileId)
d.lruCache.Remove(inode)
// TODO: don't delete trashed/hidden files? ".trash" folder?
if i.Deleted || i.File.Labels.Trashed || i.File.Labels.Hidden {
d.RemoveFileById(i.FileId, batch)
} else {
d.UpdateFile(batch, i.File)
}
// Update the checkpoint, which now encompasses one additional change.
d.setLastChangeId(i.Id)
err := d.writeCheckpoint(batch)
if err != nil {
return err
}
// Commit
err = d.db.Write(batch, nil)
if err != nil {
return err
}
}
// Signal we're synced, if we are.
if d.lastChangeId() >= c.LargestChangeId {
d.synced.Broadcast()
}
return nil
}
// sync is a background goroutine to sync drive data.
func (d *DriveDB) sync() {
var c *gdrive.ChangeList
for {
c = <-d.changes
err := d.processChange(c)
if err != nil {
// TODO: trigger reinit(), unless rate > N, then log.Fatal
log.Printf("error evaluating change from drive: %v", err)
}
}
}
// WaitUntilSynced blocks until are are synced with Drive.
func (d *DriveDB) WaitUntilSynced() {
d.synced.L.Lock()
d.synced.Wait()
d.synced.L.Unlock()
}
// Close closes DriveDB, waiting until all iterators are closed.
func (d *DriveDB) Close() {
d.iters.Wait()
d.db.Close()
d.db = nil
}
// Data is read from drive and cached on disk. The Drive read size is intended to be larger
// than the cache size, to account for higher latency to Drive than local disk.
// Chunks of (*driveCacheChunk) are stored in files next to the leveldb. Records in the leveldb
// point from FileID and chunk number to a cache file on disk. The disk cache works like a ring
// buffer.
// Map an offset and a size to low and high chunk numbers.
func (d *DriveDB) chunkNumbers(offset, size int64) (chunk0, chunkN int64) {
chunk0 = offset / (*driveCacheChunk) // lowest chunk number encompassing the offset
chunkN = (offset + size - 1) / (*driveCacheChunk) // highest chunk number, encompassing offset+size.
return
}
func (d *DriveDB) chunkToDriveChunk(chunk int64) int64 {
return chunk * (*driveCacheChunk) / d.driveSize
}
func (d *DriveDB) driveChunkToChunk(dchunk int64) int64 {
return dchunk * d.driveSize / (*driveCacheChunk)
}
func (d *DriveDB) OpenFile(fileId string) {
d.openmu.Lock()
d.openmap[fileId]++
d.openmu.Unlock()
}
func (d *DriveDB) CloseFile(fileId string) {
d.openmu.Lock()
d.openmap[fileId]--
if d.openmap[fileId] < 0 {
log.Printf("negative refcount on %s", fileId)
d.openmap[fileId] = 0
}
d.openmu.Unlock()
}
func (d *DriveDB) IsOpen(fileId string) bool {
d.openmu.Lock()
val := d.openmap[fileId]
d.openmu.Unlock()
return val > 0
}
// ReadFiledata reads a chunk of a file, possibly from cache.
func (d *DriveDB) ReadFiledata(f *File, offset, size, filesize int64) ([]byte, error) {
var ret []byte
// Read all the necessary chunks
chunk0, chunkN := d.chunkNumbers(offset, size)
for chunk := chunk0; chunk <= chunkN; chunk++ {
data, err := d.readChunk(f, chunk, filesize)
if err != nil {
log.Printf(" chunk %v read error: %v", chunk, err)
return nil, err
}
ret = append(ret, data...)
}
// We may have too much data here -- before offset and after end. Return an appropriate slice.
dsize := int64(len(ret))
low := offset - chunk0*(*driveCacheChunk)
if low < 0 {
low = 0
}
if low > dsize {
return nil, fmt.Errorf("tried to read past end of chunk (low:%d, dsize:%d): fileId: %s, offset:%d, size:%d, filesize:%d", low, dsize, f.Id, offset, size, filesize)
}
high := low + size
if high > dsize {
high = dsize
}
buf := ret[low:high]
return buf, nil
}
// clearDataCache removes the leveldb block cache records, but leaves the actual
// blocks on disk. The blocks will be recycled, so this ok.
func (d *DriveDB) clearDataCache(fileId string) {
var ids []string
d.iters.Add(1)
iter := d.db.NewIterator(util.BytesPrefix(cacheMapKeyPrefix(fileId)), nil)
for iter.Next() {
ids = append(ids, string(iter.Key()))
}
iter.Release()
d.iters.Done()
batch := new(leveldb.Batch)
for _, id := range ids {
batch.Delete([]byte(id))
}
d.db.Write(batch, nil)
}
// readChunk singleflights the read of a chunk of data from a drive file.
func (d *DriveDB) readChunk(f *File, chunk, filesize int64) ([]byte, error) {
if chunk*(*driveCacheChunk) > filesize {
return nil, fmt.Errorf("read past eof")
}
key := cacheMapKey(f.Id, chunk)
v, err := d.sf.Do(string(key), func() (interface{}, error) {
return d.readChunkImpl(f, chunk, filesize)
})
return v.([]byte), err
}
// blockFilename returns the filename at which a cache block can be found.
// It creates directories as needed.
func (d *DriveDB) blockFilename(block int64) (string, error) {
keysize := len(fmt.Sprintf("%d", d.cacheBlocks))
f := fmt.Sprintf("%%0%dd", keysize)
b := fmt.Sprintf(f, block%d.cacheBlocks)
dir := path.Join(d.data, b[:keysize/2])
err := os.MkdirAll(dir, 0700)
if err != nil {
return "", err
}
p := path.Join(dir, b)
return p, nil
}
func (d *DriveDB) writeCacheBlock(batch *leveldb.Batch, fileId string, chunk int64, data []byte) error {
// See if there's an existing block.
cacheKey := cacheMapKey(fileId, chunk)
var block int64
err := d.get(cacheKey, &block)
if err != nil {
// get a new block number
block, err = d.nextCacheBlock(batch)