Skip to content

Commit

Permalink
chunked: skip validation for prefetched images
Browse files Browse the repository at this point in the history
if the image is created locally there is no need to validate again the
files.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
  • Loading branch information
giuseppe committed Jul 25, 2023
1 parent 2e1408a commit 14dbdf7
Showing 1 changed file with 44 additions and 27 deletions.
71 changes: 44 additions & 27 deletions pkg/chunked/storage_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type chunkedDiffer struct {

convertToZstdChunked bool

skipValidation bool

blobSize int64

storeOpts *types.StoreOptions
Expand Down Expand Up @@ -935,8 +937,10 @@ func (c *chunkedDiffer) appendCompressedStreamToFile(compression compressedFileT
if err := appendHole(int(destFile.file.Fd()), size); err != nil {
return err
}
if err := hashHole(destFile.hash, size, c.copyBuffer); err != nil {
return err
if destFile.hash != nil {
if err := hashHole(destFile.hash, size, c.copyBuffer); err != nil {
return err
}
}
default:
return fmt.Errorf("unknown file type %q", c.fileType)
Expand All @@ -945,33 +949,43 @@ func (c *chunkedDiffer) appendCompressedStreamToFile(compression compressedFileT
}

type destinationFile struct {
dirfd int
file *os.File
digester digest.Digester
hash hash.Hash
to io.Writer
metadata *internal.FileMetadata
options *archive.TarOptions
digester digest.Digester
dirfd int
file *os.File
hash hash.Hash
metadata *internal.FileMetadata
options *archive.TarOptions
skipValidation bool
to io.Writer
}

func openDestinationFile(dirfd int, metadata *internal.FileMetadata, options *archive.TarOptions) (*destinationFile, error) {
func openDestinationFile(dirfd int, metadata *internal.FileMetadata, options *archive.TarOptions, skipValidation bool) (*destinationFile, error) {
file, err := openFileUnderRoot(metadata.Name, dirfd, newFileFlags, 0)
if err != nil {
return nil, err
}

digester := digest.Canonical.Digester()
hash := digester.Hash()
to := io.MultiWriter(file, hash)
var digester digest.Digester
var hash hash.Hash
var to io.Writer

if skipValidation {
to = file
} else {
digester = digest.Canonical.Digester()
hash = digester.Hash()
to = io.MultiWriter(file, hash)
}

return &destinationFile{
file: file,
digester: digester,
hash: hash,
to: to,
metadata: metadata,
options: options,
dirfd: dirfd,
file: file,
digester: digester,
hash: hash,
to: to,
metadata: metadata,
options: options,
dirfd: dirfd,
skipValidation: skipValidation,
}, nil
}

Expand All @@ -983,12 +997,14 @@ func (d *destinationFile) Close() (Err error) {
}
}()

manifestChecksum, err := digest.Parse(d.metadata.Digest)
if err != nil {
return err
}
if d.digester.Digest() != manifestChecksum {
return fmt.Errorf("checksum mismatch for %q (got %q instead of %q)", d.file.Name(), d.digester.Digest(), manifestChecksum)
if !d.skipValidation {
manifestChecksum, err := digest.Parse(d.metadata.Digest)
if err != nil {
return err
}
if d.digester.Digest() != manifestChecksum {
return fmt.Errorf("checksum mismatch for %q (got %q instead of %q)", d.file.Name(), d.digester.Digest(), manifestChecksum)
}
}

return setFileAttrs(d.dirfd, d.file, os.FileMode(d.metadata.Mode), d.metadata, d.options, false)
Expand Down Expand Up @@ -1088,7 +1104,7 @@ func (c *chunkedDiffer) storeMissingFiles(streams chan io.ReadCloser, errs chan
}
filesToClose <- destFile
}
destFile, err = openDestinationFile(dirfd, mf.File, options)
destFile, err = openDestinationFile(dirfd, mf.File, options, c.skipValidation)
if err != nil {
Err = err
goto exit
Expand Down Expand Up @@ -1512,6 +1528,7 @@ func (c *chunkedDiffer) ApplyDiff(dest string, options *archive.TarOptions, diff
c.fileType = fileTypeZstdChunked
c.manifest = manifest
c.convertToZstdChunked = false
c.skipValidation = true
c.tarSplit = tarSplit
// since we retrieved the whole file, use the diffID instead of the TOC digest.
c.tocDigest = diffID
Expand Down

0 comments on commit 14dbdf7

Please sign in to comment.