/
file.go
305 lines (272 loc) · 6.01 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// 进行一些基本的文件操作
// 参考:
// https://colobu.com/2016/10/12/go-file-operations/
package dm_1
import (
cfg "../config"
"crypto/md5"
"encoding/hex"
"errors"
"github.com/kpango/glg"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"time"
)
type (
DMResource struct {
DMFileInfo
// 一个资源的绝对路径
Path string
}
// 详见os包中的 types.go
// 用于方便转化为json对象
DMFileInfo struct {
Name string // base name of the file
Size int64 // length in bytes for regular files; system-dependent for others
Mode os.FileMode // file mode bits
ModTime time.Time // modification time
IsDir bool // abbreviation for Mode().IsDire()
Sys interface{} // underlying data source (can return nil)
}
// 详见os包中的 types.go
// 用于方便转化为json对象
DMFileInfoViewModel struct {
Name string
Size int64
Mode string
ModTime string
IsDir bool
Sys interface{}
}
)
func ( R DMResource ) ToReadable() *DMFileInfoViewModel {
return &DMFileInfoViewModel{
Name: R.Name,
Size: R.Size,
Mode: R.Mode.String(),
ModTime: R.ModTime.Format("2006-01-02 15:04:05"),
IsDir: R.IsDir,
Sys: R.Sys,
}
}
// 获取文件的基本信息
func ( pR *DMResource ) GetBasicFileInfo() ( dfi *DMFileInfo, e error ) {
var fi os.FileInfo
if fi, e = os.Stat( pR.Path); e != nil {
return
} else {
dfi = &DMFileInfo{
Name: fi.Name(),
Size: fi.Size(),
Mode: fi.Mode(),
ModTime: fi.ModTime(),
IsDir: fi.IsDir(),
Sys: fi.Sys(),
}
pR.Name = dfi.Name
pR.Size = dfi.Size
pR.Mode = dfi.Mode
pR.ModTime = dfi.ModTime
pR.IsDir = dfi.IsDir
pR.Sys = dfi.Sys
return
}
}
func ( R DMResource ) Exists() bool {
_, err := os.Stat( R.Path )
if err != nil {
if os.IsExist( err ) {
return true
}
return false
}
return true
}
// 与IsDir区分
func ( R DMResource ) IsDire() bool {
s, err := os.Stat( R.Path )
if err != nil {
return false
}
return s.IsDir()
}
// 递归全部的子项目个数
func ( R DMResource ) LsRecruitCount() int {
return len( R.LsRecruit( nil ) )
}
// 如果目录下有exe文件,说明这个目录为二进制软件的目录
// 判断方式1
func ( R DMResource ) IsBinaryDirectory1() bool {
if !R.IsDire() {
return false
}
if rs, e := R.Ls(); e != nil {
return false
} else {
for _, r := range rs {
if r.IsFile() {
if ext, _ := r.GetExt(); ext == ".exe" {
return true
}
}
}
return false
}
}
func ( R DMResource ) IsFile() bool {
return !R.IsDire()
}
// 获取某个文件的md5
// 路径直接返回空字符串
func ( R DMResource ) GetMD5() ( md5str string, e error ) {
e = nil
md5str = ""
if R.IsDire() {
// e = errors.New("cannot get md5 from a directory")
return "", nil
} else {
return fileMD5( R.Path )
}
}
// 将MD5运算暂时保存
func ( R DMResource ) GetMD5Mask() ( md5str string, e error ) {
e = nil
md5str = ""
if R.IsDire() {
// e = errors.New("cannot get md5 from a directory")
return "", nil
} else {
return "md5notcomputed", nil
}
}
// https://stackoverflow.com/questions/43000621/how-can-go-md5-be-so-fast-crypto-md5
func fileMD5(path string) (string, error) {
var md5str string
file, err := os.Open(path)
if err != nil {
return md5str, err
}
defer file.Close()
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return md5str, err
}
hashInBytes := hash.Sum(nil)[:16]
md5str = hex.EncodeToString(hashInBytes)
return md5str, nil
}
// 枚举所有的子文件夹
// ls会自动调用所有子资源的GetBasicFileInfo进行填充
func ( R DMResource ) Ls() ( rs []DMResource, e error ) {
rs = []DMResource{}
var fs []os.FileInfo
if R.IsFile() {
e = errors.New("cannot ls a file")
return
}
if fs, e = ioutil.ReadDir( R.Path ); e != nil {
return
} else {
for i, f := range fs {
rs = append( rs, DMResource{
Path: R.Path +"/"+ f.Name(),
} )
_, e = rs[i].GetBasicFileInfo()
}
return
}
}
// 枚举所有的子文件夹
// ls会自动调用所有子资源的GetBasicFileInfo进行填充
func ( R DMResource ) LsLimited( head, end int ) ( totalCount int, rs []DMResource, e error ) {
rs = []DMResource{}
var fs []os.FileInfo
if R.IsFile() {
e = errors.New("cannot ls a file")
return
}
if fs, e = ioutil.ReadDir( R.Path ); e != nil {
return
} else {
totalCount = len( fs )
if end == -1 {
end = len(fs)
}
for i, f := range fs[head:end] {
rs = append( rs, DMResource{
Path: R.Path +"/"+ f.Name(),
} )
_, e = rs[i].GetBasicFileInfo()
}
return
}
}
// 获取文件扩展名
// 返回形如 ".ext"
// e != nil 时表明路径非法或为一个目录
func ( R DMResource ) GetExt() ( ext string, e error ) {
if R.IsFile() {
ext = path.Ext( R.Path )
return
} else {
e = errors.New("cannot get ext from a directory")
return
}
}
// dm_1.DMExts
func ( R DMResource ) GetGenre() string {
if R.IsDire() {
if R.IsBinaryDirectory1() {
return "binary"
}
return "directory"
} else {
ext, _ := R.GetExt()
for k, v := range DMExts {
for _, e := range v {
if ext == e {
return k
}
}
}
return "binary"
}
}
// 包含 R 自身
func ( R DMResource ) LsRecruit( status *DMTaskStatus ) []DMResource {
if status != nil {
status.ProgressStage = "walking directories..."
}
return append( DMRecruitLs( R, status), R )
}
func dirSize(path string) (int64, error) {
var size int64
err := filepath.Walk( path, func(_ string, info os.FileInfo, err error) error {
defer func() {
if e := recover(); e != nil {
glg.Error( e ); glg.Warn( "[in dirSize] this maybe not a critical problem")
}
}()
if cfg.IsIgnoreResource( path ) {
glg.Log("path["+path+"] ignored")
return nil
}
if !info.IsDir() {
size += info.Size()
}
return err
})
return size, err
}
// 获取文件或文件夹的大小
// 与属性Size不同,当接收者为路径时GetSize可以返回文件夹的递归大小
func ( R DMResource ) GetSize() ( int64, error ) {
if R.IsDire() {
return dirSize( R.Path )
} else {
return R.Size, nil
}
}