-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileio.go
More file actions
47 lines (42 loc) · 1.11 KB
/
fileio.go
File metadata and controls
47 lines (42 loc) · 1.11 KB
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
package enstore
import (
"fmt"
"io/ioutil"
"os"
)
type LocalFileReadWriter struct {
BasePath string
}
func (lfrw *LocalFileReadWriter) Read(filename string) ([]byte, error) {
if lfrw.BasePath != "" {
if lfrw.BasePath[len(lfrw.BasePath)-1] == '/' {
filename = fmt.Sprintf("%s%s", lfrw.BasePath, filename)
} else {
filename = fmt.Sprintf("%s/%s", lfrw.BasePath, filename)
}
}
return ioutil.ReadFile(filename)
}
func (lfrw *LocalFileReadWriter) Write(filename string, bytes []byte) error {
if lfrw.BasePath != "" {
if lfrw.BasePath[len(lfrw.BasePath)-1] == '/' {
filename = fmt.Sprintf("%s%s", lfrw.BasePath, filename)
} else {
filename = fmt.Sprintf("%s/%s", lfrw.BasePath, filename)
}
}
return ioutil.WriteFile(filename, bytes, 0644)
}
func (lfrw *LocalFileReadWriter) Exists(filename string) bool {
if lfrw.BasePath != "" {
if lfrw.BasePath[len(lfrw.BasePath)-1] == '/' {
filename = fmt.Sprintf("%s%s", lfrw.BasePath, filename)
} else {
filename = fmt.Sprintf("%s/%s", lfrw.BasePath, filename)
}
}
if _, err := os.Stat(filename); os.IsNotExist(err) {
return false
}
return true
}