// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! What a server serves. //! //! An FTP server is a protocol in front of a tree of named things. Usually //! that tree is a directory — `DirFilesystem` is the implementation that makes //! it one — but nothing in the protocol requires it, and a great many useful //! servers are a front end for something that is not a file system at all: an //! object store, a database, a build artefact cache, a test fixture that lives //! entirely in memory. //! //! So `Session` never touches a file. It resolves the client's pathname into //! the virtual name space (see `protocol.path.resolve`, which is what makes //! `../../../etc/shadow` impossible before an implementation ever sees it) and //! then calls one of these. //! //! The operations are streaming rather than handle-based on purpose. A handle //! would have to be owned, closed on every path out, and given a reader and a //! writer with buffers belonging to somebody; `retrieve` and `store` instead //! hand the implementation the data connection and let it do what it likes //! with it, which is both simpler to call and simpler to implement. const std = @import("std"); const Io = std.Io; const Allocator = std.mem.Allocator; const listing = @import("../protocol/listing.zig"); const Filesystem = @This(); userdata: ?*anyopaque, vtable: *const VTable, /// What a listing holds. The name is the bare name of the entry, not a path. pub const Entry = listing.Entry; /// What can go wrong, in terms a reply code can be chosen from. /// /// Deliberately small: an implementation that has a richer error of its own /// should log it and map it to whichever of these the client can act on. /// `Session.replyFor` is what turns these into codes. pub const Error = error{ /// No such path. NotFound, /// The path exists but this session may not do that to it. AccessDenied, /// A path component that had to be a directory was not one, or a file /// operation was asked of a directory. NotADirectory, /// A directory was named where a file was needed. IsADirectory, /// The target of a create or a rename is already there. AlreadyExists, /// A directory being removed still has things in it. NotEmpty, /// The store is full, or the session's quota is. NoSpaceLeft, /// The store does not allow changes at all. ReadOnly, /// The store cannot do this, and never will. Answered with a 502 rather /// than a 550, because the difference matters to a client: one means try /// something else, the other means try somewhere else. Unsupported, /// The implementation ran out of file descriptors, memory or patience. SystemResources, /// The store itself failed. InputOutput, OutOfMemory, /// The data connection failed while the transfer was in flight. Not the /// store's fault, but it is the one holding the writer when it happens. WriteFailed, /// The same, in the other direction. ReadFailed, }; /// Whether an existing file is replaced or added to. pub const StoreMode = enum { /// `STOR`: replace whatever is there. replace, /// `APPE`: add to the end of whatever is there. append, /// `STOU`: create a file that was not there before, under a name of the /// store's choosing. The name it chose is reported back to the client. unique, }; pub const VTable = struct { /// Facts about one path. The slices in the result must live at least as /// long as `arena`. stat: *const fn ( userdata: ?*anyopaque, io: Io, arena: Allocator, path: []const u8, ) Error!Entry, /// Appends the contents of a directory to `entries`. /// /// Every slice in every entry must be allocated from `arena`, which the /// caller frees in one go when the listing has been sent. `.` and `..` /// are not included: `Session` adds them itself for `MLSD`, where RFC /// 3659 §7.5.1 asks for them, and leaves them out everywhere else. list: *const fn ( userdata: ?*anyopaque, io: Io, arena: Allocator, path: []const u8, entries: *std.ArrayList(Entry), ) Error!void, /// Writes the contents of `path`, from `offset` onwards, to `w`. /// /// Returns the number of bytes written. `w` is the data connection; it is /// not flushed by the caller afterwards, so an implementation that /// buffers must flush its own. retrieve: *const fn ( userdata: ?*anyopaque, io: Io, path: []const u8, offset: u64, w: *Io.Writer, ) Error!u64, /// Reads everything `r` has and stores it at `path`. /// /// Returns the number of bytes stored. `offset` is where in the file to /// begin, from `REST`, and is meaningful only with `.replace`. /// /// For `.unique` the implementation picks the name, writes it into /// `name_buffer` and returns the part of it used through `name`; `path` /// is then the *directory* to create it in. Every other mode leaves /// `name` null. store: *const fn ( userdata: ?*anyopaque, io: Io, path: []const u8, offset: u64, mode: StoreMode, r: *Io.Reader, name_buffer: []u8, name: *?[]const u8, ) Error!u64, makeDirectory: *const fn (userdata: ?*anyopaque, io: Io, path: []const u8) Error!void, removeDirectory: *const fn (userdata: ?*anyopaque, io: Io, path: []const u8) Error!void, removeFile: *const fn (userdata: ?*anyopaque, io: Io, path: []const u8) Error!void, rename: *const fn (userdata: ?*anyopaque, io: Io, from: []const u8, to: []const u8) Error!void, }; pub fn stat(fs: Filesystem, io: Io, arena: Allocator, path: []const u8) Error!Entry { return fs.vtable.stat(fs.userdata, io, arena, path); } pub fn list( fs: Filesystem, io: Io, arena: Allocator, path: []const u8, entries: *std.ArrayList(Entry), ) Error!void { return fs.vtable.list(fs.userdata, io, arena, path, entries); } pub fn retrieve(fs: Filesystem, io: Io, path: []const u8, offset: u64, w: *Io.Writer) Error!u64 { return fs.vtable.retrieve(fs.userdata, io, path, offset, w); } pub fn store( fs: Filesystem, io: Io, path: []const u8, offset: u64, mode: StoreMode, r: *Io.Reader, name_buffer: []u8, name: *?[]const u8, ) Error!u64 { return fs.vtable.store(fs.userdata, io, path, offset, mode, r, name_buffer, name); } pub fn makeDirectory(fs: Filesystem, io: Io, path: []const u8) Error!void { return fs.vtable.makeDirectory(fs.userdata, io, path); } pub fn removeDirectory(fs: Filesystem, io: Io, path: []const u8) Error!void { return fs.vtable.removeDirectory(fs.userdata, io, path); } pub fn removeFile(fs: Filesystem, io: Io, path: []const u8) Error!void { return fs.vtable.removeFile(fs.userdata, io, path); } pub fn rename(fs: Filesystem, io: Io, from: []const u8, to: []const u8) Error!void { return fs.vtable.rename(fs.userdata, io, from, to); } /// A file system with nothing in it, which answers every path with /// `error.NotFound` and refuses every change. /// /// Useful as the starting point for an implementation that only wants some of /// the operations, and as the thing a server is configured with before it has /// been configured with anything. pub const empty: Filesystem = .{ .userdata = null, .vtable = &empty_vtable }; const empty_vtable: VTable = .{ .stat = struct { fn f(_: ?*anyopaque, _: Io, _: Allocator, _: []const u8) Error!Entry { return error.NotFound; } }.f, .list = struct { fn f(_: ?*anyopaque, _: Io, _: Allocator, _: []const u8, _: *std.ArrayList(Entry)) Error!void { return error.NotFound; } }.f, .retrieve = struct { fn f(_: ?*anyopaque, _: Io, _: []const u8, _: u64, _: *Io.Writer) Error!u64 { return error.NotFound; } }.f, .store = struct { fn f( _: ?*anyopaque, _: Io, _: []const u8, _: u64, _: StoreMode, _: *Io.Reader, _: []u8, _: *?[]const u8, ) Error!u64 { return error.ReadOnly; } }.f, .makeDirectory = struct { fn f(_: ?*anyopaque, _: Io, _: []const u8) Error!void { return error.ReadOnly; } }.f, .removeDirectory = struct { fn f(_: ?*anyopaque, _: Io, _: []const u8) Error!void { return error.ReadOnly; } }.f, .removeFile = struct { fn f(_: ?*anyopaque, _: Io, _: []const u8) Error!void { return error.ReadOnly; } }.f, .rename = struct { fn f(_: ?*anyopaque, _: Io, _: []const u8, _: []const u8) Error!void { return error.ReadOnly; } }.f, }; test "the empty file system refuses everything" { const io = std.testing.io; try std.testing.expectError(error.NotFound, empty.stat(io, std.testing.allocator, "/")); try std.testing.expectError(error.ReadOnly, empty.makeDirectory(io, "/x")); try std.testing.expectError(error.ReadOnly, empty.rename(io, "/a", "/b")); }