-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsnapshot.go
359 lines (290 loc) · 8.46 KB
/
snapshot.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Copyright 2016 Frank Wessels <fwessels@xs4all.nl>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package s3git
import (
"os"
"fmt"
"path/filepath"
"github.com/s3git/s3git-go/internal/kv"
"github.com/s3git/s3git-go/internal/cas"
"github.com/s3git/s3git-go/internal/core"
"github.com/s3git/s3git-go/internal/backend"
"github.com/s3git/s3git-go/internal/backend/s3"
"io"
"errors"
"encoding/hex"
"sync"
)
// Create a snapshot for the repository
func (repo Repository) SnapshotCreate(path, message string) (hash string, empty bool, err error) {
// Create snapshot
snapshot, err := core.StoreSnapshotObject(path, func(filename string) (string, error) {
// Test for deduped storage of file, then we can safely skip it
deduped, key, _, err := cas.CheckLevel1HashFollowedByLeafHashes(filename)
if err != nil {
return "", err
}
if deduped { // It is stored in deduped format, so return key immediately
return key, nil
}
// Otherwise compute the hash based on the contents of the file
file, err := os.Open(filename)
if err != nil {
return "", err
}
defer file.Close()
key, _, err = repo.Add(file)
return key, err
})
if err != nil {
return "", false, err
}
// TODO: Make sure we commit a new object, even when no new blobs added (ListStage is empty)
return repo.commit(message, "master", snapshot, []string{})
}
// Checkout a snapshot for the repository
func (repo Repository) SnapshotCheckout(path, commit string, dedupe bool) error {
snapshot, err := getSnapshotFromCommit(commit)
if err != nil {
return err
}
// TODO: Check that status is clean (create 'stashing' like behaviour for temp changes?)
// For dedupe is false --> store full contents
fWriteHydrate := func(hash, filename string, mode os.FileMode) {
// Compute hash in order to prevent rewriting the content when file already exists
// TODO: Skip for now as slows down checkout (leaking go routine??)
if _, err := os.Stat(filename); false && err == nil {
digest, err := cas.Sum(filename)
if err != nil {
return
}
if digest == hash { // Contents unchanged --> exit out early
return
}
}
r, err := repo.Get(hash)
if err != nil {
return
}
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
if err != nil {
return
}
io.Copy(f, r)
}
// For dedupe is true --> store all leaf hashes as contents followed by final 64 bytes with level 1 hash
fWriteDeduped := func(hash, filename string, mode os.FileMode) {
// TODO: Can we prevent (re)writing the content (run a check when file exists)?
cas.WriteLevel1HashFollowedByLeafHashes(hash, filename, mode)
}
var fWrite func(hash, filename string, perm os.FileMode)
if dedupe {
fWrite = fWriteDeduped
} else {
fWrite = fWriteHydrate
}
return core.SnapshotCheckout(path, snapshot, fWrite)
}
type snapshotListOptions struct {
showHash bool
presignedUrls bool
jsonOutput bool
}
func SnapshotListOptionSetShowHash(showHash bool) func(optns *snapshotListOptions) {
return func(optns *snapshotListOptions) {
optns.showHash = showHash
}
}
func SnapshotListOptionSetPresignedUrls(presignedUrls bool) func(optns *snapshotListOptions) {
return func(optns *snapshotListOptions) {
optns.presignedUrls = presignedUrls
}
}
func SnapshotListOptionSetJsonOutput(jsonOutput bool) func(optns *snapshotListOptions) {
return func(optns *snapshotListOptions) {
optns.jsonOutput = jsonOutput
}
}
type SnapshotListOptions func(*snapshotListOptions)
// List a snapshot for the repository
func (repo Repository) SnapshotList(commit string, options ...SnapshotListOptions) error {
optns := &snapshotListOptions{}
for _, op := range options {
op(optns)
}
snapshot, err := getSnapshotFromCommit(commit)
if err != nil {
return err
}
funcPresignedUrl := func(hash string) (string, error) { return "", nil }
if optns.presignedUrls {
client, err := backend.GetDefaultClient()
if err != nil {
return err
}
// Look for S3 back end
s3, ok := client.(*s3.Client)
if ok {
// And if so generate function to generate presigned url
funcPresignedUrl = func(hash string) (string, error) {
return s3.GetPresignedUrl(hash)
}
}
}
// List snapshot
err = core.SnapshotList(snapshot, func(entry core.SnapshotEntry, base string) {
// TODO: Dump result in JSON format if requested
url, _ := funcPresignedUrl(entry.Blob)
if url != "" {
fmt.Printf("%s --> %s\n", filepath.Join(base, entry.Name), url)
} else if optns.showHash {
fmt.Println(filepath.Join(base, entry.Name), entry.Blob)
} else {
fmt.Println(filepath.Join(base, entry.Name))
}
}, func(base string) {}, func(base string, entries []core.SnapshotEntry) {})
return err
}
// Show status for a snapshot of the repository
func (repo Repository) SnapshotStatus(path, commit string) error {
snapshot, err := getSnapshotFromCommit(commit)
if err != nil {
return err
}
// Get status of snapshot
err = core.SnapshotStatus(path, snapshot)
return err
}
func getSnapshotFromCommit(commit string) (string, error) {
if commit == "" || commit == "HEAD" || commit == "HEAD^" { // Unspecified, so default to last commit
commits, err := kv.ListTopMostCommits()
if err != nil {
return "", err
}
parents := []string{}
for c := range commits {
parents = append(parents, hex.EncodeToString(c))
}
if len(parents) == 1 {
if commit == "HEAD^" {
// TODO: Refactor and make generic
co, err := core.GetCommitObject(parents[0])
if err != nil {
return "", err
}
if len(co.S3gitWarmParents) > 1 {
return "", errors.New("More than one grand parent found for HEAD^")
}
commit = co.S3gitWarmParents[0]
} else {
commit = parents[0]
}
} else {
// TODO: Do extra check whether the trees are the same, in that case we can safely ignore the warning
return "", errors.New("Multiple top most commits founds as parents")
}
} else {
result := make(chan string)
go func() {
// make sure we always close the channel
defer close(result)
keys, err := kv.ListLevel1Commits(commit)
if err != nil {
return
}
for key := range keys {
result <- hex.EncodeToString(key)
}
}()
var err error
commit, err = getUnique(result)
if err != nil {
return "", err
}
}
co, err := core.GetCommitObject(commit)
if err != nil {
return "", err
}
if co.S3gitSnapshot == "" {
return "", errors.New(fmt.Sprintf("Commit %s does not contain snapshot", commit))
}
b, _ := hex.DecodeString(co.S3gitSnapshot)
leafHashes, _, err := kv.GetLevel1(b)
if err != nil {
return "", err
}
// Has snapshot not yet been pulled down to disk?
if len(leafHashes) == 0 {
client, err := backend.GetDefaultClient()
if err != nil {
return "", err
}
err = pullSnapshotWithChildren(co.S3gitSnapshot, client)
if err != nil {
return "", err
}
}
return co.S3gitSnapshot, nil
}
func pullSnapshotWithChildren(hash string, client backend.Backend) error {
const pullSnapshotRoutines = 100
var wg sync.WaitGroup
var msgs = make(chan string, pullSnapshotRoutines*2)
var results = make(chan error, pullSnapshotRoutines*2)
for i := 0; i < pullSnapshotRoutines; i++ {
go func() {
for hash := range msgs {
//fmt.Println("Pull snapshot", hash)
// Now pull down snapshot object
snapshotName, snapshotBytes, err := fetchBlobTempFileAndContents(hash, client)
if err != nil {
//return err
}
defer os.Remove(snapshotName)
so, err := core.GetSnapshotObjectFromString(string(snapshotBytes))
for _, entry := range so.S3gitEntries {
if entry.IsDirectory() {
//fmt.Println("wg.Add for", entry.Blob)
wg.Add(1)
msgs <- entry.Blob
}
}
// Add snapshot object to cas
_, err = cas.StoreBlobInCache(snapshotName, kv.SNAPSHOT)
if err != nil {
//return err
}
//fmt.Println("wg.Done for", hash)
wg.Done()
}
}()
}
wg.Add(1)
msgs <- hash
go func() {
wg.Wait()
close(msgs)
close(results)
}()
var err error
for e := range results {
if e != nil {
err = e
}
}
return err
}