]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/fs/readlink/, lib/: areadlink(): Move and rename function
authorAlejandro Colomar <alx@kernel.org>
Wed, 3 Jul 2024 01:31:24 +0000 (03:31 +0200)
committerSerge Hallyn <serge@hallyn.com>
Sat, 2 Nov 2024 02:25:50 +0000 (21:25 -0500)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/Makefile.am
lib/copydir.c
lib/fs/readlink/areadlink.c [new file with mode: 0644]
lib/fs/readlink/areadlink.h [new file with mode: 0644]

index 832c3ab3294509f90a1e7b4156c7f9e2201544b5..4ea5ec4e59b593fef459faaeb59465f377e78c30 100644 (file)
@@ -103,6 +103,8 @@ libshadow_la_SOURCES = \
        find_new_sub_gids.c \
        find_new_sub_uids.c \
        fputsx.c \
+       fs/readlink/areadlink.c \
+       fs/readlink/areadlink.h \
        fs/readlink/readlinknul.c \
        fs/readlink/readlinknul.h \
        get_pid.c \
index be122ea584a3707ba017402167d9df44df38176b..c1c3c8af6b700e4539d853ad755a464909cfd1df 100644 (file)
 #include <stdio.h>
 #include <string.h>
 
-#include "alloc/malloc.h"
 #include "alloc/x/xmalloc.h"
 #include "attr.h"
-#include "fs/readlink/readlinknul.h"
+#include "fs/readlink/areadlink.h"
 #include "prototypes.h"
 #include "defines.h"
 #ifdef WITH_SELINUX
@@ -69,7 +68,6 @@ static int copy_dir (const struct path_info *src, const struct path_info *dst,
                      const struct stat *statp, const struct timespec mt[],
                      uid_t old_uid, uid_t new_uid,
                      gid_t old_gid, gid_t new_gid);
-static /*@null@*/char *readlink_malloc (const char *filename);
 static int copy_symlink (const struct path_info *src, const struct path_info *dst,
                          MAYBE_UNUSED bool reset_selinux,
                          const struct stat *statp, const struct timespec mt[],
@@ -539,35 +537,6 @@ static int copy_dir (const struct path_info *src, const struct path_info *dst,
        return err;
 }
 
-/*
- * readlink_malloc - wrapper for readlink
- *
- * return NULL on error.
- * The return string shall be freed by the caller.
- */
-static /*@null@*/char *readlink_malloc (const char *filename)
-{
-       size_t size = 1024;
-
-       while (true) {
-               int  len;
-               char *buffer = MALLOC(size, char);
-               if (NULL == buffer) {
-                       return NULL;
-               }
-
-               len = readlinknul(filename, buffer, size);
-               if (len != -1)
-                       return buffer;
-
-               free(buffer);
-               if (errno != E2BIG)
-                       return NULL;
-
-               size *= 2;
-       }
-}
-
 /*
  * copy_symlink - copy a symlink
  *
@@ -598,10 +567,9 @@ static int copy_symlink (const struct path_info *src, const struct path_info *ds
         * destination directory name.
         */
 
-       oldlink = readlink_malloc (src->full_path);
-       if (NULL == oldlink) {
+       oldlink = areadlink(src->full_path);
+       if (NULL == oldlink)
                return -1;
-       }
 
        /* If src was a link to an entry of the src_orig directory itself,
         * create a link to the corresponding entry in the dst_orig
diff --git a/lib/fs/readlink/areadlink.c b/lib/fs/readlink/areadlink.c
new file mode 100644 (file)
index 0000000..c0a759d
--- /dev/null
@@ -0,0 +1,10 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <config.h>
+
+#include "fs/readlink/areadlink.h"
+
+
+extern inline char *areadlink(const char *path);
diff --git a/lib/fs/readlink/areadlink.h b/lib/fs/readlink/areadlink.h
new file mode 100644 (file)
index 0000000..8c72960
--- /dev/null
@@ -0,0 +1,51 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#ifndef SHADOW_INCLUDE_LIB_FS_READLINK_AREADLINK_H_
+#define SHADOW_INCLUDE_LIB_FS_READLINK_AREADLINK_H_
+
+
+#include <config.h>
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdlib.h>
+
+#include "alloc/malloc.h"
+#include "attr.h"
+#include "fs/readlink/readlinknul.h"
+
+
+ATTR_STRING(1)
+inline char *areadlink(const char *path);
+
+
+// Similar to readlink(2), but allocate and terminate the string.
+inline char *
+areadlink(const char *filename)
+{
+       size_t size = 1024;
+
+       while (true) {
+               int  len;
+               char *buffer = MALLOC(size, char);
+               if (NULL == buffer) {
+                       return NULL;
+               }
+
+               len = readlinknul(filename, buffer, size);
+               if (len != -1)
+                       return buffer;
+
+               free(buffer);
+               if (errno != E2BIG)
+                       return NULL;
+
+               size *= 2;
+       }
+}
+
+
+#endif  // include guard