github.com/mheon/docker@v0.11.2-0.20150922122814-44f47903a831/builder/internals_unix.go (about)

     1  // +build freebsd linux
     2  
     3  package builder
     4  
     5  import (
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  )
    10  
    11  func getTempDir(dir, prefix string) (string, error) {
    12  	return ioutil.TempDir(dir, prefix)
    13  }
    14  
    15  func fixPermissions(source, destination string, uid, gid int, destExisted bool) error {
    16  	// If the destination didn't already exist, or the destination isn't a
    17  	// directory, then we should Lchown the destination. Otherwise, we shouldn't
    18  	// Lchown the destination.
    19  	destStat, err := os.Stat(destination)
    20  	if err != nil {
    21  		// This should *never* be reached, because the destination must've already
    22  		// been created while untar-ing the context.
    23  		return err
    24  	}
    25  	doChownDestination := !destExisted || !destStat.IsDir()
    26  
    27  	// We Walk on the source rather than on the destination because we don't
    28  	// want to change permissions on things we haven't created or modified.
    29  	return filepath.Walk(source, func(fullpath string, info os.FileInfo, err error) error {
    30  		// Do not alter the walk root iff. it existed before, as it doesn't fall under
    31  		// the domain of "things we should chown".
    32  		if !doChownDestination && (source == fullpath) {
    33  			return nil
    34  		}
    35  
    36  		// Path is prefixed by source: substitute with destination instead.
    37  		cleaned, err := filepath.Rel(source, fullpath)
    38  		if err != nil {
    39  			return err
    40  		}
    41  
    42  		fullpath = filepath.Join(destination, cleaned)
    43  		return os.Lchown(fullpath, uid, gid)
    44  	})
    45  }