This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
files.go
125 lines (102 loc) · 2.95 KB
/
files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
122
123
124
125
package sectorbuilder
import (
"context"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"github.com/filecoin-project/specs-actors/actors/abi"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-sectorbuilder/fs"
)
func (sb *SectorBuilder) SectorName(sectorNum abi.SectorNumber) string {
return fs.SectorName(sb.Miner, sectorNum)
}
func (sb *SectorBuilder) SectorPath(typ fs.DataType, sectorNum abi.SectorNumber) (fs.SectorPath, error) {
return sb.filesystem.FindSector(typ, sb.Miner, sectorNum)
}
func (sb *SectorBuilder) AllocSectorPath(typ fs.DataType, sectorNum abi.SectorNumber, cache bool) (fs.SectorPath, error) {
return sb.filesystem.AllocSector(typ, sb.Miner, sb.ssize, cache, sectorNum)
}
func (sb *SectorBuilder) ReleaseSector(typ fs.DataType, path fs.SectorPath) {
sb.filesystem.Release(path, sb.ssize)
}
func (sb *SectorBuilder) TrimCache(ctx context.Context, sectorNum abi.SectorNumber) error {
dir, err := sb.filesystem.FindSector(fs.DataCache, sb.Miner, sectorNum)
if err != nil {
return xerrors.Errorf("getting cache dir: %w", err)
}
if err := sb.filesystem.Lock(ctx, dir); err != nil {
return xerrors.Errorf("acquiring sector lock: %w", err)
}
defer sb.filesystem.Unlock(dir)
files, err := ioutil.ReadDir(string(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 strings.HasSuffix(file.Name(), "-data-tree-d.dat") { // Want to keep
continue
}
if err := os.Remove(filepath.Join(string(dir), file.Name())); err != nil {
return xerrors.Errorf("rm %s: %w", file.Name(), err)
}
}
return nil
}
func (sb *SectorBuilder) CanCommit(sectorNum abi.SectorNumber) (bool, error) {
dir, err := sb.SectorPath(fs.DataCache, sectorNum)
if err != nil {
return false, xerrors.Errorf("getting cache dir: %w", err)
}
ents, err := ioutil.ReadDir(string(dir))
if err != nil {
return false, err
}
// TODO: slightly more sophisticated check
return len(ents) == 10, nil
}
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
}