mirror of
https://github.com/EnumeratedDev/bpm.git
synced 2026-09-16 10:36:12 +00:00
Add basic compilation functionality
This commit is contained in:
@@ -5,6 +5,8 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type tarballFileReader struct {
|
||||
@@ -41,3 +43,65 @@ func readTarballContent(tarballPath, fileToExtract string) (*tarballFileReader,
|
||||
|
||||
return nil, errors.New("could not file in tarball")
|
||||
}
|
||||
|
||||
func extractTarballDirectory(tarballPath, directoryToExtract, workingDirectory string) (err error) {
|
||||
file, err := os.Open(tarballPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
tr := tar.NewReader(file)
|
||||
for {
|
||||
header, err := tr.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if strings.HasPrefix(header.Name, directoryToExtract+"/") {
|
||||
// Skip directory to extract
|
||||
if strings.TrimRight(header.Name, "/") == workingDirectory {
|
||||
continue
|
||||
}
|
||||
|
||||
// Trim directory name from header name
|
||||
header.Name = strings.TrimPrefix(header.Name, directoryToExtract+"/")
|
||||
outputPath := path.Join(workingDirectory, header.Name)
|
||||
|
||||
switch header.Typeflag {
|
||||
case tar.TypeDir:
|
||||
// Create directory
|
||||
err := os.MkdirAll(outputPath, 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case tar.TypeReg:
|
||||
// Create file and set permissions
|
||||
file, err = os.Create(outputPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err := file.Chmod(header.FileInfo().Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Copy data to file
|
||||
_, err = io.Copy(file, tr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Close file
|
||||
file.Close()
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user