-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileutil.go
75 lines (58 loc) · 1.46 KB
/
fileutil.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
package resonatefuse
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/pkg/errors"
)
func splitPath(path string) []string {
path = filepath.Clean(path)
if path == "." {
return make([]string, 0)
}
dir, file := filepath.Split(path)
return append(splitPath(dir), file)
}
func writeAt(name string, data []byte, offset int64) (int, error) {
file, err := os.OpenFile(name, os.O_RDWR, 0664)
if err != nil {
return 0, errors.Errorf("could not open file %v: %v", name, err)
}
defer file.Close()
n, err := file.WriteAt(data, offset)
if err != nil {
return n, errors.Errorf("could not write to file %v: %v", name, err)
}
return n, nil
}
func readAt(name string, data []byte, offset int64) (int, error) {
file, err := os.OpenFile(name, os.O_RDONLY, 0664)
if err != nil {
return 0, errors.Errorf("could not open file %v: %v", name, err)
}
defer file.Close()
n, err := file.ReadAt(data, offset)
if err != nil {
return n, errors.Errorf("could not write to file %v: %v", name, err)
}
return n, nil
}
func Touch(name string, mode os.FileMode) error {
file, err := os.OpenFile(name, os.O_RDONLY|os.O_CREATE, mode)
if err != nil {
return err
}
return file.Close()
}
func rm(name string) error {
return os.Remove(name)
}
func rename(oldn, newn string) error {
return os.Rename(oldn, newn)
}
func mkdir(name string, mode os.FileMode) error {
return os.Mkdir(name, mode)
}
func readall(path string) ([]byte, error) {
return ioutil.ReadFile(path)
}