forked from perkeep/perkeep
/
files.go
1198 lines (1092 loc) · 33.9 KB
/
files.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
/*
Copyright 2011 Google Inc.
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 main
import (
"bufio"
"crypto/sha1"
"errors"
"flag"
"fmt"
"hash"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"camlistore.org/internal/chanworker"
"camlistore.org/pkg/blob"
"camlistore.org/pkg/blobserver"
statspkg "camlistore.org/pkg/blobserver/stats"
"camlistore.org/pkg/client"
"camlistore.org/pkg/client/android"
"camlistore.org/pkg/cmdmain"
"camlistore.org/pkg/schema"
)
type fileCmd struct {
title string
tag string
makePermanode bool // make new, unique permanode of the root (dir or file)
filePermanodes bool // make planned permanodes for each file (based on their digest)
vivify bool
exifTime bool // use metadata (such as in EXIF) to find the creation time of the file
capCtime bool // use mtime as creation time of the file, if it would be bigger than modification time
diskUsage bool // show "du" disk usage only (dry run mode), don't actually upload
argsFromInput bool // Android mode: filenames piped into stdin, one at a time.
deleteAfterUpload bool // with fileNodes, deletes the input file once uploaded
contentsOnly bool // do not store any of the file's attributes, only its contents.
statcache bool
// Go into in-memory stats mode only; doesn't actually upload.
memstats bool
histo string // optional histogram output filename
}
var flagUseSQLiteChildCache bool // Use sqlite for the statcache and havecache.
var (
uploadWorkers = 5 // concurrent upload workers (negative means unbounded: memory hog)
dirUploadWorkers = 3 // concurrent directory uploading workers
statCacheWorkers = 5 // concurrent statcache workers
)
func init() {
cmdmain.RegisterCommand("file", func(flags *flag.FlagSet) cmdmain.CommandRunner {
cmd := new(fileCmd)
flags.BoolVar(&cmd.makePermanode, "permanode", false, "Create an associate a new permanode for the uploaded file or directory.")
flags.BoolVar(&cmd.filePermanodes, "filenodes", false, "Create (if necessary) content-based permanodes for each uploaded file.")
flags.BoolVar(&cmd.deleteAfterUpload, "delete_after_upload", false, "If using -filenodes, deletes files once they're uploaded, or if they've already been uploaded.")
flags.BoolVar(&cmd.vivify, "vivify", false,
"If true, ask the server to create and sign permanode(s) associated with each uploaded"+
" file. This permits the server to have your signing key. Used mostly with untrusted"+
" or at-risk clients, such as phones.")
flags.BoolVar(&cmd.exifTime, "exiftime", false, "Try to use metadata (such as EXIF) to get a stable creation time. If found, used as the replacement for the modtime. Mainly useful with vivify or filenodes.")
flags.StringVar(&cmd.title, "title", "", "Optional title attribute to set on permanode when using -permanode.")
flags.StringVar(&cmd.tag, "tag", "", "Optional tag(s) to set on permanode when using -permanode or -filenodes. Single value or comma separated.")
flags.BoolVar(&cmd.diskUsage, "du", false, "Dry run mode: only show disk usage information, without upload or statting dest. Used for testing skipDirs configs, mostly.")
if debug, _ := strconv.ParseBool(os.Getenv("CAMLI_DEBUG")); debug {
flags.BoolVar(&cmd.statcache, "statcache", true, "(debug flag) Use the stat cache, assuming unchanged files already uploaded in the past are still there. Fast, but potentially dangerous.")
flags.BoolVar(&cmd.memstats, "debug-memstats", false, "(debug flag) Enter debug in-memory mode; collecting stats only. Doesn't upload anything.")
flags.StringVar(&cmd.histo, "debug-histogram-file", "", "(debug flag) Optional file to create and write the blob size for each file uploaded. For use with GNU R and hist(read.table(\"filename\")$V1). Requires debug-memstats.")
flags.BoolVar(&cmd.capCtime, "capctime", false, "(debug flag) For file blobs use file modification time as creation time if it would be bigger (newer) than modification time. For stable filenode creation (you can forge mtime, but can't forge ctime).")
flags.BoolVar(&flagUseSQLiteChildCache, "sqlitecache", false, "(debug flag) Use sqlite for the statcache and havecache instead of a flat cache.")
flags.BoolVar(&cmd.contentsOnly, "contents_only", false, "(debug flag) Do not store any of the file's attributes. We write only the file's contents (the blobRefs for its parts) to the created file schema.")
} else {
cmd.statcache = true
}
if android.IsChild() {
flags.BoolVar(&cmd.argsFromInput, "stdinargs", false, "If true, filenames to upload are sent one-per-line on stdin. EOF means to quit the process with exit status 0.")
// limit number of goroutines to limit memory
uploadWorkers = 2
dirUploadWorkers = 2
statCacheWorkers = 2
}
flagCacheLog = flags.Bool("logcache", false, "log caching details")
return cmd
})
}
func (c *fileCmd) Describe() string {
return "Upload file(s)."
}
func (c *fileCmd) Usage() {
fmt.Fprintf(cmdmain.Stderr, "Usage: camput [globalopts] file [fileopts] <file/director(ies)>\n")
}
func (c *fileCmd) Examples() []string {
return []string{
"[opts] <file(s)/director(ies)",
"--permanode --title='Homedir backup' --tag=backup,homedir $HOME",
"--filenodes /mnt/camera/DCIM",
}
}
func (c *fileCmd) RunCommand(args []string) error {
if c.vivify {
if c.makePermanode || c.filePermanodes || c.tag != "" || c.title != "" {
return cmdmain.UsageError("--vivify excludes any other option")
}
}
if c.title != "" && !c.makePermanode {
return cmdmain.UsageError("Can't set title without using --permanode")
}
if c.tag != "" && !c.makePermanode && !c.filePermanodes {
return cmdmain.UsageError("Can't set tag without using --permanode or --filenodes")
}
if c.histo != "" && !c.memstats {
return cmdmain.UsageError("Can't use histo without memstats")
}
if c.deleteAfterUpload && !c.filePermanodes {
return cmdmain.UsageError("Can't set use --delete_after_upload without --filenodes")
}
if c.filePermanodes && c.contentsOnly {
return cmdmain.UsageError("--contents_only and --filenodes are exclusive. Use --permanode instead.")
}
// TODO(mpl): do it for other modes too. Or even better, do it once for all modes.
if *cmdmain.FlagVerbose {
log.SetOutput(cmdmain.Stderr)
} else {
log.SetOutput(ioutil.Discard)
}
up := getUploader()
if c.memstats {
sr := new(statspkg.Receiver)
up.altStatReceiver = sr
defer func() { DumpStats(sr, c.histo) }()
}
c.initCaches(up)
if c.makePermanode || c.filePermanodes {
testSigBlobRef := up.Client.SignerPublicKeyBlobref()
if !testSigBlobRef.Valid() {
return cmdmain.UsageError("A GPG key is needed to create permanodes; configure one or use vivify mode.")
}
}
up.fileOpts = &fileOptions{
permanode: c.filePermanodes,
tag: c.tag,
vivify: c.vivify,
exifTime: c.exifTime,
capCtime: c.capCtime,
contentsOnly: c.contentsOnly,
}
var (
permaNode *client.PutResult
lastPut *client.PutResult
err error
)
if c.makePermanode {
if len(args) != 1 {
return fmt.Errorf("The --permanode flag can only be used with exactly one file or directory argument")
}
permaNode, err = up.UploadNewPermanode()
if err != nil {
return fmt.Errorf("Uploading permanode: %v", err)
}
}
if c.diskUsage {
if len(args) != 1 {
return fmt.Errorf("The --du flag can only be used with exactly one directory argument")
}
dir := args[0]
fi, err := up.stat(dir)
if err != nil {
return err
}
if !fi.IsDir() {
return fmt.Errorf("%q is not a directory.", dir)
}
t := up.NewTreeUpload(dir)
t.DiskUsageMode = true
t.Start()
pr, err := t.Wait()
if err != nil {
return err
}
handleResult("tree-upload", pr, err)
return nil
}
if c.argsFromInput {
if len(args) > 0 {
return errors.New("args not supported with -argsfrominput")
}
tu := up.NewRootlessTreeUpload()
tu.Start()
br := bufio.NewReader(os.Stdin)
for {
path, err := br.ReadString('\n')
if path = strings.TrimSpace(path); path != "" {
tu.Enqueue(path)
}
if err == io.EOF {
android.PreExit()
os.Exit(0)
}
if err != nil {
log.Fatal(err)
}
}
}
if len(args) == 0 {
return cmdmain.UsageError("No files or directories given.")
}
if up.statCache != nil {
defer up.statCache.Close()
}
for _, filename := range args {
fi, err := os.Stat(filename)
if err != nil {
return err
}
// Skip ignored files or base directories. Failing to skip the
// latter results in a panic.
if up.Client.IsIgnoredFile(filename) {
log.Printf("Client configured to ignore %s; skipping.", filename)
continue
}
if fi.IsDir() {
if up.fileOpts.wantVivify() {
vlog.Printf("Directories not supported in vivify mode; skipping %v\n", filename)
continue
}
t := up.NewTreeUpload(filename)
t.Start()
lastPut, err = t.Wait()
} else {
lastPut, err = up.UploadFile(filename)
if err == nil && c.deleteAfterUpload {
if err := os.Remove(filename); err != nil {
log.Printf("Error deleting %v: %v", filename, err)
} else {
log.Printf("Deleted %v", filename)
}
}
}
if handleResult("file", lastPut, err) != nil {
return err
}
}
if permaNode != nil && lastPut != nil {
put, err := up.UploadAndSignBlob(schema.NewSetAttributeClaim(permaNode.BlobRef, "camliContent", lastPut.BlobRef.String()))
if handleResult("claim-permanode-content", put, err) != nil {
return err
}
if c.title != "" {
put, err := up.UploadAndSignBlob(schema.NewSetAttributeClaim(permaNode.BlobRef, "title", c.title))
handleResult("claim-permanode-title", put, err)
}
if c.tag != "" {
tags := strings.Split(c.tag, ",")
for _, tag := range tags {
m := schema.NewAddAttributeClaim(permaNode.BlobRef, "tag", tag)
put, err := up.UploadAndSignBlob(m)
handleResult("claim-permanode-tag", put, err)
}
}
handleResult("permanode", permaNode, nil)
}
return nil
}
func (c *fileCmd) initCaches(up *Uploader) {
if !c.statcache || *flagBlobDir != "" {
return
}
gen, err := up.StorageGeneration()
if err != nil {
log.Printf("WARNING: not using local caches; failed to retrieve server's storage generation: %v", err)
return
}
if c.statcache {
up.statCache = NewKvStatCache(gen)
}
}
// DumpStats creates the destFile and writes a line per received blob,
// with its blob size.
func DumpStats(sr *statspkg.Receiver, destFile string) {
sr.Lock()
defer sr.Unlock()
f, err := os.Create(destFile)
if err != nil {
log.Fatal(err)
}
var sum int64
for _, size := range sr.Have {
fmt.Fprintf(f, "%d\n", size)
}
fmt.Printf("In-memory blob stats: %d blobs, %d bytes\n", len(sr.Have), sum)
err = f.Close()
if err != nil {
log.Fatal(err)
}
}
type stats struct {
files, bytes int64
}
func (s *stats) incr(n *node) {
s.files++
if !n.fi.IsDir() {
s.bytes += n.fi.Size()
}
}
func (up *Uploader) lstat(path string) (os.FileInfo, error) {
// TODO(bradfitz): use VFS
return os.Lstat(path)
}
func (up *Uploader) stat(path string) (os.FileInfo, error) {
if up.fs == nil {
return os.Stat(path)
}
f, err := up.fs.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return f.Stat()
}
func (up *Uploader) open(path string) (http.File, error) {
if up.fs == nil {
return os.Open(path)
}
return up.fs.Open(path)
}
func (n *node) directoryStaticSet() (*schema.StaticSet, error) {
ss := new(schema.StaticSet)
for _, c := range n.children {
pr, err := c.PutResult()
if err != nil {
return nil, fmt.Errorf("Error populating directory static set for child %q: %v", c.fullPath, err)
}
ss.Add(pr.BlobRef)
}
return ss, nil
}
func (up *Uploader) uploadNode(n *node) (*client.PutResult, error) {
fi := n.fi
mode := fi.Mode()
if mode&os.ModeType == 0 {
return up.uploadNodeRegularFile(n)
}
bb := schema.NewCommonFileMap(n.fullPath, fi)
switch {
case mode&os.ModeSymlink != 0:
// TODO(bradfitz): use VFS here; not os.Readlink
target, err := os.Readlink(n.fullPath)
if err != nil {
return nil, err
}
bb.SetSymlinkTarget(target)
case mode&os.ModeDevice != 0:
// including mode & os.ModeCharDevice
fallthrough
case mode&os.ModeSocket != 0:
bb.SetType("socket")
case mode&os.ModeNamedPipe != 0: // fifo
bb.SetType("fifo")
default:
return nil, fmt.Errorf("camput.files: unsupported file type %v for file %v", mode, n.fullPath)
case fi.IsDir():
ss, err := n.directoryStaticSet()
if err != nil {
return nil, err
}
sspr, err := up.UploadBlob(ss)
if err != nil {
return nil, err
}
bb.PopulateDirectoryMap(sspr.BlobRef)
}
mappr, err := up.UploadBlob(bb)
if err == nil {
if !mappr.Skipped {
vlog.Printf("Uploaded %q, %s for %s", bb.Type(), mappr.BlobRef, n.fullPath)
}
} else {
vlog.Printf("Error uploading map for %s (%s, %s): %v", n.fullPath, bb.Type(), bb.Blob().BlobRef(), err)
}
return mappr, err
}
// statReceiver returns the StatReceiver used for checking for and uploading blobs.
//
// The optional provided node is only used for conditionally printing out status info to stdout.
func (up *Uploader) statReceiver(n *node) blobserver.StatReceiver {
statReceiver := up.altStatReceiver
if statReceiver == nil {
// TODO(mpl): simplify the altStatReceiver situation as well,
// see TODO in cmd/camput/uploader.go
statReceiver = up.Client
}
if android.IsChild() && n != nil && n.fi.Mode()&os.ModeType == 0 {
return android.StatusReceiver{Sr: statReceiver, Path: n.fullPath}
}
return statReceiver
}
func (up *Uploader) noStatReceiver(r blobserver.BlobReceiver) blobserver.StatReceiver {
return noStatReceiver{r}
}
// A haveCacheStatReceiver relays Receive calls to the embedded
// BlobReceiver and treats all Stat calls like the blob doesn't exist.
//
// This is used by the client once it's already asked the server that
// it doesn't have the whole file in some chunk layout already, so we
// know we're just writing new stuff. For resuming in the middle of
// larger uploads, it turns out that the pkg/client.Client.Upload
// already checks the have cache anyway, so going right to mid-chunk
// receives is fine.
//
// TODO(bradfitz): this probabaly all needs an audit/rationalization/tests
// to make sure all the players are agreeing on the responsibilities.
// And maybe the Android stats are wrong, too. (see pkg/client/android's
// StatReceiver)
type noStatReceiver struct {
blobserver.BlobReceiver
}
func (noStatReceiver) StatBlobs(dest chan<- blob.SizedRef, blobs []blob.Ref) error {
return nil
}
var atomicDigestOps int64 // number of files digested
// wholeFileDigest returns the sha1 digest of the regular file's absolute
// path given in fullPath.
func (up *Uploader) wholeFileDigest(fullPath string) (blob.Ref, error) {
// TODO(bradfitz): cache this.
file, err := up.open(fullPath)
if err != nil {
return blob.Ref{}, err
}
defer file.Close()
td := &trackDigestReader{r: file}
_, err = io.Copy(ioutil.Discard, td)
atomic.AddInt64(&atomicDigestOps, 1)
if err != nil {
return blob.Ref{}, err
}
return blob.MustParse(td.Sum()), nil
}
var noDupSearch, _ = strconv.ParseBool(os.Getenv("CAMLI_NO_FILE_DUP_SEARCH"))
// fileMapFromDuplicate queries the server's search interface for an
// existing file with an entire contents of sum (a blobref string).
// If the server has it, it's validated, and then fileMap (which must
// already be partially populated) has its "parts" field populated,
// and then fileMap is uploaded (if necessary) and a PutResult with
// its blobref is returned. If there's any problem, or a dup doesn't
// exist, ok is false.
// If required, Vivify is also done here.
func (up *Uploader) fileMapFromDuplicate(bs blobserver.StatReceiver, fileMap *schema.Builder, sum string) (pr *client.PutResult, ok bool) {
if noDupSearch {
return
}
_, err := up.Client.SearchRoot()
if err != nil {
return
}
dupFileRef, err := up.Client.SearchExistingFileSchema(blob.MustParse(sum))
if err != nil {
log.Printf("Warning: error searching for already-uploaded copy of %s: %v", sum, err)
return nil, false
}
if !dupFileRef.Valid() {
return nil, false
}
if *cmdmain.FlagVerbose {
log.Printf("Found dup of contents %s in file schema %s", sum, dupFileRef)
}
dupMap, err := up.Client.FetchSchemaBlob(dupFileRef)
if err != nil {
log.Printf("Warning: error fetching %v: %v", dupFileRef, err)
return nil, false
}
fileMap.PopulateParts(dupMap.PartsSize(), dupMap.ByteParts())
json, err := fileMap.JSON()
if err != nil {
return nil, false
}
uh := client.NewUploadHandleFromString(json)
if up.fileOpts.wantVivify() {
uh.Vivify = true
}
if !uh.Vivify && uh.BlobRef == dupFileRef {
// Unchanged (same filename, modtime, JSON serialization, etc)
return &client.PutResult{BlobRef: dupFileRef, Size: uint32(len(json)), Skipped: true}, true
}
pr, err = up.Upload(uh)
if err != nil {
log.Printf("Warning: error uploading file map after finding server dup of %v: %v", sum, err)
return nil, false
}
return pr, true
}
func (up *Uploader) uploadNodeRegularFile(n *node) (*client.PutResult, error) {
var filebb *schema.Builder
if up.fileOpts.contentsOnly {
filebb = schema.NewFileMap("")
} else {
filebb = schema.NewCommonFileMap(n.fullPath, n.fi)
}
filebb.SetType("file")
up.fdGate.Start()
defer up.fdGate.Done()
file, err := up.open(n.fullPath)
if err != nil {
return nil, err
}
defer file.Close()
if !up.fileOpts.contentsOnly {
if up.fileOpts.exifTime {
ra, ok := file.(io.ReaderAt)
if !ok {
return nil, errors.New("Error asserting local file to io.ReaderAt")
}
modtime, err := schema.FileTime(ra)
if err != nil {
log.Printf("warning: getting time from EXIF failed for %v: %v", n.fullPath, err)
} else {
filebb.SetModTime(modtime)
}
}
if up.fileOpts.capCtime {
filebb.CapCreationTime()
}
}
var (
size = n.fi.Size()
fileContents io.Reader = io.LimitReader(file, size)
br blob.Ref // of file schemaref
sum string // sha1 hashsum of the file to upload
pr *client.PutResult // of the final "file" schema blob
)
const dupCheckThreshold = 256 << 10
if size > dupCheckThreshold {
sumRef, err := up.wholeFileDigest(n.fullPath)
if err == nil {
sum = sumRef.String()
ok := false
pr, ok = up.fileMapFromDuplicate(up.statReceiver(n), filebb, sum)
if ok {
br = pr.BlobRef
android.NoteFileUploaded(n.fullPath, !pr.Skipped)
if up.fileOpts.wantVivify() {
// we can return early in that case, because the other options
// are disallowed in the vivify case.
return pr, nil
}
}
}
}
if up.fileOpts.wantVivify() {
// If vivify wasn't already done in fileMapFromDuplicate.
err := schema.WriteFileChunks(up.noStatReceiver(up.statReceiver(n)), filebb, fileContents)
if err != nil {
return nil, err
}
json, err := filebb.JSON()
if err != nil {
return nil, err
}
br = blob.SHA1FromString(json)
h := &client.UploadHandle{
BlobRef: br,
Size: uint32(len(json)),
Contents: strings.NewReader(json),
Vivify: true,
}
pr, err = up.Upload(h)
if err != nil {
return nil, err
}
android.NoteFileUploaded(n.fullPath, true)
return pr, nil
}
if !br.Valid() {
// br still zero means fileMapFromDuplicate did not find the file on the server,
// and the file has not just been uploaded subsequently to a vivify request.
// So we do the full file + file schema upload here.
if sum == "" && up.fileOpts.wantFilePermanode() {
fileContents = &trackDigestReader{r: fileContents}
}
br, err = schema.WriteFileMap(up.noStatReceiver(up.statReceiver(n)), filebb, fileContents)
if err != nil {
return nil, err
}
}
// The work for those planned permanodes (and the claims) is redone
// everytime we get here (i.e past the stat cache). However, they're
// caught by the have cache, so they won't be reuploaded for nothing
// at least.
if up.fileOpts.wantFilePermanode() {
if td, ok := fileContents.(*trackDigestReader); ok {
sum = td.Sum()
}
// claimTime is both the time of the "claimDate" in the
// JSON claim, as well as the date in the OpenPGP
// header.
// TODO(bradfitz): this is a little clumsy to do by hand.
// There should probably be a method on *Uploader to do this
// from an unsigned schema map. Maybe ditch the schema.Claimer
// type and just have the Uploader override the claimDate.
claimTime, ok := filebb.ModTime()
if !ok {
return nil, fmt.Errorf("couldn't get modtime for file %v", n.fullPath)
}
err = up.uploadFilePermanode(sum, br, claimTime)
if err != nil {
return nil, fmt.Errorf("Error uploading permanode for node %v: %v", n, err)
}
}
// TODO(bradfitz): faking a PutResult here to return
// is kinda gross. should instead make a
// blobserver.Storage wrapper type (wrapping
// statReceiver) that can track some of this? or make
// schemaWriteFileMap return it?
json, _ := filebb.JSON()
pr = &client.PutResult{BlobRef: br, Size: uint32(len(json)), Skipped: false}
return pr, nil
}
// uploadFilePermanode creates and uploads the planned permanode (with sum as a
// fixed key) associated with the file blobref fileRef.
// It also sets the optional tags for this permanode.
func (up *Uploader) uploadFilePermanode(sum string, fileRef blob.Ref, claimTime time.Time) error {
// Use a fixed time value for signing; not using modtime
// so two identical files don't have different modtimes?
// TODO(bradfitz): consider this more?
permaNodeSigTime := time.Unix(0, 0)
permaNode, err := up.UploadPlannedPermanode(sum, permaNodeSigTime)
if err != nil {
return fmt.Errorf("Error uploading planned permanode: %v", err)
}
handleResult("node-permanode", permaNode, nil)
contentAttr := schema.NewSetAttributeClaim(permaNode.BlobRef, "camliContent", fileRef.String())
contentAttr.SetClaimDate(claimTime)
signer, err := up.Signer()
if err != nil {
return err
}
signed, err := contentAttr.SignAt(signer, claimTime)
if err != nil {
return fmt.Errorf("Failed to sign content claim: %v", err)
}
put, err := up.uploadString(signed)
if err != nil {
return fmt.Errorf("Error uploading permanode's attribute: %v", err)
}
handleResult("node-permanode-contentattr", put, nil)
if tags := up.fileOpts.tags(); len(tags) > 0 {
errch := make(chan error)
for _, tag := range tags {
go func(tag string) {
m := schema.NewAddAttributeClaim(permaNode.BlobRef, "tag", tag)
m.SetClaimDate(claimTime)
signed, err := m.SignAt(signer, claimTime)
if err != nil {
errch <- fmt.Errorf("Failed to sign tag claim: %v", err)
return
}
put, err := up.uploadString(signed)
if err != nil {
errch <- fmt.Errorf("Error uploading permanode's tag attribute %v: %v", tag, err)
return
}
handleResult("node-permanode-tag", put, nil)
errch <- nil
}(tag)
}
for _ = range tags {
if e := <-errch; e != nil && err == nil {
err = e
}
}
if err != nil {
return err
}
}
return nil
}
func (up *Uploader) UploadFile(filename string) (*client.PutResult, error) {
fullPath, err := filepath.Abs(filename)
if err != nil {
return nil, err
}
fi, err := up.lstat(fullPath)
if err != nil {
return nil, err
}
if fi.IsDir() {
panic("must use UploadTree now for directories")
}
n := &node{
fullPath: fullPath,
fi: fi,
}
withPermanode := up.fileOpts.wantFilePermanode()
if up.statCache != nil && !up.fileOpts.wantVivify() {
// Note: ignoring cache hits if wantVivify, otherwise
// a non-vivify put followed by a vivify one wouldn't
// end up doing the vivify.
if cachedRes, err := up.statCache.CachedPutResult(
up.pwd, n.fullPath, n.fi, withPermanode); err == nil {
return cachedRes, nil
}
}
pr, err := up.uploadNode(n)
if err == nil && up.statCache != nil {
up.statCache.AddCachedPutResult(
up.pwd, n.fullPath, n.fi, pr, withPermanode)
}
return pr, err
}
// NewTreeUpload returns a TreeUpload. It doesn't begin uploading any files until a
// call to Start
func (up *Uploader) NewTreeUpload(dir string) *TreeUpload {
tu := up.NewRootlessTreeUpload()
tu.rootless = false
tu.base = dir
return tu
}
func (up *Uploader) NewRootlessTreeUpload() *TreeUpload {
return &TreeUpload{
rootless: true,
base: "",
up: up,
donec: make(chan bool, 1),
errc: make(chan error, 1),
stattedc: make(chan *node, buffered),
}
}
func (t *TreeUpload) Start() {
go t.run()
}
type node struct {
tu *TreeUpload // nil if not doing a tree upload
fullPath string
fi os.FileInfo
children []*node
// cond (and its &mu Lock) guard err and res.
cond sync.Cond // with L being &mu
mu sync.Mutex
err error
res *client.PutResult
sumBytes int64 // cached value, if non-zero. also guarded by mu.
}
func (n *node) String() string {
if n == nil {
return "<nil *node>"
}
return fmt.Sprintf("[node %s, isDir=%v, nchild=%d]", n.fullPath, n.fi.IsDir(), len(n.children))
}
func (n *node) SetPutResult(res *client.PutResult, err error) {
n.mu.Lock()
defer n.mu.Unlock()
if res == nil && err == nil {
panic("SetPutResult called with (nil, nil)")
}
if n.res != nil || n.err != nil {
panic("SetPutResult called twice on node " + n.fullPath)
}
n.res, n.err = res, err
n.cond.Signal()
}
func (n *node) PutResult() (*client.PutResult, error) {
n.mu.Lock()
defer n.mu.Unlock()
for n.err == nil && n.res == nil {
n.cond.Wait()
}
return n.res, n.err
}
func (n *node) SumBytes() (v int64) {
n.mu.Lock()
defer n.mu.Unlock()
if n.sumBytes != 0 {
return n.sumBytes
}
for _, c := range n.children {
v += c.SumBytes()
}
if n.fi.Mode()&os.ModeType == 0 {
v += n.fi.Size()
}
n.sumBytes = v
return
}
/*
A TreeUpload holds the state of an ongoing recursive directory tree
upload. Call Wait to get the final result.
Uploading a directory tree involves several concurrent processes, each
which may involve multiple goroutines:
1) one process stats all files and walks all directories as fast as possible
to calculate how much total work there will be. this goroutine also
filters out directories to be skipped. (caches, temp files, skipDirs, etc)
2) one process works though the files that were discovered and checks
the statcache to see what actually needs to be uploaded.
The statcache is
full path => {last os.FileInfo signature, put result from last time}
and is used to avoid re-reading/digesting the file even locally,
trusting that it's already on the server.
3) one process uploads files & metadata. This process checks the "havecache"
to see which blobs are already on the server. For awhile the local havecache
(if configured) and the remote blobserver "stat" RPC are raced to determine
if the local havecache is even faster. If not, it's not consulted. But if the
latency of remote stats is high enough, checking locally is preferred.
*/
type TreeUpload struct {
// If DiskUsageMode is set true before Start, only
// per-directory disk usage stats are output, like the "du"
// command.
DiskUsageMode bool
// Immutable:
rootless bool // if true, "base" will be empty.
base string // base directory
up *Uploader
stattedc chan *node // from stat-the-world goroutine to run()
donec chan bool // closed when run() finishes
err error
errc chan error // with 1 buffer item
// Owned by run goroutine:
total stats // total bytes on disk
skipped stats // not even tried to upload (trusting stat cache)
uploaded stats // uploaded (even if server said it already had it and bytes weren't sent)
finalPutRes *client.PutResult // set after run() returns
}
// Enqueue starts uploading path (a file, directory, etc).
func (t *TreeUpload) Enqueue(path string) {
t.statPath(path, nil)
}
// fi is optional (will be statted if nil)
func (t *TreeUpload) statPath(fullPath string, fi os.FileInfo) (nod *node, err error) {
defer func() {
if err == nil && nod != nil {
t.stattedc <- nod
}
}()
if t.up.Client.IsIgnoredFile(fullPath) {
return nil, nil
}
if fi == nil {
fi, err = t.up.lstat(fullPath)
if err != nil {
return nil, err
}
}
n := &node{
tu: t,
fullPath: fullPath,
fi: fi,
}
n.cond.L = &n.mu
if !fi.IsDir() {
return n, nil
}
f, err := t.up.open(fullPath)
if err != nil {
return nil, err
}
fis, err := f.Readdir(-1)
f.Close()
if err != nil {
return nil, err
}
sort.Sort(byTypeAndName(fis))
for _, fi := range fis {
depn, err := t.statPath(filepath.Join(fullPath, filepath.Base(fi.Name())), fi)
if err != nil {
return nil, err
}
if depn != nil {
n.children = append(n.children, depn)
}
}
return n, nil
}
// testHookStatCache, if non-nil, runs first in the checkStatCache worker.
var testHookStatCache func(el interface{}, ok bool)
func (t *TreeUpload) run() {
defer close(t.donec)
// Kick off scanning all files, eventually learning the root
// node (which references all its children).
var root *node // nil until received and set in loop below.
rootc := make(chan *node, 1)
if !t.rootless {
go func() {
n, err := t.statPath(t.base, nil)
if err != nil {
log.Fatalf("Error scanning files under %s: %v", t.base, err)
}
close(t.stattedc)
rootc <- n
}()
}
var lastStat, lastUpload string
dumpStats := func() {
if android.IsChild() {
printAndroidCamputStatus(t)
return