Skip to content

Commit

Permalink
Move and rename parse_path_argument() function
Browse files Browse the repository at this point in the history
This fits better in shared/, and the new parse-argument.c file is a good home
for it.
  • Loading branch information
keszybz committed Feb 15, 2021
1 parent 923e212 commit 614b022
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 60 deletions.
32 changes: 0 additions & 32 deletions src/basic/path-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -752,38 +752,6 @@ int fsck_exists(const char *fstype) {
return executable_is_good(checker);
}

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_simplify(p, false);
if (suppress_root && empty_or_root(p))
p = mfree(p);

free_and_replace(*arg, p);

return 0;
}

char* dirname_malloc(const char *path) {
char *d, *dir, *dir2;

Expand Down
2 changes: 0 additions & 2 deletions src/basic/path-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ int fsck_exists(const char *fstype);
_ret; \
})

int parse_path_argument_and_warn(const char *path, bool suppress_root, char **arg);

char* dirname_malloc(const char *path);
const char *last_path_component(const char *path);
int path_extract_filename(const char *p, char **ret);
Expand Down
5 changes: 3 additions & 2 deletions src/core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include "mount-setup.h"
#include "os-util.h"
#include "pager.h"
#include "parse-argument.h"
#include "parse-util.h"
#include "path-util.h"
#include "pretty-print.h"
Expand Down Expand Up @@ -358,7 +359,7 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
return 0;

if (path_is_absolute(value))
(void) parse_path_argument_and_warn(value, false, &arg_early_core_pattern);
(void) parse_path_argument(value, false, &arg_early_core_pattern);
else
log_warning("Specified core pattern '%s' is not an absolute path, ignoring.", value);

Expand Down Expand Up @@ -498,7 +499,7 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
if (proc_cmdline_value_missing(key, value))
return 0;

