-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
82 lines (73 loc) · 1.75 KB
/
main.go
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
package main
import (
"archive/tar"
"flag"
"log"
"os"
"github.com/sophaskins/efs2tar/efs"
"github.com/sophaskins/efs2tar/sgi"
)
func main() {
inputPath := flag.String("in", "", "the file to be read as an efs filesystem")
outputPath := flag.String("out", "", "the file to written to as a tar file")
flag.Parse()
file, err := os.Open(*inputPath)
if err != nil {
log.Fatal(err)
}
b := make([]byte, 51200)
_, err = file.Read(b)
if err != nil {
log.Fatal(err)
}
outputFile, err := os.OpenFile(*outputPath, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0755)
if err != nil {
log.Fatal(err)
}
tw := tar.NewWriter(outputFile)
vh := sgi.NewVolumeHeader(b)
p := vh.Partitions[7]
fs := efs.NewFilesystem(file, p.Blocks, p.First)
fs.WalkFilesystem(buildTarCallback(tw, fs))
tw.Close()
}
func buildTarCallback(tw *tar.Writer, fs *efs.Filesystem) func(efs.Inode, string) {
return func(in efs.Inode, path string) {
if path == "" {
return
}
if in.Type() == efs.FileTypeDirectory {
hdr := &tar.Header{
Name: path,
Mode: 0755,
Typeflag: tar.TypeDir,
}
if err := tw.WriteHeader(hdr); err != nil {
log.Fatal(err)
}
} else if in.Type() == efs.FileTypeRegular {
contents := fs.FileContents(in)
hdr := &tar.Header{
Name: path,
Mode: 0755,
Size: int64(len(contents)),
}
if err := tw.WriteHeader(hdr); err != nil {
log.Fatal(err)
}
if _, err := tw.Write([]byte(contents)); err != nil {
log.Fatal(err)
}
} else if in.Type() == efs.FileTypeSymlink {
contents := fs.FileContents(in)
hdr := &tar.Header{
Name: path,
Linkname: string(contents[:int64(len(contents))]),
Typeflag: tar.TypeSymlink,
}
if err := tw.WriteHeader(hdr); err != nil {
log.Fatal(err)
}
}
}
}