path-util: unify how we process paths specified on the command line

Let's introduce a common function that makes relative paths absolute and
warns about any errors while doing so.
This commit is contained in:
Lennart Poettering 2015-10-22 19:54:29 +02:00
parent 0f47436510
commit 0f03c2a4c0
9 changed files with 69 additions and 58 deletions

View File

@ -902,3 +902,35 @@ char *prefix_root(const char *root, const char *path) {
strcpy(p, path);
return n;
}
int parse_path_argument_and_warn(const char *path, bool suppress_root, char **arg) {
char *p;
int r;
/*
* This function is intended to be used in command line
* parsers, to handle paths that are passed in. It makes the
* path absolute, and reduces it to NULL if omitted or
* root (the latter optionally).
*
* NOTE THAT THIS WILL FREE THE PREVIOUS ARGUMENT POINTER ON
* SUCCESS! Hence, do not pass in uninitialized pointers.
*/
if (isempty(path)) {
*arg = mfree(*arg);
return 0;
}
r = path_make_absolute_cwd(path, &p);
if (r < 0)
return log_error_errno(r, "Failed to parse path \"%s\" and make it absolute: %m", path);
path_kill_slashes(p);
if (suppress_root && path_equal(p, "/"))
p = mfree(p);
free(*arg);
*arg = p;
return 0;
}

View File

@ -101,3 +101,5 @@ char *prefix_root(const char *root, const char *path);
} \
_ret; \
})
int parse_path_argument_and_warn(const char *path, bool suppress_root, char **arg);

View File

@ -690,12 +690,9 @@ static int parse_argv(int argc, char *argv[]) {
return version();
case ARG_ROOT:
arg_root = mfree(arg_root);
r = path_make_absolute_cwd(optarg, &arg_root);
r = parse_path_argument_and_warn(optarg, true, &arg_root);
if (r < 0)
return log_error_errno(r, "Failed to make root path absolute: %m");
path_kill_slashes(arg_root);
return r;
break;
case ARG_LOCALE:

View File

@ -104,7 +104,7 @@ static const char *arg_field = NULL;
static bool arg_catalog = false;
static bool arg_reverse = false;
static int arg_journal_type = 0;
static const char *arg_root = NULL;
static char *arg_root = NULL;
static const char *arg_machine = NULL;
static uint64_t arg_vacuum_size = 0;
static uint64_t arg_vacuum_n_files = 0;
@ -505,7 +505,9 @@ static int parse_argv(int argc, char *argv[]) {
break;
case ARG_ROOT:
arg_root = optarg;
r = parse_path_argument_and_warn(optarg, true, &arg_root);
if (r < 0)
return r;
break;
case 'c':
@ -2247,5 +2249,7 @@ finish:
strv_free(arg_system_units);
strv_free(arg_user_units);
free(arg_root);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}

View File

@ -27,8 +27,9 @@
#include "log.h"
#include "machine-id-setup.h"
#include "util.h"
#include "path-util.h"
static const char *arg_root = NULL;
static char *arg_root = NULL;
static bool arg_commit = false;
static void help(void) {
@ -57,7 +58,7 @@ static int parse_argv(int argc, char *argv[]) {
{}
};
int c;
int c, r;
assert(argc >= 0);
assert(argv);
@ -74,7 +75,9 @@ static int parse_argv(int argc, char *argv[]) {
return version();
case ARG_ROOT:
arg_root = optarg;
r = parse_path_argument_and_warn(optarg, true, &arg_root);
if (r < 0)
return r;
break;
case ARG_COMMIT:
@ -104,13 +107,14 @@ int main(int argc, char *argv[]) {
r = parse_argv(argc, argv);
if (r <= 0)
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
goto finish;
if (arg_commit)
r = machine_id_commit(arg_root);
else
r = machine_id_setup(arg_root);
finish:
free(arg_root);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}

View File

@ -276,28 +276,6 @@ static int custom_mounts_prepare(void) {
return 0;
}
static int set_sanitized_path(char **b, const char *path) {
char *p;
int r;
assert(b);
assert(path);
p = canonicalize_file_name(path);
if (!p) {
if (errno != ENOENT)
return -errno;
r = path_make_absolute_cwd(path, &p);
if (r < 0)
return r;
}
free(*b);
*b = path_kill_slashes(p);
return 0;
}
static int detect_unified_cgroup_hierarchy(void) {
const char *e;
int r;
@ -417,24 +395,21 @@ static int parse_argv(int argc, char *argv[]) {
return version();
case 'D':
r = set_sanitized_path(&arg_directory, optarg);
r = parse_path_argument_and_warn(optarg, false, &arg_directory);
if (r < 0)
return log_error_errno(r, "Invalid root directory: %m");
return r;
break;
case ARG_TEMPLATE:
r = set_sanitized_path(&arg_template, optarg);
r = parse_path_argument_and_warn(optarg, false, &arg_template);
if (r < 0)
return log_error_errno(r, "Invalid template directory: %m");
return r;
break;
case 'i':
r = set_sanitized_path(&arg_image, optarg);
r = parse_path_argument_and_warn(optarg, false, &arg_image);
if (r < 0)
return log_error_errno(r, "Invalid image path: %m");
return r;
break;
case 'x':
@ -2323,9 +2298,9 @@ static int determine_names(void) {
}
if (i->type == IMAGE_RAW)
r = set_sanitized_path(&arg_image, i->path);
r = free_and_strdup(&arg_image, i->path);
else
r = set_sanitized_path(&arg_directory, i->path);
r = free_and_strdup(&arg_directory, i->path);
if (r < 0)
return log_error_errno(r, "Invalid image directory: %m");

View File

@ -107,7 +107,7 @@ static UnitFilePresetMode arg_preset_mode = UNIT_FILE_PRESET_FULL;
static char **arg_wall = NULL;
static const char *arg_kill_who = NULL;
static int arg_signal = SIGTERM;
static const char *arg_root = NULL;
static char *arg_root = NULL;
static usec_t arg_when = 0;
static enum action {
_ACTION_INVALID,
@ -6612,7 +6612,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
{}
};
int c;
int c, r;
assert(argc >= 0);
assert(argv);
@ -6769,7 +6769,9 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
break;
case ARG_ROOT:
arg_root = optarg;
r = parse_path_argument_and_warn(optarg, true, &arg_root);
if (r < 0)
return r;
break;
case 'l':
@ -7778,6 +7780,7 @@ finish:
strv_free(arg_properties);
strv_free(arg_wall);
free(arg_root);
release_busses();

View File

@ -1779,12 +1779,9 @@ static int parse_argv(int argc, char *argv[]) {
return version();
case ARG_ROOT:
arg_root = mfree(arg_root);
r = path_make_absolute_cwd(optarg, &arg_root);
r = parse_path_argument_and_warn(optarg, true, &arg_root);
if (r < 0)
return log_error_errno(r, "Failed to make root path absolute: %m");
path_kill_slashes(arg_root);
return r;
break;
case '?':

View File

@ -2144,12 +2144,9 @@ static int parse_argv(int argc, char *argv[]) {
break;
case ARG_ROOT:
arg_root = mfree(arg_root);
r = path_make_absolute_cwd(optarg, &arg_root);
r = parse_path_argument_and_warn(optarg, true, &arg_root);
if (r < 0)
return log_error_errno(r, "Failed to make root path absolute: %m");
path_kill_slashes(arg_root);
return r;
break;
case '?':