This repository has been archived by the owner on Dec 12, 2018. It is now read-only.
forked from der-antikeks/go-webdav
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfilesystem.go
113 lines (93 loc) · 2.28 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
package webdav
import (
"io"
"os"
"path"
"path/filepath"
"strings"
)
// A FileSystem implements access to a collection of named files.
// The elements in a file path are separated by slash ('/', U+002F)
// characters, regardless of host operating system convention.
type FileSystem interface {
Open(name string) (File, error)
Create(name string) (File, error)
Mkdir(path string) error
Remove(name string) error
}
// A File is returned by a FileSystem's Open and Create method and can
// be served by the FileServer implementation.
type File interface {
Stat() (os.FileInfo, error)
Readdir(count int) ([]os.FileInfo, error)
Read([]byte) (int, error)
Write(p []byte) (n int, err error)
Seek(offset int64, whence int) (int64, error)
Close() error
/* TODO: needed?
Chdir() error
Chmod(mode FileMode) error
Chown(uid, gid int) error
*/
}
// A Dir implements webdav.FileSystem using the native file
// system restricted to a specific directory tree.
//
// An empty Dir is treated as ".".
type Dir string
func (d Dir) sanitizePath(name string) (string, error) {
if filepath.Separator != '/' && strings.IndexRune(name, filepath.Separator) >= 0 ||
strings.Contains(name, "\x00") {
return "", ErrInvalidCharPath
}
dir := string(d)
if dir == "" {
dir = "."
}
return filepath.Join(dir, filepath.FromSlash(path.Clean("/"+name))), nil
}
func (d Dir) Open(name string) (File, error) {
p, err := d.sanitizePath(name)
if err != nil {
return nil, err
}
f, err := os.Open(p)
if err != nil {
return nil, err
}
return f, nil
}
func (d Dir) Create(name string) (File, error) {
p, err := d.sanitizePath(name)
if err != nil {
return nil, err
}
f, err := os.Create(p)
if err != nil {
return nil, err
}
return f, nil
}
// Mkdir creates a new directory with the specified name
func (d Dir) Mkdir(name string) error {
p, err := d.sanitizePath(name)
if err != nil {
return err
}
return os.Mkdir(p, os.ModePerm)
}
func (d Dir) Remove(name string) error {
p, err := d.sanitizePath(name)
if err != nil {
return err
}
return os.Remove(p)
}
// mockup zero content file aka only header
type emptyFile struct{}
func (e emptyFile) Read(p []byte) (n int, err error) {
return 0, io.EOF
}
func (e emptyFile) Seek(offset int64, whence int) (ret int64, err error) {
return 0, nil
}