Repository navigation
Expand file tree
/
Copy pathlib.go
More file actions
185 lines (171 loc) · 4.82 KB
/
Copy pathlib.go
File metadata and controls
185 lines (171 loc) · 4.82 KB
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
package lib
import (
"context"
"errors"
"reflect"
"time"
"cloud.google.com/go/storage"
"google.golang.org/api/iterator"
)
func ListObjectsAtRestoreTime(ctx *context.Context, bucket *storage.BucketHandle, restoreTime time.Time) (map[string]*storage.ObjectAttrs, error) {
query := &storage.Query{Prefix: "", Versions: true}
it := bucket.Objects(*ctx, query)
objects := map[string]*storage.ObjectAttrs{}
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return map[string]*storage.ObjectAttrs{}, err
}
if (attrs.Updated.Before(restoreTime) || attrs.Updated.Equal(restoreTime)) &&
(restoreTime.Before(attrs.Deleted) || attrs.Deleted.IsZero()) {
objects[attrs.Name] = attrs
}
}
return objects, nil
}
func ListCurrentObjects(ctx *context.Context, bucket *storage.BucketHandle) (map[string]*storage.ObjectAttrs, error) {
query := &storage.Query{Prefix: ""}
it := bucket.Objects(*ctx, query)
objects := map[string]*storage.ObjectAttrs{}
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return map[string]*storage.ObjectAttrs{}, err
}
objects[attrs.Name] = attrs
}
return objects, nil
}
func PlanRestore(ctx *context.Context, bucket *storage.BucketHandle, restoreTime time.Time) (map[string]PlanElement, error) {
restoreObjects, err := ListObjectsAtRestoreTime(ctx, bucket, restoreTime)
if err != nil {
return map[string]PlanElement{}, err
}
currentObjects, err := ListCurrentObjects(ctx, bucket)
if err != nil {
return map[string]PlanElement{}, err
}
planElements := map[string]PlanElement{}
for name, restoreAttrs := range restoreObjects {
currentAttrs, ok := currentObjects[name]
if ok {
// check if the content and metadata are the same
if restoreAttrs.CRC32C != currentAttrs.CRC32C {
planElements[name] = PlanElement{RestoreObject, restoreAttrs, currentAttrs}
} else if !FullMetadataEqual(restoreAttrs, currentAttrs) {
planElements[name] = PlanElement{RestoreMetadata, restoreAttrs, currentAttrs}
}
} else {
planElements[name] = PlanElement{RestoreObject, restoreAttrs, nil}
}
}
for name, currentAttrs := range currentObjects {
if _, ok := restoreObjects[name]; !ok {
planElements[name] = PlanElement{Delete, nil, currentAttrs}
}
}
return planElements, nil
}
var timeFormats = []string{
"2006-01-02 15:04:05.999999999 -0700 MST",
"2006-01-02",
time.ANSIC,
time.UnixDate,
time.RubyDate,
time.RFC822,
time.RFC822Z,
time.RFC850,
time.RFC1123,
time.RFC1123Z,
time.RFC3339,
time.RFC3339Nano,
time.Kitchen,
time.Stamp,
time.StampMilli,
time.StampMicro,
time.StampNano,
}
func ParseTime(timeStr string) (time.Time, error) {
for _, timeFormat := range timeFormats {
t, err := time.Parse(timeFormat, timeStr)
if err == nil {
return t, nil
}
}
return time.Time{}, errors.New("No matching format found")
}
type Action int
const (
RestoreObject Action = iota
RestoreMetadata
Delete
)
type PlanElement struct {
Action Action
RestoreAttrs *storage.ObjectAttrs
CurrentAttrs *storage.ObjectAttrs
}
//go:generate stringer -type=Action
func FullMetadataEqual(attrs1 *storage.ObjectAttrs, attrs2 *storage.ObjectAttrs) bool {
if !reflect.DeepEqual(attrs1.ACL, attrs2.ACL) {
return false
}
if attrs1.CacheControl != attrs2.CacheControl {
return false
}
if attrs1.ContentDisposition != attrs2.ContentDisposition {
return false
}
if attrs1.ContentEncoding != attrs2.ContentEncoding {
return false
}
if attrs1.ContentLanguage != attrs2.ContentLanguage {
return false
}
if attrs1.ContentType != attrs2.ContentType {
return false
}
if attrs1.CustomTime != attrs2.CustomTime {
return false
}
if !reflect.DeepEqual(attrs1.Metadata, attrs2.Metadata) {
return false
}
return true
}
func ApplyPlan(ctx *context.Context, bucket *storage.BucketHandle, planElements map[string]PlanElement) error {
for name, planElement := range planElements {
dst := bucket.Object(name)
switch planElement.Action {
case RestoreObject:
src := bucket.Object(name).Generation(planElement.RestoreAttrs.Generation)
if _, err := dst.CopierFrom(src).Run(*ctx); err != nil {
return err
}
case RestoreMetadata:
if _, err := dst.Update(*ctx, storage.ObjectAttrsToUpdate{
ACL: planElement.RestoreAttrs.ACL,
CacheControl: planElement.RestoreAttrs.CacheControl,
ContentDisposition: planElement.RestoreAttrs.ContentDisposition,
ContentEncoding: planElement.RestoreAttrs.ContentEncoding,
ContentLanguage: planElement.RestoreAttrs.ContentLanguage,
ContentType: planElement.RestoreAttrs.ContentType,
CustomTime: planElement.RestoreAttrs.CustomTime,
Metadata: planElement.RestoreAttrs.Metadata,
}); err != nil {
return err
}
case Delete:
if err := dst.Delete(*ctx); err != nil {
return err
}
}
}
return nil
}