8
8
"io"
9
9
"os"
10
10
"path/filepath"
11
+ "strings"
11
12
12
13
"github.com/KarpelesLab/reflink"
13
14
"github.com/ipfs/go-cid"
@@ -477,14 +478,32 @@ func (sb *SealCalls) LocalStorage(ctx context.Context) ([]storiface.StoragePath,
477
478
return sb .sectors .localStore .Local (ctx )
478
479
}
479
480
480
- func (sb * SealCalls ) FinalizeSector (ctx context.Context , sector storiface.SectorRef , keepUnsealed bool ) error {
481
- alloc := storiface .FTNone
482
- if keepUnsealed {
483
- // note: In Curio we don't write the unsealed file in any of the previous stages, it's only written here from tree-d
484
- alloc = storiface .FTUnsealed
481
+ func changePathType (path string , newType storiface.SectorFileType ) (string , error ) {
482
+ // /some/parent/[type]/filename -> /some/parent/[newType]/filename
483
+
484
+ dir , file := filepath .Split (path )
485
+ if dir == "" {
486
+ return "" , xerrors .Errorf ("path has too few components: %s" , path )
487
+ }
488
+
489
+ components := strings .Split (strings .TrimRight (dir , string (filepath .Separator )), string (filepath .Separator ))
490
+ if len (components ) < 1 {
491
+ return "" , xerrors .Errorf ("path has too few components: %s" , path )
485
492
}
486
493
487
- sectorPaths , pathIDs , releaseSector , err := sb .sectors .AcquireSector (ctx , nil , sector , storiface .FTCache , alloc , storiface .PathSealing )
494
+ newTypeName := newType .String ()
495
+
496
+ components [len (components )- 1 ] = newTypeName
497
+ newPath := filepath .Join (append (components , file )... )
498
+
499
+ if filepath .IsAbs (path ) {
500
+ newPath = filepath .Join (string (filepath .Separator ), newPath )
501
+ }
502
+
503
+ return newPath , nil
504
+ }
505
+ func (sb * SealCalls ) FinalizeSector (ctx context.Context , sector storiface.SectorRef , keepUnsealed bool ) error {
506
+ sectorPaths , pathIDs , releaseSector , err := sb .sectors .AcquireSector (ctx , nil , sector , storiface .FTCache , storiface .FTNone , storiface .PathSealing )
488
507
if err != nil {
489
508
return xerrors .Errorf ("acquiring sector paths: %w" , err )
490
509
}
@@ -496,6 +515,21 @@ func (sb *SealCalls) FinalizeSector(ctx context.Context, sector storiface.Sector
496
515
}
497
516
498
517
if keepUnsealed {
518
+ // We are going to be moving the unsealed file, no need to allocate storage specifically for it
519
+ sectorPaths .Unsealed , err = changePathType (sectorPaths .Cache , storiface .FTUnsealed )
520
+ if err != nil {
521
+ return xerrors .Errorf ("changing path type: %w" , err )
522
+ }
523
+
524
+ pathIDs .Unsealed = pathIDs .Cache // this is just an uuid string
525
+
526
+ defer func () {
527
+ // We don't pass FTUnsealed to Acquire, so releaseSector won't declare it. Do it here.
528
+ if err := sb .sectors .sindex .StorageDeclareSector (ctx , storiface .ID (pathIDs .Unsealed ), sector .ID , storiface .FTUnsealed , true ); err != nil {
529
+ log .Errorf ("declare unsealed sector error: %+v" , err )
530
+ }
531
+ }()
532
+
499
533
// tree-d contains exactly unsealed data in the prefix, so
500
534
// * we move it to a temp file
501
535
// * we truncate the temp file to the sector size
@@ -579,7 +613,12 @@ afterUnsealedMove:
579
613
return xerrors .Errorf ("clearing cache: %w" , err )
580
614
}
581
615
582
- if err := sb .ensureOneCopy (ctx , sector .ID , pathIDs , storiface .FTCache | alloc ); err != nil {
616
+ maybeUns := storiface .FTUnsealed
617
+ if ! keepUnsealed {
618
+ maybeUns = storiface .FTNone
619
+ }
620
+
621
+ if err := sb .ensureOneCopy (ctx , sector .ID , pathIDs , storiface .FTCache | maybeUns ); err != nil {
583
622
return xerrors .Errorf ("ensure one copy: %w" , err )
584
623
}
585
624
0 commit comments