-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob.go
88 lines (80 loc) · 1.67 KB
/
blob.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
package Torrent
import (
"fmt"
"github.com/JRonak/Torrent/MetaData"
"os"
)
type localStruct struct {
data *[]byte
offset int
}
type BlobStorage struct {
channel chan localStruct
file *os.File
meta *MetaData.MetaInfo
}
//Returns status if file created
func (this *BlobStorage) Init(meta *MetaData.MetaInfo) bool {
this.meta = meta
this.channel = make(chan localStruct, 10)
go this.write()
f, err := os.OpenFile(this.meta.Info.Name, os.O_RDWR, os.ModeAppend)
if err == nil {
this.file = f
return false
} else {
f, err = os.Create(meta.Info.Name)
if err != nil {
fmt.Println("blob: ", err)
return false
}
this.file = f
this.fill()
return true
}
}
func (this *BlobStorage) fill() {
size := this.meta.Size()
max := MB
offset := 0
data := make([]byte, max)
for {
if size <= max {
break
}
_, err := this.file.WriteAt(data, int64(offset*max))
if err != nil {
fmt.Println("blob: ", err)
return
}
size -= max
offset += 1
}
if size > 0 {
_, err := this.file.WriteAt(data[:size], int64(offset*max))
if err != nil {
fmt.Println("blob:", err)
}
}
data = nil
}
func (this *BlobStorage) GetBlock(index, offset, size int) []byte {
data := make([]byte, size)
n, err := this.file.ReadAt(data, int64((index*this.meta.Info.PieceLength)+offset))
if err != nil {
fmt.Println(err)
return nil
}
return data[:n]
}
func (this *BlobStorage) write() {
for i := range this.channel {
_, err := this.file.WriteAt(*i.data, int64(this.meta.Info.PieceLength*i.offset))
if err != nil {
fmt.Println(err)
}
}
}
func (this *BlobStorage) PutBlock(data *[]byte, index int) {
this.channel <- localStruct{data: data, offset: index}
}