-
Notifications
You must be signed in to change notification settings - Fork 1.9k
adding archive entry paths #3638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
joeleonjr
wants to merge
13
commits into
main
Choose a base branch
from
archive-entry-paths
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8c4c83b
initial pass at adding archive entry paths
joeleonjr fc528d2
updated to combine archive paths + depth
joeleonjr 6cfae75
Merge branch 'main' into archive-entry-paths
joeleonjr 05fcfe6
Merge branch 'main' into archive-entry-paths
joeleonjr 6c1feea
updated decompressor file naming
joeleonjr 6ec0688
Merge branch 'main' into archive-entry-paths
joeleonjr 704937a
updated test cases
joeleonjr 48d9323
Merge branch 'main' into archive-entry-paths
joeleonjr 8ab76fa
Merge branch 'main' into archive-entry-paths
joeleonjr 4283b1d
Merge branch 'main' into archive-entry-paths
joeleonjr ceefd1e
Merge branch 'main' into archive-entry-paths
joeleonjr 36bdef0
swapped file.Name() for file.NameInArchive
joeleonjr a37a6dd
Merge branch 'archive-entry-paths' of https://github.com/trufflesecur…
joeleonjr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,7 +86,7 @@ func (h *archiveHandler) HandleFile(ctx logContext.Context, input fileReader) ch | |
}() | ||
|
||
start := time.Now() | ||
err := h.openArchive(ctx, 0, input, dataOrErrChan) | ||
err := h.openArchive(ctx, 0, emptyFilePath, input, dataOrErrChan) | ||
if err == nil { | ||
h.metrics.incFilesProcessed() | ||
} | ||
|
@@ -101,12 +101,13 @@ func (h *archiveHandler) HandleFile(ctx logContext.Context, input fileReader) ch | |
var ErrMaxDepthReached = errors.New("max archive depth reached") | ||
|
||
// openArchive recursively extracts content from an archive up to a maximum depth, handling nested archives if necessary. | ||
// It takes a reader from which it attempts to identify and process the archive format. Depending on the archive type, | ||
// it either decompresses or extracts the contents directly, sending data to the provided channel. | ||
// It takes a string representing the path to the archive and a reader from which it attempts to identify and process the archive format. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment reads "a string" but the actual function takes a string slice. Which is correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll fix that. That comment was from a previous version. |
||
// Depending on the archive type, it either decompresses or extracts the contents directly, sending data to the provided channel. | ||
// Returns an error if the archive cannot be processed due to issues like exceeding maximum depth or unsupported formats. | ||
func (h *archiveHandler) openArchive( | ||
ctx logContext.Context, | ||
depth int, | ||
archiveEntryPath string, | ||
joeleonjr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
reader fileReader, | ||
dataOrErrChan chan DataOrErr, | ||
) error { | ||
|
@@ -124,14 +125,14 @@ func (h *archiveHandler) openArchive( | |
|
||
if reader.format == nil { | ||
if depth > 0 { | ||
return h.handleNonArchiveContent(ctx, newMimeTypeReaderFromFileReader(reader), dataOrErrChan) | ||
return h.handleNonArchiveContent(ctx, archiveEntryPath, newMimeTypeReaderFromFileReader(reader), dataOrErrChan) | ||
} | ||
return fmt.Errorf("unknown archive format") | ||
} | ||
|
||
switch archive := reader.format.(type) { | ||
case archiver.Decompressor: | ||
// Decompress tha archive and feed the decompressed data back into the archive handler to extract any nested archives. | ||
// Decompress the archive and feed the decompressed data back into the archive handler to extract any nested archives. | ||
compReader, err := archive.OpenReader(reader) | ||
if err != nil { | ||
return fmt.Errorf("error opening decompressor with format: %s %w", reader.format.Name(), err) | ||
|
@@ -152,9 +153,13 @@ func (h *archiveHandler) openArchive( | |
} | ||
defer rdr.Close() | ||
|
||
return h.openArchive(ctx, depth+1, rdr, dataOrErrChan) | ||
// Note: We're limited in our ability to add file names to the archiveEntryPath here, as the decompressor doesn't have access to a fileName value. Instead, we're adding a generic string to indicate that the file is decompressed. This could be improved. | ||
joeleonjr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if depth > 0 { | ||
archiveEntryPath = archiveEntryPath + "(decompressed " + reader.format.Name() + " file)" | ||
} | ||
return h.openArchive(ctx, depth+1, archiveEntryPath, rdr, dataOrErrChan) | ||
case archiver.Extractor: | ||
err := archive.Extract(logContext.WithValue(ctx, depthKey, depth+1), reader, nil, h.extractorHandler(dataOrErrChan)) | ||
err := archive.Extract(logContext.WithValue(ctx, depthKey, depth+1), reader, nil, h.extractorHandler(archiveEntryPath, dataOrErrChan)) | ||
joeleonjr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return fmt.Errorf("error extracting archive with format: %s: %w", reader.format.Name(), err) | ||
} | ||
|
@@ -168,7 +173,7 @@ func (h *archiveHandler) openArchive( | |
// It logs the extraction, checks for cancellation, and decides whether to skip the file based on its name or type, | ||
// particularly for binary files if configured to skip. If the file is not skipped, it recursively calls openArchive | ||
// to handle nested archives or to continue processing based on the file's content and depth in the archive structure. | ||
func (h *archiveHandler) extractorHandler(dataOrErrChan chan DataOrErr) func(context.Context, archiver.File) error { | ||
func (h *archiveHandler) extractorHandler(archiveEntryPath string, dataOrErrChan chan DataOrErr) func(context.Context, archiver.File) error { | ||
return func(ctx context.Context, file archiver.File) error { | ||
lCtx := logContext.WithValues( | ||
logContext.AddLogger(ctx), | ||
|
@@ -240,6 +245,6 @@ func (h *archiveHandler) extractorHandler(dataOrErrChan chan DataOrErr) func(con | |
h.metrics.observeFileSize(fileSize) | ||
|
||
lCtx.Logger().V(4).Info("Processed file successfully", "filename", file.Name(), "size", file.Size()) | ||
return h.openArchive(lCtx, depth, rdr, dataOrErrChan) | ||
return h.openArchive(lCtx, depth, (archiveEntryPath + "/" + file.Name()), rdr, dataOrErrChan) | ||
joeleonjr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.