Skip to content

Commit 7f3c6c0

Browse files
authored
s2sx: Add more extraction options (#334)
* s2sx: Add more extraction options ``` s2sx Self Extracting Archive Usage: sample.tar.s2sfx.exe [options] [output-file/dir] Use - as file name to extract to stdout when not untarring. Options: -force-untar Always untar file -help Display help -q Don't write any output to terminal, except errors -untar Untar tar files if specified at creation (default true) ``` * Update condition
1 parent 3a9b92d commit 7f3c6c0

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ deploy:
5252
script: curl -sL https://git.io/goreleaser | VERSION=v0.157.0 bash || true
5353
on:
5454
tags: true
55-
condition: $TRAVIS_OS_NAME = linux AND $TRAVIS_CPU_ARCH = amd64
55+
condition: ($TRAVIS_OS_NAME = linux) && ($TRAVIS_CPU_ARCH = amd64)
5656
go: 1.16.x
5757
branches:
5858
only:

s2/cmd/_s2sx/_unpack/main.go

+75-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"debug/macho"
77
"debug/pe"
88
"errors"
9+
"flag"
910
"fmt"
1011
"io"
1112
"os"
@@ -21,9 +22,33 @@ const (
2122
opUnTar
2223
)
2324

25+
var (
26+
help = flag.Bool("help", false, "Display help")
27+
untarFlag = flag.Bool("untar", true, "Untar tar files if specified at creation")
28+
forceUntar = flag.Bool("force-untar", false, "Always untar file")
29+
quiet = flag.Bool("q", false, "Don't write any output to terminal, except errors")
30+
)
31+
2432
func main() {
33+
flag.Parse()
2534
me, err := os.Executable()
2635
exitErr(err)
36+
args := flag.Args()
37+
invalidArgs := len(args) > 1
38+
if *help || invalidArgs {
39+
_, _ = fmt.Fprintf(os.Stderr, "s2sx Self Extracting Archive\n\n")
40+
_, _ = fmt.Fprintf(os.Stderr, "Usage: %s [options] [output-file/dir]\n\n", os.Args[0])
41+
_, _ = fmt.Fprintf(os.Stderr, "Use - as file name to extract to stdout when not untarring.\n\n")
42+
_, _ = fmt.Fprintln(os.Stderr, `Options:`)
43+
flag.PrintDefaults()
44+
if invalidArgs {
45+
os.Exit(1)
46+
}
47+
os.Exit(0)
48+
}
49+
stdout := len(args) > 0 && args[0] == "-"
50+
*quiet = *quiet || stdout
51+
2752
f, err := os.Open(me)
2853
exitErr(err)
2954
defer f.Close()
@@ -35,16 +60,35 @@ func main() {
3560
_, err = io.ReadFull(rd, tmp[:])
3661
exitErr(err)
3762
dec := s2.NewReader(rd)
63+
if !*untarFlag {
64+
tmp[0] = opUnpack
65+
}
66+
if *forceUntar {
67+
tmp[0] = opUnTar
68+
}
3869
switch tmp[0] {
3970
case opUnpack:
4071
outname := me + "-extracted"
4172
if idx := strings.Index(me, ".s2sfx"); idx > 0 {
4273
// Trim from '.s2sfx'
4374
outname = me[:idx]
4475
}
45-
fmt.Printf("Extracting to %q...", outname)
46-
out, err := os.Create(outname)
47-
exitErr(err)
76+
var out io.Writer
77+
if stdout {
78+
out = os.Stdout
79+
} else {
80+
if len(args) > 0 {
81+
outname, err = filepath.Abs(args[0])
82+
exitErr(err)
83+
}
84+
if !*quiet {
85+
fmt.Printf("Extracting to \"%s\"...", outname)
86+
}
87+
f, err := os.Create(outname)
88+
exitErr(err)
89+
defer f.Close()
90+
out = f
91+
}
4892
_, err = io.Copy(out, dec)
4993
exitErr(err)
5094

@@ -53,12 +97,26 @@ func main() {
5397
if err != nil {
5498
dir = filepath.Dir(me)
5599
}
56-
fmt.Printf("Extracting TAR file to %s...\n", dir)
100+
if len(args) > 0 {
101+
if args[0] == "-" {
102+
exitErr(errors.New("cannot untar files to stdout. Use -untar=false to skip untar operation"))
103+
}
104+
if filepath.IsAbs(args[0]) {
105+
dir = args[0]
106+
} else {
107+
dir = filepath.Join(dir, args[0])
108+
}
109+
}
110+
if !*quiet {
111+
fmt.Printf("Extracting TAR file to %s...\n", dir)
112+
}
57113
exitErr(untar(dir, dec))
58114
default:
59115
exitErr(fmt.Errorf("unknown operation: %d", tmp[0]))
60116
}
61-
fmt.Println("\nDone.")
117+
if !*quiet {
118+
fmt.Println("\nDone.")
119+
}
62120
}
63121

64122
func exitErr(err error) {
@@ -191,7 +249,9 @@ func untar(dst string, r io.Reader) error {
191249

192250
// if its a dir and it doesn't exist create it
193251
case tar.TypeDir:
194-
fmt.Println(target)
252+
if !*quiet {
253+
fmt.Println(target)
254+
}
195255
if _, err := os.Stat(target); err != nil {
196256
if err := os.MkdirAll(target, 0755); err != nil {
197257
return err
@@ -201,7 +261,9 @@ func untar(dst string, r io.Reader) error {
201261
// if it's a file create it
202262
case tar.TypeReg, tar.TypeChar, tar.TypeBlock, tar.TypeFifo, tar.TypeGNUSparse:
203263
target = path.Clean(target)
204-
fmt.Println(target)
264+
if !*quiet {
265+
fmt.Println(target)
266+
}
205267

206268
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
207269
if err != nil {
@@ -224,15 +286,19 @@ func untar(dst string, r io.Reader) error {
224286
f.Close()
225287
case tar.TypeSymlink:
226288
target = path.Clean(target)
227-
fmt.Println(target)
289+
if !*quiet {
290+
fmt.Println(target)
291+
}
228292

229293
err := writeNewSymbolicLink(target, header.Linkname)
230294
if err != nil {
231295
return err
232296
}
233297
case tar.TypeLink:
234298
target = path.Clean(target)
235-
fmt.Println(target)
299+
if !*quiet {
300+
fmt.Println(target)
301+
}
236302

237303
err := writeNewHardLink(target, filepath.Join(dst, header.Linkname))
238304
if err != nil {

0 commit comments

Comments
 (0)