Browse code

add err handling, close fd

Signed-off-by: allencloud <allen.sun@daocloud.io>
(cherry picked from commit 2281ce7e98ec983514450c33c0ef1d90262c3b4f)

allencloud authored on 2016/06/28 00:41:53
Showing 4 changed files
... ...
@@ -101,11 +101,16 @@ func (pm *Manager) List() ([]types.Plugin, error) {
101 101
 // Push pushes a plugin to the store.
102 102
 func (pm *Manager) Push(name string, metaHeader http.Header, authConfig *types.AuthConfig) error {
103 103
 	p, err := pm.get(name)
104
+	if err != nil {
105
+		return err
106
+	}
104 107
 	dest := filepath.Join(pm.libRoot, p.P.ID)
105 108
 	config, err := os.Open(filepath.Join(dest, "manifest.json"))
106 109
 	if err != nil {
107 110
 		return err
108 111
 	}
112
+	defer config.Close()
113
+
109 114
 	rootfs, err := archive.Tar(filepath.Join(dest, "rootfs"), archive.Gzip)
110 115
 	if err != nil {
111 116
 		return err
... ...
@@ -191,6 +191,7 @@ func WritePullData(pd PullData, dest string, extract bool) error {
191 191
 		if !extract {
192 192
 			f, err := os.Create(filepath.Join(dest, fmt.Sprintf("layer%d.tar", i)))
193 193
 			if err != nil {
194
+				l.Close()
194 195
 				return err
195 196
 			}
196 197
 			io.Copy(f, l)
... ...
@@ -74,6 +74,7 @@ func Push(name string, rs registry.Service, metaHeader http.Header, authConfig *
74 74
 		r := io.TeeReader(f, h)
75 75
 		_, err = io.Copy(bw, r)
76 76
 		if err != nil {
77
+			f.Close()
77 78
 			logrus.Debugf("Error in io.Copy: %v", err)
78 79
 			return "", err
79 80
 		}
... ...
@@ -155,12 +155,18 @@ func Handle(capability string, callback func(string, *plugins.Client)) {
155 155
 
156 156
 func (pm *Manager) get(name string) (*plugin, error) {
157 157
 	pm.RLock()
158
+	defer pm.RUnlock()
159
+
158 160
 	id, nameOk := pm.nameToID[name]
161
+	if !nameOk {
162
+		return nil, ErrNotFound(name)
163
+	}
164
+
159 165
 	p, idOk := pm.plugins[id]
160
-	pm.RUnlock()
161
-	if !nameOk || !idOk {
166
+	if !idOk {
162 167
 		return nil, ErrNotFound(name)
163 168
 	}
169
+
164 170
 	return p, nil
165 171
 }
166 172