#ifndef SUIT_PLACE_H
#define SUIT_PLACE_H
#include "ledger.h"
#include "resolve.h"
// Place is the only code that changes the disk outside the store. It never
// replaces or removes anything suit does not own: what suit owns is what the
// ledger lists.
constexpr int EXIT_BUSY = 20;
constexpr uint32_t DIR_MAX = 4096;
constexpr size_t DIR_TEXT_MAX = 262144;
typedef enum {
ACT_CREATE, ACT_REPLACE, ACT_KEEP, ACT_REMOVE, ACT_CLASH, ACT_DROP,
ACT_WRITE, // write a joined file: DEST is its path
ACT_FILE, // a joined file that is already right
ACT_UNWRITE, // remove a joined file nothing wants any more
} ActKind;
typedef struct {
ActKind kind;
const char *dest;
const char *src; // the link to make; for ACT_REMOVE the link suit made
const char *old; // ACT_REPLACE: the link suit made before; ACT_WRITE: non-null if the ledger has the file
const char *reason; // ACT_CLASH
const Placement *want;
const Join *join; // ACT_WRITE and ACT_FILE
} Action;
// The block suit keeps in a git project's exclude file, so the links stay out of
// its version control. SET: the file has to hold exactly these lines. REMOVE: the
// ledger has a block that nothing wants any more.
typedef enum { BL_SET, BL_REMOVE } BlockKind;
constexpr uint32_t BLOCK_MAX = PROJECT_ENTRY_MAX + LEDGER_ENTRY_MAX; // wanted, and left in the ledger
// The lines are at most twice the paths in the plan, which are at most PLAN_TEXT_MAX
// bytes, and each project names one exclude file of at most EXCLUDE_PATH_MAX bytes.
constexpr size_t BLOCK_TEXT_MAX = 2 * PLAN_TEXT_MAX + 2 * PLACE_MAX + PROJECT_ENTRY_MAX * 4096;
typedef struct {
BlockKind kind;
const char *path; // the exclude file
uint32_t first; // its lines are lines[first .. first + count)
uint32_t count;
bool created; // suit made the file
bool changed; // the file's bytes will change
} Block;
typedef struct {
uint32_t n;
Action a[PLACE_MAX + LEDGER_ENTRY_MAX + JOIN_MAX];
uint32_t ndirs;
const char *dirs[DIR_MAX]; // folders to make, parents first
char text[DIR_TEXT_MAX];
size_t used;
uint32_t nblocks;
Block blocks[BLOCK_MAX];
uint32_t nlines;
const char *lines[PLACE_MAX];
char btext[BLOCK_TEXT_MAX]; // the blocks' paths and lines
size_t bused;
} Actions;
// Work out what to do. Reads the disk and the sources of joined files, changes
// nothing. Returns 0 or 1 after saying why on stderr.
int place_plan(const Plan *plan, const Ledger *ledger, Actions *out);
// Take the lock, or wait for it for a moment: 0, EXIT_BUSY or 1. With QUIET,
// EXIT_BUSY is returned without a word (the per-minute run tries again).
int place_lock(const char *path, bool quiet);
void place_unlock(const char *path);
// Do ACTS: make the folders, write the joined files, make the links, remove the
// links and joined files that are no longer wanted, and keep the ledger at LEDGER_PATH describing what is on disk at every
// step. Returns 0 or 1 after saying why. The caller holds the lock.
int place_apply(const Ledger *ledger, const Actions *acts, const char *ledger_path);
// Remove every link in the ledger that is still the one suit made, then every
// joined file, then the block in each exclude file, then every folder it made that is empty, print what was done and what was left alone, and
// empty the ledger. Returns 0 or 1. The caller holds the lock.
int place_off(const Ledger *ledger, const char *ledger_path);
// Print the actions the way plan prints them: "+ link DEST -> SOURCE",
// "- link DEST", "+ file PATH", "- file PATH", "+ block PATH", "- block PATH", "! clash DEST (reason)", and the plan's "~ skipped ..." lines.
void place_print(const Plan *plan, const Actions *acts);
// Ledger links whose DEST is gone, written one per line as "missing DEST" and
// the number of them; for status.
uint32_t place_missing(const Ledger *ledger, bool print);
#endif