#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 } 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 const char *reason; // ACT_CLASH const Placement *want; } Action; typedef struct { uint32_t n; Action a[PLACE_MAX + LEDGER_ENTRY_MAX]; uint32_t ndirs; const char *dirs[DIR_MAX]; // folders to make, parents first char text[DIR_TEXT_MAX]; size_t used; } Actions; // Work out what to do. Reads the disk, 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. int place_lock(const char *path); void place_unlock(const char *path); // Do ACTS: make the folders and links, remove the links 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 // 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", "! 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