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:
parent
b31725d0ae
commit
2d784753c0
@ -532,7 +532,6 @@ static int _load_config_file(struct cmd_context *cmd, const char *tag)
|
||||
{
|
||||
static char config_file[PATH_MAX] = "";
|
||||
const char *filler = "";
|
||||
struct stat info;
|
||||
struct config_tree_list *cfl;
|
||||
|
||||
if (*tag)
|
||||
@ -549,32 +548,11 @@ static int _load_config_file(struct cmd_context *cmd, const char *tag)
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!(cfl->cft = config_file_open(config_file, 0))) {
|
||||
log_error("config_tree allocation failed");
|
||||
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;
|
||||
}
|
||||
if (!(cfl->cft = config_file_open_and_read(config_file)))
|
||||
return_0;
|
||||
|
||||
dm_list_add(&cmd->config_files, &cfl->list);
|
||||
|
||||
out:
|
||||
if (*tag) {
|
||||
if (!_init_tags(cmd, cfl->cft))
|
||||
return_0;
|
||||
|
@ -170,6 +170,36 @@ void config_file_destroy(struct dm_config_tree *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.
|
||||
*/
|
||||
|
@ -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,
|
||||
checksum_fn_t checksum_fn, uint32_t checksum);
|
||||
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 withcomment, int withversion,
|
||||
const char *file, int argc, char **argv);
|
||||
|
Loading…
Reference in New Issue
Block a user