From: Alejandro Colomar Date: Wed, 3 Jul 2024 01:31:24 +0000 (+0200) Subject: lib/fs/readlink/, lib/: areadlink(): Move and rename function X-Git-Tag: 4.17.0-rc1~39 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=32f10c3decf17ffe833657bfcc1e2f0d730c71ec;p=thirdparty%2Fshadow.git lib/fs/readlink/, lib/: areadlink(): Move and rename function Signed-off-by: Alejandro Colomar --- diff --git a/lib/Makefile.am b/lib/Makefile.am index 832c3ab32..4ea5ec4e5 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -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 \ diff --git a/lib/copydir.c b/lib/copydir.c index be122ea58..c1c3c8af6 100644 --- a/lib/copydir.c +++ b/lib/copydir.c @@ -19,10 +19,9 @@ #include #include -#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 index 000000000..c0a759dc7 --- /dev/null +++ b/lib/fs/readlink/areadlink.c @@ -0,0 +1,10 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#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 index 000000000..8c729601e --- /dev/null +++ b/lib/fs/readlink/areadlink.h @@ -0,0 +1,51 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_FS_READLINK_AREADLINK_H_ +#define SHADOW_INCLUDE_LIB_FS_READLINK_AREADLINK_H_ + + +#include + +#include +#include +#include +#include + +#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