Merge branch 'master' into mq-subscribe

This commit is contained in:
chrislu
2024-01-05 15:36:00 -08:00
5 changed files with 35 additions and 9 deletions

View File

@@ -38,6 +38,7 @@ type WebDavOption struct {
Cipher bool
CacheDir string
CacheSizeMB int64
MaxMB int
}
type WebDavServer struct {
@@ -96,6 +97,7 @@ type FileInfo struct {
size int64
mode os.FileMode
modifiedTime time.Time
etag string
isDirectory bool
}
@@ -106,6 +108,10 @@ func (fi *FileInfo) ModTime() time.Time { return fi.modifiedTime }
func (fi *FileInfo) IsDir() bool { return fi.isDirectory }
func (fi *FileInfo) Sys() interface{} { return nil }
func (fi *FileInfo) ETag(ctx context.Context) (string, error) {
return fi.etag, nil
}
type WebDavFile struct {
fs *WebDavFileSystem
name string
@@ -236,7 +242,7 @@ func (fs *WebDavFileSystem) OpenFile(ctx context.Context, fullFilePath string, f
Name: name,
IsDirectory: perm&os.ModeDir > 0,
Attributes: &filer_pb.FuseAttributes{
Mtime: time.Now().Unix(),
Mtime: 0,
Crtime: time.Now().Unix(),
FileMode: uint32(perm),
Uid: fs.option.Uid,
@@ -257,7 +263,7 @@ func (fs *WebDavFileSystem) OpenFile(ctx context.Context, fullFilePath string, f
fs: fs,
name: fullFilePath,
isDirectory: false,
bufWriter: buffered_writer.NewBufferedWriteCloser(4 * 1024 * 1024),
bufWriter: buffered_writer.NewBufferedWriteCloser(fs.option.MaxMB * 1024 * 1024),
}, nil
}
@@ -273,7 +279,7 @@ func (fs *WebDavFileSystem) OpenFile(ctx context.Context, fullFilePath string, f
fs: fs,
name: fullFilePath,
isDirectory: false,
bufWriter: buffered_writer.NewBufferedWriteCloser(4 * 1024 * 1024),
bufWriter: buffered_writer.NewBufferedWriteCloser(fs.option.MaxMB * 1024 * 1024),
}, nil
}
@@ -369,6 +375,7 @@ func (fs *WebDavFileSystem) stat(ctx context.Context, fullFilePath string) (os.F
fi.name = string(fullpath)
fi.mode = os.FileMode(entry.Attributes.FileMode)
fi.modifiedTime = time.Unix(entry.Attributes.Mtime, 0)
fi.etag = filer.ETag(entry)
fi.isDirectory = entry.IsDirectory
if fi.name == "/" {
@@ -423,12 +430,13 @@ func (f *WebDavFile) Write(buf []byte) (int, error) {
glog.V(2).Infof("WebDavFileSystem.Write %v", f.name)
dir, _ := util.FullPath(f.name).DirAndName()
fullPath := util.FullPath(f.name)
dir, _ := fullPath.DirAndName()
var getErr error
ctx := context.Background()
if f.entry == nil {
f.entry, getErr = filer_pb.GetEntry(f.fs, util.FullPath(f.name))
f.entry, getErr = filer_pb.GetEntry(f.fs, fullPath)
}
if f.entry == nil {
@@ -445,6 +453,11 @@ func (f *WebDavFile) Write(buf []byte) (int, error) {
chunk, flushErr = f.saveDataAsChunk(util.NewBytesReader(data), f.name, offset, time.Now().UnixNano())
if flushErr != nil {
if f.entry.Attributes.Mtime == 0 {
if err := f.fs.removeAll(ctx, f.name); err != nil {
glog.Errorf("bufWriter.Flush remove file error: %+v", f.name)
}
}
return fmt.Errorf("%s upload result: %v", f.name, flushErr)
}

View File

@@ -95,3 +95,11 @@ func (w wrappedFileInfo) Name() string {
name := w.FileInfo.Name()
return strings.TrimPrefix(name, *w.subFolder)
}
func (w wrappedFileInfo) ETag(ctx context.Context) (string, error) {
etag, _ := w.FileInfo.(webdav.ETager).ETag(ctx)
if len(etag) == 0 {
return etag, webdav.ErrNotImplemented
}
return etag, nil
}