lotus/extern/sector-storage/ffiwrapper/basicfs/fs.go

93 lines
2.4 KiB
Go
Raw Normal View History

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"
"github.com/filecoin-project/specs-storage/storage"
2020-03-26 02:50:56 +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{}
}
func (b *Provider) AcquireSector(ctx context.Context, id storage.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
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{
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{}{}
}
ch, found := b.waitSector[sectorFile{id.ID, fileType}]
2020-03-26 02:50:56 +00:00
if !found {
ch = make(chan struct{}, 1)
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
}
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
}