-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfedorasource.go
More file actions
372 lines (310 loc) · 12 KB
/
fedorasource.go
File metadata and controls
372 lines (310 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//go:generate go tool -modfile=../../../../tools/mockgen/go.mod mockgen -source=fedorasource.go -destination=fedorasource_test/fedorasource_mocks.go -package=fedorasource_test --copyright_file=../../../../.license-preamble
package fedorasource
import (
"context"
"errors"
"fmt"
"log/slog"
"net/url"
"path/filepath"
"regexp"
"strings"
"github.com/microsoft/azure-linux-dev-tools/internal/global/opctx"
"github.com/microsoft/azure-linux-dev-tools/internal/utils/downloader"
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils"
"github.com/microsoft/azure-linux-dev-tools/internal/utils/retry"
)
type FedoraSourceDownloader interface {
// ExtractSourcesFromRepo processes a git repository by downloading any required
// lookaside cache files into the repository directory. Files whose names appear
// in skipFilenames are not downloaded (e.g., files already fetched separately).
ExtractSourcesFromRepo(
ctx context.Context, repoDir string, packageName string,
lookasideBaseURI string, skipFilenames []string,
) error
}
// FedoraSourceDownloaderImpl is an implementation of GitRepoExtractor.
type FedoraSourceDownloaderImpl struct {
dryRunnable opctx.DryRunnable
fileSystem opctx.FS
downloader downloader.Downloader
retryConfig retry.Config
}
// Ensure [FedoraSourceDownloaderImpl] implements [FedoraSourceDownloader].
var _ FedoraSourceDownloader = (*FedoraSourceDownloaderImpl)(nil)
// sourcesFilePattern matches lines in the Fedora/RHEL 'sources' file format.
// Example line: "SHA512 (example-1.0.tar.gz) = a1b2c3d4e5f6..."
// Capture groups:
//
// 1: Hash algorithm (e.g., "SHA512")
// 2: Filename (e.g., "example-1.0.tar.gz")
// 3: Hash value (e.g., "a1b2c3d4e5f6...")
var sourcesFilePattern = regexp.MustCompile(`^([A-Z0-9]+)\s+\(([^)]+)\)\s+=\s+([a-fA-F0-9]+)$`)
// sourcesFileLegacyPattern matches lines in the legacy sources file format.
// This is the older format used by some packages, typically with MD5 hashes.
// Example line: "7b74551e63f8ee6aab6fbc86676c0d37 zip30.tar.gz"
// Capture groups:
//
// 1: Hash value (e.g., "7b74551e63f8ee6aab6fbc86676c0d37")
// 2: Filename (e.g., "zip30.tar.gz")
var sourcesFileLegacyPattern = regexp.MustCompile(`^([a-fA-F0-9]+)\s+(\S+)$`)
// Capture group indexes for sourcesFilePattern.
const (
sourcesPatternHashTypeIndex = 1
sourcesPatternFilenameIndex = 2
sourcesPatternHashValueIndex = 3
)
// Capture group indexes for sourcesFileLegacyPattern.
const (
sourcesLegacyPatternHashValueIndex = 1
sourcesLegacyPatternFilenameIndex = 2
)
// sourceFileInfo contains metadata about a source file to be downloaded.
type sourceFileInfo struct {
fileName string
uri string
hashType fileutils.HashType
expectedHash string
}
// NewFedoraRepoExtractorImpl creates a new instance of FedoraRepoExtractorImpl
// with the provided downloader.
func NewFedoraRepoExtractorImpl(
dryRunnable opctx.DryRunnable,
fileSystem opctx.FS,
downloader downloader.Downloader,
retryCfg retry.Config,
) (*FedoraSourceDownloaderImpl, error) {
if fileSystem == nil {
return nil, errors.New("filesystem cannot be nil")
}
if downloader == nil {
return nil, errors.New("downloader cannot be nil")
}
if dryRunnable == nil {
return nil, errors.New("dry runnable cannot be nil")
}
return &FedoraSourceDownloaderImpl{
dryRunnable: dryRunnable,
fileSystem: fileSystem,
downloader: downloader,
retryConfig: retryCfg,
}, nil
}
// ExtractSourcesFromRepo processes the git repository by downloading any required
// lookaside cache files into the repository directory.
func (g *FedoraSourceDownloaderImpl) ExtractSourcesFromRepo(
ctx context.Context, repoDir string, packageName string, lookasideBaseURI string, skipFileNames []string,
) error {
if repoDir == "" {
return errors.New("repository directory cannot be empty")
}
if lookasideBaseURI == "" {
return errors.New("lookaside base URI cannot be empty")
}
repoDirExists, err := fileutils.Exists(g.fileSystem, repoDir)
if err != nil {
return fmt.Errorf("failed to check if repository directory exists at %#q:\n%w", repoDir, err)
}
if !repoDirExists {
return fmt.Errorf("repository directory does not exist at %#q, cloning failed", repoDir)
}
sourcesFilePath := filepath.Join(repoDir, "sources")
sourcesExists, err := fileutils.Exists(g.fileSystem, sourcesFilePath)
if err != nil {
return fmt.Errorf("failed to check if sources file exists at %#q:\n%w", sourcesFilePath, err)
}
// If the sources file does not exist, there are no external sources to download
if !sourcesExists {
return nil
}
sourcesContent, err := fileutils.ReadFile(g.fileSystem, sourcesFilePath)
if err != nil {
return fmt.Errorf("failed to read sources file at %#q:\n%w", sourcesFilePath, err)
}
sourceFiles, err := parseSourcesFile(string(sourcesContent), packageName, lookasideBaseURI)
if err != nil {
return fmt.Errorf("failed to parse sources file at %#q:\n%w", sourcesFilePath, err)
}
skipSet := make(map[string]bool, len(skipFileNames))
for _, name := range skipFileNames {
skipSet[name] = true
}
err = g.downloadAndVerifySources(ctx, sourceFiles, repoDir, skipSet)
if err != nil {
return fmt.Errorf("failed to download sources:\n%w", err)
}
return nil
}
func (g *FedoraSourceDownloaderImpl) downloadAndVerifySources(
ctx context.Context,
sourceFiles []sourceFileInfo,
repoDir string,
skipSet map[string]bool,
) error {
sourcesTotal := len(sourceFiles)
for sourceIndex, sourceFile := range sourceFiles {
if skipSet[sourceFile.fileName] {
slog.Debug("File already provided, skipping lookaside download",
"fileName", sourceFile.fileName)
continue
}
destFilePath := filepath.Join(repoDir, sourceFile.fileName)
exists, err := fileutils.Exists(g.fileSystem, destFilePath)
if err != nil {
return fmt.Errorf("failed to check if file exists at %#q:\n%w", destFilePath, err)
}
if exists {
slog.Debug("File already exists, skipping download", "fileName", sourceFile.fileName, "path", destFilePath)
continue
}
slog.Info("Downloading source file...",
"progress", fmt.Sprintf("%d/%d", sourceIndex+1, sourcesTotal),
"fileName", sourceFile.fileName,
"URI", sourceFile.uri,
"destPath", destFilePath,
)
if err := retry.Do(ctx, g.retryConfig, func() error {
// Remove any partially written file from a prior failed attempt.
_ = g.fileSystem.Remove(destFilePath)
if downloadErr := g.downloader.Download(ctx, sourceFile.uri, destFilePath); downloadErr != nil {
return fmt.Errorf("failed to download from %#q to %#q:\n%w",
sourceFile.uri, destFilePath, downloadErr)
}
if hashErr := g.validateDownloadedFile(destFilePath, sourceFile); hashErr != nil {
return fmt.Errorf("hash validation failed for %#q:\n%w", sourceFile.fileName, hashErr)
}
return nil
}); err != nil {
return fmt.Errorf("failed to retrieve source file %#q:\n%w", sourceFile.fileName, err)
}
}
return nil
}
func (g *FedoraSourceDownloaderImpl) validateDownloadedFile(
filePath string,
sourceFile sourceFileInfo,
) error {
if err := fileutils.ValidateFileHash(
g.dryRunnable,
g.fileSystem,
sourceFile.hashType,
filePath,
sourceFile.expectedHash,
); err != nil {
return fmt.Errorf("failed to validate file hash:\n%w", err)
}
return nil
}
// parseSourcesFile parses the content of a Fedora/RHEL sources file and returns
// the list of source files to download. It supports both the modern format
// (e.g., "SHA512 (file.tar.gz) = abc123...") and the legacy MD5 format
// (e.g., "abc123... file.tar.gz").
func parseSourcesFile(content string, packageName string, lookasideBaseURI string) ([]sourceFileInfo, error) {
sourceFiles := []sourceFileInfo{}
// Parse and validate each line in the sources file
lines := strings.Split(content, "\n")
for lineNum, line := range lines {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
var (
hashType string
fileName string
hash string
)
// Try modern format first
matches := sourcesFilePattern.FindStringSubmatch(line)
if matches != nil {
hashType = matches[sourcesPatternHashTypeIndex]
fileName = matches[sourcesPatternFilenameIndex]
hash = matches[sourcesPatternHashValueIndex]
} else {
// Try legacy format before failing
legacyMatches := sourcesFileLegacyPattern.FindStringSubmatch(line)
if legacyMatches == nil {
return nil, fmt.Errorf("invalid format in sources file at line %d: %#q", lineNum+1, line)
}
hash = legacyMatches[sourcesLegacyPatternHashValueIndex]
fileName = legacyMatches[sourcesLegacyPatternFilenameIndex]
// Legacy format historically only used MD5
hashType = "MD5"
}
sourceURI, err := BuildLookasideURL(lookasideBaseURI, packageName, fileName, hashType, hash)
if err != nil {
return nil, fmt.Errorf("failed to build lookaside URL at line %d:\n%w", lineNum+1, err)
}
sourceFiles = append(sourceFiles, sourceFileInfo{
fileName: fileName,
uri: sourceURI,
hashType: fileutils.HashType(hashType),
expectedHash: hash,
})
}
return sourceFiles, nil
}
// Lookaside URI template placeholders supported by [BuildLookasideURL].
const (
// PlaceholderPkg is replaced with the package name.
PlaceholderPkg = "$pkg"
// PlaceholderFilename is replaced with the source file name.
PlaceholderFilename = "$filename"
// PlaceholderHashType is replaced with the lowercase hash algorithm (e.g., "sha512").
PlaceholderHashType = "$hashtype"
// PlaceholderHash is replaced with the hash value.
PlaceholderHash = "$hash"
)
// BuildLookasideURL constructs a lookaside cache URL by substituting placeholders in the
// URI template with the provided values. Supported placeholders are [PlaceholderPkg],
// [PlaceholderFilename], [PlaceholderHashType], and [PlaceholderHash].
// Placeholders not present in the template are simply ignored.
//
// Substituted values are URL path-escaped via [url.PathEscape] so that reserved
// characters such as /, ?, #, and % do not alter the URL structure.
//
// Returns an error if any of the provided values contain a placeholder string, as this
// would cause ambiguous substitution results depending on replacement order, or if the
// resulting URL is not valid.
func BuildLookasideURL(template, packageName, fileName, hashType, hash string) (string, error) {
// allPlaceholders lists all supported lookaside URI template placeholders.
allPlaceholders := []string{PlaceholderPkg, PlaceholderFilename, PlaceholderHashType, PlaceholderHash}
for _, v := range []string{packageName, fileName, hashType, hash} {
for _, p := range allPlaceholders {
if strings.Contains(v, p) {
return "", fmt.Errorf("value %#q contains placeholder %s, which would cause ambiguous substitution", v, p)
}
}
}
uri := template
uri = strings.ReplaceAll(uri, PlaceholderPkg, url.PathEscape(packageName))
uri = strings.ReplaceAll(uri, PlaceholderFilename, url.PathEscape(fileName))
uri = strings.ReplaceAll(uri, PlaceholderHashType, url.PathEscape(strings.ToLower(hashType)))
uri = strings.ReplaceAll(uri, PlaceholderHash, url.PathEscape(hash))
_, err := url.Parse(uri)
if err != nil {
return "", fmt.Errorf("resulting lookaside URL is not valid:\n%w", err)
}
return uri, nil
}
// BuildDistGitURL constructs a dist-git repository URL by substituting the
// [PlaceholderPkg] placeholder in the URI template with the provided package name.
//
// The package name is URL path-escaped via [url.PathEscape] so that reserved
// characters such as /, ?, #, and % do not alter the URL structure.
//
// Returns an error if the package name contains a placeholder string, or if the
// resulting URL is not valid.
func BuildDistGitURL(template, packageName string) (string, error) {
if strings.Contains(packageName, PlaceholderPkg) {
return "", fmt.Errorf("package name %#q contains placeholder %s, which would cause ambiguous substitution",
packageName, PlaceholderPkg)
}
uri := strings.ReplaceAll(template, PlaceholderPkg, url.PathEscape(packageName))
_, err := url.Parse(uri)
if err != nil {
return "", fmt.Errorf("resulting dist-git URL is not valid:\n%w", err)
}
return uri, nil
}