X Tutup
Skip to content

Commit 95a0b3a

Browse files
committed
Refactor checking for compressed diff type
Signed-off-by: Darren Stahl <darst@microsoft.com>
1 parent e6280a7 commit 95a0b3a

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

diff/walking/differ.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"io"
88
"io/ioutil"
9-
"strings"
109
"time"
1110

1211
"github.com/containerd/containerd/archive"
@@ -75,18 +74,10 @@ func (s *walkingDiff) Apply(ctx context.Context, desc ocispec.Descriptor, mounts
7574
}).Debugf("diff applied")
7675
}
7776
}()
78-
var isCompressed bool
79-
switch desc.MediaType {
80-
case ocispec.MediaTypeImageLayer, images.MediaTypeDockerSchema2Layer:
81-
case ocispec.MediaTypeImageLayerGzip, images.MediaTypeDockerSchema2LayerGzip:
82-
isCompressed = true
83-
default:
84-
// Still apply all generic media types *.tar[.+]gzip and *.tar
85-
if strings.HasSuffix(desc.MediaType, ".tar.gzip") || strings.HasSuffix(desc.MediaType, ".tar+gzip") {
86-
isCompressed = true
87-
} else if !strings.HasSuffix(desc.MediaType, ".tar") {
88-
return emptyDesc, errors.Wrapf(errdefs.ErrNotImplemented, "unsupported diff media type: %v", desc.MediaType)
89-
}
77+
78+
isCompressed, err := images.IsCompressedDiff(ctx, desc.MediaType)
79+
if err != nil {
80+
return emptyDesc, errors.Wrapf(errdefs.ErrNotImplemented, "unsupported diff media type: %v", desc.MediaType)
9081
}
9182

9283
var ocidesc ocispec.Descriptor

diff/windows/windows.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package windows
55
import (
66
"io"
77
"io/ioutil"
8-
"strings"
98
"time"
109

1110
winio "github.com/Microsoft/go-winio"
@@ -74,18 +73,10 @@ func (s *windowsDiff) Apply(ctx context.Context, desc ocispec.Descriptor, mounts
7473
}).Debugf("diff applied")
7574
}
7675
}()
77-
var isCompressed bool
78-
switch desc.MediaType {
79-
case ocispec.MediaTypeImageLayer, images.MediaTypeDockerSchema2Layer:
80-
case ocispec.MediaTypeImageLayerGzip, images.MediaTypeDockerSchema2LayerGzip:
81-
isCompressed = true
82-
default:
83-
// Still apply all generic media types *.tar[.+]gzip and *.tar
84-
if strings.HasSuffix(desc.MediaType, ".tar.gzip") || strings.HasSuffix(desc.MediaType, ".tar+gzip") {
85-
isCompressed = true
86-
} else if !strings.HasSuffix(desc.MediaType, ".tar") {
87-
return emptyDesc, errors.Wrapf(errdefs.ErrNotImplemented, "unsupported diff media type: %v", desc.MediaType)
88-
}
76+
77+
isCompressed, err := images.IsCompressedDiff(ctx, desc.MediaType)
78+
if err != nil {
79+
return emptyDesc, errors.Wrapf(errdefs.ErrNotImplemented, "unsupported diff media type: %v", desc.MediaType)
8980
}
9081

9182
ra, err := s.store.ReaderAt(ctx, desc.Digest)

images/image.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package images
33
import (
44
"context"
55
"encoding/json"
6+
"strings"
67
"time"
78

89
"github.com/containerd/containerd/content"
@@ -359,3 +360,22 @@ func RootFS(ctx context.Context, provider content.Provider, configDesc ocispec.D
359360
}
360361
return config.RootFS.DiffIDs, nil
361362
}
363+
364+
// IsCompressedDiff returns true if mediaType is a known compressed diff media type.
365+
// It returns false if the media type is a diff, but not compressed. If the media type
366+
// is not a known diff type, it returns errdefs.ErrNotImplemented
367+
func IsCompressedDiff(ctx context.Context, mediaType string) (bool, error) {
368+
switch mediaType {
369+
case ocispec.MediaTypeImageLayer, MediaTypeDockerSchema2Layer:
370+
case ocispec.MediaTypeImageLayerGzip, MediaTypeDockerSchema2LayerGzip:
371+
return true, nil
372+
default:
373+
// Still apply all generic media types *.tar[.+]gzip and *.tar
374+
if strings.HasSuffix(mediaType, ".tar.gzip") || strings.HasSuffix(mediaType, ".tar+gzip") {
375+
return true, nil
376+
} else if !strings.HasSuffix(mediaType, ".tar") {
377+
return false, errdefs.ErrNotImplemented
378+
}
379+
}
380+
return false, nil
381+
}

0 commit comments

Comments
 (0)
X Tutup