-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
181 lines (143 loc) · 2.93 KB
/
file.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
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
)
type file struct {
fp *os.File
name string
dir string
mode string
num int64
size int64
mu *sync.Mutex
}
type fileInfos []os.FileInfo
func (fi fileInfos) Len() int {
return len(fi)
}
func (fi fileInfos) Less(i, j int) bool {
f1 := strings.Split(fi[i].Name(), "-")
f2 := strings.Split(fi[j].Name(), "-")
f1nano, _ := strconv.ParseInt(f1[len(f1)-1], 10, 64)
f2nano, _ := strconv.ParseInt(f2[len(f2)-1], 10, 64)
return f1nano < f2nano
}
func (fi fileInfos) Swap(i, j int) {
fi[i], fi[j] = fi[j], fi[i]
}
func stringToFileMode(mode string) (os.FileMode, error) {
m, err := strconv.ParseInt(mode, 8, 0)
if err != nil {
var fmode os.FileMode
return fmode, err
}
return os.FileMode(m), nil
}
func sortFileInfos(fi fileInfos, reverse bool) fileInfos {
if reverse {
sort.Sort(sort.Reverse(fileInfos(fi)))
} else {
sort.Sort(fileInfos(fi))
}
return fi
}
func (f *file) absPath() string {
return filepath.Join(f.dir, f.name)
}
func (f *file) openFile() (*os.File, error) {
var fp *os.File
m, err := stringToFileMode(f.mode)
if err != nil {
return fp, err
}
fp, err = os.OpenFile(f.absPath(), os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.FileMode(m))
return fp, err
}
func (f *file) writeLine(str string) (int, error) {
f.mu.Lock()
defer f.mu.Unlock()
return f.fp.WriteString(fmt.Sprintln(str))
}
func (f *file) rename(old, new string) error {
return os.Rename(old, new)
}
func (f *file) rotate() error {
f.mu.Lock()
defer f.mu.Unlock()
if err := f.close(); err != nil {
return err
}
old := f.absPath()
new := fmt.Sprintf("%s-%d", old, time.Now().UnixNano())
if err := f.rename(old, new); err != nil {
return err
}
fp, err := f.openFile()
if err != nil {
return err
}
f.fp = fp
err = f.cleanup()
return err
}
func (f *file) checkFileSize() (bool, error) {
stat, err := f.fp.Stat()
if err != nil {
return false, err
}
return stat.Size() > f.size, nil
}
func (f *file) listFiles() ([]os.FileInfo, error) {
infos := make([]os.FileInfo, 0)
allInfos, err := ioutil.ReadDir(f.dir)
for _, fi := range allInfos {
if fi.IsDir() {
continue
}
filename := fi.Name()
if strings.Index(filename, fmt.Sprintf("%s-", f.name)) == 0 {
infos = append(infos, fi)
}
}
sortFileInfos(infos, true)
return infos, err
}
func (f *file) remove(rmFile string) error {
return os.Remove(rmFile)
}
func (f *file) cleanup() error {
infos, err := f.listFiles()
if err != nil {
return err
}
if int64(len(infos)) < f.num {
return nil
}
for _, fi := range infos[f.num-1:] {
filename := filepath.Join(f.dir, fi.Name())
if f.remove(filename) != nil {
return err
} else {
fmt.Println("removed", filename)
}
}
return nil
}
func (f *file) close() error {
err := f.fp.Close()
f.fp = nil
return err
}
func (f *file) Close() error {
f.mu.Lock()
defer f.mu.Unlock()
return f.fp.Close()
}