Skip to content

Commit

Permalink
Merge branch 'main' into extractor_objectivec
Browse files Browse the repository at this point in the history
  • Loading branch information
Octaviusss authored Jan 17, 2025
2 parents ca86884 + a47afa5 commit 3fd24bd
Show file tree
Hide file tree
Showing 9 changed files with 510 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/supported_inventory_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ SCALIBR supports extracting software package information from a variety of OS an
* Lockfiles: Gemfile.lock (OSV)
* Rust
* Cargo.lock
* Swift
* Podfile.lock

## Container inventory

Expand Down
156 changes: 156 additions & 0 deletions extractor/filesystem/language/swift/podfilelock/podfilelock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package podfilelock extracts dependencies from Podfile.lock files.
package podfilelock

import (
"context"
"path/filepath"

"github.com/google/osv-scalibr/extractor"
"github.com/google/osv-scalibr/extractor/filesystem"
"github.com/google/osv-scalibr/extractor/filesystem/internal/units"
"github.com/google/osv-scalibr/extractor/filesystem/language/swift/swiftutils"
"github.com/google/osv-scalibr/plugin"
"github.com/google/osv-scalibr/purl"
"github.com/google/osv-scalibr/stats"
)

// Name is the unique name of this extractor.
const Name = "swift/podfilelock"

// Config represents the configuration for the extractor.
type Config struct {
Stats stats.Collector
MaxFileSizeBytes int64
}

// DefaultConfig returns the default configuration for the extractor.
func DefaultConfig() Config {
return Config{
Stats: nil,
MaxFileSizeBytes: 10 * units.MiB,
}
}

// Config returns the configuration of the extractor.
func (e Extractor) Config() Config {
return Config{
Stats: e.stats,
MaxFileSizeBytes: e.maxFileSizeBytes,
}
}

// Extractor extracts dependencies from Podfile.lock files.
type Extractor struct {
stats stats.Collector
maxFileSizeBytes int64
}

// New creates a new instance of the Podfile.lock extractor.
func New(cfg Config) *Extractor {
return &Extractor{
stats: cfg.Stats,
maxFileSizeBytes: cfg.MaxFileSizeBytes,
}
}

// Name returns the extractor's name.
func (e Extractor) Name() string { return Name }

// Version returns the extractor's version.
func (e Extractor) Version() int { return 0 }

// Requirements defines the extractor's capabilities.
func (e Extractor) Requirements() *plugin.Capabilities { return &plugin.Capabilities{} }

// FileRequired checks if a file is named Podfile.lock and meets size constraints.
func (e Extractor) FileRequired(api filesystem.FileAPI) bool {
path := api.Path()
if filepath.Base(path) != "Podfile.lock" {
return false
}

fileInfo, err := api.Stat()
if err != nil {
return false
}

if e.maxFileSizeBytes > 0 && fileInfo.Size() > e.maxFileSizeBytes {
e.reportFileRequired(path, fileInfo.Size(), stats.FileRequiredResultSizeLimitExceeded)
return false
}

e.reportFileRequired(path, fileInfo.Size(), stats.FileRequiredResultOK)
return true
}

func (e Extractor) reportFileRequired(path string, fileSizeBytes int64, result stats.FileRequiredResult) {
if e.stats == nil {
return
}
e.stats.AfterFileRequired(e.Name(), &stats.FileRequiredStats{
Path: path,
Result: result,
FileSizeBytes: fileSizeBytes,
})
}

// Extract processes and extracts dependency information from a Podfile.lock file.
func (e Extractor) Extract(ctx context.Context, input *filesystem.ScanInput) ([]*extractor.Inventory, error) {
inventory, err := e.extractFromInput(ctx, input)
if e.stats != nil {
var fileSizeBytes int64
if input.Info != nil {
fileSizeBytes = input.Info.Size()
}
e.stats.AfterFileExtracted(e.Name(), &stats.FileExtractedStats{
Path: input.Path,
Result: filesystem.ExtractorErrorToFileExtractedResult(err),
FileSizeBytes: fileSizeBytes,
})
}
return inventory, err
}

func (e Extractor) extractFromInput(ctx context.Context, input *filesystem.ScanInput) ([]*extractor.Inventory, error) {
pkgs, err := swiftutils.ParsePodfileLock(input.Reader)
if err != nil {
return nil, err
}

var inventories []*extractor.Inventory
for _, pkg := range pkgs {
inventories = append(inventories, &extractor.Inventory{
Name: pkg.Name,
Version: pkg.Version,
Locations: []string{input.Path},
})
}

return inventories, nil
}

// ToPURL converts an inventory item into a Package URL (PURL).
func (e Extractor) ToPURL(i *extractor.Inventory) *purl.PackageURL {
return &purl.PackageURL{
Type: purl.TypeCocoapods,
Name: i.Name,
Version: i.Version,
}
}

// Ecosystem returns the OSV ecosystem name for CocoaPods.
func (Extractor) Ecosystem(_ *extractor.Inventory) string { return "CocoaPods" }
Loading

0 comments on commit 3fd24bd

Please sign in to comment.