tree-wide: use path_join() instead of prefix_roota() in various cases

prefix_roota() is something we should stop using. It is bad for three
reasons:

1. As it names suggests it's supposed to be used when working relative
   to some root directory, but given it doesn't follow symlinks (and
   instead just stupidly joins paths) it is not a good choice for that.

2. More often than not it is currently used with inputs under control of
   the user, and that is icky given it typically allocates memory on the
   stack.

3. It's a redundant interface, where chase_symlinks() and path_join()
   already exist as better, safer interfaces.

Hence, let's start moving things from prefix_roota() to path_join() for
the cases where that's appropriate.
This commit is contained in:
Lennart Poettering
2022-08-22 19:15:29 +01:00
committed by Luca Boccassi
parent 8ef6106de4
commit 8e7e4a730b
8 changed files with 92 additions and 44 deletions
+19 -10
View File
@@ -29,11 +29,13 @@ int generator_open_unit_file(
const char *name,
FILE **file) {
const char *unit;
_cleanup_free_ char *unit = NULL;
FILE *f;
int r;
unit = prefix_roota(dest, name);
unit = path_join(dest, name);
if (!unit)
return log_oom();
r = fopen_unlocked(unit, "wxe", &f);
if (r < 0) {
@@ -352,8 +354,8 @@ int generator_hook_up_mkswap(
const char *what) {
_cleanup_free_ char *node = NULL, *unit = NULL, *escaped = NULL, *where_unit = NULL;
_cleanup_free_ char *unit_file = NULL;
_cleanup_fclose_ FILE *f = NULL;
const char *unit_file;
int r;
node = fstab_node_to_udev_node(what);
@@ -371,7 +373,10 @@ int generator_hook_up_mkswap(
return log_error_errno(r, "Failed to make unit instance name from path \"%s\": %m",
node);
unit_file = prefix_roota(dir, unit);
unit_file = path_join(dir, unit);
if (!unit_file)
return log_oom();
log_debug("Creating %s", unit_file);
escaped = cescape(node);
@@ -421,9 +426,8 @@ int generator_hook_up_mkfs(
const char *where,
const char *type) {
_cleanup_free_ char *node = NULL, *unit = NULL, *escaped = NULL, *where_unit = NULL;
_cleanup_free_ char *node = NULL, *unit = NULL, *unit_file = NULL, *escaped = NULL, *where_unit = NULL;
_cleanup_fclose_ FILE *f = NULL;
const char *unit_file;
int r;
node = fstab_node_to_udev_node(what);
@@ -446,7 +450,10 @@ int generator_hook_up_mkfs(
return log_error_errno(r, "Failed to make unit instance name from path \"%s\": %m",
node);
unit_file = prefix_roota(dir, unit);
unit_file = path_join(dir, unit);
if (!unit_file)
return log_oom();
log_debug("Creating %s", unit_file);
escaped = cescape(node);
@@ -499,9 +506,8 @@ int generator_hook_up_growfs(
const char *where,
const char *target) {
_cleanup_free_ char *unit = NULL, *escaped = NULL, *where_unit = NULL;
_cleanup_free_ char *unit = NULL, *escaped = NULL, *where_unit = NULL, *unit_file = NULL;
_cleanup_fclose_ FILE *f = NULL;
const char *unit_file;
int r;
assert(dir);
@@ -521,7 +527,10 @@ int generator_hook_up_growfs(
return log_error_errno(r, "Failed to make unit name from path \"%s\": %m",
where);
unit_file = prefix_roota(dir, unit);
unit_file = path_join(dir, unit);
if (!unit_file)
return log_oom();
log_debug("Creating %s", unit_file);
f = fopen(unit_file, "wxe");