From 57ba050c58e0ea2b0bdfec73b13c9de58fdaeb1f Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Tue, 11 Dec 2001 12:18:56 +0000 Subject: [PATCH] o flags.c reads and writes a status bitset --- lib/format_text/export.c | 2 +- lib/format_text/flags.c | 142 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 lib/format_text/flags.c diff --git a/lib/format_text/export.c b/lib/format_text/export.c index 88c90ba9f..4876aea4a 100644 --- a/lib/format_text/export.c +++ b/lib/format_text/export.c @@ -4,7 +4,7 @@ * This file is released under the LGPL. */ -#include "text-rep.h" +#include "import-export.h" #include "metadata.h" #include "log.h" #include "hash.h" diff --git a/lib/format_text/flags.c b/lib/format_text/flags.c new file mode 100644 index 000000000..a994535a6 --- /dev/null +++ b/lib/format_text/flags.c @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2001 Sistina Software (UK) Limited. + * + * This file is released under the LGPL. + */ + +#include "metadata.h" +#include "import-export.h" + +/* + * Bitsets held in the 'status' flags get + * converted into arrays of strings. + */ +struct flag { + int mask; + char *description; +}; + +static struct flag _vg_flags[] = { + {ACTIVE, "ACTIVE"}, + {EXPORTED_VG, "EXPORTED"}, + {EXTENDABLE_VG, "EXTENDABLE"}, + {CLUSTERED, "CLUSTERED"}, + {SHARED, "SHARED"}, + {0, NULL} +}; + +static struct flag _pv_flags[] = { + {ACTIVE, "ACTIVE"}, + {ALLOCATED_PV, "ALLOCATED"}, + {0, NULL} +}; + +static struct flag _lv_flags[] = { + {ACTIVE, "ACTIVE"}, + {READ, "READ"}, + {WRITE, "WRITE"}, + {ALLOC_SIMPLE, "ALLOC_SIMPLE"}, + {ALLOC_STRICT, "ALLOC_STRICT"}, + {ALLOC_CONTIGUOUS, "ALLOC_CONTIGUOUS"}, + {SNAPSHOT, "SNASHOT"}, + {SNAPSHOT_ORG, "SNAPSHOT_ORIGIN"}, + {0, NULL} +}; + +static struct flags *_get_flags(int type) +{ + switch (type) { + case VG_FLAGS: + return _vg_flags; + + case PV_FLAGS: + return _pv_flags; + + case LV_FLAGS: + return _lv_flags; + } + + log_err("Unknown flag set requested."); + return NULL; +} + +/* + * Converts a bitset to an array of string values, + * using one of the tables defined at the top of + * the file. + */ +int print_flags(uint32_t status, int type, char *buffer, size_t size) +{ + int f, first = 1, n; + struct flag *flags; + + if (!(flags = _get_flags(type))) { + stack; + return 0; + } + + if ((n = snprintf(buffer, size, "["))) + return 0; + size -= n; + + for (f = 0; flags[f].mask; f++) { + if (status & flags[f].mask) { + if (!first) + fprintf(fp, ", "); + else + first = 0; + + n = snprintf(buffer, size, "\"%s\"", flags[f].name); + if (n < 0) + return 0; + size -= n; + + status &= ~flags[f].mask; + } + } + + if (snprintf(buffer, size, "]")) + return 0; + + if (status) + /* FIXME: agk is this the correct log level ? */ + log_print("Not all flags were successfully exported, " + "possible bug."); + + return 1; +} + +int read_flags(uint32_t *status, int type, struct config_value *cv) +{ + int f; + uint32_t s = 0; + struct flag *flags; + + if (!(flags = _get_flags(type))) { + stack; + return 0; + } + + while (cv) { + if (cv != CFG_STRING) { + log_err("Status value is not a string."); + return 0; + } + + for (f = 0; flags[f].description; f++) + if (!strcmp(flags[f].description, cv.v.str)) { + status &= flags[f].mask; + break; + } + + if (!flags[f].description) { + log_err("Unknown status flag."); + return 0; + } + + cv = cv->next; + } + + *status = s; + return 1; +}