github.com/openshift/installer@v1.4.17/data/unpack.go (about)

     1  package data
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"path"
     7  	"path/filepath"
     8  )
     9  
    10  // Unpack unpacks the assets from this package into a target directory.
    11  func Unpack(base string, uri string) (err error) {
    12  	return UnpackWithFilePermissions(base, uri, 0666)
    13  }
    14  
    15  // UnpackWithFilePermissions unpacks the assets from this package into a target directory, setting the permissions of
    16  // each file to the specified permissions.
    17  func UnpackWithFilePermissions(base string, uri string, permissions os.FileMode) (err error) {
    18  	file, err := Assets.Open(uri)
    19  	if err != nil {
    20  		return err
    21  	}
    22  	defer file.Close()
    23  
    24  	info, err := file.Stat()
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	if info.IsDir() {
    30  		os.Mkdir(base, 0777)
    31  		children, err := file.Readdir(0)
    32  		if err != nil {
    33  			return err
    34  		}
    35  		file.Close()
    36  
    37  		for _, childInfo := range children {
    38  			name := childInfo.Name()
    39  			err = Unpack(filepath.Join(base, name), path.Join(uri, name))
    40  			if err != nil {
    41  				return err
    42  			}
    43  		}
    44  		return nil
    45  	}
    46  
    47  	out, err := os.OpenFile(base, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, permissions)
    48  	if err != nil {
    49  		return err
    50  	}
    51  	defer out.Close()
    52  
    53  	_, err = io.Copy(out, file)
    54  	return err
    55  }