#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;
typedef struct {
uint32_t n;
Action a[PLACE_MAX + LEDGER_ENTRY_MAX + CONF_HARNESS_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 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.
int place_lock(const char *path);
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 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", "! 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