-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
170 lines (140 loc) · 3.12 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
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
package main
import (
"archive/tar"
"context"
"crypto/rand"
"errors"
"fmt"
"io"
"net/http"
_ "net/http/pprof"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/cosnicolaou/pbzip2"
)
const(
dirName = "sample"
archiveName = "samples.tar.bz2"
)
var (
filesMap = map[int]int{
1: 10,
5: 5,
30: 2,
}
)
func main() {
// debug
go func() {
fmt.Println(http.ListenAndServe(":6060", nil))
}()
genSampleArchieve()
unpackTimes := 0
http.HandleFunc("/unpack", func(w http.ResponseWriter, req *http.Request){
runtime.GC()
unpackTimes += 1
os.Mkdir(dirName, 0777)
unBzip2(archiveName, "./")
os.RemoveAll(dirName)
fmt.Fprintf(w, "done count: %d\n", unpackTimes)
runtime.GC()
})
fmt.Printf("Start server\n")
http.ListenAndServe(":8090", nil)
}
func genSampleArchieve() {
fmt.Printf("Generate samples\n")
filesPaths := genSamples()
PackFiles(archiveName, filesPaths)
os.RemoveAll(dirName)
fmt.Printf("Archieve created\n")
}
func genSamples() []string {
var files []string
os.Mkdir(dirName, 0777)
for size, count := range filesMap {
for ;count > 0;count-- {
file := fmt.Sprintf("%s/file_%d_%d", dirName, size, count)
genBinFile(size, file)
files = append(files, file)
}
}
return files
}
func genBinFile(fileSizeMb int, fileName string){
file, _ := os.Create(fileName)
defer file.Close()
fileSizeMb = fileSizeMb * 1024 * 1024
data := make([]byte, fileSizeMb)
rand.Read(data)
file.Write(data)
file.Sync()
}
func unBzip2(sourcefile, dest string) error {
reader, err := os.Open(sourcefile)
if err != nil {
return err
}
defer reader.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
processPool := pbzip2.CreateConcurrencyPool(4)
bzReader := pbzip2.NewReader(
ctx,
reader,
pbzip2.DecompressionOptions(pbzip2.BZConcurrencyPool(processPool)),
)
var tarReader *tar.Reader = tar.NewReader(bzReader)
filePath := ""
var writer *os.File
var header *tar.Header
for {
header, err = tarReader.Next()
if err != nil {
if err == io.EOF {
err = nil
break
}
return err
}
filePath = filepath.Join(dest, header.Name)
switch header.Typeflag {
case tar.TypeDir:
err = os.MkdirAll(filePath, os.FileMode(header.Mode))
case tar.TypeReg, tar.TypeRegA:
writer, err = os.Create(filePath)
if err == nil {
_, err = io.Copy(writer, tarReader)
if err != nil {
fmt.Printf("UNBZIP2: copy archieve's file error: %s \n", err)
writer.Close()
return err
}
writer.Close()
err = os.Chmod(filePath, os.FileMode(header.Mode))
if err != nil {
return err
}
}
default:
fmt.Printf("UNBZIP2: provider: Unable to untar type: %c in file %s\n", header.Typeflag, filePath)
err = errors.New("provider: Unable to untar type file" + filePath)
}
if err != nil {
fmt.Printf("UNBZIP2: unpack file error: %s", err)
}
}
return err
}
func PackFiles(archiveName string, files []string) error {
args := append([]string{"-cjf", archiveName}, files...)
cmd := exec.Command("gtar", args...)
err := cmd.Run()
if err != nil {
fmt.Printf("Error while executing command: %v", err)
return err
}
return nil
}