Sftpd.Backends.Memory (Sftpd v0.1.1)

Copy Markdown View Source

In-memory storage backend for testing and development.

This backend stores all files in memory using an Agent. It's useful for:

  • Testing without external dependencies (no S3-compatible service needed)
  • Development and experimentation
  • As a reference implementation for custom backends

Usage

{:ok, ref} = Sftpd.start_server(
  port: 2222,
  backend: Sftpd.Backends.Memory,
  backend_opts: [],
  auth: {:passwords, [{"user", "pass"}]},
  system_dir: "path/to/ssh_keys"
)

State Structure

The backend maintains a map of paths to file data:

%{
  "path/to/file.txt" => %{content: "...", mtime: ~N[...]},
  "path/to/dir/.keep" => %{content: "", mtime: ~N[...]}
}

Directories are represented by .keep marker files (like S3).

Examples

iex> {:ok, state} = Sftpd.Backends.Memory.init(
...>   files: %{"hello.txt" => %{content: "hi", mtime: ~N[2024-01-01 00:00:00]}}
...> )
iex> Sftpd.Backends.Memory.list_dir(~c"/", state)
{:ok, [~c".", ~c"..", ~c"hello.txt"]}

iex> {:ok, state} = Sftpd.Backends.Memory.init([])
iex> :ok = Sftpd.Backends.Memory.write_file(~c"/notes.txt", "hello", state)
iex> Sftpd.Backends.Memory.read_file(~c"/notes.txt", state)
{:ok, "hello"}

See the Backends and Custom Backends extras in HexDocs for how this backend fits into the wider package model.

Summary

Types

File data stored in memory

Memory backend state containing the Agent process

Types

file_data()

@type file_data() :: %{content: binary(), mtime: NaiveDateTime.t()}

File data stored in memory

state()

@type state() :: %{agent: pid()}

Memory backend state containing the Agent process