2020-01-15 17:21:27 +00:00
|
|
|
package webdav
|
|
|
|
|
|
|
|
import (
|
2020-01-22 10:51:05 +00:00
|
|
|
"fmt"
|
2020-01-21 20:19:44 +00:00
|
|
|
"io"
|
2020-01-21 21:43:13 +00:00
|
|
|
"mime"
|
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
|
|
|
|
|
2020-01-22 10:07:30 +00:00
|
|
|
func (fs LocalFileSystem) localPath(name string) (string, error) {
|
2020-01-15 17:21:27 +00:00
|
|
|
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-22 12:00:42 +00:00
|
|
|
return "", internal.HTTPErrorf(http.StatusBadRequest, "webdav: expected absolute path, got %q", name)
|
2020-01-15 17:21:27 +00:00
|
|
|
}
|
|
|
|
return filepath.Join(string(fs), filepath.FromSlash(name)), nil
|
|
|
|
}
|
|
|
|
|
2020-01-22 10:07:30 +00:00
|
|
|
func (fs LocalFileSystem) externalPath(name string) (string, error) {
|
|
|
|
rel, err := filepath.Rel(string(fs), name)
|
2020-01-22 09:41:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-01-22 10:07:30 +00:00
|
|
|
return "/" + filepath.ToSlash(rel), nil
|
2020-01-22 09:41:20 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 21:19:34 +00:00
|
|
|
func (fs LocalFileSystem) Open(name string) (io.ReadCloser, error) {
|
2020-01-22 10:07:30 +00:00
|
|
|
p, err := fs.localPath(name)
|
2020-01-15 17:21:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return os.Open(p)
|
|
|
|
}
|
|
|
|
|
2020-01-22 10:07:30 +00:00
|
|
|
func fileInfoFromOS(p string, fi os.FileInfo) *FileInfo {
|
2020-01-21 21:36:42 +00:00
|
|
|
return &FileInfo{
|
2020-01-22 10:07:30 +00:00
|
|
|
Path: p,
|
2020-01-21 21:36:42 +00:00
|
|
|
Size: fi.Size(),
|
|
|
|
ModTime: fi.ModTime(),
|
|
|
|
IsDir: fi.IsDir(),
|
2020-01-21 21:43:13 +00:00
|
|
|
// TODO: fallback to http.DetectContentType?
|
2020-01-22 10:07:30 +00:00
|
|
|
MIMEType: mime.TypeByExtension(path.Ext(p)),
|
2020-01-22 10:51:05 +00:00
|
|
|
// RFC 2616 section 13.3.3 describes strong ETags. Ideally these would
|
|
|
|
// be checksums or sequence numbers, however these are expensive to
|
|
|
|
// compute. The modification time with nanosecond granularity is good
|
|
|
|
// enough, as it's very unlikely for the same file to be modified twice
|
|
|
|
// during a single nanosecond.
|
|
|
|
ETag: fmt.Sprintf("%x%x", fi.ModTime().UnixNano(), fi.Size()),
|
2020-01-21 21:36:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs LocalFileSystem) Stat(name string) (*FileInfo, error) {
|
2020-01-22 10:07:30 +00:00
|
|
|
p, err := fs.localPath(name)
|
2020-01-21 18:55:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-21 21:36:42 +00:00
|
|
|
fi, err := os.Stat(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fileInfoFromOS(name, fi), nil
|
2020-01-21 18:55:02 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 09:41:20 +00:00
|
|
|
func (fs LocalFileSystem) Readdir(name string, recursive bool) ([]FileInfo, error) {
|
2020-01-22 10:14:09 +00:00
|
|
|
path, err := fs.localPath(name)
|
2020-01-21 18:59:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-21 21:36:42 +00:00
|
|
|
|
2020-01-22 09:41:20 +00:00
|
|
|
var l []FileInfo
|
2020-01-22 10:14:09 +00:00
|
|
|
err = filepath.Walk(path, func(p string, fi os.FileInfo, err error) error {
|
2020-01-22 09:41:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-22 10:07:30 +00:00
|
|
|
href, err := fs.externalPath(p)
|
2020-01-22 09:41:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
l = append(l, *fileInfoFromOS(href, fi))
|
|
|
|
|
2020-01-22 10:14:09 +00:00
|
|
|
if !recursive && fi.IsDir() && path != p {
|
2020-01-22 09:41:20 +00:00
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return l, err
|
2020-01-21 18:59:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 20:19:44 +00:00
|
|
|
func (fs LocalFileSystem) Create(name string) (io.WriteCloser, error) {
|
2020-01-22 10:07:30 +00:00
|
|
|
p, err := fs.localPath(name)
|
2020-01-21 20:19:44 +00:00
|
|
|
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 {
|
2020-01-22 10:07:30 +00:00
|
|
|
p, err := fs.localPath(name)
|
2020-01-21 20:46:01 +00:00
|
|
|
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-21 21:05:59 +00:00
|
|
|
func (fs LocalFileSystem) Mkdir(name string) error {
|
2020-01-22 10:07:30 +00:00
|
|
|
p, err := fs.localPath(name)
|
2020-01-21 21:05:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Mkdir(p, 0755)
|
|
|
|
}
|
|
|
|
|
2020-01-22 12:00:42 +00:00
|
|
|
func copyRegularFile(src, dst string, perm os.FileMode) error {
|
|
|
|
srcFile, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcFile.Close()
|
|
|
|
|
|
|
|
dstFile, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, perm)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: send http.StatusConflict on os.IsNotExist
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dstFile.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(dstFile, srcFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dstFile.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs LocalFileSystem) Copy(src, dst string, recursive, overwrite bool) (created bool, err error) {
|
2020-01-22 10:43:36 +00:00
|
|
|
srcPath, err := fs.localPath(src)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
dstPath, err := fs.localPath(dst)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-01-22 12:00:42 +00:00
|
|
|
// TODO: "Note that an infinite-depth COPY of /A/ into /A/B/ could lead to
|
|
|
|
// infinite recursion if not handled correctly"
|
|
|
|
|
|
|
|
srcInfo, err := os.Stat(srcPath)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
srcPerm := srcInfo.Mode() & os.ModePerm
|
|
|
|
|
2020-01-22 10:43:36 +00:00
|
|
|
if _, err := os.Stat(dstPath); err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
created = true
|
|
|
|
} else {
|
2020-01-22 12:00:42 +00:00
|
|
|
if !overwrite {
|
|
|
|
return false, os.ErrExist
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(dstPath); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = filepath.Walk(srcPath, func(p string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
if err := os.Mkdir(dstPath, srcPerm); err != nil {
|
|
|
|
return err
|
2020-01-22 10:43:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-01-22 12:00:42 +00:00
|
|
|
if err := copyRegularFile(srcPath, dstPath, srcPerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() && !recursive {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return created, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs LocalFileSystem) MoveAll(src, dst string, overwrite bool) (created bool, err error) {
|
|
|
|
srcPath, err := fs.localPath(src)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
dstPath, err := fs.localPath(dst)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(dstPath); err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
created = true
|
|
|
|
} else {
|
|
|
|
if !overwrite {
|
2020-01-22 10:43:36 +00:00
|
|
|
return false, os.ErrExist
|
|
|
|
}
|
2020-01-22 12:00:42 +00:00
|
|
|
if err := os.RemoveAll(dstPath); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-01-22 10:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Rename(srcPath, dstPath); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return created, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 17:21:27 +00:00
|
|
|
var _ FileSystem = LocalFileSystem("")
|