2018-02-05 16:05:59 -05:00
|
|
|
package tarexport // import "github.com/docker/docker/image/tarexport"
|
2015-11-18 17:18:07 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2018-03-14 13:00:15 -04:00
|
|
|
"path"
|
2015-11-18 17:18:07 -05:00
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2016-05-25 22:11:51 -04:00
|
|
|
"github.com/docker/distribution"
|
2017-01-25 19:54:18 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2015-11-18 17:18:07 -05:00
|
|
|
"github.com/docker/docker/image"
|
2019-08-05 10:37:47 -04:00
|
|
|
v1 "github.com/docker/docker/image/v1"
|
2015-11-18 17:18:07 -05:00
|
|
|
"github.com/docker/docker/layer"
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
2016-02-08 18:40:12 -05:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2022-03-04 08:49:42 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2016-11-29 16:08:24 -05:00
|
|
|
"github.com/pkg/errors"
|
2015-11-18 17:18:07 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type imageDescriptor struct {
|
2017-05-23 12:40:33 -04:00
|
|
|
refs []reference.NamedTagged
|
|
|
|
layers []string
|
|
|
|
image *image.Image
|
|
|
|
layerRef layer.Layer
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type saveSession struct {
|
|
|
|
*tarexporter
|
|
|
|
outDir string
|
|
|
|
images map[image.ID]*imageDescriptor
|
|
|
|
savedLayers map[string]struct{}
|
2016-06-27 15:46:15 -04:00
|
|
|
diffIDPaths map[layer.DiffID]string // cache every diffID blob to avoid duplicates
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *tarexporter) Save(names []string, outStream io.Writer) error {
|
|
|
|
images, err := l.parseNames(names)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-23 12:40:33 -04:00
|
|
|
// Release all the image top layer references
|
|
|
|
defer l.releaseLayerReferences(images)
|
2015-11-18 17:18:07 -05:00
|
|
|
return (&saveSession{tarexporter: l, images: images}).save(outStream)
|
|
|
|
}
|
|
|
|
|
2017-05-23 12:40:33 -04:00
|
|
|
// parseNames will parse the image names to a map which contains image.ID to *imageDescriptor.
|
|
|
|
// Each imageDescriptor holds an image top layer reference named 'layerRef'. It is taken here, should be released later.
|
|
|
|
func (l *tarexporter) parseNames(names []string) (desc map[image.ID]*imageDescriptor, rErr error) {
|
2015-11-18 17:18:07 -05:00
|
|
|
imgDescr := make(map[image.ID]*imageDescriptor)
|
2017-05-23 12:40:33 -04:00
|
|
|
defer func() {
|
|
|
|
if rErr != nil {
|
|
|
|
l.releaseLayerReferences(imgDescr)
|
|
|
|
}
|
|
|
|
}()
|
2015-11-18 17:18:07 -05:00
|
|
|
|
2017-05-23 12:40:33 -04:00
|
|
|
addAssoc := func(id image.ID, ref reference.Named) error {
|
2015-11-18 17:18:07 -05:00
|
|
|
if _, ok := imgDescr[id]; !ok {
|
2017-05-23 12:40:33 -04:00
|
|
|
descr := &imageDescriptor{}
|
|
|
|
if err := l.takeLayerReference(id, descr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
imgDescr[id] = descr
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ref != nil {
|
2015-12-04 16:55:15 -05:00
|
|
|
if _, ok := ref.(reference.Canonical); ok {
|
2017-05-23 12:40:33 -04:00
|
|
|
return nil
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
2017-01-25 19:54:18 -05:00
|
|
|
tagged, ok := reference.TagNameOnly(ref).(reference.NamedTagged)
|
|
|
|
if !ok {
|
2017-05-23 12:40:33 -04:00
|
|
|
return nil
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range imgDescr[id].refs {
|
|
|
|
if tagged.String() == t.String() {
|
2017-05-23 12:40:33 -04:00
|
|
|
return nil
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
imgDescr[id].refs = append(imgDescr[id].refs, tagged)
|
|
|
|
}
|
2017-05-23 12:40:33 -04:00
|
|
|
return nil
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range names {
|
2017-01-25 19:54:18 -05:00
|
|
|
ref, err := reference.ParseAnyReference(name)
|
2015-11-18 17:18:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-25 19:54:18 -05:00
|
|
|
namedRef, ok := ref.(reference.Named)
|
|
|
|
if !ok {
|
|
|
|
// Check if digest ID reference
|
|
|
|
if digested, ok := ref.(reference.Digested); ok {
|
|
|
|
id := image.IDFromDigest(digested.Digest())
|
2017-05-23 12:40:33 -04:00
|
|
|
if err := addAssoc(id, nil); err != nil {
|
2017-01-25 19:54:18 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
continue
|
2016-03-07 13:56:24 -05:00
|
|
|
}
|
2017-01-25 19:54:18 -05:00
|
|
|
return nil, errors.Errorf("invalid reference: %v", name)
|
2016-03-07 13:56:24 -05:00
|
|
|
}
|
2017-01-25 19:54:18 -05:00
|
|
|
|
|
|
|
if reference.FamiliarName(namedRef) == string(digest.Canonical) {
|
2015-11-18 17:18:07 -05:00
|
|
|
imgID, err := l.is.Search(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-23 12:40:33 -04:00
|
|
|
if err := addAssoc(imgID, nil); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-18 17:18:07 -05:00
|
|
|
continue
|
|
|
|
}
|
2017-01-25 19:54:18 -05:00
|
|
|
if reference.IsNameOnly(namedRef) {
|
|
|
|
assocs := l.rs.ReferencesByName(namedRef)
|
2015-12-10 14:01:34 -05:00
|
|
|
for _, assoc := range assocs {
|
2017-05-23 12:40:33 -04:00
|
|
|
if err := addAssoc(image.IDFromDigest(assoc.ID), assoc.Ref); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-10 14:01:34 -05:00
|
|
|
}
|
|
|
|
if len(assocs) == 0 {
|
|
|
|
imgID, err := l.is.Search(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
2017-05-23 12:40:33 -04:00
|
|
|
if err := addAssoc(imgID, nil); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
2015-12-10 14:01:34 -05:00
|
|
|
continue
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
2017-01-25 19:54:18 -05:00
|
|
|
id, err := l.rs.Get(namedRef)
|
2016-09-15 19:37:32 -04:00
|
|
|
if err != nil {
|
2015-11-18 17:18:07 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-23 12:40:33 -04:00
|
|
|
if err := addAssoc(image.IDFromDigest(id), namedRef); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-18 17:18:07 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
return imgDescr, nil
|
|
|
|
}
|
|
|
|
|
2017-05-23 12:40:33 -04:00
|
|
|
// takeLayerReference will take/Get the image top layer reference
|
|
|
|
func (l *tarexporter) takeLayerReference(id image.ID, imgDescr *imageDescriptor) error {
|
|
|
|
img, err := l.is.Get(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-25 06:18:44 -05:00
|
|
|
if os := img.OperatingSystem(); !system.IsOSSupported(os) {
|
|
|
|
return fmt.Errorf("os %q is not supported", os)
|
|
|
|
}
|
2017-05-23 12:40:33 -04:00
|
|
|
imgDescr.image = img
|
|
|
|
topLayerID := img.RootFS.ChainID()
|
|
|
|
if topLayerID == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-19 10:34:08 -04:00
|
|
|
layer, err := l.lss.Get(topLayerID)
|
2017-05-23 12:40:33 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
imgDescr.layerRef = layer
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// releaseLayerReferences will release all the image top layer references
|
|
|
|
func (l *tarexporter) releaseLayerReferences(imgDescr map[image.ID]*imageDescriptor) error {
|
|
|
|
for _, descr := range imgDescr {
|
|
|
|
if descr.layerRef != nil {
|
2021-03-19 10:34:08 -04:00
|
|
|
l.lss.Release(descr.layerRef)
|
2017-05-23 12:40:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-18 17:18:07 -05:00
|
|
|
func (s *saveSession) save(outStream io.Writer) error {
|
|
|
|
s.savedLayers = make(map[string]struct{})
|
2016-06-27 15:46:15 -04:00
|
|
|
s.diffIDPaths = make(map[layer.DiffID]string)
|
2015-11-18 17:18:07 -05:00
|
|
|
|
|
|
|
// get image json
|
2021-08-24 06:10:50 -04:00
|
|
|
tempDir, err := os.MkdirTemp("", "docker-export-")
|
2015-11-18 17:18:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tempDir)
|
|
|
|
|
|
|
|
s.outDir = tempDir
|
|
|
|
reposLegacy := make(map[string]map[string]string)
|
|
|
|
|
|
|
|
var manifest []manifestItem
|
2016-03-21 16:52:36 -04:00
|
|
|
var parentLinks []parentLink
|
2015-11-18 17:18:07 -05:00
|
|
|
|
|
|
|
for id, imageDescr := range s.images {
|
2016-05-25 22:11:51 -04:00
|
|
|
foreignSrcs, err := s.saveImage(id)
|
|
|
|
if err != nil {
|
2015-11-18 17:18:07 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var repoTags []string
|
|
|
|
var layers []string
|
|
|
|
|
|
|
|
for _, ref := range imageDescr.refs {
|
2017-01-25 19:54:18 -05:00
|
|
|
familiarName := reference.FamiliarName(ref)
|
|
|
|
if _, ok := reposLegacy[familiarName]; !ok {
|
|
|
|
reposLegacy[familiarName] = make(map[string]string)
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
2017-01-25 19:54:18 -05:00
|
|
|
reposLegacy[familiarName][ref.Tag()] = imageDescr.layers[len(imageDescr.layers)-1]
|
|
|
|
repoTags = append(repoTags, reference.FamiliarString(ref))
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, l := range imageDescr.layers {
|
2018-03-14 13:00:15 -04:00
|
|
|
// IMPORTANT: We use path, not filepath here to ensure the layers
|
2021-07-27 07:26:52 -04:00
|
|
|
// in the manifest use Unix-style forward-slashes.
|
2018-03-14 13:00:15 -04:00
|
|
|
layers = append(layers, path.Join(l, legacyLayerFileName))
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
manifest = append(manifest, manifestItem{
|
2016-09-15 19:37:32 -04:00
|
|
|
Config: id.Digest().Hex() + ".json",
|
2016-05-25 22:11:51 -04:00
|
|
|
RepoTags: repoTags,
|
|
|
|
Layers: layers,
|
|
|
|
LayerSources: foreignSrcs,
|
2015-11-18 17:18:07 -05:00
|
|
|
})
|
2016-03-21 16:52:36 -04:00
|
|
|
|
|
|
|
parentID, _ := s.is.GetParent(id)
|
|
|
|
parentLinks = append(parentLinks, parentLink{id, parentID})
|
2016-04-19 00:45:59 -04:00
|
|
|
s.tarexporter.loggerImgEvent.LogImageEvent(id.String(), id.String(), "save")
|
2016-03-21 16:52:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, p := range validatedParentLinks(parentLinks) {
|
|
|
|
if p.parentID != "" {
|
|
|
|
manifest[i].Parent = p.parentID
|
|
|
|
}
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(reposLegacy) > 0 {
|
|
|
|
reposFile := filepath.Join(tempDir, legacyRepositoriesFileName)
|
2016-06-24 23:57:21 -04:00
|
|
|
rf, err := os.OpenFile(reposFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
2015-11-18 17:18:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-24 23:57:21 -04:00
|
|
|
|
|
|
|
if err := json.NewEncoder(rf).Encode(reposLegacy); err != nil {
|
|
|
|
rf.Close()
|
2015-11-18 17:18:07 -05:00
|
|
|
return err
|
|
|
|
}
|
2016-06-24 23:57:21 -04:00
|
|
|
|
|
|
|
rf.Close()
|
|
|
|
|
2016-02-08 18:40:12 -05:00
|
|
|
if err := system.Chtimes(reposFile, time.Unix(0, 0), time.Unix(0, 0)); err != nil {
|
2015-11-18 17:18:07 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
manifestFileName := filepath.Join(tempDir, manifestFileName)
|
|
|
|
f, err := os.OpenFile(manifestFileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-24 23:57:21 -04:00
|
|
|
|
2015-11-18 17:18:07 -05:00
|
|
|
if err := json.NewEncoder(f).Encode(manifest); err != nil {
|
2016-06-24 23:57:21 -04:00
|
|
|
f.Close()
|
2015-11-18 17:18:07 -05:00
|
|
|
return err
|
|
|
|
}
|
2016-06-24 23:57:21 -04:00
|
|
|
|
|
|
|
f.Close()
|
|
|
|
|
2016-02-08 18:40:12 -05:00
|
|
|
if err := system.Chtimes(manifestFileName, time.Unix(0, 0), time.Unix(0, 0)); err != nil {
|
2015-11-18 17:18:07 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fs, err := archive.Tar(tempDir, archive.Uncompressed)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fs.Close()
|
|
|
|
|
2016-12-14 16:16:12 -05:00
|
|
|
_, err = io.Copy(outStream, fs)
|
|
|
|
return err
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
2016-06-06 20:49:34 -04:00
|
|
|
func (s *saveSession) saveImage(id image.ID) (map[layer.DiffID]distribution.Descriptor, error) {
|
2017-05-23 12:40:33 -04:00
|
|
|
img := s.images[id].image
|
2015-11-18 17:18:07 -05:00
|
|
|
if len(img.RootFS.DiffIDs) == 0 {
|
2016-05-25 22:11:51 -04:00
|
|
|
return nil, fmt.Errorf("empty export - not implemented")
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var parent digest.Digest
|
|
|
|
var layers []string
|
2016-06-06 20:49:34 -04:00
|
|
|
var foreignSrcs map[layer.DiffID]distribution.Descriptor
|
2015-11-18 17:18:07 -05:00
|
|
|
for i := range img.RootFS.DiffIDs {
|
2016-11-29 03:44:02 -05:00
|
|
|
v1Img := image.V1Image{
|
2017-01-17 05:40:59 -05:00
|
|
|
// This is for backward compatibility used for
|
|
|
|
// pre v1.9 docker.
|
|
|
|
Created: time.Unix(0, 0),
|
2016-11-29 03:44:02 -05:00
|
|
|
}
|
2015-11-18 17:18:07 -05:00
|
|
|
if i == len(img.RootFS.DiffIDs)-1 {
|
|
|
|
v1Img = img.V1Image
|
|
|
|
}
|
|
|
|
rootFS := *img.RootFS
|
|
|
|
rootFS.DiffIDs = rootFS.DiffIDs[:i+1]
|
|
|
|
v1ID, err := v1.CreateID(v1Img, rootFS.ChainID(), parent)
|
|
|
|
if err != nil {
|
2016-05-25 22:11:51 -04:00
|
|
|
return nil, err
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
v1Img.ID = v1ID.Hex()
|
|
|
|
if parent != "" {
|
|
|
|
v1Img.Parent = parent.Hex()
|
|
|
|
}
|
|
|
|
|
2018-03-13 13:12:56 -04:00
|
|
|
v1Img.OS = img.OS
|
2016-05-25 22:11:51 -04:00
|
|
|
src, err := s.saveLayer(rootFS.ChainID(), v1Img, img.Created)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
layers = append(layers, v1Img.ID)
|
|
|
|
parent = v1ID
|
2016-06-06 20:49:34 -04:00
|
|
|
if src.Digest != "" {
|
2016-05-25 22:11:51 -04:00
|
|
|
if foreignSrcs == nil {
|
2016-06-06 20:49:34 -04:00
|
|
|
foreignSrcs = make(map[layer.DiffID]distribution.Descriptor)
|
2016-05-25 22:11:51 -04:00
|
|
|
}
|
|
|
|
foreignSrcs[img.RootFS.DiffIDs[i]] = src
|
|
|
|
}
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
configFile := filepath.Join(s.outDir, id.Digest().Hex()+".json")
|
2021-08-24 06:10:50 -04:00
|
|
|
if err := os.WriteFile(configFile, img.RawJSON(), 0644); err != nil {
|
2016-05-25 22:11:51 -04:00
|
|
|
return nil, err
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
2016-02-08 18:40:12 -05:00
|
|
|
if err := system.Chtimes(configFile, img.Created, img.Created); err != nil {
|
2016-05-25 22:11:51 -04:00
|
|
|
return nil, err
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
s.images[id].layers = layers
|
2016-05-25 22:11:51 -04:00
|
|
|
return foreignSrcs, nil
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
2016-06-06 20:49:34 -04:00
|
|
|
func (s *saveSession) saveLayer(id layer.ChainID, legacyImg image.V1Image, createdTime time.Time) (distribution.Descriptor, error) {
|
2015-11-18 17:18:07 -05:00
|
|
|
if _, exists := s.savedLayers[legacyImg.ID]; exists {
|
2016-06-06 20:49:34 -04:00
|
|
|
return distribution.Descriptor{}, nil
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
outDir := filepath.Join(s.outDir, legacyImg.ID)
|
|
|
|
if err := os.Mkdir(outDir, 0755); err != nil {
|
2016-06-06 20:49:34 -04:00
|
|
|
return distribution.Descriptor{}, err
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// todo: why is this version file here?
|
2021-08-24 06:10:50 -04:00
|
|
|
if err := os.WriteFile(filepath.Join(outDir, legacyVersionFileName), []byte("1.0"), 0644); err != nil {
|
2016-06-06 20:49:34 -04:00
|
|
|
return distribution.Descriptor{}, err
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
imageConfig, err := json.Marshal(legacyImg)
|
|
|
|
if err != nil {
|
2016-06-06 20:49:34 -04:00
|
|
|
return distribution.Descriptor{}, err
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
if err := os.WriteFile(filepath.Join(outDir, legacyConfigFileName), imageConfig, 0644); err != nil {
|
2016-06-06 20:49:34 -04:00
|
|
|
return distribution.Descriptor{}, err
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// serialize filesystem
|
2016-06-27 15:46:15 -04:00
|
|
|
layerPath := filepath.Join(outDir, legacyLayerFileName)
|
2021-03-19 10:34:08 -04:00
|
|
|
l, err := s.lss.Get(id)
|
2015-11-18 17:18:07 -05:00
|
|
|
if err != nil {
|
2016-06-06 20:49:34 -04:00
|
|
|
return distribution.Descriptor{}, err
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
2021-03-19 10:34:08 -04:00
|
|
|
defer layer.ReleaseAndLog(s.lss, l)
|
2015-11-18 17:18:07 -05:00
|
|
|
|
2016-06-27 15:46:15 -04:00
|
|
|
if oldPath, exists := s.diffIDPaths[l.DiffID()]; exists {
|
2016-07-25 16:28:10 -04:00
|
|
|
relPath, err := filepath.Rel(outDir, oldPath)
|
2016-06-27 15:46:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
2016-11-29 16:08:24 -05:00
|
|
|
if err := os.Symlink(relPath, layerPath); err != nil {
|
|
|
|
return distribution.Descriptor{}, errors.Wrap(err, "error creating symlink while saving layer")
|
|
|
|
}
|
2016-06-27 15:46:15 -04:00
|
|
|
} else {
|
2016-11-01 18:44:06 -04:00
|
|
|
// Use system.CreateSequential rather than os.Create. This ensures sequential
|
|
|
|
// file access on Windows to avoid eating into MM standby list.
|
|
|
|
// On Linux, this equates to a regular os.Create.
|
|
|
|
tarFile, err := system.CreateSequential(layerPath)
|
2016-06-27 15:46:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
defer tarFile.Close()
|
2015-11-18 17:18:07 -05:00
|
|
|
|
2016-06-27 15:46:15 -04:00
|
|
|
arch, err := l.TarStream()
|
|
|
|
if err != nil {
|
2016-06-06 20:49:34 -04:00
|
|
|
return distribution.Descriptor{}, err
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
2016-06-27 15:46:15 -04:00
|
|
|
defer arch.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(tarFile, arch); err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fname := range []string{"", legacyVersionFileName, legacyConfigFileName, legacyLayerFileName} {
|
|
|
|
// todo: maybe save layer created timestamp?
|
|
|
|
if err := system.Chtimes(filepath.Join(outDir, fname), createdTime, createdTime); err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
}
|
2015-11-18 17:18:07 -05:00
|
|
|
|
2016-06-27 15:46:15 -04:00
|
|
|
s.diffIDPaths[l.DiffID()] = layerPath
|
|
|
|
}
|
2015-11-18 17:18:07 -05:00
|
|
|
s.savedLayers[legacyImg.ID] = struct{}{}
|
2016-05-25 22:11:51 -04:00
|
|
|
|
2016-06-06 20:49:34 -04:00
|
|
|
var src distribution.Descriptor
|
|
|
|
if fs, ok := l.(distribution.Describable); ok {
|
|
|
|
src = fs.Descriptor()
|
2016-05-25 22:11:51 -04:00
|
|
|
}
|
|
|
|
return src, nil
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|