Repository navigation
Expand file tree
/
Copy pathtargz.go
More file actions
198 lines (161 loc) · 3.79 KB
/
Copy pathtargz.go
File metadata and controls
198 lines (161 loc) · 3.79 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package targz
import (
"bytes"
"compress/gzip"
"encoding/binary"
"io"
"os"
"os/user"
"runtime"
"strconv"
"github.com/sisatech/targz/structs"
)
func ArchiveFile(path, destination string) error {
// Get file path & check exists ...
if _, err := os.Stat(path); os.IsNotExist(err) {
return err
}
// os.Stat for size ...
stat, err := os.Stat(path)
if err != nil {
return err
}
// Write header ...
header := new(structs.POSIX_HEADER)
// Header Name ...
copy(header.Name[:], []byte(stat.Name()))
u, err := user.Current()
if err != nil {
return err
}
var gid string
// User ID and Group ID ...
// Do not attempt to get Group ID if running on Windows ...
uidInt, _ := strconv.Atoi(u.Uid)
gidInt, _ := strconv.Atoi(u.Gid)
gid = "000" + strconv.FormatUint(uint64(gidInt), 8)
uid := "000" + strconv.FormatUint(uint64(uidInt), 8)
copy(header.Uid[:], []byte(uid))
copy(header.Gid[:], []byte(gid))
// Size ...
octSize := "0" + strconv.FormatUint(uint64(stat.Size()), 8)
copy(header.Size[:], []byte(octSize))
// Mode ...
var mode [8]byte
mode[0] = byte(0x30)
mode[1] = byte(0x30)
mode[2] = byte(0x30)
mode[3] = byte(0x30)
mode[4] = byte(0x36)
mode[5] = byte(0x34)
mode[6] = byte(0x34)
mode[7] = byte(0x00)
header.Mode = mode
// Time ...
time := "00000000000"
copy(header.Mtime[:], []byte(time))
// Typeflag ...
header.Typeflag = 0x30
// Magic ...
magic := []byte("ustar")
byteMagic := make([]byte, len(magic)+1)
for i := 0; i < len(byteMagic); i++ {
if i == len(byteMagic)-1 {
byteMagic[i] = byte(0x20)
} else {
byteMagic[i] = magic[i]
}
}
copy(header.Magic[:], byteMagic)
// Version ...
var version [2]byte
version[0] = 0x20
version[1] = 0x00
header.Version = version
// Uname ...
uname := []byte(u.Username)
copy(header.Uname[:], uname)
// Gname ...
var gname []byte
if runtime.GOOS != "windows" {
group, err := user.LookupGroupId(u.Gid)
if err != nil {
return err
}
gname = []byte(group.Name)
copy(header.Gname[:], gname)
} else {
gname = []byte{}
for i := 0; i < 32; i++ {
gname = append(gname, 0)
}
}
// Calculate Checksum ...
headerBytes := &bytes.Buffer{}
err = binary.Write(headerBytes, binary.LittleEndian, header)
if err != nil {
return err
}
bh := make([]byte, 512)
err = binary.Read(headerBytes, binary.LittleEndian, &bh)
var csVal uint32
csVal = 0
for i := 0; i < len(bh); i++ {
csVal += uint32(bh[i])
}
// Treat the 8 Checksum Field bytes as ASCII spaces (dec 32)
csVal += (8 * 32)
csOctal := "0" + strconv.FormatUint(uint64(csVal), 8)
csBytes := []byte(csOctal)
copy(header.Chksum[:], csBytes)
header.Chksum[len(header.Chksum)-1] = 0x20
// binary.Write(buf, binary.LittleEndian, header)
// Create Archive File
f, err := os.Create(path + ".tar")
if err != nil {
return err
}
// Create Reader on targeted file ...
r, err := os.Open(path)
if err != nil {
return err
}
// Write Header to Archive File ...
// f.WriteAt([]byte(fmt.Sprintf("%v", header)), 0)
binary.Write(f, binary.LittleEndian, header)
// Offset by headersize & write to archive
f.Seek(512, 0)
io.Copy(f, r)
// Offset by file size & write end 2 empty blocks
f.Seek(stat.Size()+512, 0)
var endBlocks []byte
for i := 0; i < (15 * 512); i++ {
endBlocks = append(endBlocks, 0x00)
}
f.Write(endBlocks)
err = gzipit(path+".tar", destination+".tar.gz")
if err != nil {
return err
}
defer f.Close()
defer r.Close()
return nil
}
func gzipit(source, target string) error {
reader, err := os.Open(source)
if err != nil {
return err
}
// target = strings.TrimSuffix(target, ".img")
writer, err := os.Create(target)
if err != nil {
return err
}
defer writer.Close()
archiver := gzip.NewWriter(writer)
archiver.Name = target
defer archiver.Close()
_, err = io.Copy(archiver, reader)
os.Remove(source)
return err
}