Browse code

Store the checksums when pulling a repository

Guillaume J. Charmes authored on 2013/05/09 08:22:12
Showing 3 changed files
... ...
@@ -31,7 +31,7 @@ func NewGraph(root string) (*Graph, error) {
31 31
 		return nil, err
32 32
 	}
33 33
 	// Create the root directory if it doesn't exists
34
-	if err := os.Mkdir(root, 0700); err != nil && !os.IsExist(err) {
34
+	if err := os.MkdirAll(root, 0700); err != nil && !os.IsExist(err) {
35 35
 		return nil, err
36 36
 	}
37 37
 	graph := &Graph{
... ...
@@ -303,16 +303,12 @@ func (img *Image) Checksum() (string, error) {
303 303
 		return "", err
304 304
 	}
305 305
 
306
-	fmt.Printf("precopy %s: %s\n", img.ShortId(), time.Now().String())
307
-
308 306
 	if _, err := io.Copy(h, layerData); err != nil {
309 307
 		return "", err
310 308
 	}
311
-	fmt.Printf("postcopy presum %s: %s\n", img.ShortId(), time.Now().String())
312 309
 
313 310
 	hash := "sha256:" + hex.EncodeToString(h.Sum(nil))
314 311
 	checksums[img.Id] = hash
315
-	fmt.Printf("postsum %s: %s\n", img.ShortId(), time.Now().String())
316 312
 
317 313
 	// Reload the json file to make sure not to overwrite faster sums
318 314
 	img.graph.lockSumFile.Lock()
... ...
@@ -194,18 +194,16 @@ func (graph *Graph) getRemoteTags(stdout io.Writer, registries []string, reposit
194 194
 			return nil, fmt.Errorf("Repository not found")
195 195
 		}
196 196
 
197
-		result := new(map[string]string)
197
+		result := make(map[string]string)
198 198
 
199 199
 		rawJson, err := ioutil.ReadAll(res.Body)
200 200
 		if err != nil {
201 201
 			return nil, err
202 202
 		}
203
-		if err = json.Unmarshal(rawJson, result); err != nil {
203
+		if err = json.Unmarshal(rawJson, &result); err != nil {
204 204
 			return nil, err
205 205
 		}
206
-
207
-		return *result, nil
208
-
206
+		return result, nil
209 207
 	}
210 208
 	return nil, fmt.Errorf("Could not reach any registry endpoint")
211 209
 }
... ...
@@ -308,6 +306,50 @@ func (graph *Graph) PullRepository(stdout io.Writer, remote, askedTag string, re
308 308
 		return fmt.Errorf("Index response didn't contain any endpoints")
309 309
 	}
310 310
 
311
+	checksumsJson, err := ioutil.ReadAll(res.Body)
312
+	if err != nil {
313
+		return err
314
+	}
315
+
316
+	// Reload the json file to make sure not to overwrite faster sums
317
+	err = func() error {
318
+		localChecksums := make(map[string]string)
319
+		remoteChecksums := []struct {
320
+			Id       string `json: "id"`
321
+			Checksum string `json: "checksum"`
322
+		}{}
323
+		checksumDictPth := path.Join(graph.Root, "..", "checksums")
324
+
325
+		if err := json.Unmarshal(checksumsJson, &remoteChecksums); err != nil {
326
+			return err
327
+		}
328
+
329
+		graph.lockSumFile.Lock()
330
+		defer graph.lockSumFile.Unlock()
331
+
332
+		if checksumDict, err := ioutil.ReadFile(checksumDictPth); err == nil {
333
+			if err := json.Unmarshal(checksumDict, &localChecksums); err != nil {
334
+				return err
335
+			}
336
+		}
337
+
338
+		for _, elem := range remoteChecksums {
339
+			localChecksums[elem.Id] = elem.Checksum
340
+		}
341
+
342
+		checksumsJson, err = json.Marshal(localChecksums)
343
+		if err != nil {
344
+			return err
345
+		}
346
+		if err := ioutil.WriteFile(checksumDictPth, checksumsJson, 0600); err != nil {
347
+			return err
348
+		}
349
+		return nil
350
+	}()
351
+	if err != nil {
352
+		return err
353
+	}
354
+
311 355
 	var tagsList map[string]string
312 356
 	if askedTag == "" {
313 357
 		tagsList, err = graph.getRemoteTags(stdout, endpoints, remote, token)