-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmeta.go
312 lines (284 loc) · 8.06 KB
/
meta.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package main
import (
"fmt"
"io"
"math"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"lukechampine.com/us/merkle"
"lukechampine.com/us/renter"
"lukechampine.com/us/renter/renterutil"
"lukechampine.com/us/renterhost"
)
func metainfo(m *renter.MetaFile) {
var uploaded int64
for _, shard := range m.Shards {
for _, s := range shard {
uploaded += int64(s.NumSegments * merkle.SegmentSize)
}
}
redundancy := float64(len(m.Hosts)) / float64(m.MinShards)
pctFullRedundancy := 100 * float64(uploaded) / (float64(m.Filesize) * redundancy)
if m.Filesize == 0 || pctFullRedundancy > 100 {
pctFullRedundancy = 100
}
fmt.Printf(`Filesize: %v
Redundancy: %v-of-%v (%0.2gx replication)
Uploaded: %v (%0.2f%% of full redundancy)
`, filesizeUnits(m.Filesize), m.MinShards, len(m.Hosts), redundancy,
filesizeUnits(uploaded), pctFullRedundancy)
fmt.Println("Hosts:")
for _, hostKey := range m.Hosts {
fmt.Printf(" %v\n", hostKey)
}
}
// filesize returns a string that displays a filesize in human-readable units.
func filesizeUnits(size int64) string {
if size == 0 {
return "0 B"
}
sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
i := int(math.Log10(float64(size)) / 3)
// printf trick: * means "print to 'i' digits"
// so we get 1 decimal place for KB, 2 for MB, 3 for GB, etc.
return fmt.Sprintf("%.*f %s", i, float64(size)/math.Pow10(3*i), sizes[i])
}
func uploadmetafile(f *os.File, minShards int, hosts *renterutil.HostSet, metaPath string) error {
stat, err := f.Stat()
if err != nil {
return errors.Wrap(err, "could not stat file")
}
dir, name := filepath.Dir(metaPath), strings.TrimSuffix(filepath.Base(metaPath), ".usa")
fs := renterutil.NewFileSystem(dir, hosts)
defer fs.Close()
pf, err := fs.OpenFile(name, os.O_CREATE|os.O_TRUNC|os.O_WRONLY|os.O_APPEND, stat.Mode(), minShards)
if err != nil {
return err
}
defer pf.Close()
if err := trackUpload(pf, f, true); err != nil {
return err
}
return fs.Close()
}
func uploadmetadir(dir, metaDir string, hosts *renterutil.HostSet, minShards int) error {
fs := renterutil.NewFileSystem(metaDir, hosts)
defer fs.Close()
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
fsPath, _ := filepath.Rel(dir, path)
if info.IsDir() || err != nil {
return fs.MkdirAll(fsPath, 0700)
}
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
return err
}
pf, err := fs.OpenFile(fsPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY|os.O_APPEND, stat.Mode(), minShards)
if err != nil {
return err
}
defer pf.Close()
return trackUpload(pf, f, false)
})
if err != nil {
return err
}
return fs.Close()
}
func resumeuploadmetafile(f *os.File, hosts *renterutil.HostSet, metaPath string) error {
dir, name := filepath.Dir(metaPath), strings.TrimSuffix(filepath.Base(metaPath), ".usa")
fs := renterutil.NewFileSystem(dir, hosts)
defer fs.Close()
pf, err := fs.OpenFile(name, os.O_WRONLY|os.O_APPEND, 0, 0)
if err != nil {
return err
}
defer pf.Close()
stat, _ := pf.Stat()
if _, err := f.Seek(stat.Size(), io.SeekStart); err != nil {
return err
}
if err := trackUpload(pf, f, true); err != nil {
return err
}
return fs.Close()
}
func resumedownload(f *os.File, metaPath string, pf *renterutil.PseudoFile) error {
if ok, err := renter.MetaFileCanDownload(metaPath); err == nil && !ok {
return errors.New("file is not sufficiently uploaded")
}
// set file mode and size
stat, err := f.Stat()
if err != nil {
return errors.Wrap(err, "could not stat file")
}
pstat, err := pf.Stat()
if err != nil {
return err
}
if stat.Mode() != pstat.Mode() {
if err := f.Chmod(pstat.Mode()); err != nil {
return errors.Wrap(err, "could not set file mode")
}
}
if stat.Size() > pstat.Size() {
if err := f.Truncate(pstat.Size()); err != nil {
return errors.Wrap(err, "could not resize file")
}
}
// resume at end of file
offset := stat.Size()
if _, err := f.Seek(offset, io.SeekStart); err != nil {
return err
}
if _, err := pf.Seek(offset, io.SeekStart); err != nil {
return err
}
return trackDownload(f, pf, offset)
}
func downloadmetafile(f *os.File, hosts *renterutil.HostSet, metaPath string) error {
if ok, err := renter.MetaFileCanDownload(metaPath); err == nil && !ok {
return errors.New("file is not sufficiently uploaded")
}
dir, name := filepath.Dir(metaPath), strings.TrimSuffix(filepath.Base(metaPath), ".usa")
fs := renterutil.NewFileSystem(dir, hosts)
defer fs.Close()
pf, err := fs.Open(name)
if err != nil {
return err
}
defer pf.Close()
if err := resumedownload(f, metaPath, pf); err != nil {
return err
}
return fs.Close()
}
func downloadmetastream(w io.Writer, hosts *renterutil.HostSet, metaPath string) error {
if ok, err := renter.MetaFileCanDownload(metaPath); err == nil && !ok {
return errors.New("file is not sufficiently uploaded")
}
dir, name := filepath.Dir(metaPath), strings.TrimSuffix(filepath.Base(metaPath), ".usa")
fs := renterutil.NewFileSystem(dir, hosts)
defer fs.Close()
pf, err := fs.Open(name)
if err != nil {
return err
}
defer pf.Close()
stat, err := pf.Stat()
if err != nil {
return err
} else if stat.IsDir() {
return errors.New("is a directory")
}
index := stat.Sys().(renter.MetaIndex)
buf := make([]byte, renterhost.SectorSize*index.MinShards)
_, err = io.CopyBuffer(w, pf, buf)
if err != nil {
return err
}
return fs.Close()
}
func downloadmetadir(dir string, hosts *renterutil.HostSet, metaDir string) error {
fs := renterutil.NewFileSystem(metaDir, hosts)
defer fs.Close()
err := filepath.Walk(metaDir, func(metaPath string, info os.FileInfo, err error) error {
if info.IsDir() || err != nil {
return nil
}
name := strings.TrimSuffix(strings.TrimPrefix(metaPath, metaDir), ".usa")
pf, err := fs.Open(name)
if err != nil {
return err
}
defer pf.Close()
fpath := filepath.Join(dir, name)
os.MkdirAll(filepath.Dir(fpath), 0700)
f, err := os.OpenFile(fpath, os.O_RDWR|os.O_CREATE, info.Mode())
if err != nil {
return err
}
return resumedownload(f, metaPath, pf)
})
if err != nil {
return err
}
return fs.Close()
}
func migrateLocal(f *os.File, hosts *renterutil.HostSet, metaPath string) error {
defer hosts.Close()
migrator := renterutil.NewMigrator(hosts)
return trackMigrate(migrator, metaPath, f)
}
func migrateDirLocal(dir string, hosts *renterutil.HostSet, metaDir string) error {
defer hosts.Close()
migrator := renterutil.NewMigrator(hosts)
err := filepath.Walk(metaDir, func(metaPath string, info os.FileInfo, err error) error {
if err != nil {
return err
} else if info.IsDir() {
return nil
}
name, _ := filepath.Rel(metaDir, metaPath)
filePath := strings.TrimSuffix(filepath.Join(dir, name), metafileExt)
f, err := os.Open(filePath)
if err != nil {
return err
}
defer f.Close()
return trackMigrate(migrator, metaPath, f)
})
if err != nil {
return err
}
if err := migrator.Flush(); err != nil {
return err
}
return nil
}
func migrateRemote(hosts *renterutil.HostSet, metaPath string) error {
defer hosts.Close()
dir, name := filepath.Dir(metaPath), strings.TrimSuffix(filepath.Base(metaPath), ".usa")
fs := renterutil.NewFileSystem(dir, hosts)
defer fs.Close()
pf, err := fs.Open(name)
if err != nil {
return err
}
migrator := renterutil.NewMigrator(hosts)
return trackMigrate(migrator, metaPath, pf)
}
func migrateDirRemote(hosts *renterutil.HostSet, metaDir string) error {
defer hosts.Close()
fs := renterutil.NewFileSystem(metaDir, hosts)
defer fs.Close()
migrator := renterutil.NewMigrator(hosts)
err := filepath.Walk(metaDir, func(metaPath string, info os.FileInfo, err error) error {
if err != nil {
return err
} else if info.IsDir() {
return nil
}
fsPath, _ := filepath.Rel(metaDir, metaPath)
pf, err := fs.Open(strings.TrimSuffix(fsPath, ".usa"))
if err != nil {
return errors.Wrap(err, "could not open metafile for reading")
}
defer pf.Close()
return trackMigrate(migrator, metaPath, pf)
})
if err != nil {
return err
}
if err := migrator.Flush(); err != nil {
return err
}
return nil
}