From 1bbfb7cffda82fd46f2bf604939dba4d18fcff02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 8 May 2020 00:22:58 +0200 Subject: [PATCH] Fix ffiwrapper unseal --- ffiwrapper/sealer_cgo.go | 65 ++++++++++++++++++++++++---------------- resources.go | 2 +- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/ffiwrapper/sealer_cgo.go b/ffiwrapper/sealer_cgo.go index 6764c3d96..f90a6020e 100644 --- a/ffiwrapper/sealer_cgo.go +++ b/ffiwrapper/sealer_cgo.go @@ -108,33 +108,48 @@ func (sb *Sealer) AddPiece(ctx context.Context, sector abi.SectorID, existingPie }, werr() } +type closerFunc func() error + +func (cf closerFunc) Close() error { + return cf() +} + func (sb *Sealer) ReadPieceFromSealedSector(ctx context.Context, sector abi.SectorID, offset UnpaddedByteIndex, size abi.UnpaddedPieceSize, ticket abi.SealRandomness, unsealedCID cid.Cid) (io.ReadCloser, error) { - path, doneUnsealed, err := sb.sectors.AcquireSector(ctx, sector, stores.FTUnsealed, stores.FTUnsealed, false) - if err != nil { - return nil, xerrors.Errorf("acquire unsealed sector path: %w", err) - } - defer doneUnsealed() - f, err := os.OpenFile(path.Unsealed, os.O_RDONLY, 0644) - if err == nil { - if _, err := f.Seek(int64(offset), io.SeekStart); err != nil { - return nil, xerrors.Errorf("seek: %w", err) + { + path, doneUnsealed, err := sb.sectors.AcquireSector(ctx, sector, stores.FTUnsealed, stores.FTNone, false) + if err != nil { + return nil, xerrors.Errorf("acquire unsealed sector path: %w", err) } - lr := io.LimitReader(f, int64(size)) + f, err := os.OpenFile(path.Unsealed, os.O_RDONLY, 0644) + if err == nil { + if _, err := f.Seek(int64(offset), io.SeekStart); err != nil { + doneUnsealed() + return nil, xerrors.Errorf("seek: %w", err) + } - return &struct { - io.Reader - io.Closer - }{ - Reader: lr, - Closer: f, - }, nil - } - if !os.IsNotExist(err) { - return nil, err + lr := io.LimitReader(f, int64(size)) + + return &struct { + io.Reader + io.Closer + }{ + Reader: lr, + Closer: closerFunc(func() error { + doneUnsealed() + return f.Close() + }), + }, nil + } + + doneUnsealed() + + if !os.IsNotExist(err) { + return nil, err + } } - sealed, doneSealed, err := sb.sectors.AcquireSector(ctx, sector, stores.FTUnsealed|stores.FTCache, 0, false) + paths, doneSealed, err := sb.sectors.AcquireSector(ctx, sector, stores.FTSealed|stores.FTCache, stores.FTUnsealed, false) if err != nil { return nil, xerrors.Errorf("acquire sealed/cache sector path: %w", err) } @@ -145,9 +160,9 @@ func (sb *Sealer) ReadPieceFromSealedSector(ctx context.Context, sector abi.Sect // remove last used one (or use whatever other cache policy makes sense)) err = ffi.Unseal( sb.sealProofType, - sealed.Cache, - sealed.Sealed, - path.Unsealed, + paths.Cache, + paths.Sealed, + paths.Unsealed, sector.Number, sector.Miner, ticket, @@ -157,7 +172,7 @@ func (sb *Sealer) ReadPieceFromSealedSector(ctx context.Context, sector abi.Sect return nil, xerrors.Errorf("unseal failed: %w", err) } - f, err = os.OpenFile(string(path.Unsealed), os.O_RDONLY, 0644) + f, err := os.OpenFile(paths.Unsealed, os.O_RDONLY, 0644) if err != nil { return nil, err } diff --git a/resources.go b/resources.go index 0407816fa..87058e80a 100644 --- a/resources.go +++ b/resources.go @@ -110,7 +110,7 @@ var ResourceTable = map[sealtasks.TaskType]map[abi.RegisteredProof]Resources{ MinMemory: 32 << 30, Threads: -1, - CanGPU: true, + CanGPU: true, BaseMinMemory: 30 << 30, },