github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/language/swift/podfilelock/podfilelock.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 podfilelock extracts dependencies from Podfile.lock files.
    16  package podfilelock
    17  
    18  import (
    19  	"context"
    20  	"path/filepath"
    21  
    22  	"github.com/google/osv-scalibr/extractor"
    23  	"github.com/google/osv-scalibr/extractor/filesystem"
    24  	"github.com/google/osv-scalibr/extractor/filesystem/internal/units"
    25  	"github.com/google/osv-scalibr/extractor/filesystem/language/swift/swiftutils"
    26  	"github.com/google/osv-scalibr/inventory"
    27  	"github.com/google/osv-scalibr/plugin"
    28  	"github.com/google/osv-scalibr/purl"
    29  	"github.com/google/osv-scalibr/stats"
    30  )
    31  
    32  // Name is the unique name of this extractor.
    33  const Name = "swift/podfilelock"
    34  
    35  // Config represents the configuration for the extractor.
    36  type Config struct {
    37  	Stats            stats.Collector
    38  	MaxFileSizeBytes int64
    39  }
    40  
    41  // DefaultConfig returns the default configuration for the extractor.
    42  func DefaultConfig() Config {
    43  	return Config{
    44  		Stats:            nil,
    45  		MaxFileSizeBytes: 10 * units.MiB,
    46  	}
    47  }
    48  
    49  // Config returns the configuration of the extractor.
    50  func (e Extractor) Config() Config {
    51  	return Config{
    52  		Stats:            e.stats,
    53  		MaxFileSizeBytes: e.maxFileSizeBytes,
    54  	}
    55  }
    56  
    57  // Extractor extracts dependencies from Podfile.lock files.
    58  type Extractor struct {
    59  	stats            stats.Collector
    60  	maxFileSizeBytes int64
    61  }
    62  
    63  // New creates a new instance of the Podfile.lock extractor.
    64  func New(cfg Config) *Extractor {
    65  	return &Extractor{
    66  		stats:            cfg.Stats,
    67  		maxFileSizeBytes: cfg.MaxFileSizeBytes,
    68  	}
    69  }
    70  
    71  // NewDefault returns an extractor with the default config settings.
    72  func NewDefault() filesystem.Extractor { return New(DefaultConfig()) }
    73  
    74  // Name returns the extractor's name.
    75  func (e Extractor) Name() string { return Name }
    76  
    77  // Version returns the extractor's version.
    78  func (e Extractor) Version() int { return 0 }
    79  
    80  // Requirements defines the extractor's capabilities.
    81  func (e Extractor) Requirements() *plugin.Capabilities { return &plugin.Capabilities{} }
    82  
    83  // FileRequired checks if a file is named Podfile.lock and meets size constraints.
    84  func (e Extractor) FileRequired(api filesystem.FileAPI) bool {
    85  	path := api.Path()
    86  	if filepath.Base(path) != "Podfile.lock" {
    87  		return false
    88  	}
    89  
    90  	fileInfo, err := api.Stat()
    91  	if err != nil {
    92  		return false
    93  	}
    94  
    95  	if e.maxFileSizeBytes > 0 && fileInfo.Size() > e.maxFileSizeBytes {
    96  		e.reportFileRequired(path, fileInfo.Size(), stats.FileRequiredResultSizeLimitExceeded)
    97  		return false
    98  	}
    99  
   100  	e.reportFileRequired(path, fileInfo.Size(), stats.FileRequiredResultOK)
   101  	return true
   102  }
   103  
   104  func (e Extractor) reportFileRequired(path string, fileSizeBytes int64, result stats.FileRequiredResult) {
   105  	if e.stats == nil {
   106  		return
   107  	}
   108  	e.stats.AfterFileRequired(e.Name(), &stats.FileRequiredStats{
   109  		Path:          path,
   110  		Result:        result,
   111  		FileSizeBytes: fileSizeBytes,
   112  	})
   113  }
   114  
   115  // Extract processes and extracts dependency information from a Podfile.lock file.
   116  func (e Extractor) Extract(ctx context.Context, input *filesystem.ScanInput) (inventory.Inventory, error) {
   117  	pkgs, err := e.extractFromInput(input)
   118  	if e.stats != nil {
   119  		var fileSizeBytes int64
   120  		if input.Info != nil {
   121  			fileSizeBytes = input.Info.Size()
   122  		}
   123  		e.stats.AfterFileExtracted(e.Name(), &stats.FileExtractedStats{
   124  			Path:          input.Path,
   125  			Result:        filesystem.ExtractorErrorToFileExtractedResult(err),
   126  			FileSizeBytes: fileSizeBytes,
   127  		})
   128  	}
   129  	return inventory.Inventory{Packages: pkgs}, err
   130  }
   131  
   132  func (e Extractor) extractFromInput(input *filesystem.ScanInput) ([]*extractor.Package, error) {
   133  	packages, err := swiftutils.ParsePodfileLock(input.Reader)
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  
   138  	var result []*extractor.Package
   139  	for _, pkg := range packages {
   140  		result = append(result, &extractor.Package{
   141  			Name:      pkg.Name,
   142  			Version:   pkg.Version,
   143  			PURLType:  purl.TypeCocoapods,
   144  			Locations: []string{input.Path},
   145  		})
   146  	}
   147  
   148  	return result, nil
   149  }