فهرست منبع

Merge pull request #4818 from viirya/fix_working_dir_not_dir

fix the problem when setting existing file as working dir
Michael Crosby 11 سال پیش
والد
کامیت
ba9f9b3c92
2فایلهای تغییر یافته به همراه46 افزوده شده و 14 حذف شده
  1. 19 0
      integration/commands_test.go
  2. 27 14
      runtime/container.go

+ 19 - 0
integration/commands_test.go

@@ -252,6 +252,25 @@ func TestRunWorkdirExists(t *testing.T) {
 	}
 	}
 }
 }
 
 
+// TestRunWorkdirExistsAndIsFile checks that if 'docker run -w' with existing file can be detected
+func TestRunWorkdirExistsAndIsFile(t *testing.T) {
+
+	cli := api.NewDockerCli(nil, nil, ioutil.Discard, testDaemonProto, testDaemonAddr, nil)
+	defer cleanup(globalEngine, t)
+
+	c := make(chan struct{})
+	go func() {
+		defer close(c)
+		if err := cli.CmdRun("-w", "/bin/cat", unitTestImageID, "pwd"); err == nil {
+			t.Fatal("should have failed to run when using /bin/cat as working dir.")
+		}
+	}()
+
+	setTimeout(t, "CmdRun timed out", 5*time.Second, func() {
+		<-c
+	})
+}
+
 func TestRunExit(t *testing.T) {
 func TestRunExit(t *testing.T) {
 	stdin, stdinPipe := io.Pipe()
 	stdin, stdinPipe := io.Pipe()
 	stdout, stdoutPipe := io.Pipe()
 	stdout, stdoutPipe := io.Pipe()

+ 27 - 14
runtime/container.go

@@ -536,8 +536,18 @@ func (container *Container) Start() (err error) {
 
 
 	if container.Config.WorkingDir != "" {
 	if container.Config.WorkingDir != "" {
 		container.Config.WorkingDir = path.Clean(container.Config.WorkingDir)
 		container.Config.WorkingDir = path.Clean(container.Config.WorkingDir)
-		if err := os.MkdirAll(path.Join(container.basefs, container.Config.WorkingDir), 0755); err != nil {
-			return nil
+
+		pthInfo, err := os.Stat(path.Join(container.basefs, container.Config.WorkingDir))
+		if err != nil {
+			if !os.IsNotExist(err) {
+				return err
+			}
+			if err := os.MkdirAll(path.Join(container.basefs, container.Config.WorkingDir), 0755); err != nil {
+				return err
+			}
+		}
+		if pthInfo != nil && !pthInfo.IsDir() {
+			return fmt.Errorf("Cannot mkdir: %s is not a directory", container.Config.WorkingDir)
 		}
 		}
 	}
 	}
 
 
@@ -959,10 +969,11 @@ func (container *Container) ExportRw() (archive.Archive, error) {
 		return nil, err
 		return nil, err
 	}
 	}
 	return utils.NewReadCloserWrapper(archive, func() error {
 	return utils.NewReadCloserWrapper(archive, func() error {
-		err := archive.Close()
-		container.Unmount()
-		return err
-	}), nil
+			err := archive.Close()
+			container.Unmount()
+			return err
+		}),
+		nil
 }
 }
 
 
 func (container *Container) Export() (archive.Archive, error) {
 func (container *Container) Export() (archive.Archive, error) {
@@ -976,10 +987,11 @@ func (container *Container) Export() (archive.Archive, error) {
 		return nil, err
 		return nil, err
 	}
 	}
 	return utils.NewReadCloserWrapper(archive, func() error {
 	return utils.NewReadCloserWrapper(archive, func() error {
-		err := archive.Close()
-		container.Unmount()
-		return err
-	}), nil
+			err := archive.Close()
+			container.Unmount()
+			return err
+		}),
+		nil
 }
 }
 
 
 func (container *Container) WaitTimeout(timeout time.Duration) error {
 func (container *Container) WaitTimeout(timeout time.Duration) error {
@@ -1128,10 +1140,11 @@ func (container *Container) Copy(resource string) (io.ReadCloser, error) {
 		return nil, err
 		return nil, err
 	}
 	}
 	return utils.NewReadCloserWrapper(archive, func() error {
 	return utils.NewReadCloserWrapper(archive, func() error {
-		err := archive.Close()
-		container.Unmount()
-		return err
-	}), nil
+			err := archive.Close()
+			container.Unmount()
+			return err
+		}),
+		nil
 }
 }
 
 
 // Returns true if the container exposes a certain port
 // Returns true if the container exposes a certain port