summaryrefslogtreecommitdiff
path: root/vendor/github.com/hooklift/iso9660/cmd/iso9660/main.go
blob: d03a4400e63e805afc37aaa0a75d0f341c487bc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main

import (
	"fmt"
	"io"
	"os"
	"path/filepath"

	"github.com/docopt/docopt-go"
	"github.com/hooklift/iso9660"
)

// Version holds the CLI version and is set in compilation time.
var Version string

func main() {
	usage := `ISO9660 extractor.
Usage:
  iso9660 <image-path> <destination-path>
  iso9660 -h | --help
  iso9660 --version
`

	args, err := docopt.Parse(usage, nil, true, Version, false)
	if err != nil {
		panic(err)
	}

	file, err := os.Open(args["<image-path>"].(string))
	if err != nil {
		panic(err)
	}

	r, err := iso9660.NewReader(file)
	if err != nil {
		panic(err)
	}

	destPath := args["<destination-path>"].(string)
	if destPath == "" {
		destPath = "."
	}

	for {
		f, err := r.Next()
		if err == io.EOF {
			break
		}

		if err != nil {
			panic(err)
		}

		fp := filepath.Join(destPath, f.Name())
		if f.IsDir() {
			if err := os.MkdirAll(fp, f.Mode()); err != nil {
				panic(err)
			}
			continue
		}

		parentDir, _ := filepath.Split(fp)
		if err := os.MkdirAll(parentDir, f.Mode()); err != nil {
			panic(err)
		}

		fmt.Printf("Extracting %s...\n", fp)

		freader := f.Sys().(io.Reader)
		ff, err := os.OpenFile(fp, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, f.Mode())
		if err != nil {
			panic(err)
		}
		defer func() {
			if err := ff.Close(); err != nil {
				panic(err)
			}
		}()

		if _, err := io.Copy(ff, freader); err != nil {
			panic(err)
		}
	}
}