scrub.go 2.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
package sectorbuilder

import (
	"io/ioutil"
	"os"
	"path/filepath"

	sectorbuilder "github.com/filecoin-project/filecoin-ffi"
	"golang.org/x/xerrors"
Łukasz Magiera's avatar
Łukasz Magiera committed
10
11

	"github.com/filecoin-project/go-sectorbuilder/fs"
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
)

type Fault struct {
	SectorID uint64

	Err error
}

func (sb *SectorBuilder) Scrub(sectorSet sectorbuilder.SortedPublicSectorInfo) []*Fault {
	var faults []*Fault

	for _, sector := range sectorSet.Values() {
		err := sb.checkSector(sector.SectorID)
		if err != nil {
			faults = append(faults, &Fault{SectorID: sector.SectorID, Err: err})
		}
	}

	return faults
}

func (sb *SectorBuilder) checkSector(sectorID uint64) error {
Łukasz Magiera's avatar
Łukasz Magiera committed
34
	scache, err := sb.SectorPath(fs.DataCache, sectorID)
35
36
37
	if err != nil {
		return xerrors.Errorf("getting sector cache dir: %w", err)
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
38
	cache := string(scache)
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

	if err := assertFile(filepath.Join(cache, "p_aux"), 96, 96); err != nil {
		return err
	}
	if err := assertFile(filepath.Join(cache, "sc-01-data-tree-r-last.dat"), (2*sb.ssize)-32, (2*sb.ssize)-32); err != nil {
		return err
	}

	// TODO: better validate this
	if err := assertFile(filepath.Join(cache, "t_aux"), 100, 32000); err != nil { // TODO: what should this actually be?
		return err
	}

	dent, err := ioutil.ReadDir(cache)
	if err != nil {
		return xerrors.Errorf("reading cache dir %s", cache)
	}
	if len(dent) != 3 {
		return xerrors.Errorf("found %d files in %s, expected 3", len(dent), cache)
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
60
	sealed, err := sb.SectorPath(fs.DataSealed, sectorID)
61
	if err != nil {
Łukasz Magiera's avatar
Łukasz Magiera committed
62
		return xerrors.Errorf("getting sealed sector paths: %w", err)
63
64
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
65
	if err := assertFile(filepath.Join(string(sealed)), sb.ssize, sb.ssize); err != nil {
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
		return err
	}

	return nil
}

func assertFile(path string, minSz uint64, maxSz uint64) error {
	st, err := os.Stat(path)
	if err != nil {
		return xerrors.Errorf("stat %s: %w", path, err)
	}

	if st.IsDir() {
		return xerrors.Errorf("expected %s to be a regular file", path)
	}

	if uint64(st.Size()) < minSz || uint64(st.Size()) > maxSz {
		return xerrors.Errorf("%s wasn't within size bounds, expected %d < f < %d, got %d", minSz, maxSz, st.Size())
	}

	return nil
}