lotus/ffiwrapper/basicfs/fs.go

87 lines
1.9 KiB
Go
Raw Normal View History

2020-03-26 02:50:56 +00:00
package basicfs
import (
"context"
"os"
"path/filepath"
"sync"
"github.com/filecoin-project/specs-actors/actors/abi"
2020-03-27 23:21:36 +00:00
"github.com/filecoin-project/sector-storage/stores"
2020-05-18 23:03:42 +00:00
"github.com/filecoin-project/sector-storage/storiface"
2020-03-26 02:50:56 +00:00
)
type sectorFile struct {
abi.SectorID
stores.SectorFileType
}
type Provider struct {
Root string
lk sync.Mutex
waitSector map[sectorFile]chan struct{}
}
func (b *Provider) AcquireSector(ctx context.Context, id abi.SectorID, existing stores.SectorFileType, allocate stores.SectorFileType, sealing bool) (stores.SectorPaths, func(), error) {
2020-03-27 17:21:32 +00:00
if err := os.Mkdir(filepath.Join(b.Root, stores.FTUnsealed.String()), 0755); err != nil && !os.IsExist(err) {
return stores.SectorPaths{}, nil, err
}
if err := os.Mkdir(filepath.Join(b.Root, stores.FTSealed.String()), 0755); err != nil && !os.IsExist(err) {
return stores.SectorPaths{}, nil, err
}
if err := os.Mkdir(filepath.Join(b.Root, stores.FTCache.String()), 0755); err != nil && !os.IsExist(err) {
return stores.SectorPaths{}, nil, err
}
2020-03-26 02:50:56 +00:00
done := func() {}
2020-03-27 17:21:32 +00:00
out := stores.SectorPaths{
Id: id,
}
for _, fileType := range stores.PathTypes {
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()
return stores.SectorPaths{}, nil, ctx.Err()
}
2020-05-18 23:03:42 +00:00
path := filepath.Join(b.Root, fileType.String(), stores.SectorName(id))
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()
return stores.SectorPaths{}, nil, storiface.ErrSectorNotFound
}
}
stores.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
}