partialfile: deal with non-trailered files

This commit is contained in:
Łukasz Magiera 2024-02-10 18:43:12 +01:00
parent f644cf11e1
commit c68257236b

View File

@ -102,15 +102,37 @@ func OpenPartialFile(maxPieceSize abi.PaddedPieceSize, path string) (*PartialFil
return nil, xerrors.Errorf("openning partial file '%s': %w", path, err)
}
st, err := f.Stat()
if err != nil {
return nil, xerrors.Errorf("stat '%s': %w", path, err)
}
if st.Size() < int64(maxPieceSize) {
return nil, xerrors.Errorf("sector file '%s' was smaller than the sector size %d < %d", path, st.Size(), maxPieceSize)
}
if st.Size() == int64(maxPieceSize) {
log.Debugw("no partial file trailer, assuming fully allocated", "path", path)
allAlloc := &rlepluslazy.RunSliceIterator{Runs: []rlepluslazy.Run{{Val: true, Len: uint64(maxPieceSize)}}}
enc, err := rlepluslazy.EncodeRuns(allAlloc, []byte{})
if err != nil {
return nil, xerrors.Errorf("encoding full allocation: %w", err)
}
rle, err := rlepluslazy.FromBuf(enc)
if err != nil {
return nil, xerrors.Errorf("decoding full allocation: %w", err)
}
return &PartialFile{
maxPiece: maxPieceSize,
path: path,
allocated: rle,
file: f,
}, nil
}
var rle rlepluslazy.RLE
err = func() error {
st, err := f.Stat()
if err != nil {
return xerrors.Errorf("stat '%s': %w", path, err)
}
if st.Size() < int64(maxPieceSize) {
return xerrors.Errorf("sector file '%s' was smaller than the sector size %d < %d", path, st.Size(), maxPieceSize)
}
// read trailer
var tlen [4]byte
_, err = f.ReadAt(tlen[:], st.Size()-int64(len(tlen)))