-
Notifications
You must be signed in to change notification settings - Fork 0
/
tar.go
124 lines (98 loc) · 2.92 KB
/
tar.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
package main
import (
"archive/tar"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"go.uber.org/zap"
gzip "github.com/klauspost/pgzip"
)
// Tar takes a source and variable writers and walks 'source' writing each file
// found to the tar writer; the purpose for accepting multiple writers is to allow
// for multiple outputs (for example a file, or md5 hash)
func Tar(src string, writer io.Writer, excludes []string, after time.Time) error {
source, err := filepath.Abs(src)
if err != nil {
return fmt.Errorf("Unable to expand src - %v", err.Error())
}
// ensure the src actually exists before trying to tar it
if _, err := os.Stat(source); err != nil {
return fmt.Errorf("Unable to tar files - %v", err.Error())
}
gzw, _ := gzip.NewWriterLevel(writer, gzip.BestSpeed)
defer gzw.Close()
tw := tar.NewWriter(gzw)
defer tw.Close()
// walk path
return filepath.Walk(source, func(file string, fi os.FileInfo, err error) error {
// return on any error
if err != nil {
log.Warn(err)
return err
}
// Skip folders, we only care about files
if !fi.Mode().IsRegular() {
return nil
}
filePath := normalize(source, file, strings.HasSuffix(src, string(filepath.Separator)))
for _, exclude := range excludes {
if matched, _ := regexp.MatchString(exclude, filePath); matched {
log.Debugw("Skipped", zap.String("file", filePath))
return nil
}
}
// create a new dir/file header
fileSize := fi.Size()
header, err := tar.FileInfoHeader(fi, filePath)
if err != nil {
log.Warn(err)
return err
}
// To use AccessTime or ChangeTime, specify the Format as PAX or GNU.
// To use sub-second resolution, specify the Format as PAX.
if header.ModTime.Before(after) {
log.Debugw("Skipped due modified-date", zap.String("file", filePath))
return nil
}
// update the name to correctly reflect the desired destination when untaring
header.Name = filePath
header.Gid = 0
header.Uid = 0
// write the header
if err := tw.WriteHeader(header); err != nil {
log.Warn(err)
return err
}
// open files for taring
f, err := os.Open(file)
if err != nil {
log.Warn(err)
return err
}
// copy file data into tar writer
if _, err := io.Copy(tw, f); err != nil {
log.Warn(err)
return err
}
if fileSize != fi.Size() {
log.Fatal("File size changed after write", zap.String("file", filePath))
}
log.Infow("Added", zap.String("file", filePath))
// manually close here after each file operation; defering would cause each file close
// to wait until all operations have completed.
f.Close()
return nil
})
}
func normalize(base, file string, stripBase bool) string {
out := strings.Replace(file, base, "", -1)
// Preserver tar behavior where bases without "filepath.Separator" include the innermost directory
if !stripBase {
out = strings.Replace(file, filepath.Dir(base), "", -1)
}
return strings.TrimPrefix(out, string(filepath.Separator))
}