-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
63 lines (49 loc) · 1.09 KB
/
util.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
package filesystem
import (
"io"
"os"
"strings"
"github.com/ViBiOh/absto/pkg/model"
)
const (
writeFlags = os.O_RDWR | os.O_CREATE | os.O_TRUNC
)
func checkPathname(pathname string) error {
if strings.Contains(pathname, "..") {
return ErrRelativePath
}
return nil
}
func (a App) getRelativePath(pathname string) string {
return strings.TrimPrefix(pathname, a.rootDirectory)
}
func (a App) getFile(filename string, flags int) (*os.File, error) {
return os.OpenFile(a.path(filename), flags, getMode(filename))
}
func (a App) getWritableFile(filename string) (io.WriteCloser, error) {
return a.getFile(filename, writeFlags)
}
func getMode(name string) os.FileMode {
if strings.HasSuffix(name, "/") {
return 0o700
}
return 0o600
}
func convertToItem(pathname string, info os.FileInfo) model.Item {
return model.Item{
Name: info.Name(),
Pathname: pathname,
IsDir: info.IsDir(),
Date: info.ModTime(),
Size: info.Size(),
}
}
func convertError(err error) error {
if err == nil {
return nil
}
if os.IsNotExist(err) {
return model.ErrNotExist(err)
}
return err
}