Browse code

Change ownership to root for ADD file/directory

Docker-DCO-1.1-Signed-off-by: Guillaume J. Charmes <guillaume@charmes.net> (github: creack)

Guillaume J. Charmes authored on 2014/04/02 06:17:31
Showing 1 changed files
... ...
@@ -419,10 +419,22 @@ func (b *buildFile) addContext(container *runtime.Container, orig, dest string,
419 419
 		return err
420 420
 	}
421 421
 
422
+	chownR := func(destPath string, uid, gid int) error {
423
+		return filepath.Walk(destPath, func(path string, info os.FileInfo, err error) error {
424
+			if err := os.Lchown(path, uid, gid); err != nil {
425
+				return err
426
+			}
427
+			return nil
428
+		})
429
+	}
430
+
422 431
 	if fi.IsDir() {
423 432
 		if err := archive.CopyWithTar(origPath, destPath); err != nil {
424 433
 			return err
425 434
 		}
435
+		if err := chownR(destPath, 0, 0); err != nil {
436
+			return err
437
+		}
426 438
 		return nil
427 439
 	}
428 440
 
... ...
@@ -452,6 +464,10 @@ func (b *buildFile) addContext(container *runtime.Container, orig, dest string,
452 452
 	if err := archive.CopyWithTar(origPath, destPath); err != nil {
453 453
 		return err
454 454
 	}
455
+
456
+	if err := chownR(destPath, 0, 0); err != nil {
457
+		return err
458
+	}
455 459
 	return nil
456 460
 }
457 461
 
... ...
@@ -486,28 +502,36 @@ func (b *buildFile) CmdAdd(args string) error {
486 486
 	)
487 487
 
488 488
 	if utils.IsURL(orig) {
489
+		// Initiate the download
489 490
 		isRemote = true
490 491
 		resp, err := utils.Download(orig)
491 492
 		if err != nil {
492 493
 			return err
493 494
 		}
495
+
496
+		// Create a tmp dir
494 497
 		tmpDirName, err := ioutil.TempDir(b.contextPath, "docker-remote")
495 498
 		if err != nil {
496 499
 			return err
497 500
 		}
501
+
502
+		// Create a tmp file within our tmp dir
498 503
 		tmpFileName := path.Join(tmpDirName, "tmp")
499 504
 		tmpFile, err := os.OpenFile(tmpFileName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
500 505
 		if err != nil {
501 506
 			return err
502 507
 		}
503 508
 		defer os.RemoveAll(tmpDirName)
504
-		if _, err = io.Copy(tmpFile, resp.Body); err != nil {
509
+
510
+		// Download and dump result to tmp file
511
+		if _, err := io.Copy(tmpFile, resp.Body); err != nil {
505 512
 			tmpFile.Close()
506 513
 			return err
507 514
 		}
508
-		origPath = path.Join(filepath.Base(tmpDirName), filepath.Base(tmpFileName))
509 515
 		tmpFile.Close()
510 516
 
517
+		origPath = path.Join(filepath.Base(tmpDirName), filepath.Base(tmpFileName))
518
+
511 519
 		// Process the checksum
512 520
 		r, err := archive.Tar(tmpFileName, archive.Uncompressed)
513 521
 		if err != nil {