-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs.go
159 lines (129 loc) · 3.64 KB
/
fs.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
package fsutil
import (
"bufio"
"io"
"io/fs"
"os"
"github.com/3JoB/unsafeConvert"
)
const (
O_ModePerm fs.FileMode = 511
// O_TRUNC = os.O_WRONLY|os.O_TRUNC|os.O_CREATE
O_TRUNC int = 577
// O_RDWTRUNC = os.O_RDWR|os.O_CREATE|os.O_TRUNC
O_RDWTRUNC int = 578
// O_RDONLY = os.O_RDONLY
O_RDONLY int = 0
// O_WROC = os.O_WRONLY|os.O_CREATE
O_WROC int = 65
)
func Stat(w string) (os.FileInfo, error) {
return os.Stat(w)
}
func Create(v string) (*os.File, error) {
return OpenFile(v, O_RDWTRUNC, 0666)
}
/*
CreateTemp creates a new temporary file in the directory dir,
opens the file for reading and writing, and returns the resulting file.
The filename is generated by taking pattern and adding a random string to the end.
If pattern includes a "*", the random string replaces the last "*".
If dir is the empty string, CreateTemp uses the default directory for temporary files, as returned by TempDir.
Multiple programs or goroutines calling CreateTemp simultaneously will not choose the same file.
The caller can use the file's Name method to find the pathname of the file.
It is the caller's responsibility to remove the file when it is no longer needed.
*/
func CreateTemp(dir string, pattern string) (*os.File, error) {
return os.CreateTemp(dir, pattern)
}
func Rename(old, new string) error {
return os.Rename(old, new)
}
/*
Open opens the named file for reading.
If successful, methods on the returned file can be used for reading;
the associated file descriptor has mode O_RDONLY.
If there is an error, it will be of type *PathError.
*/
func Open(v string) (*os.File, error) {
return OpenFile(v, O_RDONLY, 0)
}
func OpenRead(v string) ([]byte, error) {
o, err := Open(v)
if err != nil {
return nil, err
}
defer o.Close()
if data, err := ReadAll(o); err != nil {
return nil, err
} else {
return data, err
}
}
func CleanFile(path string) error {
return TruncWrite(path, "")
}
/*
ReadAll reads from r until an error or EOF and returns the data it read.
A successful call returns err == nil, not err == EOF.
Because ReadAll is defined to read from src until EOF,
it does not treat an EOF from Read as an error to be reported.
*/
func ReadAll(r io.Reader) ([]byte, error) {
return io.ReadAll(r)
}
func ReaderWriter(w io.Reader, r io.Writer) (*bufio.Reader, *bufio.Writer) {
return bufio.NewReader(w), bufio.NewWriter(r)
}
/*
OpenFile is the generalized open call;
most users will use Open or Create instead.
It opens the named file with specified flag (O_RDONLY etc.).
If the file does not exist, and the O_CREATE flag is passed,
it is created with mode perm (before umask).
If successful, methods on the returned File can be used for I/O.
If there is an error, it will be of type *PathError.
*/
func OpenFile(name string, flag int, perm fs.FileMode) (*os.File, error) {
return os.OpenFile(name, flag, perm)
}
func TruncWrite(path string, d any) error {
file, err := OpenFile(path, O_TRUNC, 0666)
if err != nil {
file.Close()
return err
}
defer file.Close()
return write(file, d)
}
func Write(path string, d any) error {
file, err := OpenFile(path, O_WROC, 0666)
if err != nil {
file.Close()
return err
}
defer file.Close()
return write(file, d)
}
func write(file *os.File, d any) error {
writer := bufio.NewWriter(file)
switch d := d.(type) {
case string:
writer.Write(unsafeConvert.BytesReflect(d))
case []byte:
writer.Write(d)
default:
writer.Write(unsafeConvert.BytesReflect(d.(string)))
}
writer.Flush()
return nil
}
func Mkdir(path string, mode ...fs.FileMode) error {
if len(mode) != 0 {
return os.MkdirAll(path, mode[0])
}
return os.MkdirAll(path, O_ModePerm)
}
func Remove(v string) error {
return os.RemoveAll(v)
}