-
Notifications
You must be signed in to change notification settings - Fork 9
/
filesystem.go
167 lines (142 loc) · 3.51 KB
/
filesystem.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
package php
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"syscall"
)
// Basename returns trailing name component of path
func Basename(path string) string {
return filepath.Base(path)
}
// Dirname returns a parent directory's path
func Dirname(path string) string {
return filepath.Dir(path)
}
// DirnameWithLevels returns a parent directory's path which is
// levels up from the current directory.
func DirnameWithLevels(path string, levels int) string {
for i := 0; i < levels; i++ {
path = Dirname(path)
}
return path
}
// Realpath returns canonicalized absolute pathname
func Realpath(path string) string {
p, _ := filepath.Abs(path)
return p
}
// Touch creates the file if it does not exist
func Touch(filename string) error {
f, err := os.OpenFile(filename, os.O_RDONLY|os.O_CREATE, 0666)
if f != nil {
f.Close()
}
return err
}
// Unlink deletes a file
func Unlink(filename string) error {
return syscall.Unlink(filename)
}
// Mkdir attempts to create the directory specified by pathname
func Mkdir(pathname string, mode os.FileMode, recursive bool) error {
if mode == 0 {
mode = 0777
}
var err error
if recursive {
err = os.MkdirAll(pathname, os.FileMode(mode))
} else {
err = os.Mkdir(pathname, os.FileMode(mode))
}
return err
}
// Rmdir removes empty directory
func Rmdir(dirname string) error {
return syscall.Rmdir(dirname)
}
// Symlink creates a symbolic link
func Symlink(target, link string) error {
return os.Symlink(target, link)
}
// Link create a hard link
func Link(target, link string) error {
return os.Link(target, link)
}
// Chmod changes file mode
func Chmod(filename string, mode os.FileMode) error {
return os.Chmod(filename, os.FileMode(mode))
}
// Chown changes file owner and group
func Chown(filename string, uid, gid int) error {
return os.Chown(filename, uid, gid)
}
// IsFile tells whether the filename is a regular file
func IsFile(filename string) bool {
fi, err := os.Stat(filename)
if err != nil {
return false
}
return fi.Mode().IsRegular()
}
// IsDir tells whether the filename is a directory
func IsDir(filename string) bool {
fi, err := os.Stat(filename)
if err != nil {
return false
}
return fi.Mode().IsDir()
}
// IsLink tells whether the filename is a symbolic link
func IsLink(filename string) bool {
fi, err := os.Lstat(filename)
if err != nil {
return false
}
return fi.Mode()&os.ModeSymlink == os.ModeSymlink
}
// Copy copies the src file to dst, any existing file will be overwritten and will not copy file attributes
func Copy(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}
// FileExists checks whether a file or directory exists
func FileExists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
// FileSize gets file size
func FileSize(filename string) (int64, error) {
info, err := os.Stat(filename)
if err != nil {
return 0, err
}
return info.Size(), nil
}
// Rename renames a file or directory
func Rename(oldname, newname string) error {
return os.Rename(oldname, newname)
}
// FilePutContents writes data to a file
func FilePutContents(filename string, data string) error {
return ioutil.WriteFile(filename, []byte(data), 0666)
}
// FileGetContents reads entire file into a string
func FileGetContents(filename string) (string, error) {
data, err := ioutil.ReadFile(filename)
return string(data), err
}