ftpclient

package
v0.0.185 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package ftpclient is a minimal FTP client that supports both passive (EPSV/PASV) and active (PORT/EPRT) data connection modes, with explicit context propagation for connect and data transfer operations.

It exists because the third-party FTP clients we evaluated were passive-only, which fails against FTP servers whose advertised PASV port is firewalled.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is an FTP client. It is not safe for concurrent use across operations.

func Dial

func Dial(ctx context.Context, addr string, opts Options) (*Client, error)

Dial opens a control connection to addr and reads the welcome banner. Callers must subsequently call Login before invoking other commands.

func (*Client) Cwd

func (c *Client) Cwd(path string) error

Cwd changes the current working directory.

func (*Client) Delete

func (c *Client) Delete(path string) error

Delete removes a file (DELE).

func (*Client) List

func (c *Client) List(ctx context.Context, path string) ([]Entry, error)

List enumerates a directory. It prefers MLSD when the server advertises it (machine-parseable, format-stable) and falls back to LIST. The LIST parser handles both Unix `ls -l` and Microsoft IIS DOS-style listings, since these are the two dialects we see in the wild.

func (*Client) Login

func (c *Client) Login(user, pass string) error

Login authenticates with USER/PASS, then probes server features and switches to binary transfer mode (TYPE I). Binary mode is needed for both RETR and STOR to avoid byte mangling on text vs. binary content.

func (*Client) Mkdir

func (c *Client) Mkdir(path string) error

Mkdir creates a directory (MKD).

func (*Client) Pwd

func (c *Client) Pwd() (string, error)

Pwd returns the current working directory.

func (*Client) Quit

func (c *Client) Quit() error

Quit closes the connection politely.

func (*Client) Rename

func (c *Client) Rename(from, to string) error

Rename moves a file or directory (RNFR/RNTO).

func (*Client) Retrieve

func (c *Client) Retrieve(ctx context.Context, path string) (io.ReadCloser, error)

Retrieve downloads a file. The returned reader must be fully consumed and closed by the caller; closing reads the 226 transfer-complete response.

func (*Client) Rmdir

func (c *Client) Rmdir(path string) error

Rmdir removes a directory (RMD).

func (*Client) Size

func (c *Client) Size(path string) (int64, error)

Size returns the size of a file in bytes (SIZE).

func (*Client) Store

func (c *Client) Store(ctx context.Context, path string, r io.Reader) error

Store uploads r as path (STOR).

type DataMode

type DataMode string

DataMode controls how the data connection is established for LIST/RETR/STOR.

const (
	// DataModeAuto tries passive first and falls back to active if the passive
	// data connection cannot be dialed.
	DataModeAuto DataMode = "auto"
	// DataModePassive uses EPSV/PASV only.
	DataModePassive DataMode = "passive"
	// DataModeActive uses EPRT/PORT only.
	DataModeActive DataMode = "active"
)

type Entry

type Entry struct {
	Name    string
	Path    string
	IsDir   bool
	Size    int64
	ModTime time.Time
}

Entry is a single directory listing item parsed from LIST output.

type Options

type Options struct {
	// Mode chooses the data-connection mode. Defaults to DataModeAuto.
	Mode DataMode
	// Timeout is applied to control dial, data dial, and per-read deadlines.
	Timeout time.Duration
}

Options configures a Client.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL