Skip to content

Commit 4a46ea2

Browse files
committed
archive: Expose ChangeWriter to allow customized diff computing
Signed-off-by: Kohei Tokunaga <[email protected]>
1 parent ba70277 commit 4a46ea2

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

archive/tar.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func WriteDiff(ctx context.Context, w io.Writer, a, b string, opts ...WriteDiffO
9191
// based off AUFS whiteouts.
9292
// See https://github.com/opencontainers/image-spec/blob/master/layer.md
9393
func writeDiffNaive(ctx context.Context, w io.Writer, a, b string, _ WriteDiffOptions) error {
94-
cw := newChangeWriter(w, b)
94+
cw := NewChangeWriter(w, b)
9595
err := fs.Changes(ctx, a, b, cw.HandleChange)
9696
if err != nil {
9797
return errors.Wrap(err, "failed to create diff tar stream")
@@ -462,7 +462,17 @@ func mkparent(ctx context.Context, path, root string, parents []string) error {
462462
return nil
463463
}
464464

465-
type changeWriter struct {
465+
// ChangeWriter provides tar stream from filesystem change information.
466+
// The privided tar stream is styled as an OCI layer. Change information
467+
// (add/modify/delete/unmodified) for each file needs to be passed to this
468+
// writer through HandleChange method.
469+
//
470+
// This should be used combining with continuity's diff computing functionality
471+
// (e.g. `fs.Change` of github.com/containerd/continuity/fs).
472+
//
473+
// See also https://github.com/opencontainers/image-spec/blob/master/layer.md for details
474+
// about OCI layers
475+
type ChangeWriter struct {
466476
tw *tar.Writer
467477
source string
468478
whiteoutT time.Time
@@ -471,8 +481,11 @@ type changeWriter struct {
471481
addedDirs map[string]struct{}
472482
}
473483

474-
func newChangeWriter(w io.Writer, source string) *changeWriter {
475-
return &changeWriter{
484+
// NewChangeWriter returns ChangeWriter that writes tar stream of the source directory
485+
// to the privided writer. Change information (add/modify/delete/unmodified) for each
486+
// file needs to be passed through HandleChange method.
487+
func NewChangeWriter(w io.Writer, source string) *ChangeWriter {
488+
return &ChangeWriter{
476489
tw: tar.NewWriter(w),
477490
source: source,
478491
whiteoutT: time.Now(),
@@ -482,7 +495,10 @@ func newChangeWriter(w io.Writer, source string) *changeWriter {
482495
}
483496
}
484497

485-
func (cw *changeWriter) HandleChange(k fs.ChangeKind, p string, f os.FileInfo, err error) error {
498+
// HandleChange receives filesystem change information and reflect that information to
499+
// the result tar stream. This function implements `fs.ChangeFunc` of continuity
500+
// (github.com/containerd/continuity/fs) and should be used with that package.
501+
func (cw *ChangeWriter) HandleChange(k fs.ChangeKind, p string, f os.FileInfo, err error) error {
486502
if err != nil {
487503
return err
488504
}
@@ -630,14 +646,15 @@ func (cw *changeWriter) HandleChange(k fs.ChangeKind, p string, f os.FileInfo, e
630646
return nil
631647
}
632648

633-
func (cw *changeWriter) Close() error {
649+
// Close closes this writer.
650+
func (cw *ChangeWriter) Close() error {
634651
if err := cw.tw.Close(); err != nil {
635652
return errors.Wrap(err, "failed to close tar writer")
636653
}
637654
return nil
638655
}
639656

640-
func (cw *changeWriter) includeParents(hdr *tar.Header) error {
657+
func (cw *ChangeWriter) includeParents(hdr *tar.Header) error {
641658
if cw.addedDirs == nil {
642659
return nil
643660
}

archive/tar_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestOverlayApplyNoParents(t *testing.T) {
7171
fstest.FSSuite(t, overlayDiffApplier{
7272
tmp: base,
7373
diff: func(ctx context.Context, w io.Writer, a, b string, _ ...WriteDiffOpt) error {
74-
cw := newChangeWriter(w, b)
74+
cw := NewChangeWriter(w, b)
7575
cw.addedDirs = nil
7676
err := fs.Changes(ctx, a, b, cw.HandleChange)
7777
if err != nil {

0 commit comments

Comments
 (0)