Skip to content

Commit ebf12db

Browse files
author
Vikram bir Singh
committed
Reimplement iteration over fileInfos in getOrphan.
1. Reduce complexity due to nested if blocks by using early return/continue 2. Improve logging Changes suggested as a part of code review comments in 39748 Signed-off-by: Vikram bir Singh <vikrambir.singh@docker.com>
1 parent f505abb commit ebf12db

1 file changed

Lines changed: 31 additions & 23 deletions

File tree

layer/filestore.go

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
"github.com/docker/distribution"
1616
"github.com/docker/docker/pkg/ioutils"
17-
"github.com/opencontainers/go-digest"
17+
digest "github.com/opencontainers/go-digest"
1818
"github.com/pkg/errors"
1919
"github.com/sirupsen/logrus"
2020
)
@@ -317,32 +317,40 @@ func (fms *fileMetadataStore) getOrphan() ([]roLayer, error) {
317317
}
318318

319319
for _, fi := range fileInfos {
320-
if fi.IsDir() && strings.Contains(fi.Name(), "-removing") {
321-
nameSplit := strings.Split(fi.Name(), "-")
322-
dgst := digest.NewDigestFromEncoded(algorithm, nameSplit[0])
323-
if err := dgst.Validate(); err != nil {
324-
logrus.Debugf("Ignoring invalid digest %s:%s", algorithm, nameSplit[0])
325-
} else {
326-
chainID := ChainID(dgst)
327-
chainFile := filepath.Join(fms.root, string(algorithm), fi.Name(), "cache-id")
328-
contentBytes, err := ioutil.ReadFile(chainFile)
329-
if err != nil {
330-
logrus.WithError(err).WithField("digest", dgst).Error("cannot get cache ID")
331-
}
332-
cacheID := strings.TrimSpace(string(contentBytes))
333-
if cacheID == "" {
334-
logrus.Errorf("invalid cache id value")
335-
}
336-
337-
l := &roLayer{
338-
chainID: chainID,
339-
cacheID: cacheID,
340-
}
341-
orphanLayers = append(orphanLayers, *l)
320+
if !fi.IsDir() || !strings.HasSuffix(fi.Name(), "-removing") {
321+
continue
322+
}
323+
// At this stage, fi.Name value looks like <digest>-<random>-removing
324+
// Split on '-' to get the digest value.
325+
nameSplit := strings.Split(fi.Name(), "-")
326+
dgst := digest.NewDigestFromEncoded(algorithm, nameSplit[0])
327+
if err := dgst.Validate(); err != nil {
328+
logrus.WithError(err).WithField("digest", string(algorithm)+":"+nameSplit[0]).Debug("ignoring invalid digest")
329+
continue
330+
}
331+
332+
chainFile := filepath.Join(fms.root, string(algorithm), fi.Name(), "cache-id")
333+
contentBytes, err := ioutil.ReadFile(chainFile)
334+
if err != nil {
335+
if !os.IsNotExist(err) {
336+
logrus.WithError(err).WithField("digest", dgst).Error("failed to read cache ID")
342337
}
338+
continue
339+
}
340+
cacheID := strings.TrimSpace(string(contentBytes))
341+
if cacheID == "" {
342+
logrus.Error("invalid cache ID")
343+
continue
343344
}
345+
346+
l := &roLayer{
347+
chainID: ChainID(dgst),
348+
cacheID: cacheID,
349+
}
350+
orphanLayers = append(orphanLayers, *l)
344351
}
345352
}
353+
346354
return orphanLayers, nil
347355
}
348356

0 commit comments

Comments
 (0)