-
Notifications
You must be signed in to change notification settings - Fork 54
/
file.go
222 lines (201 loc) · 5 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
// Package zfile file and path operations in daily development
package zfile
import (
"fmt"
"io"
"math"
"os"
"path/filepath"
"strings"
)
var RootPath = RealPath(".", true)
// PathExist PathExist
// 1 exists and is a directory path, 2 exists and is a file path, 0 does not exist
func PathExist(path string) (int, error) {
f, err := os.Stat(path)
if err == nil {
isFile := 2
if f.IsDir() {
isFile = 1
}
return isFile, nil
}
return 0, err
}
// DirExist Is it an existing directory
func DirExist(path string) bool {
state, _ := PathExist(path)
return state == 1
}
// FileExist Is it an existing file?
func FileExist(path string) bool {
state, _ := PathExist(path)
return state == 2
}
// FileSize file size
func FileSize(file string) (size string) {
fileInfo, err := os.Stat(file)
if err != nil {
size = FileSizeFormat(0)
} else {
size = FileSizeFormat(uint64(fileInfo.Size()))
}
return
}
// FileSizeFormat Format file size
func FileSizeFormat(s uint64) string {
sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
humanateBytes := func(s uint64, base float64, sizes []string) string {
if s < 10 {
return fmt.Sprintf("%d B", s)
}
e := math.Floor(logSize(float64(s), base))
suffix := sizes[int(e)]
val := float64(s) / math.Pow(base, math.Floor(e))
f := "%.0f"
if val < 10 {
f = "%.1f"
}
return fmt.Sprintf(f+" %s", val, suffix)
}
return humanateBytes(s, 1024, sizes)
}
func logSize(n, b float64) float64 {
return math.Log(n) / math.Log(b)
}
// SafePath get an safe absolute path
func SafePath(path string, addSlash ...bool) string {
realPath := RealPath(path, addSlash...)
return strings.TrimPrefix(realPath, RootPath)
}
// RealPath get an absolute path
func RealPath(path string, addSlash ...bool) (realPath string) {
realPath, _ = filepath.Abs(path)
realPath = pathAddSlash(filepath.ToSlash(realPath), addSlash...)
return
}
// RealPathMkdir get an absolute path, create it if it doesn't exist
func RealPathMkdir(path string, addSlash ...bool) string {
realPath := RealPath(path, addSlash...)
if DirExist(realPath) {
return realPath
}
_ = os.MkdirAll(path, os.ModePerm)
return realPath
}
// Rmdir rmdir,support to keep the current directory
func Rmdir(path string, notIncludeSelf ...bool) (ok bool) {
realPath := RealPath(path)
err := os.RemoveAll(realPath)
ok = err == nil
if ok && len(notIncludeSelf) > 0 && notIncludeSelf[0] {
_ = os.Mkdir(path, os.ModePerm)
}
return
}
// CopyFile copies the source file to the dest file.
func CopyFile(source string, dest string) (err error) {
sourcefile, err := os.Open(source)
if err != nil {
return err
}
defer sourcefile.Close()
destfile, err := os.Create(dest)
if err != nil {
return err
}
defer destfile.Close()
_, err = io.Copy(destfile, sourcefile)
if err == nil {
sourceinfo, err := os.Stat(source)
if err != nil {
err = os.Chmod(dest, sourceinfo.Mode())
}
}
return nil
}
// CopyDir copies the source directory to the dest directory.
func CopyDir(source string, dest string, filterFn ...func(srcFilePath, destFilePath string) bool) (err error) {
sourceinfo, err := os.Stat(source)
if err != nil {
return err
}
err = os.MkdirAll(dest, sourceinfo.Mode())
if err != nil {
return err
}
directory, err := os.Open(source)
if err != nil {
return err
}
defer directory.Close()
objects, err := directory.Readdir(-1)
if err != nil {
return err
}
var filter func(srcFilePath, destFilePath string) bool
if len(filterFn) > 0 {
filter = filterFn[0]
}
copySum := len(objects)
for _, obj := range objects {
srcFilePath := filepath.Join(source, obj.Name())
destFilePath := filepath.Join(dest, obj.Name())
if obj.IsDir() {
_ = CopyDir(srcFilePath, destFilePath, filterFn...)
} else if filter == nil || filter(srcFilePath, destFilePath) {
_ = CopyFile(srcFilePath, destFilePath)
} else {
copySum--
}
}
if copySum < 1 {
Rmdir(dest)
}
return nil
}
// ProgramPath program directory path
func ProgramPath(addSlash ...bool) (path string) {
ePath, err := os.Executable()
if err != nil {
ePath = RealPath(".")
}
path = pathAddSlash(filepath.Dir(ePath), addSlash...)
return
}
func pathAddSlash(path string, addSlash ...bool) string {
if len(addSlash) > 0 && addSlash[0] && !strings.HasSuffix(path, "/") {
path += "/"
}
return path
}
// PutOffset open the specified file and write data from the specified location
func PutOffset(path string, b []byte, offset int64) (err error) {
var file *os.File
if FileExist(path) {
file, err = os.OpenFile(path, os.O_WRONLY, os.ModeAppend)
} else {
file, err = os.Create(path)
}
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteAt(b, offset)
return err
}
// PutAppend open the specified file and write data at the end of the file
func PutAppend(path string, b []byte) (err error) {
var file *os.File
if FileExist(path) {
file, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
} else {
file, err = os.Create(path)
}
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(b)
return err
}