github.com/quay/claircore@v1.5.28/whiteout/scanner.go (about)

     1  package whiteout
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io/fs"
     7  	"strings"
     8  
     9  	"github.com/quay/zlog"
    10  
    11  	"github.com/quay/claircore"
    12  	"github.com/quay/claircore/indexer"
    13  )
    14  
    15  const (
    16  	scannerName    = "whiteout"
    17  	scannerVersion = "1"
    18  	scannerKind    = "file"
    19  )
    20  
    21  var (
    22  	_ indexer.FileScanner      = (*Scanner)(nil)
    23  	_ indexer.VersionedScanner = (*Scanner)(nil)
    24  )
    25  
    26  type Scanner struct{}
    27  
    28  func (*Scanner) Name() string { return scannerName }
    29  
    30  func (*Scanner) Version() string { return scannerVersion }
    31  
    32  func (*Scanner) Kind() string { return scannerKind }
    33  
    34  func (s *Scanner) Scan(ctx context.Context, l *claircore.Layer) ([]claircore.File, error) {
    35  	ctx = zlog.ContextWithValues(ctx,
    36  		"component", "whiteout/Scanner.Scan",
    37  		"version", s.Version(),
    38  		"layer", l.Hash.String())
    39  	zlog.Debug(ctx).Msg("start")
    40  	defer zlog.Debug(ctx).Msg("done")
    41  	sys, err := l.FS()
    42  	if err != nil {
    43  		return nil, fmt.Errorf("whiteout: unable to create fs: %w", err)
    44  	}
    45  	wofs := []claircore.File{}
    46  	err = fs.WalkDir(sys, ".", func(path string, d fs.DirEntry, err error) error {
    47  		if err != nil {
    48  			return err
    49  		}
    50  		if strings.HasPrefix(d.Name(), ".wh.") {
    51  			cf := claircore.File{
    52  				Path: path,
    53  				Kind: claircore.FileKindWhiteout,
    54  			}
    55  
    56  			wofs = append(wofs, cf)
    57  		}
    58  		return nil
    59  	})
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	return wofs, nil
    64  }