2020-01-15 17:21:27 +00:00
|
|
|
package webdav
|
|
|
|
|
|
|
|
import (
|
2020-01-21 20:19:44 +00:00
|
|
|
"io"
|
2020-01-15 17:21:27 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-01-17 10:30:42 +00:00
|
|
|
|
|
|
|
"github.com/emersion/go-webdav/internal"
|
2020-01-15 17:21:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type LocalFileSystem string
|
|
|
|
|
|
|
|
func (fs LocalFileSystem) path(name string) (string, error) {
|
|
|
|
if (filepath.Separator != '/' && strings.IndexRune(name, filepath.Separator) >= 0) || strings.Contains(name, "\x00") {
|
2020-01-17 10:30:42 +00:00
|
|
|
return "", internal.HTTPErrorf(http.StatusBadRequest, "webdav: invalid character in path")
|
2020-01-15 17:21:27 +00:00
|
|
|
}
|
|
|
|
name = path.Clean(name)
|
|
|
|
if !path.IsAbs(name) {
|
2020-01-17 10:30:42 +00:00
|
|
|
return "", internal.HTTPErrorf(http.StatusBadRequest, "webdav: expected absolute path")
|
2020-01-15 17:21:27 +00:00
|
|
|
}
|
|
|
|
return filepath.Join(string(fs), filepath.FromSlash(name)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs LocalFileSystem) Open(name string) (File, error) {
|
|
|
|
p, err := fs.path(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return os.Open(p)
|
|
|
|
}
|
|
|
|
|
2020-01-21 18:55:02 +00:00
|
|
|
func (fs LocalFileSystem) Stat(name string) (os.FileInfo, error) {
|
|
|
|
p, err := fs.path(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return os.Stat(p)
|
|
|
|
}
|
|
|
|
|
2020-01-21 18:59:09 +00:00
|
|
|
func (fs LocalFileSystem) Readdir(name string) ([]os.FileInfo, error) {
|
|
|
|
p, err := fs.path(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f, err := os.Open(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
return f.Readdir(-1)
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:19:44 +00:00
|
|
|
func (fs LocalFileSystem) Create(name string) (io.WriteCloser, error) {
|
|
|
|
p, err := fs.path(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return os.Create(p)
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:46:01 +00:00
|
|
|
func (fs LocalFileSystem) RemoveAll(name string) error {
|
|
|
|
p, err := fs.path(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-21 20:49:54 +00:00
|
|
|
|
|
|
|
// WebDAV semantics are that it should return a "404 Not Found" error in
|
|
|
|
// case the resource doesn't exist. We need to Stat before RemoveAll.
|
|
|
|
if _, err = os.Stat(p); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:46:01 +00:00
|
|
|
return os.RemoveAll(p)
|
|
|
|
}
|
|
|
|
|
2020-01-15 17:21:27 +00:00
|
|
|
var _ FileSystem = LocalFileSystem("")
|