Skip to content

Commit

Permalink
Preset file with new format
Browse files Browse the repository at this point in the history
  • Loading branch information
kikakkz committed Aug 24, 2020
1 parent a8db8b9 commit 7753091
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions extern/sector-storage/ffiwrapper/sealer_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"runtime"
"sync"
"syscall"
"encoding/json"

"github.com/ipfs/go-cid"
"golang.org/x/xerrors"
Expand Down Expand Up @@ -75,16 +76,22 @@ func (sb *Sealer) presetSectorPieceCidFilename() string {
return fmt.Sprintf("%s/sector-piece-cid-%v", presetDir(), abi.PaddedPieceSize(sb.ssize))
}

func (sb *Sealer) presetSectorPieceCid() (string, error) {
func (sb *Sealer) presetSectorPieceCid() ([]abi.PieceInfo, error) {
var pieceCids []abi.PieceInfo

cidFilename := sb.presetSectorPieceCidFilename()
if _, err := os.Stat(cidFilename); os.IsNotExist(err) {
return "", xerrors.Errorf("preset sector piece cid is not exist")
return pieceCids, xerrors.Errorf("preset sector piece cids is not exist")
}
cid, err := ioutil.ReadFile(cidFilename)
body, err := ioutil.ReadFile(cidFilename)
if nil != err {
return "", xerrors.Errorf("cannot read preset sector piece cid")
return pieceCids, xerrors.Errorf("cannot read preset sector piece cids")
}
return string(cid), nil
err = json.Unmarshal(body, &pieceCids)
if nil != err {
return pieceCids, xerrors.Errorf("cannot parse preset sector piece cids")
}
return pieceCids, nil
}

func copyFile(dst string, src string) error {
Expand Down Expand Up @@ -157,26 +164,7 @@ func (sb *Sealer) tryCreateUnsealedFileFromPreset(ctx context.Context, sector ab
}

func (sb *Sealer) presetPieceCids(stagedFile *partialFile, pieceSize abi.UnpaddedPieceSize, chunk abi.PaddedPieceSize) ([]abi.PieceInfo, error) {
var pieceCids []abi.PieceInfo

presetCid, err := sb.presetSectorPieceCid()
if nil != err {
return pieceCids, err
}

presetCidV, err := cid.Decode(presetCid)
if nil != err {
return pieceCids, err
}

for total := 0; total < int(pieceSize.Padded()); total += int(chunk) {
pieceCids = append(pieceCids, abi.PieceInfo{
Size: chunk.Unpadded().Padded(),
PieceCID: presetCidV,
})
}

return pieceCids, nil
return sb.presetSectorPieceCid()
}

func (sb *Sealer) openExistUnsealedFile(ctx context.Context, sector abi.SectorID, maxPieceSize abi.PaddedPieceSize) (*partialFile, func(), error) {
Expand Down Expand Up @@ -228,6 +216,7 @@ func (sb *Sealer) pieceCids(stagedFile *partialFile, pieceSize abi.UnpaddedPiece
if err != nil {
return pieceCids, xerrors.Errorf("pieceCid error: %w", err)
}
log.Warnf("%v", c)
pieceCids = append(pieceCids, abi.PieceInfo{
Size: abi.UnpaddedPieceSize(len(buf[:read])).Padded(),
PieceCID: c,
Expand Down Expand Up @@ -290,9 +279,10 @@ func (sb *Sealer) AddPiece(ctx context.Context, sector abi.SectorID, existingPie
presetFile := sb.presetSectorFilename()
presetSectorMutex.Lock()
copyFile(presetFile, stagedFile.path)
presetSectorMutex.Unlock()
presetCidFile := sb.presetSectorPieceCidFilename()
ioutil.WriteFile(presetCidFile, []byte(pieceCids[0].PieceCID.String()), 0644)
json, _ := json.Marshal(pieceCids)
ioutil.WriteFile(presetCidFile, json, 0644)
presetSectorMutex.Unlock()
}

if err := stagedFile.MarkAllocated(storiface.UnpaddedByteIndex(offset).Padded(), pieceSize.Padded()); err != nil {
Expand Down

0 comments on commit 7753091

Please sign in to comment.