libutil/util_sys: Use util_readlink() for consistent error handling

Avoid code duplication and inconsistent error handling by replacing
readlink() with util_readlink(), which is used project-wide to
standardize readlink() usage.

Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Jan Polensky <japo@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Jan Polensky
2025-10-02 12:23:09 +02:00
committed by Jan Höppner
parent d4ee1ff01f
commit e7203069cc

View File

@@ -24,8 +24,6 @@
#include "lib/util_path.h" #include "lib/util_path.h"
#include "lib/util_sys.h" #include "lib/util_sys.h"
/* lstat() doesn't work for sysfs files, a fixed size is therefore inevitable */
#define READLINK_SIZE 256
#define PAGE_SIZE 4096 #define PAGE_SIZE 4096
/** /**
@@ -137,10 +135,10 @@ int util_sys_get_base_dev(dev_t dev, dev_t *base_dev)
*/ */
int util_sys_get_dev_addr(const char *dev, char *addr) int util_sys_get_dev_addr(const char *dev, char *addr)
{ {
char device[READLINK_SIZE], *result;
unsigned int maj, min; unsigned int maj, min;
struct stat s; struct stat s;
ssize_t len; char *linkdir;
char *result;
dev_t base; dev_t base;
char *path; char *path;
@@ -160,19 +158,18 @@ int util_sys_get_dev_addr(const char *dev, char *addr)
else else
return -1; return -1;
len = readlink(path, device, READLINK_SIZE - 1); linkdir = util_readlink(path);
free(path); free(path);
if (len != -1) if (!linkdir)
device[len] = '\0';
else
return -1; return -1;
result = strrchr(device, '/'); result = strrchr(linkdir, '/');
if (result) if (result)
result++; result++;
else else
result = device; result = linkdir;
strcpy(addr, result); strcpy(addr, result);
free(linkdir);
return 0; return 0;
} }