mirror of
git://sourceware.org/git/lvm2.git
synced 2024-11-04 05:22:01 +03:00
Move dm_config_write out of libdm, back to lib/config, as config_write.
This commit is contained in:
parent
509278c7d2
commit
11e520256b
@ -324,3 +324,51 @@ int merge_config_tree(struct cmd_context *cmd, struct dm_config_tree *cft,
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int _putline_fn(const char *line, void *baton) {
|
||||
FILE *fp = baton;
|
||||
fprintf(fp, "%s\n", line);
|
||||
return 1;
|
||||
};
|
||||
|
||||
int config_write(struct dm_config_tree *cft, const char *file,
|
||||
int argc, char **argv)
|
||||
{
|
||||
const struct dm_config_node *cn;
|
||||
int r = 1;
|
||||
FILE *fp = NULL;
|
||||
|
||||
if (!file) {
|
||||
fp = stdout;
|
||||
file = "stdout";
|
||||
} else if (!(fp = fopen(file, "w"))) {
|
||||
log_sys_error("open", file);
|
||||
return 0;
|
||||
}
|
||||
|
||||
log_verbose("Dumping configuration to %s", file);
|
||||
if (!argc) {
|
||||
if (!dm_config_write_node(cft->root, _putline_fn, fp)) {
|
||||
log_error("Failure while writing to %s", file);
|
||||
r = 0;
|
||||
}
|
||||
} else while (argc--) {
|
||||
if ((cn = dm_config_find_node(cft->root, *argv))) {
|
||||
if (!dm_config_write_node(cn, _putline_fn, fp)) {
|
||||
log_error("Failure while writing to %s", file);
|
||||
r = 0;
|
||||
}
|
||||
} else {
|
||||
log_error("Configuration node %s not found", *argv);
|
||||
r = 0;
|
||||
}
|
||||
argv++;
|
||||
}
|
||||
|
||||
out:
|
||||
if (fp && dm_fclose(fp)) {
|
||||
stack;
|
||||
r = 0;
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
@ -32,6 +32,9 @@ int read_config_fd(struct dm_config_tree *cft, struct device *dev,
|
||||
off_t offset, size_t size, off_t offset2, size_t size2,
|
||||
checksum_fn_t checksum_fn, uint32_t checksum);
|
||||
|
||||
int config_write(struct dm_config_tree *cft, const char *file,
|
||||
int argc, char **argv);
|
||||
|
||||
int read_config_file(struct dm_config_tree *cft);
|
||||
|
||||
int merge_config_tree(struct cmd_context *cmd, struct dm_config_tree *cft,
|
||||
|
@ -1346,9 +1346,6 @@ struct dm_config_tree *dm_config_insert_cascaded_tree(struct dm_config_tree *fir
|
||||
|
||||
void dm_config_destroy(struct dm_config_tree *cft);
|
||||
|
||||
int dm_config_write(struct dm_config_tree *cft, const char *file,
|
||||
int argc, char **argv);
|
||||
|
||||
typedef int (*dm_putline_fn)(const char *line, void *baton);
|
||||
int dm_config_write_node(const struct dm_config_node *cn, dm_putline_fn putline, void *baton);
|
||||
|
||||
|
@ -62,7 +62,6 @@ struct cs {
|
||||
};
|
||||
|
||||
struct output_line {
|
||||
FILE *fp;
|
||||
struct dm_pool *mem;
|
||||
dm_putline_fn putline;
|
||||
void *putline_baton;
|
||||
@ -356,14 +355,11 @@ static int _line_end(struct output_line *outline)
|
||||
}
|
||||
|
||||
line = dm_pool_end_object(outline->mem);
|
||||
if (outline->putline)
|
||||
outline->putline(line, outline->putline_baton);
|
||||
else {
|
||||
if (!outline->fp)
|
||||
log_print("%s", line);
|
||||
else
|
||||
fprintf(outline->fp, "%s\n", line);
|
||||
}
|
||||
|
||||
if (!outline->putline)
|
||||
return 0;
|
||||
|
||||
outline->putline(line, outline->putline_baton);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@ -458,7 +454,6 @@ static int _write_config(const struct dm_config_node *n, int only_one,
|
||||
int dm_config_write_node(const struct dm_config_node *cn, dm_putline_fn putline, void *baton)
|
||||
{
|
||||
struct output_line outline;
|
||||
outline.fp = NULL;
|
||||
if (!(outline.mem = dm_pool_create("config_line", 1024)))
|
||||
return_0;
|
||||
outline.putline = putline;
|
||||
@ -471,57 +466,6 @@ int dm_config_write_node(const struct dm_config_node *cn, dm_putline_fn putline,
|
||||
return 1;
|
||||
}
|
||||
|
||||
int dm_config_write(struct dm_config_tree *cft, const char *file,
|
||||
int argc, char **argv)
|
||||
{
|
||||
const struct dm_config_node *cn;
|
||||
int r = 1;
|
||||
struct output_line outline;
|
||||
outline.fp = NULL;
|
||||
outline.putline = NULL;
|
||||
|
||||
// FIXME AGK remove the fopen from libdm before release
|
||||
if (!file)
|
||||
file = "stdout";
|
||||
else if (!(outline.fp = fopen(file, "w"))) {
|
||||
log_sys_error("open", file);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!(outline.mem = dm_pool_create("config_line", 1024))) {
|
||||
r = 0;
|
||||
goto_out;
|
||||
}
|
||||
|
||||
log_verbose("Dumping configuration to %s", file);
|
||||
if (!argc) {
|
||||
if (!_write_config(cft->root, 0, &outline, 0)) {
|
||||
log_error("Failure while writing to %s", file);
|
||||
r = 0;
|
||||
}
|
||||
} else while (argc--) {
|
||||
if ((cn = dm_config_find_node(cft->root, *argv))) {
|
||||
if (!_write_config(cn, 1, &outline, 0)) {
|
||||
log_error("Failure while writing to %s", file);
|
||||
r = 0;
|
||||
}
|
||||
} else {
|
||||
log_error("Configuration node %s not found", *argv);
|
||||
r = 0;
|
||||
}
|
||||
argv++;
|
||||
}
|
||||
|
||||
dm_pool_destroy(outline.mem);
|
||||
|
||||
out:
|
||||
if (outline.fp && dm_fclose(outline.fp)) {
|
||||
stack;
|
||||
r = 0;
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/*
|
||||
* parser
|
||||
|
@ -19,7 +19,7 @@ int dumpconfig(struct cmd_context *cmd, int argc, char **argv)
|
||||
{
|
||||
const char *file = arg_str_value(cmd, file_ARG, NULL);
|
||||
|
||||
if (!dm_config_write(cmd->cft, file, argc, argv)) {
|
||||
if (!config_write(cmd->cft, file, argc, argv)) {
|
||||
stack;
|
||||
return ECMD_FAILED;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user