11package mg3
22
33import (
4+ "encoding/hex"
45 "fmt"
6+ "io/ioutil"
7+ "os"
58 "path"
9+ "path/filepath"
610 "strings"
711 "time"
812
@@ -16,6 +20,7 @@ import (
1620 mount "github.com/ipfs/fs-repo-migrations/ipfs-2-to-3/Godeps/_workspace/src/github.com/jbenet/go-datastore/mount"
1721 dsq "github.com/ipfs/fs-repo-migrations/ipfs-2-to-3/Godeps/_workspace/src/github.com/jbenet/go-datastore/query"
1822 sync "github.com/ipfs/fs-repo-migrations/ipfs-2-to-3/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
23+ rename "github.com/ipfs/fs-repo-migrations/ipfs-2-to-3/Godeps/_workspace/src/github.com/jbenet/go-os-rename"
1924 base32 "github.com/ipfs/fs-repo-migrations/ipfs-3-to-4/base32"
2025 nuflatfs "github.com/ipfs/fs-repo-migrations/ipfs-3-to-4/flatfs"
2126 mfsr "github.com/ipfs/fs-repo-migrations/mfsr"
@@ -103,10 +108,16 @@ func (m Migration) Apply(opts migrate.Options) error {
103108 }
104109
105110 log .Log ("transfering blocks to new key format" )
106- if err := rewriteKeys ( dsold , dsnew , "blocks" , newKeyFunc ( "/blocks/" ), validateOldKey , transferBlock ); err != nil {
111+ if err := transferBlocks ( filepath . Join ( opts . Path , "blocks" ) ); err != nil {
107112 return err
108113 }
109114
115+ /*
116+ if err := rewriteKeys(dsold, dsnew, "blocks", newKeyFunc("/blocks/"), validateOldKey, transferBlock); err != nil {
117+ return err
118+ }
119+ */
120+
110121 log .Log ("transferring stored public key records" )
111122 if err := rewriteKeys (dsold , dsnew , "pk" , newKeyFunc ("/pk/" ), validateOldKey , transferPubKey ); err != nil {
112123 return err
@@ -234,24 +245,12 @@ func rewriteKeys(oldds, newds dstore.Datastore, pref string, mkKey mkKeyFunc, va
234245
235246 log .Log ("got %d keys, beginning transfer. This will take some time." , len (entries ))
236247
237- before := time .Now ()
238- var skipped int
239- for i , e := range entries {
240- fmt .Printf ("\r [%d / %d]" , i , len (entries ))
241- if skipped > 0 {
242- fmt .Printf (" (skipped: %d)" , skipped )
243- }
244- if i % 10 == 9 {
245- took := time .Now ().Sub (before )
246- av := took / time .Duration (i )
247- estim := av * time .Duration (len (entries )- i )
248- est := strings .Split (estim .String (), "." )[0 ]
249-
250- fmt .Printf (" Approx time remaining: %ss " , est )
251- }
248+ prog := NewProgress (len (entries ))
249+ for _ , e := range entries {
250+ prog .Next ()
252251
253252 if ! valid (e .Key ) {
254- skipped ++
253+ prog . Skip ()
255254 continue
256255 }
257256
@@ -321,3 +320,107 @@ func revertIpnsEntries(ds dstore.Datastore, oldk dstore.Key, data []byte, mkkey
321320 dsk := dstore .NewKey ("/ipns/" + string (dec ))
322321 return ds .Put (dsk , data )
323322}
323+
324+ func transferBlocks (flatfsdir string ) error {
325+ var keys []string
326+ filepath .Walk (flatfsdir , func (p string , i os.FileInfo , err error ) error {
327+ if i .IsDir () {
328+ return nil
329+ }
330+
331+ if err != nil {
332+ return err
333+ }
334+
335+ rel := p [len (flatfsdir )+ 1 :]
336+ if ! strings .HasPrefix (rel , "1220" ) {
337+ fmt .Println ("skipping: " , rel )
338+ return nil
339+ }
340+
341+ if ! strings .HasSuffix (rel , ".data" ) {
342+ fmt .Println ("skipping (no .data): " , rel )
343+ return nil
344+ }
345+
346+ keys = append (keys , p )
347+ return nil
348+ })
349+
350+ prog := NewProgress (len (keys ))
351+ for _ , p := range keys {
352+ prog .Next ()
353+ rel := p [len (flatfsdir )+ 1 :]
354+
355+ _ , fi := filepath .Split (rel [:len (rel )- 5 ])
356+ k , err := hex .DecodeString (fi )
357+ if err != nil {
358+ fmt .Printf ("failed to decode: %s\n " , p )
359+ return err
360+ }
361+
362+ if len (k ) != 34 {
363+ data , err := ioutil .ReadFile (p )
364+ if err != nil {
365+ return err
366+ }
367+
368+ key := blocks .NewBlock (data ).Key ()
369+ k = []byte (key )
370+ }
371+
372+ nname := base32 .RawStdEncoding .EncodeToString (k ) + ".data"
373+ dirname := nname [:5 ]
374+ nfiname := filepath .Join (flatfsdir , dirname , nname )
375+
376+ err = os .MkdirAll (filepath .Join (flatfsdir , dirname ), 0755 )
377+ if err != nil {
378+ return err
379+ }
380+
381+ err = rename .Rename (p , nfiname )
382+ if err != nil {
383+ return err
384+ }
385+ }
386+
387+ fmt .Println ()
388+
389+ return nil
390+ }
391+
392+ type progress struct {
393+ total int
394+ current int
395+ skipped int
396+
397+ start time.Time
398+ }
399+
400+ func NewProgress (total int ) * progress {
401+ return & progress {
402+ total : total ,
403+ start : time .Now (),
404+ }
405+ }
406+
407+ func (p * progress ) Skip () {
408+ p .skipped ++
409+ }
410+
411+ func (p * progress ) Next () {
412+ p .current ++
413+ fmt .Printf ("\r [%d / %d]" , p .current , p .total )
414+ if p .skipped > 0 {
415+ fmt .Printf (" (skipped: %d)" , p .skipped )
416+ }
417+
418+ if p .current % 10 == 9 {
419+ took := time .Now ().Sub (p .start )
420+ av := took / time .Duration (p .current )
421+ estim := av * time .Duration (p .total - p .current )
422+ est := strings .Split (estim .String (), "." )[0 ]
423+
424+ fmt .Printf (" Approx time remaining: %ss " , est )
425+ }
426+ }
0 commit comments