Module Name: src
Committed By: mlelstv
Date: Sun Dec 22 14:31:51 UTC 2013
Modified Files:
src/lib/libutil: getdiskrawname.c
Log Message:
Resolve symlinks and cook the targets instead of the symlink names.
To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/lib/libutil/getdiskrawname.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/lib/libutil/getdiskrawname.c
diff -u src/lib/libutil/getdiskrawname.c:1.1 src/lib/libutil/getdiskrawname.c:1.2
--- src/lib/libutil/getdiskrawname.c:1.1 Sat Apr 7 16:44:39 2012
+++ src/lib/libutil/getdiskrawname.c Sun Dec 22 14:31:51 2013
@@ -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
#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 bufs
{
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 bufs
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;