mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-10-31 09:20:50 +01:00
* sftp: refuse mkdir and uploads under a missing folder * sftp: re-check the parent directory when a buffered upload commits The open-time check cannot catch a parent removed while the upload buffered; the filer would recreate it when the entry lands. * sftp: cover mkdir and upload commit against a missing parent Extend the test filer with CreateEntry so makeDir can be exercised end to end. * sftp: refuse a rename into a missing folder The filer's rename path creates the target entry through CreateEntry, which recreates absent parents the same way uploads did. --------- Co-authored-by: user.email <chrislusf@users.noreply.github.com>
95 lines
2.1 KiB
Go
95 lines
2.1 KiB
Go
// sftp_helpers.go
|
|
package sftpd
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/pkg/sftp"
|
|
)
|
|
|
|
// FileInfo implements os.FileInfo.
|
|
type FileInfo struct {
|
|
name string
|
|
size int64
|
|
mode os.FileMode
|
|
modTime time.Time
|
|
isDir bool
|
|
}
|
|
|
|
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 }
|
|
func (fi *FileInfo) ModTime() time.Time { return fi.modTime }
|
|
func (fi *FileInfo) IsDir() bool { return fi.isDir }
|
|
func (fi *FileInfo) Sys() interface{} { return nil }
|
|
|
|
// bufferReader wraps a byte slice to io.ReaderAt.
|
|
type bufferReader struct {
|
|
b []byte
|
|
i int64
|
|
}
|
|
|
|
// listerat implements sftp.ListerAt.
|
|
type listerat []os.FileInfo
|
|
|
|
func (l listerat) ListAt(ls []os.FileInfo, offset int64) (int, error) {
|
|
if offset >= int64(len(l)) {
|
|
return 0, io.EOF
|
|
}
|
|
n := copy(ls, l[offset:])
|
|
if n < len(ls) {
|
|
return n, io.EOF
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
// SeaweedSftpFileWriter buffers writes and flushes on Close.
|
|
type SeaweedSftpFileWriter struct {
|
|
fs SftpServer
|
|
req *sftp.Request
|
|
absPath string // Absolute path after HomeDir translation
|
|
mu sync.Mutex
|
|
tmpFile *os.File
|
|
permissions os.FileMode
|
|
uid uint32
|
|
gid uint32
|
|
offset int64
|
|
}
|
|
|
|
func (w *SeaweedSftpFileWriter) Write(p []byte) (int, error) {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
n, err := w.tmpFile.WriteAt(p, w.offset)
|
|
w.offset += int64(n)
|
|
return n, err
|
|
}
|
|
|
|
func (w *SeaweedSftpFileWriter) WriteAt(p []byte, off int64) (int, error) {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
return w.tmpFile.WriteAt(p, off)
|
|
}
|
|
|
|
func (w *SeaweedSftpFileWriter) Close() error {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
|
|
defer os.Remove(w.tmpFile.Name()) // Clean up temp file
|
|
defer w.tmpFile.Close()
|
|
|
|
if _, err := w.tmpFile.Seek(0, io.SeekStart); err != nil {
|
|
return err
|
|
}
|
|
|
|
// The parent may have been removed while the upload buffered.
|
|
if err := w.fs.requireParentDir(w.absPath); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Stream the file to the absolute path (after HomeDir translation)
|
|
return w.fs.putFile(w.absPath, w.tmpFile, w.fs.user)
|
|
}
|