-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
102 lines (87 loc) · 1.71 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
package gfx
import (
"os"
"syscall"
)
var (
_ = Create
_ = Rename
_ = Move
_ = Delete
)
// Create 创建文件或者目录
// 默认创建文件
// 默认权限是0777
func Create(path string, opts ...option) (err error) {
_options := defaultOptions()
for _, opt := range opts {
opt.apply(_options)
}
/*if existsWithPath(path) {
switch _options.writeMode {
case WriteModeError:
err = errFileExists
case WriteModeSkip:
return
case WriteModeOverride:
err = Delete(path)
case WriteModeRename:
path = NewName(path)
}
}*/
if nil != err {
return
}
// 创建文件或者目录
switch _options.typ {
case TypeDir:
err = os.MkdirAll(path, _options.fileMode)
default:
_, err = os.Create(path)
}
if nil != err {
return
}
// 改变文件的拥有者
if nil != _options.owner {
err = os.Chown(path, _options.owner.uid, _options.owner.gid)
}
return
}
// Rename 重命名文件或者目录
func Rename(from string, to string) (err error) {
/*if !existsWithPath(filepath.Directory(to)) {
err = os.MkdirAll(to, os.ModePerm)
}*/
err = syscall.Rename(from, to)
return
}
// Move 移动文件
func Move(from string, to string) error {
return Rename(from, to)
}
// Delete 删除文件或者目录
func Delete(filename string) error {
return os.RemoveAll(filename)
}
// Is 判断所给路径是否为文件或者目录
func Is(path string, opts ...isOption) (is bool, err error) {
_options := defaultIsOptions()
for _, opt := range opts {
opt.applyCheck(_options)
}
var stat os.FileInfo
if stat, err = os.Stat(path); nil != err {
return
}
switch _options.typ {
case TypeDir:
is = stat.IsDir()
case TypeFile:
is = !stat.IsDir()
default:
is = true
}
stat = nil
return
}