|
| 1 | +// Copyright 2025 Interlynk.io |
| 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 | + |
| 16 | +package folder |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "log" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + |
| 24 | + "github.com/fsnotify/fsnotify" |
| 25 | + "github.com/interlynk-io/sbommv/pkg/iterator" |
| 26 | + "github.com/interlynk-io/sbommv/pkg/logger" |
| 27 | + "github.com/interlynk-io/sbommv/pkg/sbom" |
| 28 | + "github.com/interlynk-io/sbommv/pkg/source" |
| 29 | + "github.com/interlynk-io/sbommv/pkg/tcontext" |
| 30 | +) |
| 31 | + |
| 32 | +type WatcherFetcher struct{} |
| 33 | + |
| 34 | +func NewWatcherFetcher() *WatcherFetcher { |
| 35 | + return &WatcherFetcher{} |
| 36 | +} |
| 37 | + |
| 38 | +func (f *WatcherFetcher) Fetch(ctx *tcontext.TransferMetadata, config *FolderConfig) (iterator.SBOMIterator, error) { |
| 39 | + logger.LogDebug(ctx.Context, "Starting folder watcher", "path", config.FolderPath, "recurssive", config.ProcessingMode) |
| 40 | + |
| 41 | + // Create new watcher. |
| 42 | + watcher, err := fsnotify.NewWatcher() |
| 43 | + if err != nil { |
| 44 | + return nil, fmt.Errorf("failed to create watcher: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + sbomChan := make(chan *iterator.SBOM, 10) |
| 48 | + |
| 49 | + // add to watch more sub-directories if recurssive is true |
| 50 | + err = filepath.Walk(config.FolderPath, func(path string, info os.FileInfo, err error) error { |
| 51 | + if err != nil { |
| 52 | + logger.LogError(ctx.Context, err, "Error accessing path", "path", path) |
| 53 | + return nil |
| 54 | + } |
| 55 | + if info.IsDir() { |
| 56 | + if !config.Recursive && path != config.FolderPath { |
| 57 | + return filepath.SkipDir |
| 58 | + } |
| 59 | + |
| 60 | + // add it to the watcher |
| 61 | + if err := watcher.Add(path); err != nil { |
| 62 | + logger.LogError(ctx.Context, err, "Failed to watch directory", "path", path) |
| 63 | + } else { |
| 64 | + logger.LogDebug(ctx.Context, "Watching directory", "path", path) |
| 65 | + } |
| 66 | + } |
| 67 | + return nil |
| 68 | + }) |
| 69 | + if err != nil { |
| 70 | + watcher.Close() |
| 71 | + return nil, fmt.Errorf("failed to walk directory: %w", err) |
| 72 | + } |
| 73 | + |
| 74 | + // Start listening for events. |
| 75 | + go func() { |
| 76 | + defer watcher.Close() |
| 77 | + for { |
| 78 | + select { |
| 79 | + case event, ok := <-watcher.Events: |
| 80 | + if !ok { |
| 81 | + close(sbomChan) |
| 82 | + return |
| 83 | + } |
| 84 | + log.Println("event:", event) |
| 85 | + // if event.Has(fsnotify.Write) { |
| 86 | + // log.Println("modified file:", event.Name) |
| 87 | + // } |
| 88 | + |
| 89 | + if event.Op&(fsnotify.Create|fsnotify.Write) != 0 && source.IsSBOMFile(event.Name) { |
| 90 | + content, err := os.ReadFile(event.Name) |
| 91 | + if err != nil { |
| 92 | + logger.LogError(ctx.Context, err, "Failed to read SBOM", "path", event.Name) |
| 93 | + continue |
| 94 | + } |
| 95 | + |
| 96 | + primaryComp, err := sbom.ExtractPrimaryComponentName(content) |
| 97 | + if err != nil { |
| 98 | + logger.LogDebug(ctx.Context, "Failed to parse SBOM for primary component", "path", event.Name, "error", err) |
| 99 | + } |
| 100 | + |
| 101 | + fileName := getFilePath(config.FolderPath, event.Name) |
| 102 | + logger.LogDebug(ctx.Context, "Detected SBOM", "file", fileName) |
| 103 | + sbomChan <- &iterator.SBOM{ |
| 104 | + Data: content, |
| 105 | + Path: fileName, |
| 106 | + Namespace: primaryComp, |
| 107 | + } |
| 108 | + } |
| 109 | + case err, ok := <-watcher.Errors: |
| 110 | + if !ok { |
| 111 | + close(sbomChan) |
| 112 | + return |
| 113 | + } |
| 114 | + logger.LogError(ctx.Context, err, "Watcher error") |
| 115 | + |
| 116 | + case <-ctx.Done(): |
| 117 | + close(sbomChan) |
| 118 | + return |
| 119 | + } |
| 120 | + } |
| 121 | + }() |
| 122 | + |
| 123 | + return &WatcherIterator{sbomChan: sbomChan}, nil |
| 124 | +} |
| 125 | + |
| 126 | +// type WatcherIterator struct { |
| 127 | +// sbomChan chan *iterator.SBOM |
| 128 | +// } |
| 129 | + |
| 130 | +// func (it *WatcherIterator) Next(ctx context.Context) (*iterator.SBOM, error) { |
| 131 | +// select { |
| 132 | +// case sbom, ok := <-it.sbomChan: |
| 133 | +// if !ok { |
| 134 | +// return nil, fmt.Errorf("watcher channel closed") |
| 135 | +// } |
| 136 | +// return sbom, nil |
| 137 | +// case <-ctx.Done(): |
| 138 | +// return nil, ctx.Err() |
| 139 | +// } |
| 140 | +// } |
0 commit comments