-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage.go
49 lines (40 loc) · 1.09 KB
/
storage.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
package torrent
import "os"
var fileWriterChannel chan *FileWriterMessage
type FileWriterMessage struct {
filename string
offset int64
data []byte
torrent *Torrent
}
func resendFileWriterMessage(message *FileWriterMessage) {
fileWriterChannel <- message
}
func fileWriterListener() {
for {
message := <-fileWriterChannel
file, err := os.OpenFile(message.filename, os.O_WRONLY, 0600)
if err != nil {
if os.IsNotExist(err) {
if file, err = os.Create(message.filename); err != nil {
//log.Printf("Failed to create file %s: %s", message.filename, err)
go resendFileWriterMessage(message)
continue
}
} else {
//log.Printf("Failed to open file %s: %s", message.filename, err)
go resendFileWriterMessage(message)
continue
}
}
if _, err := file.WriteAt(message.data, message.offset); err != nil {
//log.Printf("Failed to write to file %s: %s", message.filename, err)
go resendFileWriterMessage(message)
continue
}
file.Close()
go func(fileWriteDone chan struct{}) {
fileWriteDone <- struct{}{}
}(message.torrent.fileWriteDone)
}
}