How pkg/sftp works
Main Components
a) Core Components:
• SFTP Client
• SFTP Server
• SSH Transport Layer
• File System Interface
• Packet Management System
b) Supporting Components:
• Encoding/Protocol Layer
• Request Handler
• File Operation Handlers
• Extension Handlers
[Application Layer]
• ↑↓
[SFTP Protocol Layer]
• ↑↓
[SSH Transport Layer]
• ↑↓
[Operating System / File System]
Component Relationships
a) Client-side flow:
Client → Packet Manager → SSH Transport → Server
b) Server-side flow:
Server → Request Handler → File System Operations → OS File System
Specific Implementation Details
Key elements based on the codebase:
• Separation between platform-specific implementations (note the _unix, _windows, and _plan9 files)
• OpenSSH extensions (found in internal/encoding/ssh/filexfer/openssh/)
• Packet management system (packet-manager.go)
• Request handling pipeline (request.go and related files)
Detailed Component Breakdown
[Client Interface]
• client.go
• conn.go
• packet-manager.go
[Server Implementation]
• server.go
• request-server.go
• server_unix.go/server_windows.go
[Protocol Handling]
• internal/encoding/ssh/filexfer/*
• packet.go
• attrs.go
[File Operations]
• request.go
• stat.go
• ls_formatting.go
Implementation Notes
• The separation between client and server implementations
• The packet-based nature of the protocol
• Platform-specific adaptations
• The relationship with the underlying SSH transport
• Extension mechanisms for OpenSSH features