# pkg/sftp architecture

> Architecture diagram of the GitHub repository pkg/sftp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pkg/sftp
- Repository: https://github.com/pkg/sftp

## Overview

##### 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

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles and classes
    classDef core fill:#2196F3,stroke:#1565C0,color:white
    classDef fs fill:#4CAF50,stroke:#388E3B,color:white
    classDef transport fill:#FFC107,stroke:#FFA000,color:black
    classDef util fill:#9E9E9E,stroke:#616161,color:white
    classDef security fill:#F44336,stroke:#D32F2F,color:white

    %% Application Layer
    subgraph AppLayer["Application Layer"]
        Client["SFTP Client"]:::core
        Server["SFTP Server"]:::core
    end

    %% SFTP Protocol Layer
    subgraph ProtocolLayer["SFTP Protocol Layer"]
        PacketMgr["Packet Manager"]:::core
        ProtocolEnc["Protocol Encoding"]:::core
        ReqHandler["Request Handler"]:::core
    end

    %% SSH Transport Layer
    subgraph TransportLayer["SSH Transport Layer"]
        SSHTransport["SSH Transport"]:::transport
        ConnPool["Connection Pool"]:::transport
    end

    %% File System Layer
    subgraph FSLayer["File System Layer"]
        FileOps["File Operations"]:::fs
        
        subgraph PlatformFS["Platform-Specific FS"]
            UnixFS["Unix FS"]:::fs
            WinFS["Windows FS"]:::fs
            Plan9FS["Plan9 FS"]:::fs
        end
    end

    %% Extensions
    subgraph Extensions["Extensions"]
        OpenSSHExt["OpenSSH Extensions"]:::util
        ExtPackets["Extended Packets"]:::util
    end

    %% Relationships
    Client --> PacketMgr
    PacketMgr --> SSHTransport
    SSHTransport --> Server
    Server --> ReqHandler
    ReqHandler --> FileOps
    FileOps --> PlatformFS
    ProtocolEnc <--> PacketMgr
    SSHTransport <--> ConnPool
    ReqHandler <--> OpenSSHExt
    OpenSSHExt <--> ExtPackets

    %% Click events
    click Client "https://github.com/pkg/sftp/blob/master/client.go"
    click Server "https://github.com/pkg/sftp/blob/master/server.go"
    click PacketMgr "https://github.com/pkg/sftp/blob/master/packet-manager.go"
    click ProtocolEnc "https://github.com/pkg/sftp/tree/master/internal/encoding/ssh/filexfer"
    click ReqHandler "https://github.com/pkg/sftp/blob/master/request-server.go"
    click FileOps "https://github.com/pkg/sftp/blob/master/request.go"
    click UnixFS "https://github.com/pkg/sftp/blob/master/server_unix.go"
    click WinFS "https://github.com/pkg/sftp/blob/master/server_windows.go"
    click Plan9FS "https://github.com/pkg/sftp/blob/master/server_plan9.go"
    click OpenSSHExt "https://github.com/pkg/sftp/tree/master/internal/encoding/ssh/filexfer/openssh"
    click ConnPool "https://github.com/pkg/sftp/blob/master/pool.go"
    click ExtPackets "https://github.com/pkg/sftp/blob/master/internal/encoding/ssh/filexfer/extended_packets.go"

    %% Legend
    subgraph Legend
        CoreComp["Core Components"]:::core
        FSComp["File System Components"]:::fs
        TransComp["Transport Components"]:::transport
        UtilComp["Utility Components"]:::util
    end
```
