mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 01:57:10 +01:00
Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c52e2b9d51 | ||
|
|
ba31775360 | ||
|
|
4a43a9f014 | ||
|
|
e1aca2255c | ||
|
|
ad4015eecd | ||
|
|
d001a706fb | ||
|
|
ce7a80ca9f | ||
|
|
a4fae5a244 |
No files matched your search
@@ -192,7 +192,7 @@ func TestSetstatSizeClearsMd5(t *testing.T) {
|
||||
req := sftp.NewRequest("Setstat", "/b/f")
|
||||
req.Flags = 1 // SSH_FILEXFER_ATTR_SIZE
|
||||
req.Attrs = binary.BigEndian.AppendUint64(nil, size)
|
||||
if err := fs.setFileStatWithRequest("/buckets/b/f", req); err != nil {
|
||||
if err := fs.setFileStat("/buckets/b/f", req.AttrFlags(), req.Attributes()); err != nil {
|
||||
t.Fatalf("setstat size %d: %v", size, err)
|
||||
}
|
||||
got := fake.lastUpdate.Entry.Attributes
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
package sftpd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -48,7 +50,7 @@ func (l listerat) ListAt(ls []os.FileInfo, offset int64) (int, error) {
|
||||
|
||||
// SeaweedSftpFileWriter buffers writes and flushes on Close.
|
||||
type SeaweedSftpFileWriter struct {
|
||||
fs SftpServer
|
||||
fs *SftpServer
|
||||
req *sftp.Request
|
||||
absPath string // Absolute path after HomeDir translation
|
||||
mu sync.Mutex
|
||||
@@ -57,6 +59,68 @@ type SeaweedSftpFileWriter struct {
|
||||
uid uint32
|
||||
gid uint32
|
||||
offset int64
|
||||
closed bool
|
||||
setStats []fileStat // attributes set while the upload buffered
|
||||
}
|
||||
|
||||
type fileStat struct {
|
||||
flags sftp.FileAttrFlags
|
||||
attrs *sftp.FileStat
|
||||
}
|
||||
|
||||
func (fs *SftpServer) trackUpload(w *SeaweedSftpFileWriter) {
|
||||
fs.uploadsMu.Lock()
|
||||
defer fs.uploadsMu.Unlock()
|
||||
if fs.uploads == nil {
|
||||
fs.uploads = make(map[string][]*SeaweedSftpFileWriter)
|
||||
}
|
||||
fs.uploads[w.absPath] = append(fs.uploads[w.absPath], w)
|
||||
}
|
||||
|
||||
func (fs *SftpServer) untrackUpload(w *SeaweedSftpFileWriter) {
|
||||
fs.uploadsMu.Lock()
|
||||
defer fs.uploadsMu.Unlock()
|
||||
open := slices.DeleteFunc(fs.uploads[w.absPath], func(o *SeaweedSftpFileWriter) bool { return o == w })
|
||||
if len(open) == 0 {
|
||||
delete(fs.uploads, w.absPath)
|
||||
} else {
|
||||
fs.uploads[w.absPath] = open
|
||||
}
|
||||
}
|
||||
|
||||
// openUpload returns the upload open on absPath, or nil. A setstat names only
|
||||
// the path, so with several uploads open there it cannot tell which one it is
|
||||
// for and is refused.
|
||||
func (fs *SftpServer) openUpload(absPath string) (*SeaweedSftpFileWriter, error) {
|
||||
fs.uploadsMu.Lock()
|
||||
defer fs.uploadsMu.Unlock()
|
||||
switch open := fs.uploads[absPath]; len(open) {
|
||||
case 0:
|
||||
return nil, nil
|
||||
case 1:
|
||||
return open[0], nil
|
||||
default:
|
||||
return nil, fmt.Errorf("%s has %d uploads open: %w", absPath, len(open), sftp.ErrSSHFxFailure)
|
||||
}
|
||||
}
|
||||
|
||||
// setStat takes an fsetstat on the handle of a buffering upload, whose entry
|
||||
// may not exist yet: a size cuts the buffered bytes, the rest is applied once
|
||||
// the upload is stored. It does not handle it once the upload has closed.
|
||||
func (w *SeaweedSftpFileWriter) setStat(flags sftp.FileAttrFlags, attrs *sftp.FileStat) (handled bool, err error) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
if w.closed {
|
||||
return false, nil
|
||||
}
|
||||
if flags.Size {
|
||||
if err := w.tmpFile.Truncate(int64(attrs.Size)); err != nil {
|
||||
return true, err
|
||||
}
|
||||
flags.Size = false
|
||||
}
|
||||
w.setStats = append(w.setStats, fileStat{flags: flags, attrs: attrs})
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (w *SeaweedSftpFileWriter) Write(p []byte) (int, error) {
|
||||
@@ -77,6 +141,8 @@ func (w *SeaweedSftpFileWriter) Close() error {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
|
||||
defer w.fs.untrackUpload(w)
|
||||
w.closed = true
|
||||
defer os.Remove(w.tmpFile.Name()) // Clean up temp file
|
||||
defer w.tmpFile.Close()
|
||||
|
||||
@@ -88,10 +154,22 @@ func (w *SeaweedSftpFileWriter) Close() error {
|
||||
if err := w.fs.requireParentDir(w.absPath); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.fs.checkWritable(w.absPath); err != nil {
|
||||
existing, err := w.fs.checkWritable(w.absPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Like open(2) on an existing file, an overwrite keeps the file's mode;
|
||||
// a zero mode is an entry written without one.
|
||||
mode := w.permissions
|
||||
if perm := os.FileMode(existing.GetAttributes().GetFileMode()).Perm(); perm != 0 {
|
||||
mode = perm
|
||||
}
|
||||
for _, st := range w.setStats {
|
||||
if st.flags.Permissions {
|
||||
mode = st.attrs.FileMode().Perm()
|
||||
}
|
||||
}
|
||||
|
||||
// Stream the file to the absolute path (after HomeDir translation)
|
||||
return w.fs.putFile(w.absPath, w.tmpFile, w.fs.user)
|
||||
return w.fs.putFile(w.absPath, w.tmpFile, w.fs.user, mode, w.setStats)
|
||||
}
|
||||
+55
-30
@@ -127,7 +127,16 @@ func (fs *SftpServer) dispatchCmd(r *sftp.Request) error {
|
||||
case "Rmdir":
|
||||
return fs.removeDir(absPath)
|
||||
case "Setstat":
|
||||
return fs.setFileStatWithRequest(absPath, r)
|
||||
w, err := fs.openUpload(absPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if w != nil {
|
||||
if handled, err := w.setStat(r.AttrFlags(), r.Attributes()); handled {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return fs.setFileStat(absPath, r.AttrFlags(), r.Attributes())
|
||||
default:
|
||||
return fmt.Errorf("unsupported: %s", r.Method)
|
||||
}
|
||||
@@ -162,7 +171,7 @@ func (fs *SftpServer) newFileWriter(r *sftp.Request) (io.WriterAt, error) {
|
||||
if err := fs.requireParentDir(absPath); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := fs.checkWritable(absPath); err != nil {
|
||||
if _, err := fs.checkWritable(absPath); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Create a temporary file to buffer writes
|
||||
@@ -179,8 +188,8 @@ func (fs *SftpServer) newFileWriter(r *sftp.Request) (io.WriterAt, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return &SeaweedSftpFileWriter{
|
||||
fs: *fs,
|
||||
w := &SeaweedSftpFileWriter{
|
||||
fs: fs,
|
||||
req: r,
|
||||
absPath: absPath,
|
||||
tmpFile: tmpFile,
|
||||
@@ -188,23 +197,26 @@ func (fs *SftpServer) newFileWriter(r *sftp.Request) (io.WriterAt, error) {
|
||||
uid: fs.user.Uid,
|
||||
gid: fs.user.Gid,
|
||||
offset: 0,
|
||||
}, nil
|
||||
}
|
||||
fs.trackUpload(w)
|
||||
return w, nil
|
||||
}
|
||||
|
||||
// checkWritable refuses an upload onto a directory, which the filer would
|
||||
// redirect to a child, or onto a protected entry.
|
||||
func (fs *SftpServer) checkWritable(absPath string) error {
|
||||
// redirect to a child, or onto a protected entry. It returns the existing
|
||||
// file, or nil when there is none.
|
||||
func (fs *SftpServer) checkWritable(absPath string) (*filer_pb.Entry, error) {
|
||||
entry, err := fs.getEntry(absPath)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
if entry.IsDirectory {
|
||||
return fmt.Errorf("%s is a directory", absPath)
|
||||
return nil, fmt.Errorf("%s is a directory", absPath)
|
||||
}
|
||||
return fs.checkEntryProtected(absPath, entry)
|
||||
return entry, fs.checkEntryProtected(absPath, entry)
|
||||
}
|
||||
|
||||
func (fs *SftpServer) copyExistingContent(absPath string, dst io.Writer) error {
|
||||
@@ -457,7 +469,7 @@ func (fs *SftpServer) checkRenameKeepsAcl(absPath, absTarget string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fs *SftpServer) setFileStatWithRequest(absPath string, r *sftp.Request) error {
|
||||
func (fs *SftpServer) setFileStat(absPath string, flags sftp.FileAttrFlags, attrs *sftp.FileStat) error {
|
||||
if err := fs.checkFilePermission(absPath, "write"); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -469,25 +481,29 @@ func (fs *SftpServer) setFileStatWithRequest(absPath string, r *sftp.Request) er
|
||||
return err
|
||||
}
|
||||
dir, _ := util.FullPath(absPath).DirAndName()
|
||||
// apply attrs
|
||||
if r.AttrFlags().Permissions {
|
||||
entry.Attributes.FileMode = uint32(r.Attributes().FileMode())
|
||||
applyFileStat(entry, flags, attrs)
|
||||
return fs.updateEntry(dir, entry)
|
||||
}
|
||||
|
||||
func applyFileStat(entry *filer_pb.Entry, flags sftp.FileAttrFlags, attrs *sftp.FileStat) {
|
||||
if flags.Permissions {
|
||||
entry.Attributes.FileMode = uint32(attrs.FileMode())
|
||||
}
|
||||
if r.AttrFlags().UidGid {
|
||||
entry.Attributes.Uid = uint32(r.Attributes().UID)
|
||||
entry.Attributes.Gid = uint32(r.Attributes().GID)
|
||||
if flags.UidGid {
|
||||
entry.Attributes.Uid = uint32(attrs.UID)
|
||||
entry.Attributes.Gid = uint32(attrs.GID)
|
||||
}
|
||||
if r.AttrFlags().Acmodtime {
|
||||
entry.Attributes.Mtime = int64(r.Attributes().Mtime)
|
||||
if flags.Acmodtime {
|
||||
entry.Attributes.Mtime = int64(attrs.Mtime)
|
||||
entry.Attributes.MtimeNs = 0
|
||||
}
|
||||
if r.AttrFlags().Size {
|
||||
if size := uint64(r.Attributes().Size); size != entry.Attributes.FileSize {
|
||||
if flags.Size {
|
||||
if size := uint64(attrs.Size); size != entry.Attributes.FileSize {
|
||||
entry.Attributes.FileSize = size
|
||||
// the stored MD5 described the old content
|
||||
entry.Attributes.Md5 = nil
|
||||
}
|
||||
}
|
||||
return fs.updateEntry(dir, entry)
|
||||
}
|
||||
|
||||
// ==================== Directory Operations ====================
|
||||
@@ -647,10 +663,13 @@ func (fs *SftpServer) requireEntry(absPath string, isDir bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fs *SftpServer) putFile(filepath string, reader io.Reader, user *user.User) error {
|
||||
// putFile uploads the file, then sets the owner to user and applies stats,
|
||||
// the attributes a client set while the upload buffered.
|
||||
func (fs *SftpServer) putFile(filepath string, reader io.Reader, user *user.User, mode os.FileMode, stats []fileStat) error {
|
||||
dir, filename := util.FullPath(filepath).DirAndName()
|
||||
// Escape the path so a "?" in the filename cannot inject filer query commands like cp.from/mv.from.
|
||||
uploadUrl := (&url.URL{Scheme: "http", Host: fs.filerAddr.ToHttpAddress(), Path: filepath}).String()
|
||||
query := url.Values{"mode": {strconv.FormatUint(uint64(mode.Perm()), 8)}}
|
||||
uploadUrl := (&url.URL{Scheme: "http", Host: fs.filerAddr.ToHttpAddress(), Path: filepath, RawQuery: query.Encode()}).String()
|
||||
// Let the global HTTP client normalize the scheme to https:// when TLS is configured
|
||||
normalizedUrl, err := util_http.NormalizeUrl(uploadUrl)
|
||||
if err != nil {
|
||||
@@ -702,8 +721,7 @@ func (fs *SftpServer) putFile(filepath string, reader io.Reader, user *user.User
|
||||
if result.Error != "" {
|
||||
return fmt.Errorf("filer error: %s", result.Error)
|
||||
}
|
||||
// Update file ownership using the same pattern as other functions
|
||||
if user != nil {
|
||||
if user != nil || len(stats) > 0 {
|
||||
err := fs.callWithClient(false, func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
|
||||
// Look up the file to get its current entry
|
||||
lookupResp, err := client.LookupDirectoryEntry(ctx, &filer_pb.LookupDirectoryEntryRequest{
|
||||
@@ -718,10 +736,14 @@ func (fs *SftpServer) putFile(filepath string, reader io.Reader, user *user.User
|
||||
return fmt.Errorf("file not found after upload: %s/%s", dir, filename)
|
||||
}
|
||||
|
||||
// Update the entry with new uid/gid
|
||||
entry := lookupResp.Entry
|
||||
entry.Attributes.Uid = user.Uid
|
||||
entry.Attributes.Gid = user.Gid
|
||||
if user != nil {
|
||||
entry.Attributes.Uid = user.Uid
|
||||
entry.Attributes.Gid = user.Gid
|
||||
}
|
||||
for _, st := range stats {
|
||||
applyFileStat(entry, st.flags, st.attrs)
|
||||
}
|
||||
|
||||
// Update the entry in the filer
|
||||
_, err = client.UpdateEntry(ctx, &filer_pb.UpdateEntryRequest{
|
||||
@@ -731,6 +753,9 @@ func (fs *SftpServer) putFile(filepath string, reader io.Reader, user *user.User
|
||||
return err
|
||||
})
|
||||
|
||||
if err != nil && len(stats) > 0 {
|
||||
return fmt.Errorf("set attributes of %s: %w", filepath, err)
|
||||
}
|
||||
if err != nil {
|
||||
// Log the error but don't fail the whole operation
|
||||
glog.Errorf("Failed to update file ownership for %s: %v", filepath, err)
|
||||
|
||||
@@ -3,12 +3,16 @@ package sftpd
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -16,6 +20,7 @@ import (
|
||||
"github.com/pkg/sftp"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
||||
"github.com/seaweedfs/seaweedfs/weed/security"
|
||||
"github.com/seaweedfs/seaweedfs/weed/sftpd/user"
|
||||
"google.golang.org/grpc"
|
||||
@@ -42,11 +47,11 @@ func TestPutFileEscapesQueryInjection(t *testing.T) {
|
||||
fs := &SftpServer{filerAddr: pb.ServerAddress(strings.TrimPrefix(ts.URL, "http://"))}
|
||||
|
||||
malicious := "/home/alice/steal?cp.from=/home/bob/secret.txt"
|
||||
if err := fs.putFile(malicious, strings.NewReader("dummy"), nil); err != nil {
|
||||
if err := fs.putFile(malicious, strings.NewReader("dummy"), nil, 0644, nil); err != nil {
|
||||
t.Fatalf("putFile: %v", err)
|
||||
}
|
||||
|
||||
if gotRawQuery != "" {
|
||||
if gotRawQuery != "mode=644" {
|
||||
t.Errorf("filename leaked into query string: RawQuery=%q", gotRawQuery)
|
||||
}
|
||||
if gotPath != malicious {
|
||||
@@ -272,3 +277,155 @@ func TestListAllPagesMissingDirectory(t *testing.T) {
|
||||
t.Fatalf("directory: got %d entries, want a.txt", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadFileMode(t *testing.T) {
|
||||
fs, _ := newLockTestServer(t, map[string]*filer_pb.Entry{
|
||||
"/buckets/plain": {Name: "plain", IsDirectory: true},
|
||||
"/buckets/plain/private": {Name: "private", Attributes: &filer_pb.FuseAttributes{FileMode: 0600}},
|
||||
"/buckets/plain/shared": {Name: "shared", Attributes: &filer_pb.FuseAttributes{FileMode: 0664}},
|
||||
"/buckets/plain/unset": {Name: "unset", Attributes: &filer_pb.FuseAttributes{}},
|
||||
})
|
||||
var gotMode string
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotMode = r.URL.Query().Get("mode")
|
||||
_, _ = w.Write([]byte(`{}`))
|
||||
}))
|
||||
t.Cleanup(ts.Close)
|
||||
_, grpcPort, _ := net.SplitHostPort(fs.filerAddr.ToGrpcAddress())
|
||||
fs.filerAddr = pb.ServerAddress(strings.TrimPrefix(ts.URL, "http://") + "." + grpcPort)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
req *sftp.Request
|
||||
want string
|
||||
}{
|
||||
{"default", sftp.NewRequest("Put", "/plain/new"), "644"},
|
||||
{"overwrite keeps private mode", sftp.NewRequest("Put", "/plain/private"), "600"},
|
||||
{"overwrite keeps shared mode", sftp.NewRequest("Put", "/plain/shared"), "664"},
|
||||
{"overwrite of a file without a mode", sftp.NewRequest("Put", "/plain/unset"), "644"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.req.Flags |= 0x1a // write, create, truncate
|
||||
gotMode = ""
|
||||
w, err := fs.newFileWriter(tt.req)
|
||||
if err != nil {
|
||||
t.Fatalf("newFileWriter: %v", err)
|
||||
}
|
||||
if _, err := w.WriteAt([]byte("data"), 0); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
if err := w.(io.Closer).Close(); err != nil {
|
||||
t.Fatalf("close: %v", err)
|
||||
}
|
||||
if gotMode != tt.want {
|
||||
t.Errorf("mode = %q, want %q", gotMode, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// An fsetstat on an upload's handle, as "put -p" sends before closing, finds
|
||||
// no entry yet: it has to wait for the upload and apply then, even when the
|
||||
// upload took the bucket's default retention.
|
||||
func TestSetstatOnBufferingUpload(t *testing.T) {
|
||||
fs, fake := newLockTestServer(t, map[string]*filer_pb.Entry{
|
||||
"/buckets/plain": {Name: "plain", IsDirectory: true},
|
||||
})
|
||||
var gotMode, gotBody string
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
gotMode, gotBody = r.URL.Query().Get("mode"), string(body)
|
||||
fake.mu.Lock()
|
||||
entry := lockedEntry(path.Base(r.URL.Path), map[string]string{s3_constants.ExtObjectLockModeKey: s3_constants.RetentionModeCompliance, s3_constants.ExtRetentionUntilDateKey: retainUntil(time.Hour)})
|
||||
entry.Attributes = &filer_pb.FuseAttributes{FileMode: 0644, FileSize: uint64(len(body)), MtimeNs: 123}
|
||||
fake.entries[r.URL.Path] = entry
|
||||
fake.mu.Unlock()
|
||||
_, _ = w.Write([]byte(`{}`))
|
||||
}))
|
||||
t.Cleanup(ts.Close)
|
||||
_, grpcPort, _ := net.SplitHostPort(fs.filerAddr.ToGrpcAddress())
|
||||
fs.filerAddr = pb.ServerAddress(strings.TrimPrefix(ts.URL, "http://") + "." + grpcPort)
|
||||
|
||||
open := sftp.NewRequest("Put", "/plain/new")
|
||||
open.Flags = 0x1a // write, create, truncate
|
||||
w, err := fs.newFileWriter(open)
|
||||
if err != nil {
|
||||
t.Fatalf("newFileWriter: %v", err)
|
||||
}
|
||||
if _, err := w.WriteAt([]byte("hello world"), 0); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
const mtime = 1700000000
|
||||
setstat := sftp.NewRequest("Setstat", "/plain/new")
|
||||
setstat.Flags = 0x1 | 0x4 | 0x8 // size, permissions, times
|
||||
setstat.Attrs = binary.BigEndian.AppendUint64(nil, 5)
|
||||
setstat.Attrs = binary.BigEndian.AppendUint32(setstat.Attrs, 0100600)
|
||||
setstat.Attrs = binary.BigEndian.AppendUint32(setstat.Attrs, mtime)
|
||||
setstat.Attrs = binary.BigEndian.AppendUint32(setstat.Attrs, mtime)
|
||||
if err := fs.dispatchCmd(setstat); err != nil {
|
||||
t.Fatalf("setstat before close: %v", err)
|
||||
}
|
||||
if err := w.(io.Closer).Close(); err != nil {
|
||||
t.Fatalf("close: %v", err)
|
||||
}
|
||||
if gotMode != "600" || gotBody != "hello" {
|
||||
t.Errorf("uploaded mode %q body %q, want 600 and the body cut to 5 bytes", gotMode, gotBody)
|
||||
}
|
||||
got := fake.lastUpdate.GetEntry().GetAttributes()
|
||||
if got.GetMtime() != mtime || got.GetMtimeNs() != 0 || os.FileMode(got.GetFileMode()).Perm() != 0600 {
|
||||
t.Errorf("stored mtime %d.%09d mode %o, want %d.000000000 and 600", got.GetMtime(), got.GetMtimeNs(), got.GetFileMode(), mtime)
|
||||
}
|
||||
if w, _ := fs.openUpload("/buckets/plain/new"); w != nil {
|
||||
t.Error("closed upload is still tracked")
|
||||
}
|
||||
|
||||
// The client was told the setstat worked, so losing it must fail the close.
|
||||
fake.mu.Lock()
|
||||
fake.updateErr = errors.New("update refused")
|
||||
fake.mu.Unlock()
|
||||
open.Filepath, setstat.Filepath = "/plain/other", "/plain/other"
|
||||
w, err = fs.newFileWriter(open)
|
||||
if err != nil {
|
||||
t.Fatalf("newFileWriter: %v", err)
|
||||
}
|
||||
if err := fs.dispatchCmd(setstat); err != nil {
|
||||
t.Fatalf("setstat before close: %v", err)
|
||||
}
|
||||
if err := w.(io.Closer).Close(); err == nil {
|
||||
t.Error("close succeeded although the queued attributes were not stored")
|
||||
}
|
||||
}
|
||||
|
||||
// A setstat names only the path, so it can't pick between two uploads open
|
||||
// on the same one, and changing the stored entry instead would be wrong too.
|
||||
func TestSetstatWithTwoUploadsOnOnePath(t *testing.T) {
|
||||
fs, fake := newLockTestServer(t, map[string]*filer_pb.Entry{
|
||||
"/buckets/plain": {Name: "plain", IsDirectory: true},
|
||||
"/buckets/plain/new": {Name: "new", Attributes: &filer_pb.FuseAttributes{FileMode: 0644, FileSize: 10}},
|
||||
})
|
||||
open := func() io.WriteCloser {
|
||||
req := sftp.NewRequest("Put", "/plain/new")
|
||||
req.Flags = 0x1a // write, create, truncate
|
||||
w, err := fs.newFileWriter(req)
|
||||
if err != nil {
|
||||
t.Fatalf("newFileWriter: %v", err)
|
||||
}
|
||||
return w.(io.WriteCloser)
|
||||
}
|
||||
first, second := open(), open()
|
||||
setstat := sftp.NewRequest("Setstat", "/plain/new")
|
||||
setstat.Flags = 0x1 // size
|
||||
setstat.Attrs = binary.BigEndian.AppendUint64(nil, 0)
|
||||
if err := fs.dispatchCmd(setstat); err == nil {
|
||||
t.Error("setstat with two uploads open succeeded")
|
||||
}
|
||||
if fake.lastUpdate != nil {
|
||||
t.Errorf("setstat changed the stored entry: %v", fake.lastUpdate.Entry.Attributes)
|
||||
}
|
||||
_ = second.Close()
|
||||
if got, err := fs.openUpload("/buckets/plain/new"); got != first || err != nil {
|
||||
t.Fatalf("open upload = %p, %v, want the remaining one %p", got, err, first)
|
||||
}
|
||||
_ = first.Close()
|
||||
}
|
||||
@@ -37,6 +37,7 @@ type lockTestFiler struct {
|
||||
volume string
|
||||
|
||||
lastUpdate *filer_pb.UpdateEntryRequest
|
||||
updateErr error
|
||||
}
|
||||
|
||||
func (f *lockTestFiler) LookupDirectoryEntry(_ context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
|
||||
@@ -228,6 +229,9 @@ func TestRenameRefusesProtectedSourceAndTarget(t *testing.T) {
|
||||
func (f *lockTestFiler) UpdateEntry(_ context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
if f.updateErr != nil {
|
||||
return nil, f.updateErr
|
||||
}
|
||||
f.updates++
|
||||
f.lastUpdate = req
|
||||
return &filer_pb.UpdateEntryResponse{}, nil
|
||||
@@ -250,14 +254,14 @@ func TestWriteAndSetstatRefuseProtectedEntries(t *testing.T) {
|
||||
}
|
||||
t.Fatalf("overwrite of %s: got %v, want os.ErrPermission", p, err)
|
||||
}
|
||||
if err := fs.setFileStatWithRequest("/buckets"+p, sftp.NewRequest("Setstat", p)); !errors.Is(err, os.ErrPermission) {
|
||||
if err := fs.setFileStat("/buckets"+p, sftp.FileAttrFlags{}, &sftp.FileStat{}); !errors.Is(err, os.ErrPermission) {
|
||||
t.Fatalf("setstat of %s: got %v, want os.ErrPermission", p, err)
|
||||
}
|
||||
}
|
||||
if fake.updates != 0 {
|
||||
t.Fatalf("setstat of a protected entry reached the filer")
|
||||
}
|
||||
if err := fs.setFileStatWithRequest("/buckets/b/unlocked", sftp.NewRequest("Setstat", "/b/unlocked")); err != nil {
|
||||
if err := fs.setFileStat("/buckets/b/unlocked", sftp.FileAttrFlags{}, &sftp.FileStat{}); err != nil {
|
||||
t.Fatalf("setstat of an unprotected entry: %v", err)
|
||||
}
|
||||
if _, ok := fake.lastUpdate.ExpectedExtended[s3_constants.ExtLegalHoldKey]; !ok {
|
||||
@@ -423,7 +427,7 @@ func TestPutFileAppliesBucketDefaultRetention(t *testing.T) {
|
||||
fs.bucketsDir = bucketsDir
|
||||
for _, tt := range tests {
|
||||
gotHeader = nil
|
||||
if err := fs.putFile(tt.path, strings.NewReader("data"), nil); err != nil {
|
||||
if err := fs.putFile(tt.path, strings.NewReader("data"), nil, 0644, nil); err != nil {
|
||||
t.Fatalf("putFile %s: %v", tt.path, err)
|
||||
}
|
||||
mode := gotHeader[s3_constants.ExtObjectLockModeKey]
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
@@ -29,6 +30,9 @@ type SftpServer struct {
|
||||
filerSigningKey []byte
|
||||
filerSigningExpiresAfter int
|
||||
bucketsDir string
|
||||
|
||||
uploadsMu sync.Mutex
|
||||
uploads map[string][]*SeaweedSftpFileWriter
|
||||
}
|
||||
|
||||
// NewSftpServer constructs the server.
|
||||
|
||||
Reference in new issue
Block a user