1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

refactor: factor out common part of the code to open and read config files

This commit is contained in:
Peter Rajnoha 2013-06-25 12:25:23 +02:00
parent b31725d0ae
commit 2d784753c0
3 changed files with 33 additions and 24 deletions

View File

@ -532,7 +532,6 @@ static int _load_config_file(struct cmd_context *cmd, const char *tag)
{ {
static char config_file[PATH_MAX] = ""; static char config_file[PATH_MAX] = "";
const char *filler = ""; const char *filler = "";
struct stat info;
struct config_tree_list *cfl; struct config_tree_list *cfl;
if (*tag) if (*tag)
@ -549,32 +548,11 @@ static int _load_config_file(struct cmd_context *cmd, const char *tag)
return 0; return 0;
} }
if (!(cfl->cft = config_file_open(config_file, 0))) { if (!(cfl->cft = config_file_open_and_read(config_file)))
log_error("config_tree allocation failed"); return_0;
return 0;
}
/* Is there a config file? */
if (stat(config_file, &info) == -1) {
if (errno == ENOENT) {
dm_list_add(&cmd->config_files, &cfl->list);
goto out;
}
log_sys_error("stat", config_file);
config_file_destroy(cfl->cft);
return 0;
}
log_very_verbose("Loading config file: %s", config_file);
if (!config_file_read(cfl->cft)) {
log_error("Failed to load config file %s", config_file);
config_file_destroy(cfl->cft);
return 0;
}
dm_list_add(&cmd->config_files, &cfl->list); dm_list_add(&cmd->config_files, &cfl->list);
out:
if (*tag) { if (*tag) {
if (!_init_tags(cmd, cfl->cft)) if (!_init_tags(cmd, cfl->cft))
return_0; return_0;

View File

@ -170,6 +170,36 @@ void config_file_destroy(struct dm_config_tree *cft)
dm_config_destroy(cft); dm_config_destroy(cft);
} }
struct dm_config_tree *config_file_open_and_read(const char *config_file)
{
struct dm_config_tree *cft;
struct stat info;
if (!(cft = config_file_open(config_file, 0))) {
log_error("config_tree allocation failed");
return NULL;
}
/* Is there a config file? */
if (stat(config_file, &info) == -1) {
if (errno == ENOENT)
return cft;
log_sys_error("stat", config_file);
goto bad;
}
log_very_verbose("Loading config file: %s", config_file);
if (!config_file_read(cft)) {
log_error("Failed to load config file %s", config_file);
goto bad;
}
return cft;
bad:
config_file_destroy(cft);
return NULL;
}
/* /*
* Returns config tree if it was removed. * Returns config tree if it was removed.
*/ */

View File

@ -120,6 +120,7 @@ int config_file_read_fd(struct dm_config_tree *cft, struct device *dev,
off_t offset, size_t size, off_t offset2, size_t size2, off_t offset, size_t size, off_t offset2, size_t size2,
checksum_fn_t checksum_fn, uint32_t checksum); checksum_fn_t checksum_fn, uint32_t checksum);
int config_file_read(struct dm_config_tree *cft); int config_file_read(struct dm_config_tree *cft);
struct dm_config_tree *config_file_open_and_read(const char *config_file);
int config_write(struct dm_config_tree *cft, int config_write(struct dm_config_tree *cft,
int withcomment, int withversion, int withcomment, int withversion,
const char *file, int argc, char **argv); const char *file, int argc, char **argv);