Commit be05aba4 authored by Amos Wenger's avatar Amos Wenger

Introduce lilboar, be a tiny bit less verbose, TryOpenParams => ProbeParams

parent 8c95c8a7
Pipeline #10348 passed with stage
in 2 minutes and 4 seconds
package main
import (
"log"
"os"
"path/filepath"
"github.com/itchio/boar"
"github.com/itchio/wharf/eos"
"github.com/itchio/wharf/state"
)
func main() {
args := os.Args[1:]
if len(args) < 1 {
log.Fatal("Usage: lilboar FILE [...FILE]")
}
consumer := &state.Consumer{
OnMessage: func(lvl string, msg string) {
log.Printf("[%s] %s", lvl, msg)
},
}
ignoreErrors := len(args) > 1
errorf := func(msg string, args ...interface{}) {
if ignoreErrors {
return
}
consumer.Errorf(msg, args...)
}
doFile := func(filePath string) {
file, err := eos.Open(filePath)
if err != nil {
errorf("%v", err)
return
}
defer file.Close()
info, err := boar.Probe(&boar.ProbeParams{
File: file,
Consumer: consumer,
})
if err != nil {
errorf("%v", err)
return
}
consumer.Infof("%s: %s", filepath.Base(filePath), info)
}
for _, arg := range args {
doFile(arg)
}
}
......@@ -67,7 +67,18 @@ type ArchiveInfo struct {
PostExtract []string
}
func Probe(params *TryOpenParams) (*ArchiveInfo, error) {
func (ai *ArchiveInfo) String() string {
res := ""
res += fmt.Sprintf("Format: %s", ai.Format)
res += fmt.Sprintf(", Features: %s", ai.Features)
if ai.StageTwoStrategy != StageTwoStrategyNone {
res += fmt.Sprintf(", StageTwoStrategy: %s", ai.StageTwoStrategy)
res += fmt.Sprintf(", PostExtract: %v", ai.PostExtract)
}
return res
}
func Probe(params *ProbeParams) (*ArchiveInfo, error) {
var strategy ArchiveStrategy
if params.Candidate != nil && params.Candidate.Flavor == dash.FlavorNativeLinux {
......@@ -113,7 +124,6 @@ func Probe(params *TryOpenParams) (*ArchiveInfo, error) {
"scripts/mojosetup_init.luac": StageTwoStrategyMojoSetup,
}
params.Consumer.Debugf("Scanning %d entries for a stage two marker...", len(entries))
for _, e := range entries {
if strat, ok := stageTwoMarkers[e.CanonicalPath]; ok {
stageTwoStrategy = strat
......
......@@ -21,7 +21,7 @@ func SimpleExtract(params *SimpleExtractParams) (*savior.ExtractorResult, error)
}
defer f.Close()
ai, err := Probe(&TryOpenParams{
ai, err := Probe(&ProbeParams{
Consumer: params.Consumer,
File: f,
})
......
......@@ -36,7 +36,7 @@ func (ai *ArchiveInfo) applyMojoSetupStageTwo(consumer *state.Consumer, aRes *sa
}
defer file.Close()
archiveInfo, err := Probe(&TryOpenParams{
archiveInfo, err := Probe(&ProbeParams{
Consumer: consumer,
File: file,
})
......
......@@ -2,6 +2,7 @@ package szextractor
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
......@@ -82,8 +83,10 @@ func New(file eos.File, consumer *state.Consumer) (SzExtractor, error) {
}
se.in = in
var tries []string
// try by extension first
consumer.Debugf("Trying by extension '%s'", ext)
tries = append(tries, fmt.Sprintf("ext:%s", ext))
a, err := lib.OpenArchive(in, false)
if err != nil {
// try by signature next
......@@ -92,7 +95,7 @@ func New(file eos.File, consumer *state.Consumer) (SzExtractor, error) {
return nil, errors.WithStack(err)
}
consumer.Debugf("Trying by signature")
tries = append(tries, "signature")
a, err = lib.OpenArchive(in, true)
if err != nil {
// With the current libc7zip setup, 7-zip will refuse to
......@@ -101,12 +104,11 @@ func New(file eos.File, consumer *state.Consumer) (SzExtractor, error) {
// Maybe the multivolume interface takes care of that?
// Command-line `7z` has no issue with them.
if ext == "exe" {
consumer.Debugf("Trying by extension 'cab'")
// if it was an .exe, try with a .cab extension
in.Free()
ext = "cab"
tries = append(tries, fmt.Sprintf("ext:%s", ext))
in, err := sz.NewInStream(file, ext, stats.Size())
if err != nil {
......@@ -115,11 +117,11 @@ func New(file eos.File, consumer *state.Consumer) (SzExtractor, error) {
a, err = lib.OpenArchive(in, false) // by ext
if err != nil {
return nil, errors.Wrap(err, "opening archive with 7-zip")
return nil, errors.WithMessage(err, fmt.Sprintf("could not open with 7-zip (tried %v)", tries))
}
} else {
// well, we're out of options
return nil, errors.Errorf("could not open with 7-zip: %s", stats.Name())
return nil, errors.Errorf("could not open with 7-zip (tried %v): %s", tries, stats.Name())
}
}
}
......
......@@ -27,7 +27,7 @@ type ExtractParams struct {
Save SaveFunc
}
type TryOpenParams struct {
type ProbeParams struct {
File eos.File
Consumer *state.Consumer
Candidate *dash.Candidate
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment