2018-02-05 16:05:59 -05:00
|
|
|
package chrootarchive // import "github.com/docker/docker/pkg/chrootarchive"
|
2014-10-29 15:06:51 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-07-27 09:46:20 -04:00
|
|
|
"io/ioutil"
|
2014-10-29 15:06:51 -04:00
|
|
|
"os"
|
2014-12-08 16:19:24 -05:00
|
|
|
"path/filepath"
|
2014-10-29 15:06:51 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
2015-10-14 14:35:48 -04:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2014-10-29 15:06:51 -04:00
|
|
|
)
|
|
|
|
|
2017-05-24 11:53:41 -04:00
|
|
|
// NewArchiver returns a new Archiver which uses chrootarchive.Untar
|
2017-11-16 01:20:33 -05:00
|
|
|
func NewArchiver(idMapping *idtools.IdentityMapping) *archive.Archiver {
|
|
|
|
if idMapping == nil {
|
|
|
|
idMapping = &idtools.IdentityMapping{}
|
2017-05-24 11:53:41 -04:00
|
|
|
}
|
2017-08-03 20:22:00 -04:00
|
|
|
return &archive.Archiver{
|
2017-11-16 01:20:33 -05:00
|
|
|
Untar: Untar,
|
|
|
|
IDMapping: idMapping,
|
2017-08-03 20:22:00 -04:00
|
|
|
}
|
2017-05-24 11:53:41 -04:00
|
|
|
}
|
2014-12-08 16:14:56 -05:00
|
|
|
|
2015-06-16 05:51:27 -04:00
|
|
|
// Untar reads a stream of bytes from `archive`, parses it as a tar archive,
|
|
|
|
// and unpacks it into the directory at `dest`.
|
|
|
|
// The archive may be compressed with one of the following algorithms:
|
|
|
|
// identity (uncompressed), gzip, bzip2, xz.
|
2014-12-08 16:14:56 -05:00
|
|
|
func Untar(tarArchive io.Reader, dest string, options *archive.TarOptions) error {
|
2019-06-12 00:06:21 -04:00
|
|
|
return untarHandler(tarArchive, dest, options, true)
|
2015-07-27 09:46:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// UntarUncompressed reads a stream of bytes from `archive`, parses it as a tar archive,
|
|
|
|
// and unpacks it into the directory at `dest`.
|
|
|
|
// The archive must be an uncompressed stream.
|
|
|
|
func UntarUncompressed(tarArchive io.Reader, dest string, options *archive.TarOptions) error {
|
2019-06-12 00:06:21 -04:00
|
|
|
return untarHandler(tarArchive, dest, options, false)
|
2015-07-27 09:46:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handler for teasing out the automatic decompression
|
2019-06-12 00:06:21 -04:00
|
|
|
func untarHandler(tarArchive io.Reader, dest string, options *archive.TarOptions, decompress bool) error {
|
2014-12-08 16:14:56 -05:00
|
|
|
if tarArchive == nil {
|
|
|
|
return fmt.Errorf("Empty archive")
|
|
|
|
}
|
|
|
|
if options == nil {
|
|
|
|
options = &archive.TarOptions{}
|
|
|
|
}
|
2014-10-23 17:30:11 -04:00
|
|
|
if options.ExcludePatterns == nil {
|
|
|
|
options.ExcludePatterns = []string{}
|
2014-12-08 16:14:56 -05:00
|
|
|
}
|
2014-10-29 15:06:51 -04:00
|
|
|
|
2017-11-16 01:20:33 -05:00
|
|
|
idMapping := idtools.NewIDMappingsFromMaps(options.UIDMaps, options.GIDMaps)
|
|
|
|
rootIDs := idMapping.RootPair()
|
2015-10-14 14:35:48 -04:00
|
|
|
|
2015-01-29 10:28:44 -05:00
|
|
|
dest = filepath.Clean(dest)
|
2014-10-29 15:06:51 -04:00
|
|
|
if _, err := os.Stat(dest); os.IsNotExist(err) {
|
2017-05-31 17:36:48 -04:00
|
|
|
if err := idtools.MkdirAllAndChownNew(dest, 0755, rootIDs); err != nil {
|
2014-10-29 15:06:51 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-01-29 10:28:44 -05:00
|
|
|
|
2015-07-27 09:46:20 -04:00
|
|
|
r := ioutil.NopCloser(tarArchive)
|
|
|
|
if decompress {
|
|
|
|
decompressedArchive, err := archive.DecompressStream(tarArchive)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer decompressedArchive.Close()
|
|
|
|
r = decompressedArchive
|
2014-12-08 16:19:24 -05:00
|
|
|
}
|
2014-11-08 10:38:42 -05:00
|
|
|
|
2019-06-12 00:06:21 -04:00
|
|
|
return invokeUnpack(r, dest, options)
|
2014-10-29 15:06:51 -04:00
|
|
|
}
|