path-util: unify path_extract_filename/directory into path_split_prefix_filename() (#40608)

This commit is contained in:
Mike Yuan
2026-02-09 11:03:14 +01:00
committed by GitHub
10 changed files with 74 additions and 96 deletions
+2 -7
View File
@@ -922,6 +922,7 @@ int blockdev_get_root(int level, dev_t *ret) {
}
int partition_node_of(const char *node, unsigned nr, char **ret) {
_cleanup_free_ char *fn = NULL, *dn = NULL;
int r;
assert(node);
@@ -931,18 +932,12 @@ int partition_node_of(const char *node, unsigned nr, char **ret) {
/* Given a device node path to a block device returns the device node path to the partition block
* device of the specified partition */
_cleanup_free_ char *fn = NULL;
r = path_extract_filename(node, &fn);
r = path_split_prefix_filename(node, &dn, &fn);
if (r < 0)
return r;
if (r == O_DIRECTORY)
return -EISDIR;
_cleanup_free_ char *dn = NULL;
r = path_extract_directory(node, &dn);
if (r < 0 && r != -EDESTADDRREQ) /* allow if only filename is specified */
return r;
size_t l = strlen(fn);
assert(l > 0); /* underflow check for the subtraction below */
+6 -6
View File
@@ -480,14 +480,13 @@ int get_credential_host_secret(CredentialSecretFlags flags, struct iovec *ret) {
if (!path_is_absolute(e))
return -EINVAL;
r = path_extract_directory(e, &_dirname);
if (r < 0)
return r;
r = path_extract_filename(e, &_filename);
r = path_split_prefix_filename(e, &_dirname, &_filename);
if (r < 0)
return r;
if (r == O_DIRECTORY)
return -EINVAL;
/* We validate that the path is absolute above, hence dirname must be extractable. */
dirname = _dirname;
filename = _filename;
} else {
@@ -498,7 +497,8 @@ int get_credential_host_secret(CredentialSecretFlags flags, struct iovec *ret) {
assert(dirname);
assert(filename);
mkdir_parents(dirname, 0755);
(void) mkdir_parents(dirname, 0755);
dfd = open_mkdir(dirname, O_CLOEXEC, 0755);
if (dfd < 0)
return log_debug_errno(dfd, "Failed to create or open directory '%s': %m", dirname);
+2 -6
View File
@@ -109,13 +109,9 @@ int generator_add_symlink_full(
*
* If <instance> is specified, then <src> must be a template unit name, and we'll instantiate it. */
r = path_extract_directory(src, &dn);
if (r < 0 && r != -EDESTADDRREQ) /* EDESTADDRREQ → just a file name was passed */
return log_error_errno(r, "Failed to extract directory name from '%s': %m", src);
r = path_extract_filename(src, &fn);
r = path_split_prefix_filename(src, &dn, &fn);
if (r < 0)
return log_error_errno(r, "Failed to extract file name from '%s': %m", src);
return log_error_errno(r, "Failed to split '%s' into directory prefix and filename: %m", src);
if (r == O_DIRECTORY)
return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Expected path to regular file name, but got '%s', refusing.", src);
+1
View File
@@ -7,6 +7,7 @@
#include "discover-image.h"
#include "log.h"
#include "mountpoint-util.h"
#include "runtime-scope.h"
#include "snapshot-util.h"
#include "signal-util.h"
#include "tmpfile-util.h"
+1 -1
View File
@@ -1,7 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "runtime-scope.h"
#include "shared-forward.h"
/* create_ephemeral_snapshot - create a snapshot of the given directory.
*
+4 -6
View File
@@ -291,17 +291,15 @@ int unit_file_resolve_symlink(
dir, dir ? "/" : "", filename);
if (!dir) {
r = path_extract_directory(filename, &_dir);
if (r < 0)
return r;
dir = _dir;
r = path_extract_filename(filename, &_filename);
r = path_split_prefix_filename(filename, &_dir, &_filename);
if (r < 0)
return r;
if (r == O_DIRECTORY)
return log_warning_errno(SYNTHETIC_ERRNO(EISDIR),
"Unexpected path to a directory \"%s\", refusing.", filename);
/* We validate that the path is absolute above, hence dir must be extractable. */
dir = ASSERT_PTR(_dir);
filename = _filename;
}