-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathfunc_file.go
189 lines (163 loc) · 3.27 KB
/
func_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
package antnet
import (
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"sync"
"sync/atomic"
)
func PathBase(p string) string {
return path.Base(p)
}
func PathAbs(p string) string {
f, err := filepath.Abs(p)
if err != nil {
LogError("get abs path failed path:%v err:%v", p, err)
return ""
}
return f
}
func GetEXEDir() string {
return PathDir(GetEXEPath())
}
func GetEXEPath() string {
return PathAbs(os.Args[0])
}
func GetExeName() string {
return PathBase(GetEXEPath())
}
func GetExeDir() string {
return PathDir(GetEXEPath())
}
func GetExePath() string {
return PathAbs(os.Args[0])
}
func GetEXEName() string {
return PathBase(GetEXEPath())
}
func PathDir(p string) string {
return path.Dir(p)
}
func PathExt(p string) string {
return path.Ext(p)
}
func PathClean(p string) string {
return path.Clean(p)
}
func PathExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
func NewDir(path string) error {
return os.MkdirAll(path, 0777)
}
func ReadFile(path string) ([]byte, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
LogError("read file filed path:%v err:%v", path, err)
return nil, ErrFileRead
}
return data, nil
}
func WriteFile(path string, data []byte) {
dir := PathDir(path)
if !PathExists(dir) {
NewDir(dir)
}
ioutil.WriteFile(path, data, 0777)
}
func GetFiles(path string) []string {
files := []string{}
filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
if f == nil {
return err
}
if f.IsDir() {
return nil
}
files = append(files, path)
return nil
})
return files
}
func DelFile(path string) {
os.Remove(path)
}
func DelDir(path string) {
os.RemoveAll(path)
}
func CreateFile(path string) (*os.File, error) {
dir := PathDir(path)
if !PathExists(dir) {
NewDir(dir)
}
return os.Create(path)
}
func CopyFile(dst io.Writer, src io.Reader) (written int64, err error) {
return io.Copy(dst, src)
}
func walkDirTrue(dir string, level int, maxLevel int, wg *sync.WaitGroup, fun func(path string, info os.FileInfo)) {
wg.Add(1)
defer wg.Done()
infos, err := ioutil.ReadDir(dir)
if err != nil {
LogError("walk dir failed dir:%v err:%v", dir, err)
return
}
for _, info := range infos {
if info.IsDir() {
subDir := filepath.Join(dir, info.Name())
fun(subDir, info)
if maxLevel > 0 && level+1 > maxLevel {
continue
}
go walkDirTrue(subDir, level+1, maxLevel, wg, fun)
} else {
file := filepath.Join(dir, info.Name())
fun(file, info)
}
}
}
func WalkDir(dir string, maxLevel int, fun func(dir string, info os.FileInfo)) {
if fun == nil {
return
}
wg := &sync.WaitGroup{}
walkDirTrue(dir, 0, maxLevel, wg, fun)
wg.Wait()
}
func FileCount(dir string) int32 {
var count int32 = 0
WalkDir(dir, -1, func(dir string, info os.FileInfo) {
if !info.IsDir() {
atomic.AddInt32(&count, 1)
}
})
return count
}
func DirCount(dir string) int32 {
var count int32 = 0
WalkDir(dir, -1, func(dir string, info os.FileInfo) {
if info.IsDir() {
atomic.AddInt32(&count, 1)
}
})
return count
}
func DirSize(dir string) int64 {
var size int64 = 0
WalkDir(dir, -1, func(dir string, info os.FileInfo) {
if !info.IsDir() {
atomic.AddInt64(&size, info.Size())
}
})
return size
}