-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.go
123 lines (116 loc) · 2.58 KB
/
util.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
package logrotate
import (
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"unicode"
)
const (
compressSuffix = ".gz"
)
func stringToBytes(s string) (bytes int64, err error) {
s = strings.TrimSpace(s)
s = strings.ToUpper(s)
if s == "" {
return 0, nil
}
var numStr, unit string
i := strings.IndexFunc(s, unicode.IsLetter)
if i == -1 {
numStr = s
} else {
numStr, unit = s[:i], s[i:]
}
bytes, err = strconv.ParseInt(numStr, 10, 64)
if err != nil {
return
}
if bytes <= 0 {
bytes = 0
return
}
switch unit {
case "E", "EB", "EIB":
bytes *= 1 << 60
case "P", "PB", "PIB":
bytes *= 1 << 50
case "T", "TB", "TIB":
bytes *= 1 << 40
case "G", "GB", "GIB":
bytes *= 1 << 30
case "M", "MB", "MIB":
bytes *= 1 << 20
case "K", "KB", "KIB":
bytes *= 1 << 10
case "B":
default:
}
return
}
func archiveName(name string, timeFormat string) string {
dir := filepath.Dir(name)
prefix, ext := splitFilename(name)
t := time.Now().Format(timeFormat)
return filepath.Join(dir, fmt.Sprintf("%s-%s%s", prefix, t, ext))
}
func splitFilename(name string) (prefix, ext string) {
filename := filepath.Base(name)
ext = filepath.Ext(filename)
prefix = filename[:len(filename)-len(ext)]
return
}
func timeFromName(timeFormat, filename, prefix, ext string) (time.Time, error) {
if !strings.HasPrefix(filename, prefix) {
return time.Time{}, fmt.Errorf("mismatched prefix(%s) filename(%s)", prefix, filename)
}
if !strings.HasSuffix(filename, ext) {
return time.Time{}, fmt.Errorf("mismatched extension(%s) filename(%s)", ext, filename)
}
ts := filename[len(prefix) : len(filename)-len(ext)]
return time.Parse(timeFormat, ts)
}
func compressFile(path string) error {
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open log file, error(%v)", err)
}
defer f.Close()
fi, err := os.Stat(path)
if err != nil {
return fmt.Errorf("failed to stat log file, error(%v)", err)
}
dst := path + compressSuffix
gzf, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, fi.Mode())
if err != nil {
return fmt.Errorf("failed to open compressed log file, error(%v)", err)
}
defer gzf.Close()
gz := gzip.NewWriter(gzf)
defer func() {
if err != nil {
_ = os.Remove(dst)
err = fmt.Errorf("failed to compress file, error(%v)", err)
}
}()
if _, err := io.Copy(gz, f); err != nil {
return err
}
if err := gz.Close(); err != nil {
return err
}
if err := gzf.Close(); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
if err := os.Remove(path); err != nil {
return err
}
return nil
}