forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
155 lines (132 loc) · 3.67 KB
/
helper.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
// Copyright 2017-2019 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
type tarWriter interface {
io.Writer
WriteHeader(hdr *tar.Header) error
}
type walker struct {
baseDir, dbgDir string
output tarWriter
log io.Writer
}
func newWalker(baseDir, dbgDir string, output tarWriter, logger io.Writer) *walker {
return &walker{
baseDir: baseDir,
dbgDir: dbgDir,
output: output,
log: logger,
}
}
func (w *walker) walkPath(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Fprintf(w.log, "Error while walking path %s: %s", path, err)
return nil
}
if info == nil {
fmt.Fprintf(w.log, "No file info available")
return nil
}
file, err := os.Open(path)
if err != nil {
fmt.Fprintf(w.log, "Failed to open %s: %s\n", path, err)
// TODO: Write an empty file here, just to hint that this file
// existed by there was some problem attempting to add it.
return nil
}
defer file.Close()
if info.IsDir() {
fmt.Fprintf(w.log, "Skipping directory %s\n", info.Name())
return nil
}
// Just get the latest fileInfo to make sure that the size is correctly
// when the file is write to tar file
fpInfo, err := file.Stat()
if err != nil {
fpInfo, err = os.Lstat(file.Name())
if err != nil {
fmt.Fprintf(w.log, "Failed to retrieve file information: %s\n", err)
return nil
}
}
header, err := tar.FileInfoHeader(fpInfo, fpInfo.Name())
if err != nil {
fmt.Fprintf(w.log, "Failed to prepare file info %s: %s\n", fpInfo.Name(), err)
return nil
}
if w.baseDir != "" {
header.Name = filepath.Join(w.baseDir, strings.TrimPrefix(path, w.dbgDir))
}
if err := w.output.WriteHeader(header); err != nil {
fmt.Fprintf(w.log, "Failed to write header: %s\n", err)
return nil
}
_, err = io.Copy(w.output, file)
return err
}
func createArchive(dbgDir string) (string, error) {
// Based on http://blog.ralch.com/tutorial/golang-working-with-tar-and-gzip/
archivePath := fmt.Sprintf("%s.tar", dbgDir)
file, err := os.Create(archivePath)
if err != nil {
return "", err
}
defer file.Close()
writer := tar.NewWriter(file)
defer writer.Close()
var baseDir string
if info, err := os.Stat(dbgDir); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Debug directory does not exist %s\n", err)
return "", err
} else if err == nil && info.IsDir() {
baseDir = filepath.Base(dbgDir)
}
walker := newWalker(baseDir, dbgDir, writer, os.Stderr)
return archivePath, filepath.Walk(dbgDir, walker.walkPath)
}
func createGzip(dbgDir string) (string, error) {
// Based on http://blog.ralch.com/tutorial/golang-working-with-tar-and-gzip/
source, err := createArchive(dbgDir)
if err != nil {
return "", err
}
reader, err := os.Open(source)
if err != nil {
return "", err
}
filename := filepath.Base(source)
target := fmt.Sprintf("%s.gz", source)
writer, err := os.Create(target)
if err != nil {
return "", err
}
defer writer.Close()
archiver := gzip.NewWriter(writer)
archiver.Name = filename
defer archiver.Close()
_, err = io.Copy(archiver, reader)
if err != nil {
return "", err
}
return target, nil
}