(void) parse_path_argument_and_warn(value, false, &arg_watchdog_device);
(void) parse_path_argument(value, false, &arg_watchdog_device);

} else if (proc_cmdline_key_streq(key, "systemd.clock_usec")) {

Expand Down
3 changes: 2 additions & 1 deletion src/cryptenroll/cryptenroll.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "libfido2-util.h"
#include "main-func.h"
#include "memory-util.h"
#include "parse-argument.h"
#include "parse-util.h"
#include "path-util.h"
#include "pkcs11-util.h"
Expand Down Expand Up @@ -323,7 +324,7 @@ static int parse_argv(int argc, char *argv[]) {
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Too many arguments, refusing.");

r = parse_path_argument_and_warn(argv[optind], false, &arg_node);
r = parse_path_argument(argv[optind], false, &arg_node);
if (r < 0)
return r;

Expand Down
3 changes: 2 additions & 1 deletion src/dissect/dissect.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "mkdir.h"
#include "mount-util.h"
#include "namespace-util.h"
#include "parse-argument.h"
#include "parse-util.h"
#include "path-util.h"
#include "pretty-print.h"
Expand Down Expand Up @@ -245,7 +246,7 @@ static int parse_argv(int argc, char *argv[]) {
}

case ARG_VERITY_DATA:
r = parse_path_argument_and_warn(optarg, false, &arg_verity_settings.data_path);
r = parse_path_argument(optarg, false, &arg_verity_settings.data_path);
if (r < 0)
return r;
break;
Expand Down
5 changes: 3 additions & 2 deletions src/firstboot/firstboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "mkdir.h"
#include "mount-util.h"
#include "os-util.h"
#include "parse-argument.h"
#include "parse-util.h"
#include "path-util.h"
#include "pretty-print.h"
Expand Down Expand Up @@ -1046,13 +1047,13 @@ static int parse_argv(int argc, char *argv[]) {
return version();

case ARG_ROOT:
r = parse_path_argument_and_warn(optarg, true, &arg_root);
r = parse_path_argument(optarg, true, &arg_root);
if (r < 0)
return r;
break;

case ARG_IMAGE:
r = parse_path_argument_and_warn(optarg, false, &arg_image);
r = parse_path_argument(optarg, false, &arg_image);
if (r < 0)
return r;
break;
Expand Down
5 changes: 3 additions & 2 deletions src/home/homectl.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "main-func.h"
#include "memory-util.h"
#include "pager.h"
#include "parse-argument.h"
#include "parse-util.h"
#include "path-util.h"
#include "pkcs11-util.h"
Expand Down Expand Up @@ -2260,7 +2261,7 @@ static int parse_argv(int argc, char *argv[]) {
break;
}

r = parse_path_argument_and_warn(optarg, false, &hd);
r = parse_path_argument(optarg, false, &hd);
if (r < 0)
return r;

Expand Down Expand Up @@ -2481,7 +2482,7 @@ static int parse_argv(int argc, char *argv[]) {
break;
}

r = parse_path_argument_and_warn(optarg, false, &v);
r = parse_path_argument(optarg, false, &v);
if (r < 0)
return r;

Expand Down
5 changes: 3 additions & 2 deletions src/journal/journalctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "mountpoint-util.h"
#include "nulstr-util.h"
#include "pager.h"
#include "parse-argument.h"
#include "parse-util.h"
#include "path-util.h"
#include "pcre2-dlopen.h"
Expand Down Expand Up @@ -719,13 +720,13 @@ static int parse_argv(int argc, char *argv[]) {
break;

case ARG_ROOT:
r = parse_path_argument_and_warn(optarg, /* suppress_root= */ true, &arg_root);
r = parse_path_argument(optarg, /* suppress_root= */ true, &arg_root);
if (r < 0)
return r;
break;

case ARG_IMAGE:
r = parse_path_argument_and_warn(optarg, /* suppress_root= */ false, &arg_image);
r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
if (r < 0)
return r;
break;
Expand Down
3 changes: 2 additions & 1 deletion src/machine-id-setup/machine-id-setup-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "log.h"
#include "machine-id-setup.h"
#include "main-func.h"
#include "parse-argument.h"
#include "path-util.h"
#include "pretty-print.h"
#include "util.h"
Expand Down Expand Up @@ -76,7 +77,7 @@ static int parse_argv(int argc, char *argv[]) {
return version();

case ARG_ROOT:
r = parse_path_argument_and_warn(optarg, true, &arg_root);
r = parse_path_argument(optarg, true, &arg_root);
if (r < 0)
return r;
break;
Expand Down
13 changes: 7 additions & 6 deletions src/nspawn/nspawn.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
#include "nulstr-util.h"
#include "os-util.h"
#include "pager.h"
#include "parse-argument.h"
#include "parse-util.h"
#include "path-util.h"
#include "pretty-print.h"
Expand Down Expand Up @@ -791,31 +792,31 @@ static int parse_argv(int argc, char *argv[]) {
return version();

case 'D':
r = parse_path_argument_and_warn(optarg, false, &arg_directory);
r = parse_path_argument(optarg, false, &arg_directory);
if (r < 0)
return r;

arg_settings_mask |= SETTING_DIRECTORY;
break;

case ARG_TEMPLATE:
r = parse_path_argument_and_warn(optarg, false, &arg_template);
r = parse_path_argument(optarg, false, &arg_template);
if (r < 0)
return r;

arg_settings_mask |= SETTING_DIRECTORY;
break;

case 'i':
r = parse_path_argument_and_warn(optarg, false, &arg_image);
r = parse_path_argument(optarg, false, &arg_image);
if (r < 0)
return r;

arg_settings_mask |= SETTING_DIRECTORY;
break;

case ARG_OCI_BUNDLE:
r = parse_path_argument_and_warn(optarg, false, &arg_oci_bundle);
r = parse_path_argument(optarg, false, &arg_oci_bundle);
if (r < 0)
return r;

Expand Down Expand Up @@ -934,7 +935,7 @@ static int parse_argv(int argc, char *argv[]) {
break;

case ARG_NETWORK_NAMESPACE_PATH:
r = parse_path_argument_and_warn(optarg, false, &arg_network_namespace_path);
r = parse_path_argument(optarg, false, &arg_network_namespace_path);
if (r < 0)
return r;

Expand Down Expand Up @@ -1386,7 +1387,7 @@ static int parse_argv(int argc, char *argv[]) {
}

case ARG_VERITY_DATA:
r = parse_path_argument_and_warn(optarg, false, &arg_verity_settings.data_path);
r = parse_path_argument(optarg, false, &arg_verity_settings.data_path);
if (r < 0)
return r;
break;
Expand Down
5 changes: 3 additions & 2 deletions src/partition/repart.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "mkfs-util.h"
#include "mount-util.h"
#include "parse-util.h"
#include "parse-argument.h"
#include "path-util.h"
#include "pretty-print.h"
#include "proc-cmdline.h"
Expand Down Expand Up @@ -3623,7 +3624,7 @@ static int parse_argv(int argc, char *argv[]) {
break;

case ARG_ROOT:
r = parse_path_argument_and_warn(optarg, false, &arg_root);
r = parse_path_argument(optarg, false, &arg_root);
if (r < 0)
return r;
break;
Expand Down Expand Up @@ -3653,7 +3654,7 @@ static int parse_argv(int argc, char *argv[]) {
break;

case ARG_DEFINITIONS:
r = parse_path_argument_and_warn(optarg, false, &arg_definitions);
r = parse_path_argument(optarg, false, &arg_definitions);
if (r < 0)
return r;
break;
Expand Down
3 changes: 2 additions & 1 deletion src/run/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "fd-util.h"
#include "format-util.h"
#include "main-func.h"
#include "parse-argument.h"
#include "parse-util.h"
#include "path-util.h"
#include "pretty-print.h"
Expand Down Expand Up @@ -470,7 +471,7 @@ static int parse_argv(int argc, char *argv[]) {
break;

case ARG_WORKING_DIRECTORY:
r = parse_path_argument_and_warn(optarg, true, &arg_working_directory);
r = parse_path_argument(optarg, true, &arg_working_directory);
if (r < 0)
return r;

Expand Down
31 changes: 31 additions & 0 deletions src/shared/parse-argument.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,42 @@

#include "format-table.h"
#include "parse-argument.h"
#include "path-util.h"
#include "signal-util.h"
#include "stdio-util.h"
#include "string-table.h"
#include "string-util.h"

/* All functions in this file emit warnigs. */

int parse_path_argument(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_simplify(p, false);
if (suppress_root && empty_or_root(p))
p = mfree(p);

return free_and_replace(*arg, p);
}

int parse_signal_argument(const char *s, int *ret) {
int r;

Expand Down
1 change: 1 addition & 0 deletions src/shared/parse-argument.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once

int parse_path_argument(const char *path, bool suppress_root, char **arg);
int parse_signal_argument(const char *s, int *ret);
3 changes: 2 additions & 1 deletion src/sysext/sysext.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "mountpoint-util.h"
#include "os-util.h"
#include "pager.h"
#include "parse-argument.h"
#include "parse-util.h"
#include "pretty-print.h"
#include "process-util.h"
Expand Down Expand Up @@ -943,7 +944,7 @@ static int parse_argv(int argc, char *argv[]) {
break;

case ARG_ROOT:
r = parse_path_argument_and_warn(optarg, false, &arg_root);
r = parse_path_argument(optarg, false, &arg_root);
if (r < 0)
return r;
break;
Expand Down
2 changes: 1 addition & 1 deletion src/systemctl/systemctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
break;

case ARG_ROOT:
r = parse_path_argument_and_warn(optarg, false, &arg_root);
r = parse_path_argument(optarg, false, &arg_root);
if (r < 0)
return r;
break;
Expand Down
Loading

0 comments on commit 614b022

Please sign in to comment.