Browse code

when the file that was opened has been read into buffer, the file should be close.

Signed-off-by: Mabin <bin.ma@huawei.com>

Mabin authored on 2015/02/27 12:53:11
Showing 2 changed files
... ...
@@ -140,14 +140,12 @@ func (img *Image) RawJson() ([]byte, error) {
140 140
 	if err != nil {
141 141
 		return nil, fmt.Errorf("Failed to get root for image %s: %s", img.ID, err)
142 142
 	}
143
-	fh, err := os.Open(jsonPath(root))
144
-	if err != nil {
145
-		return nil, fmt.Errorf("Failed to open json for image %s: %s", img.ID, err)
146
-	}
147
-	buf, err := ioutil.ReadAll(fh)
143
+
144
+	buf, err := ioutil.ReadFile(jsonPath(root))
148 145
 	if err != nil {
149 146
 		return nil, fmt.Errorf("Failed to read json for image %s: %s", img.ID, err)
150 147
 	}
148
+
151 149
 	return buf, nil
152 150
 }
153 151
 
... ...
@@ -851,14 +851,11 @@ func writeFile(dst, content string, t *testing.T) {
851 851
 // Return the contents of file at path `src`.
852 852
 // Call t.Fatal() at the first error (including if the file doesn't exist)
853 853
 func readFile(src string, t *testing.T) (content string) {
854
-	f, err := os.Open(src)
855
-	if err != nil {
856
-		t.Fatal(err)
857
-	}
858
-	data, err := ioutil.ReadAll(f)
854
+	data, err := ioutil.ReadFile(src)
859 855
 	if err != nil {
860 856
 		t.Fatal(err)
861 857
 	}
858
+
862 859
 	return string(data)
863 860
 }
864 861