-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosec.go
48 lines (45 loc) · 1.14 KB
/
gosec.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
package main
import (
"archive/tar"
"bytes"
"io"
"os"
"path/filepath"
)
func UnpackArchive(archive []byte, rootPath string) error {
t := tar.NewReader(bytes.NewReader(archive))
for {
hdr, err := t.Next()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
if hdr == nil {
continue
}
targetPath := filepath.Join(rootPath, hdr.Name)
switch hdr.Typeflag {
case tar.TypeDir:
if _, err := os.Stat(targetPath); err != nil {
if err := os.MkdirAll(targetPath, os.FileMode(hdr.Mode)); err != nil {
return err
}
}
case tar.TypeReg:
f, err := os.OpenFile(targetPath, os.O_CREATE|os.O_RDWR, os.FileMode(hdr.Mode))
if err != nil {
return err
}
if _, err := io.Copy(f, t); err != nil {
return err
}
// Not sure why opening and writing would work but closing would fail... but error handling is error handling!
// Not sure why opening and writing would work but closing would fail... but error handling is error handling!
if err := f.Close(); err != nil {
return err
}
}
}
}