2020-03-26 02:50:56 +00:00
|
|
|
package basicfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-03-26 02:50:56 +00:00
|
|
|
|
2022-06-15 10:06:22 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/storiface"
|
2020-03-26 02:50:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type sectorFile struct {
|
|
|
|
abi.SectorID
|
2020-09-06 16:54:00 +00:00
|
|
|
storiface.SectorFileType
|
2020-03-26 02:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Provider struct {
|
|
|
|
Root string
|
|
|
|
|
|
|
|
lk sync.Mutex
|
|
|
|
waitSector map[sectorFile]chan struct{}
|
|
|
|
}
|
|
|
|
|
2022-06-17 11:31:05 +00:00
|
|
|
func (b *Provider) AcquireSector(ctx context.Context, id storiface.SectorRef, existing storiface.SectorFileType, allocate storiface.SectorFileType, ptype storiface.PathType) (storiface.SectorPaths, func(), error) {
|
2020-09-06 16:54:00 +00:00
|
|
|
if err := os.Mkdir(filepath.Join(b.Root, storiface.FTUnsealed.String()), 0755); err != nil && !os.IsExist(err) { // nolint
|
|
|
|
return storiface.SectorPaths{}, nil, err
|
2020-03-27 17:21:32 +00:00
|
|
|
}
|
2020-09-06 16:54:00 +00:00
|
|
|
if err := os.Mkdir(filepath.Join(b.Root, storiface.FTSealed.String()), 0755); err != nil && !os.IsExist(err) { // nolint
|
|
|
|
return storiface.SectorPaths{}, nil, err
|
2020-03-27 17:21:32 +00:00
|
|
|
}
|
2020-09-06 16:54:00 +00:00
|
|
|
if err := os.Mkdir(filepath.Join(b.Root, storiface.FTCache.String()), 0755); err != nil && !os.IsExist(err) { // nolint
|
2022-01-18 21:35:07 +00:00
|
|
|
return storiface.SectorPaths{}, nil, err
|
|
|
|
}
|
|
|
|
if err := os.Mkdir(filepath.Join(b.Root, storiface.FTUpdate.String()), 0755); err != nil && !os.IsExist(err) { // nolint
|
|
|
|
return storiface.SectorPaths{}, nil, err
|
|
|
|
}
|
|
|
|
if err := os.Mkdir(filepath.Join(b.Root, storiface.FTUpdateCache.String()), 0755); err != nil && !os.IsExist(err) { // nolint
|
2020-09-06 16:54:00 +00:00
|
|
|
return storiface.SectorPaths{}, nil, err
|
2020-03-27 17:21:32 +00:00
|
|
|
}
|
2020-03-26 02:50:56 +00:00
|
|
|
|
|
|
|
done := func() {}
|
|
|
|
|
2020-09-06 16:54:00 +00:00
|
|
|
out := storiface.SectorPaths{
|
2020-11-04 20:29:08 +00:00
|
|
|
ID: id.ID,
|
2020-03-27 17:21:32 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 16:54:00 +00:00
|
|
|
for _, fileType := range storiface.PathTypes {
|
2020-03-27 17:21:32 +00:00
|
|
|
if !existing.Has(fileType) && !allocate.Has(fileType) {
|
2020-03-26 02:50:56 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
b.lk.Lock()
|
|
|
|
if b.waitSector == nil {
|
|
|
|
b.waitSector = map[sectorFile]chan struct{}{}
|
|
|
|
}
|
2020-11-04 20:29:08 +00:00
|
|
|
ch, found := b.waitSector[sectorFile{id.ID, fileType}]
|
2020-03-26 02:50:56 +00:00
|
|
|
if !found {
|
|
|
|
ch = make(chan struct{}, 1)
|
2020-11-04 20:29:08 +00:00
|
|
|
b.waitSector[sectorFile{id.ID, fileType}] = ch
|
2020-03-26 02:50:56 +00:00
|
|
|
}
|
|
|
|
b.lk.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ch <- struct{}{}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
done()
|
2020-09-06 16:54:00 +00:00
|
|
|
return storiface.SectorPaths{}, nil, ctx.Err()
|
2020-03-26 02:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 20:29:08 +00:00
|
|
|
path := filepath.Join(b.Root, fileType.String(), storiface.SectorName(id.ID))
|
2020-05-18 23:03:42 +00:00
|
|
|
|
2020-03-26 02:50:56 +00:00
|
|
|
prevDone := done
|
|
|
|
done = func() {
|
|
|
|
prevDone()
|
|
|
|
<-ch
|
|
|
|
}
|
2020-03-27 17:21:32 +00:00
|
|
|
|
2020-05-18 23:03:42 +00:00
|
|
|
if !allocate.Has(fileType) {
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
|
|
done()
|
2020-09-06 16:54:00 +00:00
|
|
|
return storiface.SectorPaths{}, nil, storiface.ErrSectorNotFound
|
2020-05-18 23:03:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:54:00 +00:00
|
|
|
storiface.SetPathByType(&out, fileType, path)
|
2020-03-26 02:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 17:21:32 +00:00
|
|
|
return out, done, nil
|
2020-03-26 02:50:56 +00:00
|
|
|
}
|
2023-06-28 14:02:43 +00:00
|
|
|
|
|
|
|
func (b *Provider) AcquireSectorCopy(ctx context.Context, id storiface.SectorRef, existing storiface.SectorFileType, allocate storiface.SectorFileType, ptype storiface.PathType) (storiface.SectorPaths, func(), error) {
|
|
|
|
return b.AcquireSector(ctx, id, existing, allocate, ptype)
|
|
|
|
}
|