Resolve symlinks and cook the targets instead of the symlink names.

This commit is contained in:
mlelstv 2013-12-22 14:31:51 +00:00
parent 523bbfbb4c
commit 1d339cd8f0

View File

@ -1,4 +1,4 @@
/* $NetBSD: getdiskrawname.c,v 1.1 2012/04/07 16:44:39 christos Exp $ */
/* $NetBSD: getdiskrawname.c,v 1.2 2013/12/22 14:31:51 mlelstv Exp $ */
/*-
* Copyright (c) 2012 The NetBSD Foundation, Inc.
@ -29,7 +29,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: getdiskrawname.c,v 1.1 2012/04/07 16:44:39 christos Exp $");
__RCSID("$NetBSD: getdiskrawname.c,v 1.2 2013/12/22 14:31:51 mlelstv Exp $");
#include <sys/stat.h>
@ -37,18 +37,26 @@ __RCSID("$NetBSD: getdiskrawname.c,v 1.1 2012/04/07 16:44:39 christos Exp $");
#include <string.h>
#include <errno.h>
#include <util.h>
#include <unistd.h>
const char *
getdiskrawname(char *buf, size_t bufsiz, const char *name)
{
const char *dp = strrchr(name, '/');
struct stat st;
ssize_t len;
if (dp == NULL) {
errno = EINVAL;
return NULL;
}
len = readlink(name, buf, bufsiz-1);
if (len > 0) {
buf[len] = '\0';
name = buf;
}
if (stat(name, &st) == -1)
return NULL;
@ -67,11 +75,19 @@ getdiskcookedname(char *buf, size_t bufsiz, const char *name)
{
const char *dp;
struct stat st;
ssize_t len;
if ((dp = strrchr(name, '/')) == NULL) {
errno = EINVAL;
return NULL;
}
len = readlink(name, buf, bufsiz-1);
if (len > 0) {
buf[len] = '\0';
name = buf;
}
if (stat(name, &st) == -1)
return NULL;
@ -79,10 +95,12 @@ getdiskcookedname(char *buf, size_t bufsiz, const char *name)
errno = EFTYPE;
return NULL;
}
if (dp[1] != 'r') {
errno = EINVAL;
return NULL;
}
(void)snprintf(buf, bufsiz, "%.*s/%s", (int)(dp - name), name, dp + 2);
return buf;