Skip to content

Commit

Permalink
fix: better management of archive extraction (close #56)
Browse files Browse the repository at this point in the history
  • Loading branch information
M0Rf30 committed Mar 16, 2024
1 parent a5bccb2 commit 5985df9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 52 deletions.
4 changes: 3 additions & 1 deletion pkg/dpkg/dpkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ func (d *Deb) PrepareEnvironment(golang bool) error {
}

if golang {
return utils.GOSetup()
if err := utils.GOSetup(); err != nil {
return err
}
}

return nil
Expand Down
4 changes: 3 additions & 1 deletion pkg/rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ func (r *RPM) PrepareEnvironment(golang bool) error {
}

if golang {
return utils.GOSetup()
if err := utils.GOSetup(); err != nil {
return err
}
}

return nil
Expand Down
22 changes: 1 addition & 21 deletions pkg/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,13 @@ func (src *Source) Get() error {
return err
}

if err := src.extract(sourceFilePath); err != nil {
if err := utils.Unarchive(sourceFilePath, src.SrcDir); err != nil {
return err
}

return nil
}

// extract extracts the contents of a source file to the specified source
// directory.
//
// sourceFilePath: The path to the source file to be extracted. Returns: An
// error if there was a problem opening the source file or extracting its
// contents.
func (src *Source) extract(sourceFilePath string) error {
dlFile, err := utils.Open(filepath.Clean(sourceFilePath))
if err != nil {
return err
}

err = utils.Unarchive(dlFile, src.SrcDir)
if err != nil {
return fmt.Errorf("failed to extract source %s: %w", src.SourceItemPath, err)
}

return nil
}

// getReferenceType returns the reference type for the given source.
//
// It takes no parameters.
Expand Down
52 changes: 23 additions & 29 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,7 @@ func GOSetup() error {
log.Panic(err)
}

dlFile, err := Open(goArchivePath)
if err != nil {
return err
}

err = Unarchive(dlFile, "/usr/lib")
err = Unarchive(goArchivePath, "/usr/lib")
if err != nil {
return err
}
Expand Down Expand Up @@ -265,35 +260,39 @@ func RunScript(cmds string) error {
return runner.Run(context.TODO(), script)
}

// Unarchive extracts files from an archive and saves them to a destination directory.
// Unarchive is a function that takes a source file and a destination. It opens
// the source archive file, identifies its format, and extracts it to the
// destination.
//
// archiveReader is an io.Reader that represents the archive file.
// destination is the path to the directory where the files will be saved.
// Returns an error if there was a problem extracting the files.
func Unarchive(archiveReader io.Reader, destination string) error {
format, archiveReader, _ := archiver.Identify("", archiveReader)
func Unarchive(source, destination string) error {
// Open the source archive file
archiveFile, err := Open(source)
if err != nil {
return err
}

// Identify the archive file's format
format, archiveReader, err := archiver.Identify("", archiveFile)
if err != nil {
return err
}

dirMap := make(map[string]bool)
// Check if the format is an extractor. If not, skip the archive file.
extractor, ok := format.(archiver.Extractor)

if !ok {
return nil
}

handler := func(_ context.Context, archiveFile archiver.File) error {
fileName := archiveFile.NameInArchive
newPath := filepath.Join(destination, fileName)

if archiveFile.IsDir() {
dirMap[newPath] = true

return os.MkdirAll(newPath, archiveFile.Mode())
}

fileDir := filepath.Dir(newPath)
_, seenDir := dirMap[fileDir]

if !seenDir {
dirMap[fileDir] = true
// #nosec
return os.MkdirAll(fileDir, 0777)
}

cleanNewPath := filepath.Clean(newPath)

newFile, err := os.OpenFile(cleanNewPath,
Expand All @@ -315,12 +314,7 @@ func Unarchive(archiveReader io.Reader, destination string) error {
return err
}

ex, ok := format.(archiver.Extractor)
if !ok {
return nil
}

return ex.Extract(context.Background(),
return extractor.Extract(context.Background(),
archiveReader,
nil,
handler)
Expand Down

0 comments on commit 5985df9

Please sign in to comment.