forked from sajari/storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal.go
91 lines (76 loc) · 2.1 KB
/
local.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
package storage
import (
"io"
"os"
"path/filepath"
"strings"
"golang.org/x/net/context"
)
// DefaultLocalCreatePathMode is the default os.FileMode used when creating directories
// during a Local.Create call.
const DefaultLocalCreatePathMode = os.FileMode(0755)
// LocalCreatePathMode is the os.FileMode used when creating directories via Local.Create
var LocalCreatePathMode = DefaultLocalCreatePathMode
// Local is a local FS and Walker implementation.
type Local string
func (l Local) fullPath(path string) string {
return filepath.Join(string(l), path)
}
func (l Local) wrapError(path string, err error) error {
if os.IsNotExist(err) {
return ¬ExistError{
Path: path,
}
}
return err
}
// Open implements FS.
func (l Local) Open(_ context.Context, path string) (*File, error) {
path = l.fullPath(path)
stat, err := os.Stat(path)
if err != nil {
return nil, l.wrapError(path, err)
}
f, err := os.Open(path)
if err != nil {
return nil, l.wrapError(path, err)
}
return &File{
ReadCloser: f,
Name: stat.Name(),
ModTime: stat.ModTime(),
Size: stat.Size(),
}, nil
}
// Create implements FS. If the path contains any directories which do not already exist
// then Create will try to make them, returning an error if it fails.
func (l Local) Create(_ context.Context, path string) (io.WriteCloser, error) {
dir := l.fullPath(filepath.Dir(path))
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, LocalCreatePathMode); err != nil {
return nil, err
}
}
f, err := os.Create(l.fullPath(path))
if err != nil {
return nil, err
}
return f, nil
}
// Delete implements FS. All files underneath path will be removed.
func (l Local) Delete(_ context.Context, path string) error {
return os.RemoveAll(l.fullPath(path))
}
// Walk implements Walker.
func (l Local) Walk(_ context.Context, path string, fn WalkFn) error {
return filepath.Walk(l.fullPath(path), func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if !f.IsDir() {
path = strings.TrimPrefix(path, string(l))
return fn(path)
}
return nil
})
}