Browse code

check if working dir is a directory and raise corresponding errors when making dir.

Docker-DCO-1.1-Signed-off-by: Liang-Chi Hsieh <viirya@gmail.com> (github: viirya)

viirya authored on 2014/03/24 00:55:20
Showing 2 changed files
... ...
@@ -252,6 +252,25 @@ func TestRunWorkdirExists(t *testing.T) {
252 252
 	}
253 253
 }
254 254
 
255
+// TestRunWorkdirExistsAndIsFile checks that if 'docker run -w' with existing file can be detected
256
+func TestRunWorkdirExistsAndIsFile(t *testing.T) {
257
+
258
+	cli := api.NewDockerCli(nil, nil, ioutil.Discard, testDaemonProto, testDaemonAddr, nil)
259
+	defer cleanup(globalEngine, t)
260
+
261
+	c := make(chan struct{})
262
+	go func() {
263
+		defer close(c)
264
+		if err := cli.CmdRun("-w", "/bin/cat", unitTestImageID, "pwd"); err == nil {
265
+			t.Fatal("should have failed to run when using /bin/cat as working dir.")
266
+		}
267
+	}()
268
+
269
+	setTimeout(t, "CmdRun timed out", 5*time.Second, func() {
270
+		<-c
271
+	})
272
+}
273
+
255 274
 func TestRunExit(t *testing.T) {
256 275
 	stdin, stdinPipe := io.Pipe()
257 276
 	stdout, stdoutPipe := io.Pipe()
... ...
@@ -535,8 +535,18 @@ func (container *Container) Start() (err error) {
535 535
 
536 536
 	if container.Config.WorkingDir != "" {
537 537
 		container.Config.WorkingDir = path.Clean(container.Config.WorkingDir)
538
-		if err := os.MkdirAll(path.Join(container.basefs, container.Config.WorkingDir), 0755); err != nil {
539
-			return nil
538
+
539
+		pthInfo, err := os.Stat(path.Join(container.basefs, container.Config.WorkingDir))
540
+		if err != nil {
541
+			if !os.IsNotExist(err) {
542
+				return err
543
+			}
544
+			if err := os.MkdirAll(path.Join(container.basefs, container.Config.WorkingDir), 0755); err != nil {
545
+				return err
546
+			}
547
+		}
548
+		if pthInfo != nil && !pthInfo.IsDir() {
549
+			return fmt.Errorf("Cannot mkdir: %s is not a directory", container.Config.WorkingDir)
540 550
 		}
541 551
 	}
542 552
 
... ...
@@ -950,10 +960,11 @@ func (container *Container) ExportRw() (archive.Archive, error) {
950 950
 		return nil, err
951 951
 	}
952 952
 	return utils.NewReadCloserWrapper(archive, func() error {
953
-		err := archive.Close()
954
-		container.Unmount()
955
-		return err
956
-	}), nil
953
+			err := archive.Close()
954
+			container.Unmount()
955
+			return err
956
+		}),
957
+		nil
957 958
 }
958 959
 
959 960
 func (container *Container) Export() (archive.Archive, error) {
... ...
@@ -967,10 +978,11 @@ func (container *Container) Export() (archive.Archive, error) {
967 967
 		return nil, err
968 968
 	}
969 969
 	return utils.NewReadCloserWrapper(archive, func() error {
970
-		err := archive.Close()
971
-		container.Unmount()
972
-		return err
973
-	}), nil
970
+			err := archive.Close()
971
+			container.Unmount()
972
+			return err
973
+		}),
974
+		nil
974 975
 }
975 976
 
976 977
 func (container *Container) WaitTimeout(timeout time.Duration) error {
... ...
@@ -1119,10 +1131,11 @@ func (container *Container) Copy(resource string) (io.ReadCloser, error) {
1119 1119
 		return nil, err
1120 1120
 	}
1121 1121
 	return utils.NewReadCloserWrapper(archive, func() error {
1122
-		err := archive.Close()
1123
-		container.Unmount()
1124
-		return err
1125
-	}), nil
1122
+			err := archive.Close()
1123
+			container.Unmount()
1124
+			return err
1125
+		}),
1126
+		nil
1126 1127
 }
1127 1128
 
1128 1129
 // Returns true if the container exposes a certain port