tstor/webdav/fs.go

218 lines
4 KiB
Go
Raw Normal View History

2021-03-01 18:04:59 +00:00
package webdav
import (
"context"
"io"
"os"
2021-11-18 19:36:07 +00:00
"path/filepath"
2021-03-01 18:04:59 +00:00
"sync"
"time"
"github.com/distribyted/distribyted/fs"
"github.com/distribyted/distribyted/iio"
"golang.org/x/net/webdav"
)
var _ webdav.FileSystem = &WebDAV{}
type WebDAV struct {
2021-04-04 17:24:58 +00:00
fs fs.Filesystem
2021-03-01 18:04:59 +00:00
}
2021-04-04 17:24:58 +00:00
func newFS(fs fs.Filesystem) *WebDAV {
return &WebDAV{fs: fs}
2021-03-01 18:04:59 +00:00
}
func (wd *WebDAV) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) {
p := "/" + name
// TODO handle flag and permissions
f, err := wd.lookupFile(p)
if err != nil {
return nil, err
}
2021-11-29 10:07:54 +00:00
wdf := newFile(filepath.Base(p), f, func() ([]os.FileInfo, error) {
return wd.listDir(p)
})
2021-03-01 18:04:59 +00:00
return wdf, nil
}
func (wd *WebDAV) Stat(ctx context.Context, name string) (os.FileInfo, error) {
p := "/" + name
f, err := wd.lookupFile(p)
if err != nil {
return nil, err
}
fi := newFileInfo(name, f.Size(), f.IsDir())
return fi, nil
}
func (wd *WebDAV) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
return webdav.ErrNotImplemented
}
func (wd *WebDAV) RemoveAll(ctx context.Context, name string) error {
return webdav.ErrNotImplemented
}
func (wd *WebDAV) Rename(ctx context.Context, oldName, newName string) error {
return webdav.ErrNotImplemented
}
func (wd *WebDAV) lookupFile(path string) (fs.File, error) {
2021-04-04 17:24:58 +00:00
return wd.fs.Open(path)
2021-03-01 18:04:59 +00:00
}
func (wd *WebDAV) listDir(path string) ([]os.FileInfo, error) {
2021-04-04 17:24:58 +00:00
files, err := wd.fs.ReadDir(path)
if err != nil {
return nil, err
}
2021-03-01 18:04:59 +00:00
2021-04-04 17:24:58 +00:00
var out []os.FileInfo
for n, f := range files {
out = append(out, newFileInfo(n, f.Size(), f.IsDir()))
2021-03-01 18:04:59 +00:00
}
return out, nil
}
var _ webdav.File = &webDAVFile{}
type webDAVFile struct {
iio.Reader
fi os.FileInfo
2021-11-29 10:07:54 +00:00
mudp sync.Mutex
dirPos int
mup sync.Mutex
2021-03-01 18:04:59 +00:00
pos int64
2021-11-29 10:07:54 +00:00
dirFunc func() ([]os.FileInfo, error)
2021-03-01 18:04:59 +00:00
dirContent []os.FileInfo
}
2021-11-29 10:07:54 +00:00
func newFile(name string, f fs.File, df func() ([]os.FileInfo, error)) *webDAVFile {
2021-03-01 18:04:59 +00:00
return &webDAVFile{
2021-11-29 10:07:54 +00:00
fi: newFileInfo(name, f.Size(), f.IsDir()),
dirFunc: df,
Reader: f,
2021-03-01 18:04:59 +00:00
}
}
func (wdf *webDAVFile) Readdir(count int) ([]os.FileInfo, error) {
2021-11-29 10:07:54 +00:00
wdf.mudp.Lock()
defer wdf.mudp.Unlock()
2021-03-01 18:04:59 +00:00
if !wdf.fi.IsDir() {
return nil, os.ErrInvalid
}
2021-11-29 10:07:54 +00:00
if wdf.dirContent == nil {
dc, err := wdf.dirFunc()
if err != nil {
return nil, err
}
wdf.dirContent = dc
}
2021-03-01 18:04:59 +00:00
old := wdf.dirPos
if old >= len(wdf.dirContent) {
// The os.File Readdir docs say that at the end of a directory,
// the error is io.EOF if count > 0 and nil if count <= 0.
if count > 0 {
return nil, io.EOF
}
return nil, nil
}
if count > 0 {
wdf.dirPos += count
if wdf.dirPos > len(wdf.dirContent) {
wdf.dirPos = len(wdf.dirContent)
}
} else {
wdf.dirPos = len(wdf.dirContent)
old = 0
}
return wdf.dirContent[old:wdf.dirPos], nil
}
func (wdf *webDAVFile) Stat() (os.FileInfo, error) {
return wdf.fi, nil
}
func (wdf *webDAVFile) Read(p []byte) (int, error) {
2021-11-29 10:07:54 +00:00
wdf.mup.Lock()
defer wdf.mup.Unlock()
2021-03-01 18:04:59 +00:00
n, err := wdf.Reader.ReadAt(p, wdf.pos)
wdf.pos += int64(n)
return n, err
}
func (wdf *webDAVFile) Seek(offset int64, whence int) (int64, error) {
2021-11-29 10:07:54 +00:00
wdf.mup.Lock()
defer wdf.mup.Unlock()
2021-03-01 18:04:59 +00:00
switch whence {
case io.SeekStart:
wdf.pos = offset
case io.SeekCurrent:
wdf.pos = wdf.pos + offset
case io.SeekEnd:
wdf.pos = wdf.fi.Size() + offset
}
return wdf.pos, nil
}
func (wdf *webDAVFile) Write(p []byte) (n int, err error) {
return 0, webdav.ErrNotImplemented
}
type webDAVFileInfo struct {
name string
size int64
isDir bool
}
func newFileInfo(name string, size int64, isDir bool) *webDAVFileInfo {
return &webDAVFileInfo{
name: name,
size: size,
isDir: isDir,
}
}
func (wdfi *webDAVFileInfo) Name() string {
return wdfi.name
}
func (wdfi *webDAVFileInfo) Size() int64 {
return wdfi.size
}
func (wdfi *webDAVFileInfo) Mode() os.FileMode {
if wdfi.isDir {
return 0555 | os.ModeDir
}
return 0555
}
func (wdfi *webDAVFileInfo) ModTime() time.Time {
// TODO fix it
return time.Now()
}
func (wdfi *webDAVFileInfo) IsDir() bool {
return wdfi.isDir
}
func (wdfi *webDAVFileInfo) Sys() interface{} {
return nil
}