瀏覽代碼

don't user os.Stderr in attach

Docker-DCO-1.1-Signed-off-by: Victor Vieux <victor.vieux@docker.com> (github: vieux)
Victor Vieux 11 年之前
父節點
當前提交
e7a9d23640
共有 3 個文件被更改,包括 16 次插入14 次删除
  1. 4 8
      api.go
  2. 11 3
      engine/streams.go
  3. 1 3
      engine/streams_test.go

+ 4 - 8
api.go

@@ -169,9 +169,7 @@ func getContainersExport(srv *Server, version float64, w http.ResponseWriter, r
 		return fmt.Errorf("Missing parameter")
 		return fmt.Errorf("Missing parameter")
 	}
 	}
 	job := srv.Eng.Job("export", vars["name"])
 	job := srv.Eng.Job("export", vars["name"])
-	if err := job.Stdout.Add(w); err != nil {
-		return err
-	}
+	job.Stdout.Add(w)
 	if err := job.Run(); err != nil {
 	if err := job.Run(); err != nil {
 		return err
 		return err
 	}
 	}
@@ -573,9 +571,7 @@ func getImagesGet(srv *Server, version float64, w http.ResponseWriter, r *http.R
 		w.Header().Set("Content-Type", "application/x-tar")
 		w.Header().Set("Content-Type", "application/x-tar")
 	}
 	}
 	job := srv.Eng.Job("image_export", vars["name"])
 	job := srv.Eng.Job("image_export", vars["name"])
-	if err := job.Stdout.Add(w); err != nil {
-		return err
-	}
+	job.Stdout.Add(w)
 	return job.Run()
 	return job.Run()
 }
 }
 
 
@@ -806,7 +802,7 @@ func postContainersAttach(srv *Server, version float64, w http.ResponseWriter, r
 	job.Setenv("stderr", r.Form.Get("stderr"))
 	job.Setenv("stderr", r.Form.Get("stderr"))
 	job.Stdin.Add(inStream)
 	job.Stdin.Add(inStream)
 	job.Stdout.Add(outStream)
 	job.Stdout.Add(outStream)
-	job.Stderr.Add(errStream)
+	job.Stderr.Set(errStream)
 	if err := job.Run(); err != nil {
 	if err := job.Run(); err != nil {
 		fmt.Fprintf(outStream, "Error: %s\n", err)
 		fmt.Fprintf(outStream, "Error: %s\n", err)
 
 
@@ -836,7 +832,7 @@ func wsContainersAttach(srv *Server, version float64, w http.ResponseWriter, r *
 		job.Setenv("stderr", r.Form.Get("stderr"))
 		job.Setenv("stderr", r.Form.Get("stderr"))
 		job.Stdin.Add(ws)
 		job.Stdin.Add(ws)
 		job.Stdout.Add(ws)
 		job.Stdout.Add(ws)
-		job.Stderr.Add(ws)
+		job.Stderr.Set(ws)
 		if err := job.Run(); err != nil {
 		if err := job.Run(); err != nil {
 			utils.Errorf("Error: %s", err)
 			utils.Errorf("Error: %s", err)
 		}
 		}

+ 11 - 3
engine/streams.go

@@ -31,12 +31,20 @@ func (o *Output) Used() bool {
 // Add attaches a new destination to the Output. Any data subsequently written
 // Add attaches a new destination to the Output. Any data subsequently written
 // to the output will be written to the new destination in addition to all the others.
 // to the output will be written to the new destination in addition to all the others.
 // This method is thread-safe.
 // This method is thread-safe.
-// FIXME: Add cannot fail
-func (o *Output) Add(dst io.Writer) error {
+func (o *Output) Add(dst io.Writer) {
 	o.Mutex.Lock()
 	o.Mutex.Lock()
 	defer o.Mutex.Unlock()
 	defer o.Mutex.Unlock()
 	o.dests = append(o.dests, dst)
 	o.dests = append(o.dests, dst)
-	return nil
+}
+
+// Set closes and remove existing destination and then attaches a new destination to
+// the Output. Any data subsequently written to the output will be written to the new
+// destination in addition to all the others. This method is thread-safe.
+func (o *Output) Set(dst io.Writer) {
+	o.Close()
+	o.Mutex.Lock()
+	defer o.Mutex.Unlock()
+	o.dests = []io.Writer{dst}
 }
 }
 
 
 // AddPipe creates an in-memory pipe with io.Pipe(), adds its writing end as a destination,
 // AddPipe creates an in-memory pipe with io.Pipe(), adds its writing end as a destination,

+ 1 - 3
engine/streams_test.go

@@ -95,9 +95,7 @@ func TestOutputAddEnv(t *testing.T) {
 func TestOutputAddClose(t *testing.T) {
 func TestOutputAddClose(t *testing.T) {
 	o := NewOutput()
 	o := NewOutput()
 	var s sentinelWriteCloser
 	var s sentinelWriteCloser
-	if err := o.Add(&s); err != nil {
-		t.Fatal(err)
-	}
+	o.Add(&s)
 	if err := o.Close(); err != nil {
 	if err := o.Close(); err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}