/
bucket.go
208 lines (188 loc) · 4.35 KB
/
bucket.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
package cfs
import (
"crypto/md5"
"fmt"
"io/ioutil"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
)
// ExcludePatterns は除外するファイルのパターンを表す
var ExcludePatterns = []string{".*", "*.vdat", "cfs", "*.meta", "*.tmx"}
// Verbose 詳細なログを表示するかどうか
var Verbose = false
// Bucket バケット
type Bucket struct {
Tag string
Path string
Hash string
ExcludeList []string
Contents map[string]Content
HashType string
UploadCount int
}
// ContentAttribute はコンテンツの圧縮や暗号の状態を表すenum
type ContentAttribute int
const (
// NoContentAttribute は圧縮も暗号化もしないことを示す
NoContentAttribute ContentAttribute = 0
// Compressed は圧縮をするかどうかを示す
Compressed = 1
// Crypted は暗号化をするかどうかを示す
Crypted = 2
)
// Content は一つのファイルの内容を表すstruct
type Content struct {
Path string
Hash string
Time time.Time
Size int
OrigHash string
OrigSize int
Attr ContentAttribute
Touched bool
}
// DefaultContentAttribute デフォルトのContentAttributeを取得する
func DefaultContentAttribute() ContentAttribute {
var result ContentAttribute
if Option.Compress {
result |= Compressed
}
if Option.EncryptKey != "" {
result |= Crypted
}
return ContentAttribute(result)
}
// Compressed 圧縮するかどうかを返す
func (c ContentAttribute) Compressed() bool {
return (int(c) & Compressed) != 0
}
func (c ContentAttribute) Crypted() bool {
return (int(c) & Crypted) != 0
}
func NewBucket() *Bucket {
return &Bucket{
Contents: make(map[string]Content),
HashType: "md5",
}
}
func BucketFromFile(path string) (*Bucket, error) {
b := &Bucket{
Path: path,
Contents: make(map[string]Content),
HashType: "md5",
}
data, err := ioutil.ReadFile(filepath.FromSlash(path))
if err == nil {
if Verbose {
fmt.Printf("read bucket from '%s'\n", b.Path)
}
err := b.Parse(data)
if err != nil {
return nil, err
}
}
return b, nil
}
func (b *Bucket) Parse(s []byte) error {
for _, line := range strings.Split(string(s), "\n") {
if len(s) != 0 {
col := strings.Split(line, "\t")
if len(col) >= 3 {
size, err := strconv.Atoi(col[2])
if err != nil {
return err
}
time, err := time.Parse(time.RFC3339, col[3])
if err != nil {
return err
}
origSize, err := strconv.Atoi(col[5])
if err != nil {
return err
}
attr, err := strconv.Atoi(col[6])
if err != nil {
return err
}
b.Contents[col[1]] = Content{
Hash: col[0],
Path: col[1],
Size: size,
Time: time,
OrigHash: col[4],
OrigSize: origSize,
Attr: ContentAttribute(attr),
}
}
}
}
return nil
}
// Merge bにfromの内容をマージする
// 両方に存在する場合は、fromの内容で上書きされる
func (b *Bucket) Merge(from *Bucket) {
for _, c := range from.Contents {
b.Contents[c.Path] = c
}
}
func (b *Bucket) Sum(data []byte) string {
switch b.HashType {
case "sha1":
panic("not supported")
//return fmt.Sprintf("%x", sha1.Sum(data))
case "md5":
return fmt.Sprintf("%x", md5.Sum(data))
default:
panic("invalid hash type")
}
}
func (b *Bucket) RemoveUntouched() {
newContents := make(map[string]Content)
for _, c := range b.Contents {
if c.Touched {
newContents[c.Path] = c
} else {
if Verbose {
fmt.Printf("removed: %s\n", c.Path)
}
}
}
b.Contents = newContents
}
// Dump は、Bucketの内容をダンプする
func (b *Bucket) Dump() string {
keys := []string{}
for k := range b.Contents {
keys = append(keys, k)
}
sort.Strings(keys)
r := make([]string, 0)
for _, k := range keys {
c := b.Contents[k]
r = append(
r,
strings.Join(
[]string{
c.Hash,
c.Path,
strconv.Itoa(c.Size),
c.Time.Format(time.RFC3339),
c.OrigHash,
strconv.Itoa(c.OrigSize),
strconv.Itoa(int(c.Attr)),
},
"\t"))
}
return strings.Join(r, "\n") + "\n"
}
func (b *Bucket) GetAttribute(path string) ContentAttribute {
var attr = DefaultContentAttribute()
// TODO: とりあえずフィルタを固定している
if filepath.Ext(path) == ".ab" || filepath.Ext(path) == ".raw" || filepath.Ext(path) == ".pbx" || filepath.Ext(path) == ".mp4" {
attr = ContentAttribute(0)
}
return attr
}