Skip to content

Commit

Permalink
Beautify the implementation of layerID
Browse files Browse the repository at this point in the history
Should not change behavior.

Signed-off-by: Miloslav Trmač <[email protected]>
  • Loading branch information
mtrmac committed Jan 21, 2025
1 parent c32db7d commit 9b2e568
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions storage/storage_dest.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,21 +913,23 @@ func (s *storageImageDestination) commitLayer(index int, info addedLayerInfo, si
return false, nil
}

// layerID computes a layer (“chain”) ID for (a possibly-empty parentLayer, trusted)
func layerID(parentLayer string, trusted trustedLayerIdentityData) string {
var layerIDComponent string
// layerID computes a layer (“chain”) ID for (a possibly-empty parentID, trusted)
func layerID(parentID string, trusted trustedLayerIdentityData) string {
var component string
mustHash := false
if trusted.layerIdentifiedByTOC {
// "@" is not a valid start of a digest.Digest.Encoded(), so this is unambiguous with the !layerIdentifiedByTOC case.
// But we _must_ hash this below to get a Digest.Encoded()-formatted value.
layerIDComponent = "@TOC=" + trusted.tocDigest.Encoded()
component = "@TOC=" + trusted.tocDigest.Encoded()
mustHash = true
} else {
layerIDComponent = trusted.diffID.Encoded() // This looks like chain IDs, and it uses the traditional value.
component = trusted.diffID.Encoded() // This looks like chain IDs, and it uses the traditional value.
}
id := layerIDComponent
if trusted.layerIdentifiedByTOC || parentLayer != "" {
id = digest.Canonical.FromString(parentLayer + "+" + layerIDComponent).Encoded()

if parentID == "" && !mustHash {
return component
}
return id
return digest.Canonical.FromString(parentID + "+" + component).Encoded()
}

// createNewLayer creates a new layer newLayerID for (index, layerDigest) on top of parentLayer (which may be "").
Expand Down

0 comments on commit 9b2e568

Please sign in to comment.