Artifact 43daf6d7644db8bb6ecd6491ea211f9829f18059e33cb4069f963ee3d6658cc7:
- File
src/remote.h
— part of check-in
[0c65a50c7f]
at
2026-11-14 00:48:47
on branch trunk
— Phase 7c-1: a wardrobe can be cloned onto a second machine, and one call tends them all
suit clone NAME URL makes a wardrobe on this machine from a remote. It clones into a temporary file, renames it into place, opens the store at the tip of trunk and records the remote last. A failure at any step removes what it made: no partial repository file, no store folder, no remotes line. A name already in use is refused with exit 20 and nothing is overwritten. A login saved beforehand is used, so a private wardrobe can be cloned.
suit tend with no name tends every wardrobe in name order. One wardrobe's failure does not stop the others; the exit code is the highest any of them returned, with one line for each that did not end clean.
After three failed syncs in a row the attention file names the wardrobe and the host. A sync that reaches the remote clears the count and the line.
A limit: libfossil's clone builds its own context, so fault injection cannot reach the system calls made inside the clone. The tests cover every step suit takes around it, and a bad remote covers a failure inside it.
One existing case changed: tend-usage expected suit tend with no name to be an error, which is now the behaviour above. It tests suit tend with two names instead.
1204 unit checks, 227 shell cases, 669 store cases, 612 placement cases and 378 project cases, under clang, under gcc 16, with no user name set, and under a strict umask. 100% coverage in all 22 source files. No suite has fewer cases than at the last check-in. (user: dmestas, size: 3516) [annotate] [blame] [check-ins using]
#ifndef SUIT_REMOTE_H #define SUIT_REMOTE_H #include <stddef.h> #include <stdint.h> // Two small files in $SUIT_HOME, each parsed strictly (a line that is not // exactly what is described is an error and the whole file is rejected) and // rewritten atomically. // // remotes: one line per wardrobe, "NAME URL". A URL is file://ABSOLUTE/PATH, // http://HOST[:PORT]/PATH or https://HOST[:PORT]/PATH, with no user name or // password in it. // // credentials: mode 0600, one line per wardrobe, "NAME USER SECRET". The // secret is printable ASCII with no space. It is never printed or written // anywhere else, and is wiped from memory after use. constexpr size_t REMOTE_FILE_MAX = 65536; constexpr size_t REMOTE_URL_MAX = 1024; constexpr size_t REMOTE_LINE_MAX = 1280; constexpr uint32_t REMOTE_ENTRY_MAX = 256; constexpr size_t CRED_USER_MAX = 64; constexpr size_t CRED_SECRET_MAX = 256; typedef struct { int line; // 0 when the error concerns the whole file char msg[200]; } RemoteError; typedef struct { char name[129]; char url[REMOTE_URL_MAX + 1]; } RemoteEntry; typedef struct { uint32_t n; RemoteEntry e[REMOTE_ENTRY_MAX]; } Remotes; typedef struct { char name[129]; char user[CRED_USER_MAX + 1]; char secret[CRED_SECRET_MAX + 1]; } CredEntry; typedef struct { uint32_t n; CredEntry e[REMOTE_ENTRY_MAX]; } Creds; // Nullptr if the LEN bytes are an address suit accepts, else why not. The // reason never repeats the address, which might carry a password. const char *remote_url_check(const char *url, size_t len); // Parse DATA. On failure R is empty and ERR says what and where. [[nodiscard]] bool remotes_parse(Remotes *r, const char *data, size_t len, RemoteError *err); [[nodiscard]] bool creds_parse(Creds *c, const char *data, size_t len, RemoteError *err); // Read PATH. A file that is not there is an empty list. creds_load also // refuses a file that anyone but its owner can use. [[nodiscard]] bool remotes_load(Remotes *r, const char *path, RemoteError *err); [[nodiscard]] bool creds_load(Creds *c, const char *path, RemoteError *err); // Write the entries sorted by name, atomically. Returns 0 or an errno value. int remotes_write(const Remotes *r, const char *path); int creds_write(const Creds *c, const char *path); RemoteEntry *remotes_find(Remotes *r, const char *name); CredEntry *creds_find(Creds *c, const char *name); // Overwrite N bytes at P so the compiler cannot drop it. void secret_wipe(void *p, size_t n); // The host (and port) part of URL, or for file:// its path, for messages. void remote_host(const char *url, char *out, size_t cap); // The commands. Each returns an exit code after reporting on stderr or stdout. // cmd_remote: URL null shows (exit 1 if none), "--none" clears. int cmd_remote(const char *name, const char *url); // Reads the secret from standard input. int cmd_login(const char *name, const char *user); int cmd_logout(const char *name); // $SUIT_HOME/NAME.syncfail: how many syncs of a wardrobe have failed in a row, // as 1 to 6 decimal digits (no leading zero, at least 1) and a newline. No // such file means none. Rewritten atomically. constexpr unsigned SYNC_FAILS_MAX = 999999; [[nodiscard]] bool syncfail_parse(const char *data, size_t len, unsigned *n); [[nodiscard]] bool syncfail_load(const char *path, unsigned *n, RemoteError *err); int syncfail_write(const char *path, unsigned n); // The files' paths for the sync. int remote_paths(char *remotes, char *creds, size_t cap); #endif