-
Notifications
You must be signed in to change notification settings - Fork 195
/
tarfile.go
85 lines (75 loc) · 1.75 KB
/
tarfile.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
83
84
85
package main
import (
"archive/tar"
"io"
"log"
"os"
"path/filepath"
)
func untar(tarball, target string) error {
hardLinks := make(map[string]string)
reader, err := os.Open(tarball)
if err != nil {
return err
}
defer reader.Close()
tarReader := tar.NewReader(reader)
for {
header, err := tarReader.Next()
if err == io.EOF {
break
} else if err != nil {
return err
}
path := filepath.Join(target, header.Name)
info := header.FileInfo()
switch header.Typeflag {
case tar.TypeDir:
if err = os.MkdirAll(path, info.Mode()); err != nil {
return err
}
continue
case tar.TypeLink:
/* Store details of hard links, which we process finally */
linkPath := filepath.Join(target, header.Linkname)
linkPath2 := filepath.Join(target, header.Name)
hardLinks[linkPath2] = linkPath
continue
case tar.TypeSymlink:
linkPath := filepath.Join(target, header.Name)
if err := os.Symlink(header.Linkname, linkPath); err != nil {
if os.IsExist(err) {
continue
}
return err
}
continue
case tar.TypeReg:
/* Ensure any missing directories are created */
if _, err := os.Stat(filepath.Dir(path)); os.IsNotExist(err) {
os.MkdirAll(filepath.Dir(path), 0755)
}
file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
if os.IsExist(err) {
continue
}
if err != nil {
return err
}
_, err = io.Copy(file, tarReader)
file.Close()
if err != nil {
return err
}
default:
log.Printf("Warning: File type %d unhandled by untar function!\n", header.Typeflag)
}
}
/* To create hard links the targets must exist, so we do this finally */
for k, v := range hardLinks {
if err := os.Link(v, k); err != nil {
return err
}
}
return nil
}