raw-posix: Fix raw_getlength() to always return -errno on error

We got a merry mix of -1 and -errno here.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Benoit Canet <benoit@irqsave.net>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
This commit is contained in:
Markus Armbruster 2014-06-26 13:23:16 +02:00 committed by Stefan Hajnoczi
parent a42a1facb7
commit aa729704f4
1 changed files with 22 additions and 6 deletions

View File

@ -1133,12 +1133,12 @@ static int64_t raw_getlength(BlockDriverState *bs)
struct stat st; struct stat st;
if (fstat(fd, &st)) if (fstat(fd, &st))
return -1; return -errno;
if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
struct disklabel dl; struct disklabel dl;
if (ioctl(fd, DIOCGDINFO, &dl)) if (ioctl(fd, DIOCGDINFO, &dl))
return -1; return -errno;
return (uint64_t)dl.d_secsize * return (uint64_t)dl.d_secsize *
dl.d_partitions[DISKPART(st.st_rdev)].p_size; dl.d_partitions[DISKPART(st.st_rdev)].p_size;
} else } else
@ -1152,7 +1152,7 @@ static int64_t raw_getlength(BlockDriverState *bs)
struct stat st; struct stat st;
if (fstat(fd, &st)) if (fstat(fd, &st))
return -1; return -errno;
if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
struct dkwedge_info dkw; struct dkwedge_info dkw;
@ -1162,7 +1162,7 @@ static int64_t raw_getlength(BlockDriverState *bs)
struct disklabel dl; struct disklabel dl;
if (ioctl(fd, DIOCGDINFO, &dl)) if (ioctl(fd, DIOCGDINFO, &dl))
return -1; return -errno;
return (uint64_t)dl.d_secsize * return (uint64_t)dl.d_secsize *
dl.d_partitions[DISKPART(st.st_rdev)].p_size; dl.d_partitions[DISKPART(st.st_rdev)].p_size;
} }
@ -1175,6 +1175,7 @@ static int64_t raw_getlength(BlockDriverState *bs)
BDRVRawState *s = bs->opaque; BDRVRawState *s = bs->opaque;
struct dk_minfo minfo; struct dk_minfo minfo;
int ret; int ret;
int64_t size;
ret = fd_open(bs); ret = fd_open(bs);
if (ret < 0) { if (ret < 0) {
@ -1193,7 +1194,11 @@ static int64_t raw_getlength(BlockDriverState *bs)
* There are reports that lseek on some devices fails, but * There are reports that lseek on some devices fails, but
* irc discussion said that contingency on contingency was overkill. * irc discussion said that contingency on contingency was overkill.
*/ */
return lseek(s->fd, 0, SEEK_END); size = lseek(s->fd, 0, SEEK_END);
if (size < 0) {
return -errno;
}
return size;
} }
#elif defined(CONFIG_BSD) #elif defined(CONFIG_BSD)
static int64_t raw_getlength(BlockDriverState *bs) static int64_t raw_getlength(BlockDriverState *bs)
@ -1231,6 +1236,9 @@ again:
size = LLONG_MAX; size = LLONG_MAX;
#else #else
size = lseek(fd, 0LL, SEEK_END); size = lseek(fd, 0LL, SEEK_END);
if (size < 0) {
return -errno;
}
#endif #endif
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
switch(s->type) { switch(s->type) {
@ -1247,6 +1255,9 @@ again:
#endif #endif
} else { } else {
size = lseek(fd, 0, SEEK_END); size = lseek(fd, 0, SEEK_END);
if (size < 0) {
return -errno;
}
} }
return size; return size;
} }
@ -1255,13 +1266,18 @@ static int64_t raw_getlength(BlockDriverState *bs)
{ {
BDRVRawState *s = bs->opaque; BDRVRawState *s = bs->opaque;
int ret; int ret;
int64_t size;
ret = fd_open(bs); ret = fd_open(bs);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
return lseek(s->fd, 0, SEEK_END); size = lseek(s->fd, 0, SEEK_END);
if (size < 0) {
return -errno;
}
return size;
} }
#endif #endif