" -C, --clear clear the cache completely (except configuration)\n"
" -F, --max-files=N set maximum number of files in cache to N (use 0 for\n"
" no limit)\n"
+ " -k, --get-config=K get the value of the configuration key K\n"
" -M, --max-size=SIZE set maximum size of cache to SIZE (use 0 for no\n"
" limit); available suffixes: k, M, G, T (decimal) and\n"
" Ki, Mi, Gi, Ti (binary); default suffix: G\n"
{"cleanup", no_argument, 0, 'c'},
{"clear", no_argument, 0, 'C'},
{"dump-manifest", required_argument, 0, DUMP_MANIFEST},
+ {"get-config", required_argument, 0, 'k'},
{"hash-file", required_argument, 0, HASH_FILE},
{"help", no_argument, 0, 'h'},
{"max-files", required_argument, 0, 'F'},
};
int c;
- while ((c = getopt_long(argc, argv, "cChF:M:o:psVz", options, NULL)) != -1) {
+ while ((c = getopt_long(argc, argv, "cCk:hF:M:o:psVz", options, NULL))
+ != -1) {
switch (c) {
case DUMP_MANIFEST:
manifest_dump(optarg, stdout);
fputs(USAGE_TEXT, stdout);
x_exit(0);
+ case 'k': // --get-config
+ {
+ initialize();
+ char *errmsg;
+ if (!conf_print_value(conf, optarg, stdout, &errmsg)) {
+ fatal("%s", errmsg);
+ }
+ }
+ break;
+
case 'F': // --max-files
{
initialize();
return true;
}
+bool
+conf_print_value(struct conf *conf, const char *key,
+ FILE *file, char **errmsg)
+{
+ const struct conf_item *item = find_conf(key);
+ if (!item) {
+ *errmsg = format("unknown configuration option \"%s\"", key);
+ return false;
+ }
+ void *value = (char *)conf + item->offset;
+ char *str = (char *)item->formatter(value);
+ fprintf(file, "%s\n", str);
+ free(str);
+ return true;
+}
+
static bool
print_item(struct conf *conf, const char *key,
void (*printer)(const char *descr, const char *origin,
void conf_free(struct conf *conf);
bool conf_read(struct conf *conf, const char *path, char **errmsg);
bool conf_update_from_environment(struct conf *conf, char **errmsg);
+bool conf_print_value(struct conf *conf, const char *key,
+ FILE *file, char **errmsg);
bool conf_set_value_in_file(const char *path, const char *key,
const char *value, char **errmsg);
bool conf_print_items(struct conf *conf,
CHECK_STR_EQ_FREE2("path = vanilla\nstats = chocolate\n", data);
}
+TEST(conf_print_existing_value)
+{
+ struct conf *conf = conf_create();
+ conf->max_files = 42;
+ char *errmsg;
+ {
+ FILE *log = fopen("log", "w");
+ CHECK(log);
+ CHECK(conf_print_value(conf, "max_files", log, &errmsg));
+ fclose(log);
+ }
+ {
+ FILE *log = fopen("log", "r");
+ CHECK(log);
+ char buf[100];
+ CHECK(fgets(buf, 100, log));
+ CHECK_STR_EQ("42\n", buf);
+ fclose(log);
+ }
+ conf_free(conf);
+}
+
+TEST(conf_print_unknown_value)
+{
+ struct conf *conf = conf_create();
+ char *errmsg;
+ {
+ FILE *log = fopen("log", "w");
+ CHECK(log);
+ CHECK(!conf_print_value(conf, "foo", log, &errmsg));
+ CHECK_STR_EQ_FREE2("unknown configuration option \"foo\"",
+ errmsg);
+ fclose(log);
+ }
+ {
+ FILE *log = fopen("log", "r");
+ CHECK(log);
+ char buf[100];
+ CHECK(!fgets(buf, 100, log));
+ fclose(log);
+ }
+ conf_free(conf);
+}
+
TEST(conf_print_items)
{
size_t i;