2019-05-03 05:22:10 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-05-03 15:24:35 +08:00
|
|
|
"fmt"
|
2019-05-03 05:22:10 +08:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util/buffered_writer"
|
2019-12-07 23:56:05 +08:00
|
|
|
"golang.org/x/net/webdav"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/operation"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util/chunk_cache"
|
2019-12-07 23:56:05 +08:00
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
2019-05-03 05:22:10 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type WebDavOption struct {
|
2021-09-15 01:37:06 +08:00
|
|
|
Filer pb.ServerAddress
|
2022-12-18 12:07:02 +08:00
|
|
|
FilerRootPath string
|
2021-09-15 01:37:06 +08:00
|
|
|
DomainName string
|
|
|
|
BucketsPath string
|
|
|
|
GrpcDialOption grpc.DialOption
|
|
|
|
Collection string
|
|
|
|
Replication string
|
|
|
|
DiskType string
|
|
|
|
Uid uint32
|
|
|
|
Gid uint32
|
|
|
|
Cipher bool
|
|
|
|
CacheDir string
|
|
|
|
CacheSizeMB int64
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type WebDavServer struct {
|
|
|
|
option *WebDavOption
|
|
|
|
secret security.SigningKey
|
2020-09-01 15:21:19 +08:00
|
|
|
filer *filer.Filer
|
2019-05-03 05:22:10 +08:00
|
|
|
grpcDialOption grpc.DialOption
|
|
|
|
Handler *webdav.Handler
|
|
|
|
}
|
|
|
|
|
2022-06-28 02:19:21 +08:00
|
|
|
func max(x, y int64) int64 {
|
|
|
|
if x <= y {
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2019-05-03 05:22:10 +08:00
|
|
|
func NewWebDavServer(option *WebDavOption) (ws *WebDavServer, err error) {
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
fs, _ := NewWebDavFileSystem(option)
|
2019-05-03 05:22:10 +08:00
|
|
|
|
2022-12-18 12:07:02 +08:00
|
|
|
// Fix no set filer.path , accessing "/" returns "//"
|
|
|
|
if option.FilerRootPath == "/" {
|
|
|
|
option.FilerRootPath = ""
|
|
|
|
}
|
2023-11-02 04:12:49 +08:00
|
|
|
// filer.path non "/" option means we are accessing filer's sub-folders
|
|
|
|
if option.FilerRootPath != "" {
|
|
|
|
fs = NewWrappedFs(fs, path.Clean(option.FilerRootPath))
|
|
|
|
}
|
2022-12-18 12:07:02 +08:00
|
|
|
|
2019-05-03 05:22:10 +08:00
|
|
|
ws = &WebDavServer{
|
|
|
|
option: option,
|
2020-01-30 01:09:55 +08:00
|
|
|
grpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.filer"),
|
2019-05-03 05:22:10 +08:00
|
|
|
Handler: &webdav.Handler{
|
|
|
|
FileSystem: fs,
|
|
|
|
LockSystem: webdav.NewMemLS(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return ws, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// adapted from https://github.com/mattn/davfs/blob/master/plugin/mysql/mysql.go
|
|
|
|
|
|
|
|
type WebDavFileSystem struct {
|
2019-05-03 15:24:35 +08:00
|
|
|
option *WebDavOption
|
|
|
|
secret security.SigningKey
|
|
|
|
grpcDialOption grpc.DialOption
|
2020-08-18 11:15:53 +08:00
|
|
|
chunkCache *chunk_cache.TieredChunkCache
|
2023-01-17 14:43:02 +08:00
|
|
|
readerCache *filer.ReaderCache
|
2020-08-29 14:48:48 +08:00
|
|
|
signature int32
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type FileInfo struct {
|
2022-09-15 14:06:44 +08:00
|
|
|
name string
|
|
|
|
size int64
|
|
|
|
mode os.FileMode
|
2022-09-15 03:20:27 +08:00
|
|
|
modifiedTime time.Time
|
2022-09-15 14:06:44 +08:00
|
|
|
isDirectory bool
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fi *FileInfo) Name() string { return fi.name }
|
|
|
|
func (fi *FileInfo) Size() int64 { return fi.size }
|
|
|
|
func (fi *FileInfo) Mode() os.FileMode { return fi.mode }
|
2022-09-15 03:20:27 +08:00
|
|
|
func (fi *FileInfo) ModTime() time.Time { return fi.modifiedTime }
|
2019-05-03 15:24:35 +08:00
|
|
|
func (fi *FileInfo) IsDir() bool { return fi.isDirectory }
|
2019-05-03 05:22:10 +08:00
|
|
|
func (fi *FileInfo) Sys() interface{} { return nil }
|
|
|
|
|
|
|
|
type WebDavFile struct {
|
2023-01-03 15:20:45 +08:00
|
|
|
fs *WebDavFileSystem
|
|
|
|
name string
|
|
|
|
isDirectory bool
|
|
|
|
off int64
|
|
|
|
entry *filer_pb.Entry
|
|
|
|
visibleIntervals *filer.IntervalList[*filer.VisibleInterval]
|
|
|
|
reader io.ReaderAt
|
|
|
|
bufWriter *buffered_writer.BufferedWriteCloser
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
func NewWebDavFileSystem(option *WebDavOption) (webdav.FileSystem, error) {
|
2020-04-12 12:12:41 +08:00
|
|
|
|
2021-09-13 13:47:52 +08:00
|
|
|
cacheUniqueId := util.Md5String([]byte("webdav" + string(option.Filer) + util.Version()))[0:8]
|
2021-02-01 14:02:03 +08:00
|
|
|
cacheDir := path.Join(option.CacheDir, cacheUniqueId)
|
|
|
|
|
2021-02-01 14:16:52 +08:00
|
|
|
os.MkdirAll(cacheDir, os.FileMode(0755))
|
2021-02-01 14:02:03 +08:00
|
|
|
chunkCache := chunk_cache.NewTieredChunkCache(256, cacheDir, option.CacheSizeMB, 1024*1024)
|
2023-01-17 14:43:02 +08:00
|
|
|
t := &WebDavFileSystem{
|
2020-03-29 04:43:31 +08:00
|
|
|
option: option,
|
2020-04-12 12:12:41 +08:00
|
|
|
chunkCache: chunkCache,
|
2020-08-29 14:48:48 +08:00
|
|
|
signature: util.RandomInt32(),
|
2023-01-17 14:43:02 +08:00
|
|
|
}
|
|
|
|
t.readerCache = filer.NewReaderCache(32, chunkCache, filer.LookupFn(t))
|
|
|
|
return t, nil
|
2019-05-03 15:24:35 +08:00
|
|
|
}
|
|
|
|
|
2020-04-30 04:26:02 +08:00
|
|
|
var _ = filer_pb.FilerClient(&WebDavFileSystem{})
|
|
|
|
|
2021-12-26 16:15:03 +08:00
|
|
|
func (fs *WebDavFileSystem) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {
|
2019-05-03 15:24:35 +08:00
|
|
|
|
2023-01-20 17:48:12 +08:00
|
|
|
return pb.WithGrpcClient(streamingMode, fs.signature, func(grpcConnection *grpc.ClientConn) error {
|
2019-05-03 15:24:35 +08:00
|
|
|
client := filer_pb.NewSeaweedFilerClient(grpcConnection)
|
2020-02-26 13:50:12 +08:00
|
|
|
return fn(client)
|
2022-08-24 14:18:21 +08:00
|
|
|
}, fs.option.Filer.ToGrpcAddress(), false, fs.option.GrpcDialOption)
|
2019-05-03 15:24:35 +08:00
|
|
|
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
2020-10-12 11:15:10 +08:00
|
|
|
func (fs *WebDavFileSystem) AdjustedUrl(location *filer_pb.Location) string {
|
|
|
|
return location.Url
|
2020-02-27 08:46:01 +08:00
|
|
|
}
|
2022-08-05 08:35:00 +08:00
|
|
|
func (fs *WebDavFileSystem) GetDataCenter() string {
|
2022-08-15 14:33:30 +08:00
|
|
|
return ""
|
2022-08-05 08:35:00 +08:00
|
|
|
}
|
2019-05-03 05:22:10 +08:00
|
|
|
|
|
|
|
func clearName(name string) (string, error) {
|
|
|
|
slashed := strings.HasSuffix(name, "/")
|
|
|
|
name = path.Clean(name)
|
|
|
|
if !strings.HasSuffix(name, "/") && slashed {
|
|
|
|
name += "/"
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(name, "/") {
|
|
|
|
return "", os.ErrInvalid
|
|
|
|
}
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
func (fs *WebDavFileSystem) Mkdir(ctx context.Context, fullDirPath string, perm os.FileMode) error {
|
2019-05-03 05:22:10 +08:00
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
glog.V(2).Infof("WebDavFileSystem.Mkdir %v", fullDirPath)
|
2019-05-03 05:22:10 +08:00
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
if !strings.HasSuffix(fullDirPath, "/") {
|
|
|
|
fullDirPath += "/"
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2019-05-03 15:24:35 +08:00
|
|
|
if fullDirPath, err = clearName(fullDirPath); err != nil {
|
2019-05-03 05:22:10 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
_, err = fs.stat(ctx, fullDirPath)
|
2019-05-03 05:22:10 +08:00
|
|
|
if err == nil {
|
|
|
|
return os.ErrExist
|
|
|
|
}
|
|
|
|
|
2021-12-26 16:15:03 +08:00
|
|
|
return fs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
2020-03-23 15:01:34 +08:00
|
|
|
dir, name := util.FullPath(fullDirPath).DirAndName()
|
2019-05-04 04:13:08 +08:00
|
|
|
request := &filer_pb.CreateEntryRequest{
|
|
|
|
Directory: dir,
|
|
|
|
Entry: &filer_pb.Entry{
|
|
|
|
Name: name,
|
|
|
|
IsDirectory: true,
|
|
|
|
Attributes: &filer_pb.FuseAttributes{
|
|
|
|
Mtime: time.Now().Unix(),
|
|
|
|
Crtime: time.Now().Unix(),
|
|
|
|
FileMode: uint32(perm | os.ModeDir),
|
|
|
|
Uid: fs.option.Uid,
|
|
|
|
Gid: fs.option.Gid,
|
2019-05-03 15:24:35 +08:00
|
|
|
},
|
2019-05-04 04:13:08 +08:00
|
|
|
},
|
2020-08-29 14:48:48 +08:00
|
|
|
Signatures: []int32{fs.signature},
|
2019-05-04 04:13:08 +08:00
|
|
|
}
|
2019-05-03 15:24:35 +08:00
|
|
|
|
2019-05-04 04:13:08 +08:00
|
|
|
glog.V(1).Infof("mkdir: %v", request)
|
2020-02-26 13:50:12 +08:00
|
|
|
if err := filer_pb.CreateEntry(client, request); err != nil {
|
2019-05-04 04:13:08 +08:00
|
|
|
return fmt.Errorf("mkdir %s/%s: %v", dir, name, err)
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
2019-05-04 04:13:08 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
func (fs *WebDavFileSystem) OpenFile(ctx context.Context, fullFilePath string, flag int, perm os.FileMode) (webdav.File, error) {
|
2019-12-07 23:56:05 +08:00
|
|
|
glog.V(2).Infof("WebDavFileSystem.OpenFile %v %x", fullFilePath, flag)
|
2019-05-03 05:22:10 +08:00
|
|
|
|
|
|
|
var err error
|
2019-05-03 15:24:35 +08:00
|
|
|
if fullFilePath, err = clearName(fullFilePath); err != nil {
|
2019-05-03 05:22:10 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if flag&os.O_CREATE != 0 {
|
|
|
|
// file should not have / suffix.
|
2019-05-03 15:24:35 +08:00
|
|
|
if strings.HasSuffix(fullFilePath, "/") {
|
2019-05-03 05:22:10 +08:00
|
|
|
return nil, os.ErrInvalid
|
|
|
|
}
|
2019-05-03 15:24:35 +08:00
|
|
|
_, err = fs.stat(ctx, fullFilePath)
|
2019-05-03 05:22:10 +08:00
|
|
|
if err == nil {
|
|
|
|
if flag&os.O_EXCL != 0 {
|
|
|
|
return nil, os.ErrExist
|
|
|
|
}
|
2019-05-03 15:24:35 +08:00
|
|
|
fs.removeAll(ctx, fullFilePath)
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
2019-05-03 15:24:35 +08:00
|
|
|
|
2020-03-23 15:01:34 +08:00
|
|
|
dir, name := util.FullPath(fullFilePath).DirAndName()
|
2021-12-26 16:15:03 +08:00
|
|
|
err = fs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
2020-02-26 13:50:12 +08:00
|
|
|
if err := filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
|
2019-05-03 15:24:35 +08:00
|
|
|
Directory: dir,
|
|
|
|
Entry: &filer_pb.Entry{
|
|
|
|
Name: name,
|
|
|
|
IsDirectory: perm&os.ModeDir > 0,
|
|
|
|
Attributes: &filer_pb.FuseAttributes{
|
2022-06-06 15:39:35 +08:00
|
|
|
Mtime: time.Now().Unix(),
|
|
|
|
Crtime: time.Now().Unix(),
|
|
|
|
FileMode: uint32(perm),
|
|
|
|
Uid: fs.option.Uid,
|
|
|
|
Gid: fs.option.Gid,
|
|
|
|
TtlSec: 0,
|
2019-05-03 15:24:35 +08:00
|
|
|
},
|
|
|
|
},
|
2020-08-29 14:48:48 +08:00
|
|
|
Signatures: []int32{fs.signature},
|
2019-05-03 15:24:35 +08:00
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("create %s: %v", fullFilePath, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2019-05-03 05:22:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-03 15:24:35 +08:00
|
|
|
return &WebDavFile{
|
|
|
|
fs: fs,
|
|
|
|
name: fullFilePath,
|
|
|
|
isDirectory: false,
|
2020-12-02 07:32:27 +08:00
|
|
|
bufWriter: buffered_writer.NewBufferedWriteCloser(4 * 1024 * 1024),
|
2019-05-03 15:24:35 +08:00
|
|
|
}, nil
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
fi, err := fs.stat(ctx, fullFilePath)
|
2019-05-03 05:22:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, os.ErrNotExist
|
|
|
|
}
|
2019-05-03 15:24:35 +08:00
|
|
|
if !strings.HasSuffix(fullFilePath, "/") && fi.IsDir() {
|
|
|
|
fullFilePath += "/"
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
return &WebDavFile{
|
|
|
|
fs: fs,
|
|
|
|
name: fullFilePath,
|
|
|
|
isDirectory: false,
|
2020-12-02 07:32:27 +08:00
|
|
|
bufWriter: buffered_writer.NewBufferedWriteCloser(4 * 1024 * 1024),
|
2019-05-03 15:24:35 +08:00
|
|
|
}, nil
|
2019-05-03 05:22:10 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
func (fs *WebDavFileSystem) removeAll(ctx context.Context, fullFilePath string) error {
|
2019-05-03 05:22:10 +08:00
|
|
|
var err error
|
2019-05-03 15:24:35 +08:00
|
|
|
if fullFilePath, err = clearName(fullFilePath); err != nil {
|
2019-05-03 05:22:10 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:01:34 +08:00
|
|
|
dir, name := util.FullPath(fullFilePath).DirAndName()
|
2019-05-03 15:24:35 +08:00
|
|
|
|
2020-09-10 02:21:23 +08:00
|
|
|
return filer_pb.Remove(fs, dir, name, true, false, false, false, []int32{fs.signature})
|
2019-05-03 15:24:35 +08:00
|
|
|
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *WebDavFileSystem) RemoveAll(ctx context.Context, name string) error {
|
|
|
|
|
|
|
|
glog.V(2).Infof("WebDavFileSystem.RemoveAll %v", name)
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
return fs.removeAll(ctx, name)
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *WebDavFileSystem) Rename(ctx context.Context, oldName, newName string) error {
|
|
|
|
|
|
|
|
glog.V(2).Infof("WebDavFileSystem.Rename %v to %v", oldName, newName)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if oldName, err = clearName(oldName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if newName, err = clearName(newName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
of, err := fs.stat(ctx, oldName)
|
2019-05-03 05:22:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return os.ErrExist
|
|
|
|
}
|
2019-05-04 05:12:51 +08:00
|
|
|
if of.IsDir() {
|
|
|
|
if strings.HasSuffix(oldName, "/") {
|
|
|
|
oldName = strings.TrimRight(oldName, "/")
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(newName, "/") {
|
|
|
|
newName = strings.TrimRight(newName, "/")
|
|
|
|
}
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
_, err = fs.stat(ctx, newName)
|
2019-05-03 05:22:10 +08:00
|
|
|
if err == nil {
|
|
|
|
return os.ErrExist
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:01:34 +08:00
|
|
|
oldDir, oldBaseName := util.FullPath(oldName).DirAndName()
|
|
|
|
newDir, newBaseName := util.FullPath(newName).DirAndName()
|
2019-05-03 15:24:35 +08:00
|
|
|
|
2021-12-26 16:15:03 +08:00
|
|
|
return fs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
2019-05-03 15:24:35 +08:00
|
|
|
|
|
|
|
request := &filer_pb.AtomicRenameEntryRequest{
|
|
|
|
OldDirectory: oldDir,
|
|
|
|
OldName: oldBaseName,
|
|
|
|
NewDirectory: newDir,
|
|
|
|
NewName: newBaseName,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := client.AtomicRenameEntry(ctx, request)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("renaming %s/%s => %s/%s: %v", oldDir, oldBaseName, newDir, newBaseName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
func (fs *WebDavFileSystem) stat(ctx context.Context, fullFilePath string) (os.FileInfo, error) {
|
2019-05-03 05:22:10 +08:00
|
|
|
var err error
|
2019-05-03 15:24:35 +08:00
|
|
|
if fullFilePath, err = clearName(fullFilePath); err != nil {
|
2019-05-03 05:22:10 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:01:34 +08:00
|
|
|
fullpath := util.FullPath(fullFilePath)
|
2020-01-20 15:59:46 +08:00
|
|
|
|
2019-05-03 05:22:10 +08:00
|
|
|
var fi FileInfo
|
2020-03-23 15:01:34 +08:00
|
|
|
entry, err := filer_pb.GetEntry(fs, fullpath)
|
2019-05-04 05:12:51 +08:00
|
|
|
if entry == nil {
|
|
|
|
return nil, os.ErrNotExist
|
|
|
|
}
|
2019-05-03 05:22:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-01 15:21:19 +08:00
|
|
|
fi.size = int64(filer.FileSize(entry))
|
2020-01-20 15:59:46 +08:00
|
|
|
fi.name = string(fullpath)
|
2019-05-03 15:24:35 +08:00
|
|
|
fi.mode = os.FileMode(entry.Attributes.FileMode)
|
2022-09-15 03:20:27 +08:00
|
|
|
fi.modifiedTime = time.Unix(entry.Attributes.Mtime, 0)
|
2019-05-03 15:24:35 +08:00
|
|
|
fi.isDirectory = entry.IsDirectory
|
|
|
|
|
2020-01-20 15:59:46 +08:00
|
|
|
if fi.name == "/" {
|
2022-09-15 03:20:27 +08:00
|
|
|
fi.modifiedTime = time.Now()
|
2019-05-03 15:24:35 +08:00
|
|
|
fi.isDirectory = true
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
|
|
|
return &fi, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *WebDavFileSystem) Stat(ctx context.Context, name string) (os.FileInfo, error) {
|
|
|
|
glog.V(2).Infof("WebDavFileSystem.Stat %v", name)
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
return fs.stat(ctx, name)
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
|
|
|
|
2023-01-03 15:20:45 +08:00
|
|
|
func (f *WebDavFile) saveDataAsChunk(reader io.Reader, name string, offset int64, tsNs int64) (chunk *filer_pb.FileChunk, err error) {
|
2019-05-03 15:55:52 +08:00
|
|
|
|
2022-08-21 10:18:12 +08:00
|
|
|
fileId, uploadResult, flushErr, _ := operation.UploadWithRetry(
|
|
|
|
f.fs,
|
|
|
|
&filer_pb.AssignVolumeRequest{
|
|
|
|
Count: 1,
|
|
|
|
Replication: f.fs.option.Replication,
|
|
|
|
Collection: f.fs.option.Collection,
|
|
|
|
DiskType: f.fs.option.DiskType,
|
|
|
|
Path: name,
|
|
|
|
},
|
|
|
|
&operation.UploadOption{
|
|
|
|
Filename: f.name,
|
|
|
|
Cipher: f.fs.option.Cipher,
|
|
|
|
IsInputCompressed: false,
|
|
|
|
MimeType: "",
|
|
|
|
PairMap: nil,
|
|
|
|
},
|
|
|
|
func(host, fileId string) string {
|
|
|
|
return fmt.Sprintf("http://%s/%s", host, fileId)
|
|
|
|
},
|
|
|
|
reader,
|
|
|
|
)
|
2019-05-03 15:55:52 +08:00
|
|
|
|
2020-12-02 07:32:27 +08:00
|
|
|
if flushErr != nil {
|
2022-08-21 10:18:12 +08:00
|
|
|
glog.V(0).Infof("upload data %v: %v", f.name, flushErr)
|
2022-08-21 09:14:57 +08:00
|
|
|
return nil, fmt.Errorf("upload data: %v", flushErr)
|
2019-05-03 15:55:52 +08:00
|
|
|
}
|
|
|
|
if uploadResult.Error != "" {
|
2022-08-21 10:18:12 +08:00
|
|
|
glog.V(0).Infof("upload failure %v: %v", f.name, flushErr)
|
2022-08-21 09:14:57 +08:00
|
|
|
return nil, fmt.Errorf("upload result: %v", uploadResult.Error)
|
2020-12-02 07:32:27 +08:00
|
|
|
}
|
2023-01-03 15:20:45 +08:00
|
|
|
return uploadResult.ToPbFileChunk(fileId, offset, tsNs), nil
|
2020-12-02 07:32:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *WebDavFile) Write(buf []byte) (int, error) {
|
|
|
|
|
|
|
|
glog.V(2).Infof("WebDavFileSystem.Write %v", f.name)
|
|
|
|
|
|
|
|
dir, _ := util.FullPath(f.name).DirAndName()
|
|
|
|
|
|
|
|
var getErr error
|
|
|
|
ctx := context.Background()
|
|
|
|
if f.entry == nil {
|
|
|
|
f.entry, getErr = filer_pb.GetEntry(f.fs, util.FullPath(f.name))
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.entry == nil {
|
|
|
|
return 0, getErr
|
|
|
|
}
|
|
|
|
if getErr != nil {
|
|
|
|
return 0, getErr
|
2019-05-03 15:55:52 +08:00
|
|
|
}
|
|
|
|
|
2020-12-02 07:32:27 +08:00
|
|
|
if f.bufWriter.FlushFunc == nil {
|
|
|
|
f.bufWriter.FlushFunc = func(data []byte, offset int64) (flushErr error) {
|
2019-05-03 15:55:52 +08:00
|
|
|
|
2020-12-02 07:32:27 +08:00
|
|
|
var chunk *filer_pb.FileChunk
|
2023-01-03 15:20:45 +08:00
|
|
|
chunk, flushErr = f.saveDataAsChunk(util.NewBytesReader(data), f.name, offset, time.Now().UnixNano())
|
2019-05-03 15:55:52 +08:00
|
|
|
|
2020-12-02 07:32:27 +08:00
|
|
|
if flushErr != nil {
|
|
|
|
return fmt.Errorf("%s upload result: %v", f.name, flushErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
f.entry.Content = nil
|
2022-11-15 22:33:36 +08:00
|
|
|
f.entry.Chunks = append(f.entry.GetChunks(), chunk)
|
2020-12-02 07:32:27 +08:00
|
|
|
|
|
|
|
return flushErr
|
2019-05-03 15:55:52 +08:00
|
|
|
}
|
2020-12-02 07:32:27 +08:00
|
|
|
f.bufWriter.CloseFunc = func() error {
|
|
|
|
|
2022-11-15 22:33:36 +08:00
|
|
|
manifestedChunks, manifestErr := filer.MaybeManifestize(f.saveDataAsChunk, f.entry.GetChunks())
|
2020-12-02 07:32:27 +08:00
|
|
|
if manifestErr != nil {
|
|
|
|
// not good, but should be ok
|
2020-12-02 11:37:21 +08:00
|
|
|
glog.V(0).Infof("file %s close MaybeManifestize: %v", f.name, manifestErr)
|
2020-12-02 07:32:27 +08:00
|
|
|
} else {
|
|
|
|
f.entry.Chunks = manifestedChunks
|
|
|
|
}
|
|
|
|
|
2021-12-26 16:15:03 +08:00
|
|
|
flushErr := f.fs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
2020-12-02 07:32:27 +08:00
|
|
|
f.entry.Attributes.Mtime = time.Now().Unix()
|
|
|
|
|
|
|
|
request := &filer_pb.UpdateEntryRequest{
|
|
|
|
Directory: dir,
|
|
|
|
Entry: f.entry,
|
|
|
|
Signatures: []int32{f.fs.signature},
|
|
|
|
}
|
2019-05-03 15:55:52 +08:00
|
|
|
|
2020-12-02 07:32:27 +08:00
|
|
|
if _, err := client.UpdateEntry(ctx, request); err != nil {
|
|
|
|
return fmt.Errorf("update %s: %v", f.name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return flushErr
|
2019-05-03 15:55:52 +08:00
|
|
|
}
|
2020-12-02 07:32:27 +08:00
|
|
|
}
|
2019-05-03 15:55:52 +08:00
|
|
|
|
2020-12-02 07:32:27 +08:00
|
|
|
written, err := f.bufWriter.Write(buf)
|
2019-05-03 15:55:52 +08:00
|
|
|
|
2019-12-07 23:56:51 +08:00
|
|
|
if err == nil {
|
2022-06-28 02:19:21 +08:00
|
|
|
f.entry.Attributes.FileSize = uint64(max(f.off+int64(written), int64(f.entry.Attributes.FileSize)))
|
2019-12-07 23:56:05 +08:00
|
|
|
glog.V(3).Infof("WebDavFileSystem.Write %v: written [%d,%d)", f.name, f.off, f.off+int64(len(buf)))
|
2020-12-02 07:32:27 +08:00
|
|
|
f.off += int64(written)
|
2019-05-03 15:55:52 +08:00
|
|
|
}
|
2019-12-07 23:56:05 +08:00
|
|
|
|
2020-12-02 07:32:27 +08:00
|
|
|
return written, err
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *WebDavFile) Close() error {
|
|
|
|
|
|
|
|
glog.V(2).Infof("WebDavFileSystem.Close %v", f.name)
|
|
|
|
|
2020-12-02 07:32:27 +08:00
|
|
|
err := f.bufWriter.Close()
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
if f.entry != nil {
|
|
|
|
f.entry = nil
|
2023-01-03 15:20:45 +08:00
|
|
|
f.visibleIntervals = nil
|
2019-05-03 15:24:35 +08:00
|
|
|
}
|
|
|
|
|
2020-12-02 07:32:27 +08:00
|
|
|
return err
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
func (f *WebDavFile) Read(p []byte) (readSize int, err error) {
|
2019-05-03 05:22:10 +08:00
|
|
|
|
|
|
|
glog.V(2).Infof("WebDavFileSystem.Read %v", f.name)
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
if f.entry == nil {
|
2020-03-23 15:01:34 +08:00
|
|
|
f.entry, err = filer_pb.GetEntry(f.fs, util.FullPath(f.name))
|
2019-05-03 15:24:35 +08:00
|
|
|
}
|
2019-05-04 05:12:51 +08:00
|
|
|
if f.entry == nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2019-05-03 05:22:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-09-01 15:21:19 +08:00
|
|
|
fileSize := int64(filer.FileSize(f.entry))
|
2020-08-16 15:49:08 +08:00
|
|
|
if fileSize == 0 {
|
2019-05-03 15:24:35 +08:00
|
|
|
return 0, io.EOF
|
|
|
|
}
|
2023-01-03 15:20:45 +08:00
|
|
|
if f.visibleIntervals == nil {
|
|
|
|
f.visibleIntervals, _ = filer.NonOverlappingVisibleIntervals(filer.LookupFn(f.fs), f.entry.GetChunks(), 0, fileSize)
|
2020-03-22 16:30:27 +08:00
|
|
|
f.reader = nil
|
2019-05-03 15:24:35 +08:00
|
|
|
}
|
2020-03-22 16:30:27 +08:00
|
|
|
if f.reader == nil {
|
2023-01-03 15:20:45 +08:00
|
|
|
chunkViews := filer.ViewFromVisibleIntervals(f.visibleIntervals, 0, fileSize)
|
2023-01-17 14:43:02 +08:00
|
|
|
f.reader = filer.NewChunkReaderAtFromClient(f.fs.readerCache, chunkViews, fileSize)
|
2019-05-03 15:24:35 +08:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:50:51 +08:00
|
|
|
readSize, err = f.reader.ReadAt(p, f.off)
|
2019-12-07 23:56:05 +08:00
|
|
|
|
2020-03-22 16:39:08 +08:00
|
|
|
glog.V(3).Infof("WebDavFileSystem.Read %v: [%d,%d)", f.name, f.off, f.off+int64(readSize))
|
2020-03-22 16:30:27 +08:00
|
|
|
f.off += int64(readSize)
|
|
|
|
|
2020-10-17 03:53:37 +08:00
|
|
|
if err != nil && err != io.EOF {
|
2020-03-22 16:30:27 +08:00
|
|
|
glog.Errorf("file read %s: %v", f.name, err)
|
2019-05-03 15:24:35 +08:00
|
|
|
}
|
2019-12-07 23:56:05 +08:00
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
return
|
2020-03-22 16:30:27 +08:00
|
|
|
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
func (f *WebDavFile) Readdir(count int) (ret []os.FileInfo, err error) {
|
2019-05-03 05:22:10 +08:00
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
glog.V(2).Infof("WebDavFileSystem.Readdir %v count %d", f.name, count)
|
2019-05-03 05:22:10 +08:00
|
|
|
|
2020-03-23 15:01:34 +08:00
|
|
|
dir, _ := util.FullPath(f.name).DirAndName()
|
2019-05-03 15:24:35 +08:00
|
|
|
|
2020-04-30 08:40:08 +08:00
|
|
|
err = filer_pb.ReadDirAllEntries(f.fs, util.FullPath(dir), "", func(entry *filer_pb.Entry, isLast bool) error {
|
2019-05-03 15:24:35 +08:00
|
|
|
fi := FileInfo{
|
2022-09-15 14:06:44 +08:00
|
|
|
size: int64(filer.FileSize(entry)),
|
|
|
|
name: entry.Name,
|
|
|
|
mode: os.FileMode(entry.Attributes.FileMode),
|
2022-09-15 03:20:27 +08:00
|
|
|
modifiedTime: time.Unix(entry.Attributes.Mtime, 0),
|
2022-09-15 14:06:44 +08:00
|
|
|
isDirectory: entry.IsDirectory,
|
2019-05-03 15:24:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(fi.name, "/") && fi.IsDir() {
|
|
|
|
fi.name += "/"
|
|
|
|
}
|
|
|
|
glog.V(4).Infof("entry: %v", fi.name)
|
|
|
|
ret = append(ret, &fi)
|
2020-04-30 08:40:08 +08:00
|
|
|
return nil
|
2019-05-03 15:24:35 +08:00
|
|
|
})
|
2023-11-08 09:33:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-03 15:24:35 +08:00
|
|
|
|
|
|
|
old := f.off
|
|
|
|
if old >= int64(len(ret)) {
|
|
|
|
if count > 0 {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if count > 0 {
|
|
|
|
f.off += int64(count)
|
|
|
|
if f.off > int64(len(ret)) {
|
|
|
|
f.off = int64(len(ret))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
f.off = int64(len(ret))
|
|
|
|
old = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret[old:f.off], nil
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *WebDavFile) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
|
|
|
|
glog.V(2).Infof("WebDavFile.Seek %v %v %v", f.name, offset, whence)
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-05-03 05:22:10 +08:00
|
|
|
var err error
|
|
|
|
switch whence {
|
2020-09-16 15:37:57 +08:00
|
|
|
case io.SeekStart:
|
2019-05-03 05:22:10 +08:00
|
|
|
f.off = 0
|
2020-09-16 15:37:57 +08:00
|
|
|
case io.SeekEnd:
|
2019-05-03 15:24:35 +08:00
|
|
|
if fi, err := f.fs.stat(ctx, f.name); err != nil {
|
2019-05-03 05:22:10 +08:00
|
|
|
return 0, err
|
|
|
|
} else {
|
|
|
|
f.off = fi.Size()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.off += offset
|
|
|
|
return f.off, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *WebDavFile) Stat() (os.FileInfo, error) {
|
|
|
|
|
|
|
|
glog.V(2).Infof("WebDavFile.Stat %v", f.name)
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
return f.fs.stat(ctx, f.name)
|
2019-05-03 05:22:10 +08:00
|
|
|
}
|