-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
executable file
·146 lines (139 loc) · 3.79 KB
/
file.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
package daobackup
import (
"crypto/sha256"
"fmt"
"io"
"os"
"path"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
type ErrorBytes struct {
Error error
Bytes []byte
}
func ChunkFile(path string) func(func(ChunkHash, ErrorBytes) bool) {
// Setup
filehandle, err := os.Open(path)
if err != nil {
return func(yield func(ChunkHash, ErrorBytes) bool) {
yield(ChunkHash{}, ErrorBytes{err, nil})
}
}
// Fill in the metadata
filemeta := FileMeta{}
// Fill in parts of the metadata
stat, err := filehandle.Stat()
if err != nil {
return func(yield func(ChunkHash, ErrorBytes) bool) {
yield(ChunkHash{}, ErrorBytes{err, nil})
filehandle.Close()
}
}
if stat.IsDir() {
return func(yield func(ChunkHash, ErrorBytes) bool) {
yield(ChunkHash{}, ErrorBytes{fmt.Errorf("ChunkFile can't chunk a directory"), nil})
}
}
filemeta.Type = FileType_NormalFile
filemeta.Name = stat.Name()
filemeta.Size = uint64(stat.Size())
filemeta.ModificationTime = timestamppb.New(stat.ModTime())
filemeta.Mode = uint32(stat.Mode())
hasher := sha256.New()
chunkbuffer := make([]byte, MaxChunkSize)
chunker := NewChunker(filehandle)
return func(yield func(ChunkHash, ErrorBytes) bool) {
for {
chunk, err := chunker.Next(chunkbuffer)
if err == io.EOF { // No more data chunks, return FileMeta chunk
filemeta.Hash = ChunkHash(hasher.Sum([]byte{})).Bytes()
bytesbuf, err := proto.Marshal(&filemeta)
if err != nil {
yield(ChunkHash{}, ErrorBytes{err, nil})
filehandle.Close()
return
}
yield(HashChunk(bytesbuf), ErrorBytes{nil, bytesbuf})
filehandle.Close()
return
}
if err != nil {
yield(ChunkHash{}, ErrorBytes{err, nil})
filehandle.Close()
return
}
chunkhash := HashChunk(chunk.Data)
filemeta.Chunks = append(filemeta.Chunks, &ChunkMeta{Offset: uint64(chunk.Start), Size: uint64(chunk.Length), Hash: chunkhash.Bytes()})
// update total file hash
hasher.Write(chunk.Data)
if !yield(chunkhash, ErrorBytes{nil, chunk.Data}) {
filehandle.Close()
return
}
}
}
}
func ChunkDir(dirpath string) func(func(ChunkHash, ErrorBytes) bool) {
// Setup
filehandle, err := os.Open(dirpath)
if err != nil {
return func(yield func(ChunkHash, ErrorBytes) bool) {
yield(ChunkHash{}, ErrorBytes{err, nil})
}
}
defer filehandle.Close()
// Fill in the metadata
filemeta := FileMeta{}
// Fill in parts of the metadata
stat, err := filehandle.Stat()
if err != nil {
return func(yield func(ChunkHash, ErrorBytes) bool) {
yield(ChunkHash{}, ErrorBytes{err, nil})
}
}
if !stat.IsDir() {
return func(yield func(ChunkHash, ErrorBytes) bool) {
yield(ChunkHash{}, ErrorBytes{fmt.Errorf("ChunkDir can't chunk a file"), nil})
}
}
filemeta.Type = FileType_Directory
filemeta.Name = stat.Name()
filemeta.Size = uint64(stat.Size())
filemeta.ModificationTime = timestamppb.New(stat.ModTime())
filemeta.Mode = uint32(stat.Mode())
filemeta.DirectoryEntries = map[string][]byte{}
return func(yield func(ChunkHash, ErrorBytes) bool) {
files, err := os.ReadDir(dirpath)
if err != nil {
yield(ChunkHash{}, ErrorBytes{err, nil})
return
}
for _, file := range files {
fullpath := path.Join(dirpath, file.Name())
hash := ChunkHash{}
blob := ErrorBytes{}
if file.IsDir() {
for hash, blob = range ChunkDir(fullpath) {
if !yield(hash, blob) {
break
}
}
filemeta.DirectoryEntries[file.Name()] = hash.Bytes()
} else {
for hash, blob := range ChunkFile(fullpath) {
if !yield(hash, blob) {
break
}
}
filemeta.DirectoryEntries[file.Name()] = hash.Bytes()
}
}
bytesbuf, err := proto.Marshal(&filemeta)
if err != nil {
yield(ChunkHash{}, ErrorBytes{err, nil})
return
}
yield(HashChunk(bytesbuf), ErrorBytes{nil, bytesbuf})
}
}