archive: replace extract test with call to gzip -t
A custom extract test includes reading AND writing the extracted archive contents to /tmp (which is probably a slow disk), so this uses busybox's gzip -t ("test file integrity") and seems to take about half the time (~10s vs ~6s measured loosely on pinephone)
This commit is contained in:
@@ -11,9 +11,9 @@ import (
|
|||||||
"github.com/klauspost/pgzip"
|
"github.com/klauspost/pgzip"
|
||||||
"gitlab.com/postmarketOS/postmarketos-mkinitfs/pkgs/misc"
|
"gitlab.com/postmarketOS/postmarketos-mkinitfs/pkgs/misc"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -46,22 +46,15 @@ func (archive *Archive) Write(path string, mode os.FileMode) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpExtractDir, err := ioutil.TempDir("", filepath.Base(path))
|
|
||||||
if err != nil {
|
|
||||||
log.Print("Unable to create temporary work dir")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tmpExtractDir)
|
|
||||||
|
|
||||||
// Write archive to path
|
// Write archive to path
|
||||||
if err := archive.writeCompressed(path, mode); err != nil {
|
if err := archive.writeCompressed(path, mode); err != nil {
|
||||||
log.Print("Unable to write archive to location: ", path)
|
log.Print("Unable to write archive to location: ", path)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract the archive to make sure it's valid / can be extracted
|
// test the archive to make sure it's valid
|
||||||
if err := extract(path, tmpExtractDir); err != nil {
|
if err := test(path); err != nil {
|
||||||
log.Print("Extraction verification of archive failed!")
|
log.Print("Verification of archive failed!")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,45 +180,15 @@ func (archive *Archive) AddFile(file string, dest string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func extract(path string, dest string) error {
|
// Use busybox gzip to test archive
|
||||||
tDir, err := ioutil.TempDir("", "archive-extract")
|
func test(path string) error {
|
||||||
if err != nil {
|
cmd := exec.Command("busybox", "gzip", "-t", path)
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
log.Print("'boot-deploy' command failed: ")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(tDir)
|
|
||||||
|
|
||||||
srcFd, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer srcFd.Close()
|
|
||||||
|
|
||||||
// TODO: support more compression types
|
|
||||||
gz, err := pgzip.NewReader(srcFd)
|
|
||||||
|
|
||||||
cpioArchive := cpio.NewReader(gz)
|
|
||||||
for {
|
|
||||||
hdr, err := cpioArchive.Next()
|
|
||||||
if err == io.EOF {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
destPath := filepath.Join(dest, hdr.Name)
|
|
||||||
if hdr.Mode.IsDir() {
|
|
||||||
os.MkdirAll(destPath, 0755)
|
|
||||||
} else {
|
|
||||||
destFd, err := os.Create(destPath)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer destFd.Close()
|
|
||||||
if _, err := io.Copy(destFd, cpioArchive); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user