Skip to content

Commit 34d6d48

Browse files
committed
storage/pkg/chunked: refactor findDigestInternal to return struct
Refactor findDigestInternal to return a digestLookupResult struct instead of multiple return values. Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
1 parent 4cac6ec commit 34d6d48

1 file changed

Lines changed: 38 additions & 15 deletions

File tree

storage/pkg/chunked/cache_linux.go

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -783,17 +783,27 @@ func findBinaryTag(binaryDigest []byte, cacheFile *cacheFile) (bool, uint64, uin
783783
return false, 0, 0
784784
}
785785

786-
func (c *layersCache) findDigestInternal(digest string) (string, string, int64, error) {
786+
// digestLookupResult contains the result of looking up a digest in the cache.
787+
type digestLookupResult struct {
788+
// Target is the layer ID where the content was found.
789+
Target string
790+
// Path is the file path within the layer.
791+
Path string
792+
// Offset is the offset within the file where the content starts.
793+
Offset int64
794+
}
795+
796+
func (c *layersCache) findDigestInternal(digest string) (*digestLookupResult, error) {
787797
if digest == "" {
788-
return "", "", -1, nil
798+
return nil, nil
789799
}
790800

791801
c.mutex.RLock()
792802
defer c.mutex.RUnlock()
793803

794804
binaryDigest, err := makeBinaryDigest(digest)
795805
if err != nil {
796-
return "", "", 0, err
806+
return nil, err
797807
}
798808

799809
for _, layer := range c.layers {
@@ -803,31 +813,34 @@ func (c *layersCache) findDigestInternal(digest string) (string, string, int64,
803813
found, off, tagLen := findBinaryTag(binaryDigest, layer.cacheFile)
804814
if found {
805815
if uint64(len(layer.cacheFile.vdata)) < off+tagLen {
806-
return "", "", 0, fmt.Errorf("corrupted cache file for layer %q", layer.id)
816+
return nil, fmt.Errorf("corrupted cache file for layer %q", layer.id)
807817
}
808818
fileLocationData := layer.cacheFile.vdata[off : off+tagLen]
809819

810820
fnamePosition, offFile, _, err := parseFileLocation(fileLocationData)
811821
if err != nil {
812-
return "", "", 0, fmt.Errorf("corrupted cache file for layer %q", layer.id)
822+
return nil, fmt.Errorf("corrupted cache file for layer %q", layer.id)
813823
}
814824

815825
if len(layer.cacheFile.fnames) < fnamePosition+4 {
816-
return "", "", 0, fmt.Errorf("corrupted cache file for layer %q", layer.id)
826+
return nil, fmt.Errorf("corrupted cache file for layer %q", layer.id)
817827
}
818828
lenPath := int(binary.LittleEndian.Uint32(layer.cacheFile.fnames[fnamePosition : fnamePosition+4]))
819829

820830
if len(layer.cacheFile.fnames) < fnamePosition+lenPath+4 {
821-
return "", "", 0, fmt.Errorf("corrupted cache file for layer %q", layer.id)
831+
return nil, fmt.Errorf("corrupted cache file for layer %q", layer.id)
822832
}
823833
path := string(layer.cacheFile.fnames[fnamePosition+4 : fnamePosition+lenPath+4])
824834

825-
// parts[1] is the chunk length, currently unused.
826-
return layer.target, path, int64(offFile), nil
835+
return &digestLookupResult{
836+
Target: layer.target,
837+
Path: path,
838+
Offset: int64(offFile),
839+
}, nil
827840
}
828841
}
829842

830-
return "", "", -1, nil
843+
return nil, nil
831844
}
832845

833846
// findFileInOtherLayers finds the specified file in other layers.
@@ -841,15 +854,25 @@ func (c *layersCache) findFileInOtherLayers(file *fileMetadata, useHardLinks boo
841854
return "", "", err
842855
}
843856
}
844-
target, name, off, err := c.findDigestInternal(digest)
845-
if off == 0 {
846-
return target, name, err
857+
result, err := c.findDigestInternal(digest)
858+
if err != nil {
859+
return "", "", err
847860
}
848-
return "", "", nil
861+
if result == nil || result.Offset != 0 {
862+
return "", "", nil
863+
}
864+
return result.Target, result.Path, nil
849865
}
850866

851867
func (c *layersCache) findChunkInOtherLayers(chunk *minimal.FileMetadata) (string, string, int64, error) {
852-
return c.findDigestInternal(chunk.ChunkDigest)
868+
result, err := c.findDigestInternal(chunk.ChunkDigest)
869+
if err != nil {
870+
return "", "", -1, err
871+
}
872+
if result == nil {
873+
return "", "", -1, nil
874+
}
875+
return result.Target, result.Path, result.Offset, nil
853876
}
854877

855878
func unmarshalToc(manifest []byte) (*minimal.TOC, error) {

0 commit comments

Comments
 (0)