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