-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
203 lines (155 loc) · 3.75 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
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
package gost
import (
"os"
"path/filepath"
)
// Creates a new, empty directory at the provided path
func CreateDir(path String) Result[any] {
err := os.Mkdir(string(path), os.ModePerm)
if err != nil {
return Err[any](err)
} else {
return Ok[any](nil)
}
}
// Removes an empty directory.
func RemoveDir(path String) Result[any] {
err := os.Remove(string(path))
if err != nil {
return Err[any](err)
} else {
return Ok[any](nil)
}
}
// Write a slice as the entire contents of a file.
func Write(path String, data []Byte) Result[any] {
file, err := os.Create(string(path))
if err != nil {
return Err[any](err)
}
defer file.Close()
casted := []byte{}
for _, b := range data {
casted = append(casted, byte(b))
}
_, err = file.Write([]byte(casted))
if err != nil {
return Err[any](err)
} else {
return Ok[any](nil)
}
}
// Removes a file from the filesystem.
func RemoveFile(path String) Result[any] {
err := os.Remove(string(path))
if err != nil {
return Err[any](err)
} else {
return Ok[any](nil)
}
}
// Rename a file or directory to a new name, replacing the original file if to already exists.
// This will not work if the new name is on a different mount point.
func Rename(from String, to String) Result[any] {
err := os.Rename(string(from), string(to))
if err != nil {
return Err[any](err)
} else {
return Ok[any](nil)
}
}
// Read the entire contents of a file into a bytes vector.
func Read(path String) Result[[]Byte] {
file, err := os.Open(string(path))
if err != nil {
return Err[[]Byte](err)
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
return Err[[]Byte](err)
}
data := make([]byte, stat.Size())
_, err = file.Read(data)
if err != nil {
return Err[[]Byte](err)
} else {
casted := []Byte{}
for _, b := range data {
casted = append(casted, Byte(b))
}
return Ok[[]Byte](casted)
}
}
type FileType struct {
typeInfo string
}
func (self FileType) IsDir() bool {
return self.typeInfo == "dir"
}
func (self FileType) IsFile() bool {
return self.typeInfo == "file"
}
func (self FileType) IsSymlink() bool {
return self.typeInfo == "symlink"
}
type DirEntry struct {
FileName String
Path String
FileType FileType
}
// Returns an iterator over the entries within a directory.
func ReadDir(path String) Result[Vec[DirEntry]] {
entries, err := os.ReadDir(string(path))
if err != nil {
return Err[Vec[DirEntry]](err)
} else {
vec := VecWithLen[DirEntry](USize(len(entries)))
for _, entry := range entries {
fileType := FileType{}
if entry.Type().Perm().IsDir() {
fileType.typeInfo = "dir"
} else if entry.Type().Perm().IsRegular() {
fileType.typeInfo = "file"
} else if entry.Type().Perm()&os.ModeSymlink != 0 {
fileType.typeInfo = "symlink"
}
entryPath := filepath.Join(string(path), entry.Name())
vec.Push(DirEntry{
FileName: String(entry.Name()),
FileType: fileType,
Path: String(entryPath),
})
}
return Ok[Vec[DirEntry]](vec)
}
}
// Copies the contents of one file to another. This function will also copy the permission bits of the original file to the destination file.
// This function will overwrite the contents of to.
func Copy(from String, to String) Result[any] {
err := os.Link(string(from), string(to))
if err != nil {
return Err[any](err)
} else {
return Ok[any](nil)
}
}
// Read the entire contents of a file into a string.
func ReadToString(path String) Result[String] {
file, err := os.Open(string(path))
if err != nil {
return Err[String](err)
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
return Err[String](err)
}
data := make([]byte, stat.Size())
_, err = file.Read(data)
if err != nil {
return Err[String](err)
} else {
return Ok[String](String(string(data)))
}
}