Skip to content

Commit

Permalink
server uses commonHandler, client uses handle
Browse files Browse the repository at this point in the history
  • Loading branch information
puellanivis committed Nov 15, 2024
1 parent 88ff45f commit 6366220
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,25 +948,25 @@ func (cl *Client) LStat(name string) (fs.FileInfo, error) {
}, nil
}

type clHandle struct {
type handle struct {
value atomic.Pointer[string]
closed chan struct{}
}

func (h *clHandle) init(handle string) {
func (h *handle) init(handle string) {
h.value.Store(&handle)
h.closed = make(chan struct{})
}

func (h *clHandle) get() (handle string, cancel <-chan struct{}, err error) {
func (h *handle) get() (handle string, cancel <-chan struct{}, err error) {
p := h.value.Load()
if p == nil {
return "", nil, fs.ErrClosed
}
return *p, h.closed, nil
}

func (h *clHandle) close(cl *Client) error {
func (h *handle) close(cl *Client) error {
// The design principle here is that when `openssh-portable/sftp-server.c` is doing `handle_close`,
// it will unconditionally mark the handle as unused,
// so we need to also unconditionally mark this handle as invalid.
Expand All @@ -988,7 +988,7 @@ func (h *clHandle) close(cl *Client) error {
// but since an outstanding method could still be holding the handle, we still need a close signal.
// Since this close HAPPENS BEFORE the sendPacket below,
// this ensures that after closing this channel, no further requests will be dispatched.
// Meaning we know that the close request below will be the final request from this clHandle.
// Meaning we know that the close request below will be the final request from this handle.
close(h.closed)

// One might assume we could just simply use the closed channel alone,
Expand All @@ -1011,7 +1011,7 @@ type Dir struct {
cl *Client
name string

handle clHandle
handle handle

mu sync.RWMutex
entries []*sshfx.NameEntry
Expand Down Expand Up @@ -1205,7 +1205,7 @@ type File struct {
cl *Client
name string

handle clHandle
handle handle

mu sync.RWMutex
offset int64 // current offset within remote file
Expand Down
6 changes: 3 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ type DirHandler interface {

type wrapHandler func(ctx context.Context, req sshfx.Packet) (sshfx.Packet, error)

// srvHandle is the intersection of FileHandler and DirHandler
type srvHandle interface {
// commonHandle is the intersection of FileHandler and DirHandler
type commonHandle interface {
io.Closer

Name() string
Expand All @@ -212,7 +212,7 @@ type Server struct {
Debug io.Writer

wg sync.WaitGroup
handles sync.Map[string, srvHandle]
handles sync.Map[string, commonHandle]
hijacks map[sshfx.PacketType]wrapHandler

dataPktPool *sync.Pool[sshfx.DataPacket]
Expand Down

0 comments on commit 6366220

Please sign in to comment.