Skip to content
Snippets Groups Projects
Select Git revision
  • 7bdf35745f774b37e27027671d5a61549ab6de2d
  • master default protected
  • v1.23.2
  • v1.23.1
  • v1.23.0
  • v1.22.0
  • v1.21.1
  • v1.21.0
  • v1.20.3
  • v1.20.2
  • v1.20.1
  • v1.20.0
  • v1.19.4
  • v1.19.3
  • v1.19.2
  • v1.19.1
  • v1.19.0
  • v1.18.2
  • v1.18.1
  • v1.18.0
  • v1.17.0
  • v1.16.1
22 results

server.go

Blame
  • server.go 15.30 KiB
    package sftp
    
    // sftp server counterpart
    
    import (
    	"encoding"
    	"errors"
    	"fmt"
    	"io"
    	"io/ioutil"
    	"os"
    	"path/filepath"
    	"strconv"
    	"sync"
    	"syscall"
    	"time"
    )
    
    const (
    	// SftpServerWorkerCount defines the number of workers for the SFTP server
    	SftpServerWorkerCount = 8
    )
    
    // Server is an SSH File Transfer Protocol (sftp) server.
    // This is intended to provide the sftp subsystem to an ssh server daemon.
    // This implementation currently supports most of sftp server protocol version 3,
    // as specified at https://filezilla-project.org/specs/draft-ietf-secsh-filexfer-02.txt
    type Server struct {
    	*serverConn
    	debugStream   io.Writer
    	readOnly      bool
    	pktMgr        *packetManager
    	openFiles     map[string]*os.File
    	openFilesLock sync.RWMutex
    	handleCount   int
    	workDir       string
    	maxTxPacket   uint32
    }
    
    func (svr *Server) nextHandle(f *os.File) string {
    	svr.openFilesLock.Lock()
    	defer svr.openFilesLock.Unlock()
    	svr.handleCount++
    	handle := strconv.Itoa(svr.handleCount)
    	svr.openFiles[handle] = f
    	return handle
    }
    
    func (svr *Server) closeHandle(handle string) error {
    	svr.openFilesLock.Lock()
    	defer svr.openFilesLock.Unlock()
    	if f, ok := svr.openFiles[handle]; ok {
    		delete(svr.openFiles, handle)
    		return f.Close()
    	}
    
    	return EBADF
    }
    
    func (svr *Server) getHandle(handle string) (*os.File, bool) {
    	svr.openFilesLock.RLock()
    	defer svr.openFilesLock.RUnlock()
    	f, ok := svr.openFiles[handle]
    	return f, ok
    }
    
    type serverRespondablePacket interface {
    	encoding.BinaryUnmarshaler
    	id() uint32
    	respond(svr *Server) responsePacket