forked from containers/image
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a `MultiImageArchive{Reader,Writer}` to `docker/archive` to support docker archives with more than one image. To allow the new archive reader/writer to be used for copying images, add an `Image{Destination,Source}` to `copy.Options`. When set, the destination/source referenced will be ignored and the specified `Image{Destination,Source}` will be used instead. Fixes: containers#610 Signed-off-by: Valentin Rothberg <[email protected]>
- Loading branch information
Showing
8 changed files
with
232 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package archive | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/containers/image/v5/docker/tarfile" | ||
"github.com/containers/image/v5/types" | ||
) | ||
|
||
// MultiImageArchiveReader allows for reading a docker-archive with multiple | ||
// images. It can be used in subsequent copy.Image operations when set as the | ||
// copy.Options.ImageSource. Note that you need to call Next() before each | ||
// copy.Image operation. | ||
// | ||
// For using a MultiImageArchiveReader in copy.Image, set it as the | ||
// copy.Option.ImageSource. | ||
type MultiImageArchiveReader struct { | ||
*archiveImageSource | ||
|
||
manifests []tarfile.ManifestItem | ||
manifestIndex int | ||
} | ||
|
||
// NewMultiImageArchiveReader returns a new MultiImageArchiveReader based on the reference. | ||
func NewMultiImageArchiveReader(ctx context.Context, sys *types.SystemContext, ref types.ImageReference) (*MultiImageArchiveReader, error) { | ||
path, destinationRef, err := parsePathAndNamedRef(ref.StringWithinTransport()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
archRef := archiveReference{ | ||
path: path, | ||
destinationRef: destinationRef, | ||
} | ||
|
||
src, err := newArchiveImageSource(ctx, sys, archRef) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
manifests, err := src.LoadTarManifest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &MultiImageArchiveReader{ | ||
archiveImageSource: src, | ||
manifests: manifests, | ||
manifestIndex: -1, // So we can call Next() before each copy.Image call. | ||
}, nil | ||
} | ||
|
||
// Next selects the next image in the archive. False is returned when no image | ||
// is left. | ||
func (m *MultiImageArchiveReader) Next() bool { | ||
m.manifestIndex++ | ||
if m.manifestIndex >= len(m.manifests) { | ||
return false | ||
} | ||
m.ChangeManifest(m.manifestIndex) | ||
return true | ||
} | ||
|
||
// Close is a NOP to allow for using the MultiImageArchiveReader as an | ||
// types.ImageSource but without closing the underlying tarfile.Source. | ||
func (m *MultiImageArchiveReader) Close() error { | ||
return nil | ||
} | ||
|
||
// Finalize closes the underlying tarfile. | ||
func (m *MultiImageArchiveReader) Finalize() error { | ||
return m.archiveImageSource.Close() | ||
} | ||
|
||
// LoadTarManifest returns the tarfile.ManifestItem of the current image. | ||
func (m *MultiImageArchiveReader) LoadTarManifest() ([]tarfile.ManifestItem, error) { | ||
return []tarfile.ManifestItem{m.manifests[m.manifestIndex]}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package archive | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/containers/image/v5/types" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// MultiImageArchiveReader allows for creating and writing to a docker-archive | ||
// with multiple images. Once created, it can be used in subsequent copy.Image | ||
// operations when set as the copy.Options.ImageDestination. | ||
// | ||
// For using a MultiImageArchiveWriter in copy.Image, set it as the | ||
// copy.Option.ImageDestination. | ||
type MultiImageArchiveWriter struct { | ||
*archiveImageDestination | ||
} | ||
|
||
func NewMultiImageArchiveWriter(ctx context.Context, sys *types.SystemContext, path string) (*MultiImageArchiveWriter, error) { | ||
ref := archiveReference{path: path} | ||
dst, err := newArchiveImageDestination(sys, ref) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &MultiImageArchiveWriter{archiveImageDestination: dst}, nil | ||
} | ||
|
||
// Close is a NOP. Use Finalize() instead. | ||
func (m *MultiImageArchiveWriter) Close() error { | ||
return nil | ||
} | ||
|
||
// Commit is a NOP. Use Finalize() instead. | ||
func (m *MultiImageArchiveWriter) Commit(_ context.Context, _ types.UnparsedImage) error { | ||
return nil | ||
} | ||
|
||
// Finalize commits pending data and closes the underlying tarfile. | ||
func (m *MultiImageArchiveWriter) Finalize(ctx context.Context) (finalErr error) { | ||
defer func() { | ||
if err := m.writer.Close(); err != nil { | ||
if finalErr == nil { | ||
finalErr = err | ||
} else { | ||
finalErr = errors.Wrap(finalErr, err.Error()) | ||
} | ||
} | ||
}() | ||
return m.Destination.Commit(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.