in container/go/pkg/compat/write.go [74:109]
func writeImageLayer(l v1.Layer, idx int, outDir string) error {
d, err := l.Digest()
if err != nil {
return errors.Wrapf(err, "unable to get digest from layer %d", idx)
}
outDigestFile := path.Join(outDir, fmt.Sprintf("%03d.sha256", idx))
if err := ioutil.WriteFile(outDigestFile, []byte(d.Hex), os.ModePerm); err != nil {
return errors.Wrapf(err, "unable to write the digest of layer %d to %s", idx, outDigestFile)
}
m, err := l.MediaType()
if err != nil {
return errors.Wrap(err, "unable to get the media type of layer")
}
var contents io.ReadCloser
var outLayerFile string
if isCompressed(m) {
contents, err = l.Compressed()
outLayerFile = path.Join(outDir, fmt.Sprintf("%03d.tar.gz", idx))
} else {
contents, err = l.Uncompressed()
outLayerFile = path.Join(outDir, fmt.Sprintf("%03d.tar", idx))
}
if err != nil {
return errors.Wrapf(err, "unable to get the contents of layer %d", idx)
}
defer contents.Close()
o, err := os.Create(outLayerFile)
if err != nil {
return errors.Wrapf(err, "unable to create %s to write layer %d", outLayerFile, idx)
}
if _, err := io.Copy(o, contents); err != nil {
return errors.Wrapf(err, "unable to write the contents of layer %d to %s", idx, outLayerFile)
}
return nil
}