lotus/lib/sectorbuilder/files.go

127 lines
2.8 KiB
Go
Raw Normal View History

2019-11-07 16:39:27 +00:00
package sectorbuilder
import (
"fmt"
"io"
2019-12-06 21:15:06 +00:00
"io/ioutil"
2019-11-07 16:39:27 +00:00
"os"
"path/filepath"
2019-12-06 21:15:06 +00:00
"strings"
2019-11-07 16:39:27 +00:00
"sync"
"golang.org/x/xerrors"
)
2019-11-21 00:52:59 +00:00
func (sb *SectorBuilder) SectorName(sectorID uint64) string {
2019-11-07 16:39:27 +00:00
return fmt.Sprintf("s-%s-%d", sb.Miner, sectorID)
}
2019-11-21 00:52:59 +00:00
func (sb *SectorBuilder) StagedSectorPath(sectorID uint64) string {
2019-12-16 18:49:32 +00:00
return filepath.Join(sb.filesystem.pathFor(dataStaging), sb.SectorName(sectorID))
2019-11-07 16:39:27 +00:00
}
2019-12-01 17:58:31 +00:00
func (sb *SectorBuilder) unsealedSectorPath(sectorID uint64) string {
2019-12-16 18:49:32 +00:00
return filepath.Join(sb.filesystem.pathFor(dataUnsealed), sb.SectorName(sectorID))
2019-12-01 17:58:31 +00:00
}
2019-11-07 16:39:27 +00:00
func (sb *SectorBuilder) stagedSectorFile(sectorID uint64) (*os.File, error) {
2019-11-21 00:52:59 +00:00
return os.OpenFile(sb.StagedSectorPath(sectorID), os.O_RDWR|os.O_CREATE, 0644)
2019-11-07 16:39:27 +00:00
}
2019-11-21 00:52:59 +00:00
func (sb *SectorBuilder) SealedSectorPath(sectorID uint64) (string, error) {
2019-12-16 18:49:32 +00:00
path := filepath.Join(sb.filesystem.pathFor(dataSealed), sb.SectorName(sectorID))
2019-11-07 16:39:27 +00:00
return path, nil
2019-11-07 16:39:27 +00:00
}
func (sb *SectorBuilder) sectorCacheDir(sectorID uint64) (string, error) {
2019-12-16 18:49:32 +00:00
dir := filepath.Join(sb.filesystem.pathFor(dataCache), sb.SectorName(sectorID))
2019-11-07 16:39:27 +00:00
err := os.Mkdir(dir, 0755)
if os.IsExist(err) {
err = nil
}
return dir, err
}
2019-11-30 13:22:50 +00:00
func (sb *SectorBuilder) GetPath(typ string, sectorName string) (string, error) {
2019-12-16 18:49:32 +00:00
_, found := overheadMul[dataType(typ)]
if !found {
return "", xerrors.Errorf("unknown sector type: %s", typ)
}
2019-12-16 18:49:32 +00:00
return sb.filesystem.pathFor(dataType(typ)), nil
}
2019-12-06 21:15:06 +00:00
func (sb *SectorBuilder) TrimCache(sectorID uint64) error {
dir, err := sb.sectorCacheDir(sectorID)
if err != nil {
return xerrors.Errorf("getting cache dir: %w", err)
}
files, err := ioutil.ReadDir(dir)
if err != nil {
return xerrors.Errorf("readdir: %w", err)
}
for _, file := range files {
if !strings.HasSuffix(file.Name(), ".dat") { // _aux probably
continue
}
if strings.HasSuffix(file.Name(), "-data-tree-r-last.dat") { // Want to keep
continue
}
if err := os.Remove(filepath.Join(dir, file.Name())); err != nil {
return xerrors.Errorf("rm %s: %w", file.Name(), err)
}
}
return nil
}
2019-11-07 16:39:27 +00:00
func toReadableFile(r io.Reader, n int64) (*os.File, func() error, error) {
f, ok := r.(*os.File)
if ok {
return f, func() error { return nil }, nil
}
var w *os.File
f, w, err := os.Pipe()
if err != nil {
return nil, nil, err
}
var wait sync.Mutex
var werr error
wait.Lock()
go func() {
defer wait.Unlock()
2019-12-05 05:14:19 +00:00
var copied int64
copied, werr = io.CopyN(w, r, n)
2019-11-07 16:39:27 +00:00
if werr != nil {
log.Warnf("toReadableFile: copy error: %+v", werr)
}
err := w.Close()
if werr == nil && err != nil {
werr = err
log.Warnf("toReadableFile: close error: %+v", err)
return
}
if copied != n {
log.Warnf("copied different amount than expected: %d != %d", copied, n)
werr = xerrors.Errorf("copied different amount than expected: %d != %d", copied, n)
}
}()
return f, func() error {
wait.Lock()
return werr
}, nil
}