]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: use path_split_prefix_filename() where appropriate 40608/head
authorMike Yuan <me@yhndnzj.com>
Mon, 9 Feb 2026 01:30:38 +0000 (02:30 +0100)
committerMike Yuan <me@yhndnzj.com>
Mon, 9 Feb 2026 08:07:15 +0000 (09:07 +0100)
src/basic/lock-util.c
src/basic/tmpfile-util.c
src/shared/blockdev-util.c
src/shared/creds-util.c
src/shared/generator.c
src/shared/unit-file.c

index 85c7dc0562fc1b45335b21104230938dd3e060d7..bd267eabf99d4d6a77deb99a39455e0c157ff9b7 100644 (file)
@@ -69,15 +69,14 @@ int make_lock_file_for(const char *p, int operation, LockFile *ret) {
         assert(p);
         assert(ret);
 
-        r = path_extract_filename(p, &fn);
+        r = path_split_prefix_filename(p, &dn, &fn);
         if (r < 0)
                 return r;
 
-        r = path_extract_directory(p, &dn);
-        if (r < 0)
-                return r;
-
-        t = strjoin(dn, "/.#", fn, ".lck");
+        if (dn)
+                t = strjoin(dn, "/.#", fn, ".lck");
+        else
+                t = strjoin(".#", fn, ".lck");
         if (!t)
                 return -ENOMEM;
 
index bbfd4f58ab63f7092710a07543980b6cba09f13f..2be44dcd77772e91b25618cb6b5e54bf4ff6ce8d 100644 (file)
@@ -161,17 +161,12 @@ static int tempfn_build(const char *p, const char *pre, const char *post, bool c
                 if (!d)
                         return -ENOMEM;
         } else {
-                r = path_extract_directory(p, &d);
-                if (r < 0 && r != -EDESTADDRREQ) /* EDESTADDRREQ → No directory specified, just a filename */
-                        return r;
-
-                r = path_extract_filename(p, &fn);
+                r = path_split_prefix_filename(p, &d, &fn);
                 if (r < 0)
                         return r;
 
-                if (strlen(fn) > NAME_MAX - len_add)
-                        /* We cannot simply prepend and append strings to the filename. Let's truncate the filename. */
-                        fn[NAME_MAX - len_add] = '\0';
+                /* Truncate the filename if it would become too long after mangling. */
+                strshorten(fn, NAME_MAX - len_add);
         }
 
         nf = strjoin(".#", strempty(pre), strempty(fn), strempty(post));
index 44feae5ab60c62524ab0c4de90f369d8c52e6b9c..12a4f59c28a6942b29cfc7f1d463421c247dfebe 100644 (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 */
 
index a54883744fe076b3f532ee4468d089291a241501..2aac4d253bb760a6569df2a46a7bae2a5f5aa838 100644 (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);
index a419fa4e1876a72c6b2ea6aacbae17a213441893..603e969436e9b4852e83904ec968b5a5e9ae73bd 100644 (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);
 
index c5f35dba16931be8a9bf1ceb5f75a8d74cbb1cde..f056b679a9ce487b84b168e83d259ffd95f27960 100644 (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;
         }