files.go 2.75 KB
Newer Older
1
2
3
package sectorbuilder

import (
Łukasz Magiera's avatar
Łukasz Magiera committed
4
	"context"
5
6
7
8
9
10
11
12
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"sync"

	"golang.org/x/xerrors"
Łukasz Magiera's avatar
Łukasz Magiera committed
13
14

	"github.com/filecoin-project/go-sectorbuilder/fs"
15
16
17
)

func (sb *SectorBuilder) SectorName(sectorID uint64) string {
Łukasz Magiera's avatar
Łukasz Magiera committed
18
	return fs.SectorName(sb.Miner, sectorID)
19
20
}

Łukasz Magiera's avatar
Łukasz Magiera committed
21
22
func (sb *SectorBuilder) SectorPath(typ fs.DataType, sectorID uint64) (fs.SectorPath, error) {
	return sb.filesystem.FindSector(typ, sb.Miner, sectorID)
23
24
}

Łukasz Magiera's avatar
Łukasz Magiera committed
25
26
func (sb *SectorBuilder) AllocSectorPath(typ fs.DataType, sectorID uint64, cache bool) (fs.SectorPath, error) {
	return sb.filesystem.AllocSector(typ, sb.Miner, sb.ssize, cache, sectorID)
27
28
}

Łukasz Magiera's avatar
Łukasz Magiera committed
29
func (sb *SectorBuilder) ReleaseSector(typ fs.DataType, path fs.SectorPath) {
Łukasz Magiera's avatar
Łukasz Magiera committed
30
	sb.filesystem.Release(path, sb.ssize)
31
32
}

Łukasz Magiera's avatar
Łukasz Magiera committed
33
34
func (sb *SectorBuilder) TrimCache(ctx context.Context, sectorID uint64) error {
	dir, err := sb.filesystem.FindSector(fs.DataCache, sb.Miner, sectorID)
35
36
37
	if err != nil {
		return xerrors.Errorf("getting cache dir: %w", err)
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
38
39
40
41
	if err := sb.filesystem.Lock(ctx, dir); err != nil {
		return xerrors.Errorf("acquiring sector lock: %w", err)
	}
	defer sb.filesystem.Unlock(dir)
42

Łukasz Magiera's avatar
Łukasz Magiera committed
43
	files, err := ioutil.ReadDir(string(dir))
44
45
46
47
48
49
50
51
52
53
54
55
	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
		}

Łukasz Magiera's avatar
Łukasz Magiera committed
56
		if err := os.Remove(filepath.Join(string(dir), file.Name())); err != nil {
57
58
59
60
61
62
63
			return xerrors.Errorf("rm %s: %w", file.Name(), err)
		}
	}

	return nil
}

Łukasz Magiera's avatar
Łukasz Magiera committed
64
func (sb *SectorBuilder) CanCommit(sectorID uint64) (bool, error) {
Łukasz Magiera's avatar
Łukasz Magiera committed
65
	dir, err := sb.SectorPath(fs.DataCache, sectorID)
Łukasz Magiera's avatar
Łukasz Magiera committed
66
67
68
69
	if err != nil {
		return false, xerrors.Errorf("getting cache dir: %w", err)
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
70
	ents, err := ioutil.ReadDir(string(dir))
Łukasz Magiera's avatar
Łukasz Magiera committed
71
72
73
74
75
76
77
78
	if err != nil {
		return false, err
	}

	// TODO: slightly more sophisticated check
	return len(ents) == 10, nil
}

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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()

		var copied int64
		copied, werr = io.CopyN(w, r, n)
		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
}