forked from projectatomic/atomic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gotar.go
164 lines (121 loc) · 3.2 KB
/
gotar.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// This source code will generate gotar binary
// which is needed for atomic migrate export and import commands.
package main
import (
"archive/tar"
"io"
"log"
"os"
"path"
"path/filepath"
)
func main() {
if len(os.Args) < 3 {
log.Fatalln("Missing arguments")
}
if os.Args[1] == "" {
log.Fatalln("tar flag cannot be empty. Please use -cf for creation or -xf for extraction")
}
if os.Args[1] != "-cf" && os.Args[1] != "-xf" {
log.Fatalf("%s is not a valid tar flag\n", os.Args[1])
}
if os.Args[2] == "" {
log.Fatalln("Destination filename cannot be empty")
}
if os.Args[1] == "-cf" && os.Args[3] == "" {
log.Fatalln("Source directory cannot be empty")
}
tarFlag := os.Args[1]
destinationFilename := os.Args[2]
if tarFlag == "-cf" {
sourceDir := os.Args[3]
tarDir(destinationFilename, sourceDir)
} else if tarFlag == "-xf" {
untarDir(destinationFilename)
}
}
func checkError(err error) {
if err != nil {
log.Fatalln(err)
}
}
func tarDir(destinationFilename, sourceDir string) {
if destinationFilename[len(destinationFilename)-3:] != "tar" {
log.Fatalln("Please provide a valid tar filename")
}
tarFile, err := os.Create(destinationFilename)
checkError(err)
defer tarFile.Close()
var fileWriter io.WriteCloser = tarFile
tarfileWriter := tar.NewWriter(fileWriter)
defer tarfileWriter.Close()
walkTar(sourceDir, tarfileWriter)
}
func walkTar(dirPath string, tarfileWriter *tar.Writer) {
dir, err := os.Open(dirPath)
checkError(err)
dirInfo, err := dir.Stat()
checkError(err)
// prepare the tar header for dir entry.
dheader, err := tar.FileInfoHeader(dirInfo, "")
checkError(err)
dheader.Name = dir.Name()[1:]
err = tarfileWriter.WriteHeader(dheader)
checkError(err)
files, err := dir.Readdir(0) // grab the files list
checkError(err)
for _, fileInfo := range files {
if fileInfo.IsDir() {
walkTar(path.Join(dir.Name(), fileInfo.Name()), tarfileWriter)
} else {
file, err := os.Open(dir.Name() + string(filepath.Separator) + fileInfo.Name())
checkError(err)
defer file.Close()
// prepare the tar header for file entry.
header, err := tar.FileInfoHeader(fileInfo, "")
checkError(err)
header.Name = file.Name()[1:]
err = tarfileWriter.WriteHeader(header)
checkError(err)
_, err = io.Copy(tarfileWriter, file)
checkError(err)
}
}
}
func untarDir(destinationFilename string) {
var fileReader io.ReadCloser
if destinationFilename == "-" {
fileReader = os.Stdin
} else {
if destinationFilename[len(destinationFilename)-3:] != "tar" {
log.Fatalln("Please provide a valid tar filename")
}
file, err := os.Open(destinationFilename)
checkError(err)
defer file.Close()
fileReader = file
}
tarfileReader := tar.NewReader(fileReader)
pwd, err := os.Getwd()
checkError(err)
for {
header, err := tarfileReader.Next()
if err != nil {
if err == io.EOF {
break
}
log.Fatalln(err)
}
fileInfo := header.FileInfo()
if fileInfo.IsDir() {
err = os.MkdirAll(path.Join(pwd, header.Name), fileInfo.Mode())
checkError(err)
} else {
tarFile, err := os.Create(path.Join(pwd, header.Name))
checkError(err)
defer tarFile.Close()
_, err = io.Copy(tarFile, tarfileReader)
checkError(err)
}
}
}