github.com/google/osv-scalibr@v0.4.1/artifact/image/require/require.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package require provides an interface for specifying which files we are interested
    16  // in during a container image extraction.
    17  package require
    18  
    19  import "io/fs"
    20  
    21  // FileRequirer determines if a file is required to unpack.
    22  type FileRequirer interface {
    23  	FileRequired(path string, fileinfo fs.FileInfo) bool
    24  }
    25  
    26  // FileRequirerAll requires all files.
    27  type FileRequirerAll struct{}
    28  
    29  // FileRequired always returns true.
    30  func (f *FileRequirerAll) FileRequired(path string, fileinfo fs.FileInfo) bool {
    31  	return true
    32  }
    33  
    34  // FileRequirerNone requires no files.
    35  type FileRequirerNone struct{}
    36  
    37  // FileRequired always returns false.
    38  func (f *FileRequirerNone) FileRequired(path string, fileinfo fs.FileInfo) bool {
    39  	return false
    40  }
    41  
    42  // FileRequirerPaths requires files that match a collection of paths.
    43  type FileRequirerPaths struct {
    44  	required map[string]bool
    45  }
    46  
    47  // NewFileRequirerPaths returns a new FileRequirerPaths.
    48  func NewFileRequirerPaths(required []string) *FileRequirerPaths {
    49  	fr := &FileRequirerPaths{
    50  		required: make(map[string]bool),
    51  	}
    52  	for _, r := range required {
    53  		fr.required[r] = true
    54  	}
    55  	return fr
    56  }
    57  
    58  // FileRequired returns true if the file is required.
    59  func (fr *FileRequirerPaths) FileRequired(path string, fileinfo fs.FileInfo) bool {
    60  	return fr.required[path]
    61  }