Browse code

Eliminate json.Marshal from graph/export.go and volumes/volume.go

Fixes #12531
Fixes #12530

Signed-off-by: Ankush Agarwal <ankushagarwal11@gmail.com>

Ankush Agarwal authored on 2015/04/21 05:05:48
Showing 2 changed files
... ...
@@ -85,8 +85,15 @@ func (s *TagStore) ImageExport(imageExportConfig *ImageExportConfig) error {
85 85
 	}
86 86
 	// write repositories, if there is something to write
87 87
 	if len(rootRepoMap) > 0 {
88
-		rootRepoJson, _ := json.Marshal(rootRepoMap)
89
-		if err := ioutil.WriteFile(path.Join(tempdir, "repositories"), rootRepoJson, os.FileMode(0644)); err != nil {
88
+		f, err := os.OpenFile(path.Join(tempdir, "repositories"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
89
+		if err != nil {
90
+			f.Close()
91
+			return err
92
+		}
93
+		if err := json.NewEncoder(f).Encode(rootRepoMap); err != nil {
94
+			return err
95
+		}
96
+		if err := f.Close(); err != nil {
90 97
 			return err
91 98
 		}
92 99
 	} else {
... ...
@@ -2,7 +2,6 @@ package volumes
2 2
 
3 3
 import (
4 4
 	"encoding/json"
5
-	"io/ioutil"
6 5
 	"os"
7 6
 	"path/filepath"
8 7
 	"sync"
... ...
@@ -81,17 +80,19 @@ func (v *Volume) ToDisk() error {
81 81
 }
82 82
 
83 83
 func (v *Volume) toDisk() error {
84
-	data, err := json.Marshal(v)
84
+	jsonPath, err := v.jsonPath()
85 85
 	if err != nil {
86 86
 		return err
87 87
 	}
88
-
89
-	pth, err := v.jsonPath()
88
+	f, err := os.OpenFile(jsonPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
90 89
 	if err != nil {
91 90
 		return err
92 91
 	}
93
-
94
-	return ioutil.WriteFile(pth, data, 0666)
92
+	if err := json.NewEncoder(f).Encode(v); err != nil {
93
+		f.Close()
94
+		return err
95
+	}
96
+	return f.Close()
95 97
 }
96 98
 
97 99
 func (v *Volume) FromDisk() error {