-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfileWriter.go
70 lines (56 loc) · 1.41 KB
/
fileWriter.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
package main
import (
"os"
"path/filepath"
"sync"
"github.com/chrisfarms/yenc"
)
type FileChannels struct {
channels map[string]chan *yenc.Part
}
type FileWriters struct {
sync.Mutex
writers map[string]bool
}
func (fileWriter *FileWriters) runOnce(name string) {
fileWriter.Lock()
defer fileWriter.Unlock()
if _, ok := fileWriter.writers[name]; ok {
return
} else {
fileChannels.channels[name] = make(chan *yenc.Part, conf.Connections*2)
fileWriterWG.Add(1)
go writeFile(fileChannels.channels[name], name, &fileWriterWG)
fileWriter.writers[name] = true
}
}
var (
fileChannels FileChannels
fileWriters FileWriters
writtenBytes int
)
func writeFile(parts <-chan *yenc.Part, name string, wg *sync.WaitGroup) {
Log.Debug("Start writing file \"%v\"", name)
defer wg.Done()
var (
destFile *os.File
err error
)
if destFile, err = os.OpenFile(filepath.Join(conf.TempPath, name), os.O_CREATE|os.O_WRONLY, 0644); err != nil {
Log.Error("WRITER: Unable to create file \"%v\": %v", conf.TempPath, err)
exit(1)
}
defer destFile.Close()
for {
part, ok := <-parts
if !ok {
return
}
if writtenBytes, err = destFile.WriteAt(part.Body, part.Begin-1); err != nil {
Log.Warn("Unable to write bytes %v to %v to destination file \"%v\": %v", part.Begin-1, part.Begin-1+int64(len(part.Body)), part.Name, err)
}
if conf.Verbose > 0 {
downloadProgressBar.Add(writtenBytes)
}
}
}