shared/install: modernize find_symlinks and friends

This commit is contained in:
Mike Yuan
2024-06-11 23:17:21 +01:00
committed by Luca Boccassi
parent 1d2e1cad30
commit df152dbbe5
+26 -35
View File
@@ -830,6 +830,9 @@ static int remove_marked_symlinks(
static int is_symlink_with_known_name(const InstallInfo *i, const char *name) {
int r;
assert(i);
assert(name);
if (streq(name, i->name))
return true;
@@ -863,11 +866,17 @@ static int find_symlinks_in_directory(
const char *config_path,
bool *same_name_link) {
int r = 0;
int r, ret = 0;
assert(dir);
assert(dir_path);
assert(info);
assert(unit_name_is_valid(info->name, UNIT_NAME_ANY));
assert(config_path);
assert(same_name_link);
FOREACH_DIRENT(de, dir, return -errno) {
bool found_path = false, found_dest = false, b = false;
int q;
if (de->d_type != DT_LNK)
continue;
@@ -876,32 +885,17 @@ static int find_symlinks_in_directory(
_cleanup_free_ char *dest = NULL;
/* Acquire symlink destination */
q = readlinkat_malloc(dirfd(dir), de->d_name, &dest);
if (q == -ENOENT)
r = readlinkat_malloc(dirfd(dir), de->d_name, &dest);
if (r < 0) {
if (r != -ENOENT)
RET_GATHER(ret, r);
continue;
if (q < 0) {
if (r == 0)
r = q;
continue;
}
/* Make absolute */
if (!path_is_absolute(dest)) {
char *x;
x = path_join(dir_path, dest);
if (!x)
return -ENOMEM;
free_and_replace(dest, x);
}
/* Check if what the symlink points to matches what we are looking for */
found_dest = streq(basename(dest), info->name);
found_dest = path_equal_filename(dest, info->name);
}
assert(unit_name_is_valid(info->name, UNIT_NAME_ANY));
/* Check if the symlink itself matches what we are looking for.
*
* If ignore_destination is specified, we only look at the source name.
@@ -916,10 +910,10 @@ static int find_symlinks_in_directory(
if (!found_path && ignore_destination) {
_cleanup_free_ char *template = NULL;
q = unit_name_template(de->d_name, &template);
if (q < 0 && q != -EINVAL)
return q;
if (q >= 0)
r = unit_name_template(de->d_name, &template);
if (r < 0 && r != -EINVAL)
return r;
if (r >= 0)
found_dest = streq(template, info->name);
}
@@ -929,7 +923,6 @@ static int find_symlinks_in_directory(
/* Filter out same name links in the main config path */
p = path_make_absolute(de->d_name, dir_path);
t = path_make_absolute(info->name, config_path);
if (!p || !t)
return -ENOMEM;
@@ -943,15 +936,13 @@ static int find_symlinks_in_directory(
return 1;
/* Check if symlink name is in the set of names used by [Install] */
q = is_symlink_with_known_name(info, de->d_name);
if (q < 0)
return q;
if (q > 0)
return 1;
r = is_symlink_with_known_name(info, de->d_name);
if (r != 0)
return r;
}
}
return r;
return ret;
}
static int find_symlinks(
@@ -963,7 +954,7 @@ static int find_symlinks(
bool *same_name_link) {
_cleanup_closedir_ DIR *config_dir = NULL;
int r = 0;
int r;
assert(i);
assert(config_path);
@@ -1006,7 +997,7 @@ static int find_symlinks(
same_name_link);
if (r > 0)
return 1;
else if (r < 0)
if (r < 0)
log_debug_errno(r, "Failed to look up symlinks in \"%s\": %m", path);
}