Something went wrong. Try again.
FTP for Zig 0.16: a client, a server, and the wire protocol as a layer you can use on its own, all built on std.Io.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! A three-digit FTP reply code, and what its digits mean.//!//! RFC 959 §4.2 gives the code a grammar rather than only a list: the first//! digit says whether the command succeeded, the second says what part of the//! server is talking, and the third is a bare serial number within that pair.//! A client that only understands the list cannot cope with a server that//! invents a code, and servers do — so `Code` is a non-exhaustive enum whose//! named members are the codes the standards define, and whose `severity` and//! `category` read the digits of whatever else arrives.
const std = @import("std");const Io = std.Io;
const Code = @This();
value: u16,
// -- 1yz: positive preliminary --------------------------------------------//// The command has been accepted and another reply will follow. Nothing else// may be sent on the control connection until it does.
/// Restart marker reply. The text holds the marker, in a format the two ends/// agreed on out of band.pub const restart_marker: Code = .{ .value = 110 };/// Service ready in `nnn` minutes.pub const service_ready_soon: Code = .{ .value = 120 };/// Data connection already open; transfer starting.pub const data_connection_open_starting: Code = .{ .value = 125 };/// File status okay; about to open the data connection.pub const file_status_okay: Code = .{ .value = 150 };
// -- 2yz: positive completion ---------------------------------------------
/// Command okay.pub const command_okay: Code = .{ .value = 200 };/// Command not implemented, superfluous at this site.pub const command_superfluous: Code = .{ .value = 202 };/// System status, or a system help reply.pub const system_status: Code = .{ .value = 211 };/// Directory status.pub const directory_status: Code = .{ .value = 212 };/// File status. The reply to `SIZE` and `MDTM`.pub const file_status: Code = .{ .value = 213 };/// Help message.pub const help_message: Code = .{ .value = 214 };/// The reply to `SYST`: a system name from the IANA operating system registry.pub const system_type: Code = .{ .value = 215 };/// Service ready for a new user. The greeting.pub const service_ready: Code = .{ .value = 220 };/// Service closing the control connection. The reply to `QUIT`.pub const closing_control: Code = .{ .value = 221 };/// Data connection open; no transfer in progress.pub const data_connection_open: Code = .{ .value = 225 };/// Closing the data connection; the requested action succeeded.pub const closing_data_connection: Code = .{ .value = 226 };/// Entering passive mode. The text holds `(h1,h2,h3,h4,p1,p2)`.pub const passive_mode: Code = .{ .value = 227 };/// Entering long passive mode (RFC 1639, `LPSV`).pub const long_passive_mode: Code = .{ .value = 228 };/// Entering extended passive mode (RFC 2428, `EPSV`). The text holds/// `(|||port|)`.pub const extended_passive_mode: Code = .{ .value = 229 };/// User logged in, proceed.pub const user_logged_in: Code = .{ .value = 230 };/// User logged out; service terminated (RFC 2228).pub const user_logged_out: Code = .{ .value = 231 };/// Logout command noted, will complete when the transfer does (RFC 2228).pub const logout_pending: Code = .{ .value = 232 };/// Security data exchange complete; no further data needed (RFC 2228). The/// reply to an accepted `AUTH TLS`, after which the handshake begins.pub const security_exchange_complete: Code = .{ .value = 234 };/// Security data exchange complete, with data in the reply (RFC 2228).pub const security_data_complete: Code = .{ .value = 235 };/// Requested file action okay, completed.pub const file_action_okay: Code = .{ .value = 250 };/// `"PATHNAME" created`. The reply to `MKD` and to `PWD`, whose pathname is/// quoted and doubled per RFC 959 — see `protocol.path.quote`.pub const pathname_created: Code = .{ .value = 257 };
// -- 3yz: positive intermediate -------------------------------------------//// The command was accepted but the server is waiting for another command// before it can act.
/// User name okay, need a password.pub const need_password: Code = .{ .value = 331 };/// Need an account to log in.pub const need_account: Code = .{ .value = 332 };/// Security mechanism accepted; expecting more data (RFC 2228).pub const security_data_expected: Code = .{ .value = 334 };/// Security data is acceptable; more is expected (RFC 2228).pub const security_data_acceptable: Code = .{ .value = 335 };/// Username okay, need a password; the text holds a challenge (RFC 2228).pub const need_password_challenge: Code = .{ .value = 336 };/// Requested file action pending further information. The reply to `RNFR` and/// to `REST`.pub const pending_further_information: Code = .{ .value = 350 };
// -- 4yz: transient negative completion -----------------------------------//// The command failed but the condition is temporary: the same command may be// tried again later.
/// Service not available, closing the control connection. Also the reply an/// idle-timed-out session is closed with.pub const service_unavailable: Code = .{ .value = 421 };/// Cannot open the data connection.pub const cannot_open_data_connection: Code = .{ .value = 425 };/// Connection closed; transfer aborted. What `ABOR` interrupts a transfer/// with, and what a data connection that broke is reported as.pub const transfer_aborted: Code = .{ .value = 426 };/// Invalid username or password (RFC 2228).pub const invalid_credentials: Code = .{ .value = 430 };/// Need an unavailable resource to process the security request (RFC 2228).pub const security_resource_unavailable: Code = .{ .value = 431 };/// Requested host unavailable.pub const host_unavailable: Code = .{ .value = 434 };/// Requested file action not taken: the file is busy.pub const file_busy: Code = .{ .value = 450 };/// Requested action aborted: a local error in processing.pub const local_error: Code = .{ .value = 451 };/// Requested action not taken: insufficient storage space.pub const insufficient_storage: Code = .{ .value = 452 };
// -- 5yz: permanent negative completion -----------------------------------//// The command failed and retrying it unchanged will fail the same way.
/// Syntax error, command unrecognised.pub const syntax_error: Code = .{ .value = 500 };/// Syntax error in parameters or arguments.pub const bad_arguments: Code = .{ .value = 501 };/// Command not implemented.pub const not_implemented: Code = .{ .value = 502 };/// Bad sequence of commands. `PASS` before `USER`, `RNTO` before `RNFR`.pub const bad_sequence: Code = .{ .value = 503 };/// Command not implemented for that parameter. `TYPE E`, say.pub const parameter_not_implemented: Code = .{ .value = 504 };/// Not logged in.pub const not_logged_in: Code = .{ .value = 530 };/// Need an account for storing files.pub const need_account_for_storing: Code = .{ .value = 532 };/// Command protection level denied for policy reasons (RFC 2228).pub const protection_denied: Code = .{ .value = 533 };/// Request denied for policy reasons (RFC 2228).pub const request_denied: Code = .{ .value = 534 };/// Failed security check (RFC 2228).pub const failed_security_check: Code = .{ .value = 535 };/// Requested `PROT` level not supported (RFC 2228).pub const protection_level_unsupported: Code = .{ .value = 536 };/// Command protection level not supported by the security mechanism (RFC 2228).pub const command_protection_unsupported: Code = .{ .value = 537 };/// Requested action not taken: the file is unavailable. The catch-all for a/// path that does not exist or may not be reached.pub const file_unavailable: Code = .{ .value = 550 };/// Requested action aborted: page type unknown.pub const page_type_unknown: Code = .{ .value = 551 };/// Requested file action aborted: storage allocation exceeded.pub const storage_allocation_exceeded: Code = .{ .value = 552 };/// Requested action not taken: file name not allowed.pub const file_name_not_allowed: Code = .{ .value = 553 };
/// What the first digit says about the command.pub const Severity = enum(u4) { /// 1yz. Accepted; another reply follows before anything else may be sent. preliminary = 1, /// 2yz. The command finished and a new one may be sent. completion = 2, /// 3yz. Accepted, but held pending a further command. intermediate = 3, /// 4yz. Failed for a condition that may pass; the command may be retried. transient = 4, /// 5yz. Failed for a condition that will not pass; retrying is pointless. permanent = 5, /// 6yz. A protected reply, whose text is a base 64 encoded, integrity /// protected message (RFC 2228 §5). protected = 6, _,
/// Whether the command was accepted. A preliminary or intermediate reply /// counts as accepted: the exchange is not over, but nothing has gone /// wrong. Note that a `protected` reply says nothing either way until it /// is unwrapped, and is reported as not positive. pub fn isPositive(s: Severity) bool { return switch (s) { .preliminary, .completion, .intermediate => true, else => false, }; }};
/// What the second digit says about which part of the server replied. Only/// `file_system` and `connections` carry information a client normally acts/// on; the rest are useful for logging and little else.pub const Category = enum(u4) { /// x0z. Syntax: errors of form, and commands the server does not have. syntax = 0, /// x1z. Information: status and help. information = 1, /// x2z. Connections: the control and data connections themselves. connections = 2, /// x3z. Authentication and accounting: logging in. authentication = 3, /// x4z. Unspecified by RFC 959, and claimed by RFC 2228 for security. unspecified = 4, /// x5z. File system: the server's view of the files being acted on. file_system = 5, _,};
/// The first digit.pub fn severity(c: Code) Severity { return @fromBackingInt(@as(u4, @truncate(c.value / 100)));}
/// The second digit.pub fn category(c: Code) Category { return @fromBackingInt(@as(u4, @truncate(c.value / 10 % 10)));}
/// Whether the command was accepted; see `Severity.isPositive`.pub fn isPositive(c: Code) bool { return c.severity().isPositive();}
/// Whether this reply is the last one the command will produce. A 1yz reply/// promises another; every other class ends the exchange.pub fn isFinal(c: Code) bool { return c.severity() != .preliminary;}
/// Whether the code is three digits with a first digit in 1-6, which is the/// most that can be said about a code the caller has never seen. Codes outside/// this range come from a server that is not speaking FTP.pub fn isValid(c: Code) bool { return c.value >= 100 and c.value <= 699;}
pub fn format(c: Code, w: *Io.Writer) Io.Writer.Error!void { // Always three digits: a code that somehow holds a value outside the // valid range still has to occupy the three columns the wire format // reserves for it, or the line it is written into is unparseable. try w.printInt(c.value, 10, .lower, .{ .width = 3, .fill = '0' });}
test "digits" { try std.testing.expectEqual(Severity.completion, Code.pathname_created.severity()); try std.testing.expectEqual(Category.file_system, Code.pathname_created.category()); try std.testing.expect(Code.pathname_created.isPositive()); try std.testing.expect(Code.pathname_created.isFinal());
try std.testing.expect(Code.file_status_okay.isPositive()); try std.testing.expect(!Code.file_status_okay.isFinal());
try std.testing.expect(!Code.not_logged_in.isPositive()); try std.testing.expectEqual(Category.authentication, Code.not_logged_in.category());}
test "codes nobody named still parse" { // 555 is not in any RFC, but a client can still tell it is a permanent // failure about the file system, which is all it needs to give up. const unknown: Code = .{ .value = 555 }; try std.testing.expectEqual(Severity.permanent, unknown.severity()); try std.testing.expectEqual(Category.file_system, unknown.category()); try std.testing.expect(unknown.isValid());
try std.testing.expect(!(Code{ .value = 99 }).isValid()); try std.testing.expect(!(Code{ .value = 700 }).isValid());}
test "format pads to three digits" { var buffer: [8]u8 = undefined; try std.testing.expectEqualStrings("220", try std.fmt.bufPrint(&buffer, "{f}", .{Code.service_ready})); try std.testing.expectEqualStrings("007", try std.fmt.bufPrint(&buffer, "{f}", .{Code{ .value = 7 }}));}