lib/fs/readlink/, lib/: areadlink(): Move and rename function

Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar
2024-07-03 03:31:24 +02:00
committed by Serge Hallyn
parent f8c7955bbb
commit 32f10c3dec
4 changed files with 66 additions and 35 deletions

View 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 \

View File

@@ -19,10 +19,9 @@
#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

View File

@@ -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);

View File

@@ -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