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
|
|
|
|
2020-08-17 13:26:18 +00:00
|
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/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{}
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:54:00 +00:00
|
|
|
func (b *Provider) AcquireSector(ctx context.Context, id abi.SectorID, existing storiface.SectorFileType, allocate storiface.SectorFileType, ptype storiface.PathType) (storiface.SectorPaths, func(), error) {
|
|
|
|
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
|
|
|
|
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-08-16 10:40:35 +00:00
|
|
|
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-03-27 17:21:32 +00:00
|
|
|
ch, found := b.waitSector[sectorFile{id, fileType}]
|
2020-03-26 02:50:56 +00:00
|
|
|
if !found {
|
|
|
|
ch = make(chan struct{}, 1)
|
2020-03-27 17:21:32 +00:00
|
|
|
b.waitSector[sectorFile{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-09-06 16:54:00 +00:00
|
|
|
path := filepath.Join(b.Root, fileType.String(), storiface.SectorName(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
|
|
|
}
|