/
fileDataRecording.go
196 lines (185 loc) · 4.54 KB
/
fileDataRecording.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// 文件数据记录类
// create by gloomy 2017-04-06 10:11:35
package gutil
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
const maxFileDataRecordingBytes = 1000000 // 默认文件大小
// 文件数据记录对象
// create by gloomy 2017-04-06 10:15:00
type FileDataRecording struct {
sync.Mutex // 锁
F *os.File // 文件对象
FilePre string // 文件开头字符串
Fn string // 文件路径
Bytes int // 文件大小
Seq int // 第几个
FileProgram string // 文件存放路径
MaxFileDataRecordingBytes int // 文件大小
}
// 打开文件数据记录
// create by gloomy 2017-04-06 10:17:38
// 文件存放目录地址 文件开头字符串 文件大小
// 文件数据对象
func OpenLoadFile(fileProgram, filePre string, maxSize int) *FileDataRecording {
if maxSize == 0 {
maxSize = maxFileDataRecordingBytes
}
lf := &FileDataRecording{
FilePre: filePre,
FileProgram: fileProgram,
MaxFileDataRecordingBytes: maxSize,
}
lf.Rotate()
return lf
}
// 文件退出
// create by gloomy 2017-04-06 10:27:58
func (f *FileDataRecording) Exit() {
f.Lock()
f.Close()
f.F = nil
f.Unlock()
}
// 文件关闭
// create by gloomy 2017-04-06 10:22:14
func (f *FileDataRecording) Close() {
if f.F != nil {
f.F.Close()
os.Rename(f.Fn, f.Fn[0:len(f.Fn)-4]) //去掉末尾的.tmp
}
}
// 文件切换
// create by gloomy 2017-04-06 10:30:05
func (f *FileDataRecording) Rotate() {
f.Lock()
f.Seq = 0
f.Close()
f.CreateNewFile()
f.Unlock()
}
// 创建新文件
// create by gloomy 2017-04-06 10:33:11
// 错误对象
func (f *FileDataRecording) CreateNewFile() (err error) {
f.Bytes = 0
if !strings.HasSuffix(f.FileProgram, "/") {
f.FileProgram += "/"
}
f.Fn = fmt.Sprintf("%s%s-%d-%d.tmp", f.FileProgram, f.FilePre, time.Now().UnixNano(), f.Seq)
f.Seq++
f.F, err = os.OpenFile(f.Fn, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Printf("create file %s failed: %s \n", f.Fn, err.Error())
}
return
}
// 写入数据
// create by gloomy 2017-04-06 11:40:55
// 需要写入的数据
// 错误对象
func (f *FileDataRecording) WriteData(dataStr string) (err error) {
f.Lock()
defer f.Unlock()
if f.F == nil {
err = f.CreateNewFile()
if err != nil {
return
}
}
dataStrLen := len(dataStr)
if f.Bytes+dataStrLen > f.MaxFileDataRecordingBytes {
f.Close()
if err = f.CreateNewFile(); err != nil {
return
}
}
f.Bytes += dataStrLen
_, err = f.F.WriteString(dataStr)
return
}
// 获取所有完成的文件列表
// create by gloomy 2017-04-06 13:46:51
// 文件列表
func (f *FileDataRecording) FileList() *[]string {
var (
fileArray []string
ext string
err error
fileInfo os.FileInfo
fileNamePath string
)
fileNameIn, err := GetMyAllFileByDir(f.FileProgram)
if err != nil {
return &fileArray
}
for _, fileName := range *fileNameIn {
if ext = filepath.Ext(fileName); ext == ".tmp" {
continue
}
fileNamePath = fmt.Sprintf("%s%s", f.FileProgram, fileName)
fileInfo, err = os.Stat(fileNamePath)
if err != nil {
continue
}
if fileInfo.Size() == 0 {
os.Remove(fileNamePath)
continue
}
if strings.HasPrefix(fileName, f.FilePre) {
fileArray = append(fileArray, fileNamePath)
}
}
return &fileArray
}
// 删除过期文件
// create by gloomy 2017-04-06 22:53:17
// 几天前
func (f *FileDataRecording) RemoveOldFileList(days int) {
var (
timeDate = time.Now().AddDate(0, 0, days).UnixNano()
unixNumber int
fileNameSplit []string
err error
ext string
fileInfo os.FileInfo
fileNamePath string
)
fileNameIn, err := GetMyAllFileByDir(f.FileProgram)
if err != nil {
return
}
for _, fileName := range *fileNameIn {
if ext = filepath.Ext(fileName); ext == ".tmp" {
continue
}
fileNamePath = fmt.Sprintf("%s%s", f.FileProgram, fileName)
fileInfo, err = os.Stat(fileNamePath)
if err != nil {
continue
}
if fileInfo.Size() == 0 {
os.Remove(fileNamePath)
continue
}
if strings.HasPrefix(fileName, f.FilePre) {
fileNameSplit = strings.Split(fileName, "-")
if len(fileNameSplit) == 3 {
unixNumber, err = strconv.Atoi(fileNameSplit[1])
if err != nil {
fmt.Printf("FileListRemoveOld fileProgram: %s path: %s err: %s \n", f.FileProgram, fileName, err.Error())
continue
}
if timeDate >= int64(unixNumber) {
os.Remove(fileNamePath)
}
}
}
}
}