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;
+}
} \
_ret; \
})
+
+int parse_path_argument_and_warn(const char *path, bool suppress_root, char **arg);
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:
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;
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':
strv_free(arg_system_units);
strv_free(arg_user_units);
+ free(arg_root);
+
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
#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) {
{}
};
- int c;
+ int c, r;
assert(argc >= 0);
assert(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:
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;
}
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;
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':
}
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");
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,
{}
};
- int c;
+ int c, r;
assert(argc >= 0);
assert(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':
strv_free(arg_properties);
strv_free(arg_wall);
+ free(arg_root);
release_busses();
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 '?':
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 '?':