-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
226 lines (193 loc) · 4.47 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
package types
import (
"github.com/geekymedic/neon/errors"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
)
const (
DefFlag = os.O_CREATE | os.O_WRONLY | os.O_EXCL
DefPerm = 0644
)
var ErrFileType = errors.NewStackError("file type error")
func NewBaseFile(abs string) *BaseFile {
var (
extraName string
fileName string
fullName string
dir string
)
dir, fullName = filepath.Split(abs)
partIdx := strings.LastIndex(fullName, ".")
if partIdx >= 0 && partIdx+1 < len(fullName) {
extraName = fullName[partIdx+1:]
}
if partIdx >= 0 {
fileName = fullName[0:partIdx]
} else {
fileName = fullName
}
return &BaseFile{
absPath: abs,
dir: filepath.Clean(dir),
name: fileName,
extraName: extraName,
}
}
// Opz add buffer for write
type BaseFile struct {
absPath string
dir string
name string
extraName string
*os.File
}
func (baseFile *BaseFile) Name() string {
return baseFile.name
}
func (baseFile *BaseFile) ExtraName() string {
return baseFile.extraName
}
func (baseFile *BaseFile) NodeType() NodeType {
return NodeTypeFile
}
func (baseFile *BaseFile) BaseDir() string {
return baseFile.dir
}
func (baseFile *BaseFile) Abs() string {
return baseFile.absPath
}
func (baseFile *BaseFile) Split() []string {
var absPath = path.Clean(baseFile.absPath)
switch OsType() {
case LinuxOs, MacOs:
return strings.Split(absPath, Separator)
case WindowsOs:
return strings.Split(absPath, Separator)
default:
panic("unsupported os type")
}
}
func (baseFile *BaseFile) Create(flag int, per os.FileMode) (err error) {
if baseFile.File != nil {
return ErrFileOpened
}
baseFile.File, err = os.OpenFile(baseFile.absPath, flag, per)
return
}
func (baseFile *BaseFile) IsExist() error {
info, err := os.Stat(baseFile.Abs())
if err != nil {
return err
}
if info.IsDir() {
return ErrFileType
}
return os.ErrExist
}
func (baseFile *BaseFile) MustCreate(flag int, per os.FileMode) {
if baseFile.File != nil {
PanicSanity(ErrFileOpened)
}
err := os.MkdirAll(baseFile.BaseDir(), os.ModePerm)
AssertNil(err)
err = baseFile.Create(flag, per)
AssertNil(err)
}
func (baseFile *BaseFile) Close() error {
return baseFile.File.Close()
}
func (baseFile *BaseFile) Remove() (err error) {
if err = baseFile.Close(); err != nil {
return
}
return os.RemoveAll(baseFile.absPath)
}
func (baseFile *BaseFile) ReadAll() ([]byte, error) {
return ioutil.ReadAll(baseFile.File)
}
func (baseFile *BaseFile) Copy(src io.Reader) (int64, error) {
return io.Copy(baseFile.File, src)
}
func (baseFile *BaseFile) Walk(fn filepath.WalkFunc) error {
return filepath.Walk(baseFile.BaseDir(), fn)
}
type BaseDir struct {
absPath string
name string
}
// dir must a directory
func NewBaseDir(dir string) *BaseDir {
absPath := filepath.Clean(dir)
_, name := filepath.Split(absPath)
//var parts = strings.Split(filepath.Clean(dir), Separator)
return &BaseDir{
absPath: absPath,
name: name,
}
}
func (baseDir *BaseDir) Name() string {
return baseDir.name
}
func (baseDir *BaseDir) NodeType() NodeType {
return NodeTypeDir
}
func (baseDir *BaseDir) BaseDir() string {
idx := strings.LastIndex(baseDir.absPath, Separator)
if idx >= 0 {
return baseDir.absPath[0:idx]
}
return "."
}
func (baseDir *BaseDir) Abs() string {
return baseDir.absPath
}
func (baseDir *BaseDir) Split() []string {
var absPath = strings.Trim(path.Clean(baseDir.absPath), Separator)
switch OsType() {
case LinuxOs, MacOs:
return strings.Split(absPath, Separator)
case WindowsOs:
// Notic
// C:\Program\neon --> [C:, Program, neon]
return strings.Split(absPath, Separator)
default:
panic("unsupported os type")
}
}
func (baseDir *BaseDir) Create(per os.FileMode) (err error) {
info, err := os.Stat(baseDir.absPath)
if err == nil {
if info.IsDir() {
return
}
return errors.NewStackError("directory has exists, but it's type is not directory")
}
return os.MkdirAll(baseDir.absPath, per)
}
func (baseDir *BaseDir) IsExist() error {
info, err := os.Stat(baseDir.Abs())
if err != nil {
return err
}
if info.IsDir() {
return os.ErrExist
}
return ErrFileType
}
func (baseDir *BaseDir) Append(dirs ...string) DireOperation {
var newDir = baseDir
for _, dir := range dirs {
newDir = NewBaseDir(newDir.Abs() + Separator + filepath.Clean(dir))
}
return newDir
}
func (baseDir *BaseDir) Remove() error {
return os.RemoveAll(baseDir.absPath)
}
func (baseDir *BaseDir) Walk(fn filepath.WalkFunc) error {
return filepath.Walk(baseDir.Abs(), fn)
}