Browse Source

add err handling, close fd

Signed-off-by: allencloud <allen.sun@daocloud.io>
allencloud 9 years ago
parent
commit
2281ce7e98
4 changed files with 15 additions and 2 deletions
  1. 5 0
      plugin/backend.go
  2. 1 0
      plugin/distribution/pull.go
  3. 1 0
      plugin/distribution/push.go
  4. 8 2
      plugin/manager.go

+ 5 - 0
plugin/backend.go

@@ -101,11 +101,16 @@ func (pm *Manager) List() ([]types.Plugin, error) {
 // Push pushes a plugin to the store.
 func (pm *Manager) Push(name string, metaHeader http.Header, authConfig *types.AuthConfig) error {
 	p, err := pm.get(name)
+	if err != nil {
+		return err
+	}
 	dest := filepath.Join(pm.libRoot, p.P.ID)
 	config, err := os.Open(filepath.Join(dest, "manifest.json"))
 	if err != nil {
 		return err
 	}
+	defer config.Close()
+
 	rootfs, err := archive.Tar(filepath.Join(dest, "rootfs"), archive.Gzip)
 	if err != nil {
 		return err

+ 1 - 0
plugin/distribution/pull.go

@@ -191,6 +191,7 @@ func WritePullData(pd PullData, dest string, extract bool) error {
 		if !extract {
 			f, err := os.Create(filepath.Join(dest, fmt.Sprintf("layer%d.tar", i)))
 			if err != nil {
+				l.Close()
 				return err
 			}
 			io.Copy(f, l)

+ 1 - 0
plugin/distribution/push.go

@@ -74,6 +74,7 @@ func Push(name string, rs registry.Service, metaHeader http.Header, authConfig *
 		r := io.TeeReader(f, h)
 		_, err = io.Copy(bw, r)
 		if err != nil {
+			f.Close()
 			logrus.Debugf("Error in io.Copy: %v", err)
 			return "", err
 		}

+ 8 - 2
plugin/manager.go

@@ -155,12 +155,18 @@ func Handle(capability string, callback func(string, *plugins.Client)) {
 
 func (pm *Manager) get(name string) (*plugin, error) {
 	pm.RLock()
+	defer pm.RUnlock()
+
 	id, nameOk := pm.nameToID[name]
+	if !nameOk {
+		return nil, ErrNotFound(name)
+	}
+
 	p, idOk := pm.plugins[id]
-	pm.RUnlock()
-	if !nameOk || !idOk {
+	if !idOk {
 		return nil, ErrNotFound(name)
 	}
+
 	return p, nil
 }