1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-07 17:28:15 +00:00

Make virStorageBackendCopyToFD return -errno.

Previously virStorageBackendCopyToFD would simply return -1 on
error. This made the error return from one of its callers inconsistent
(createRawFileOpHook is supposed to return -errno, but if
virStorageBackendCopyToFD failed, createRawFileOpHook would just
return -1). Since there is a useful errno in every case of error
return from virStorageBackendCopyToFD, and since the other uses of
that function ignore the return code (beyond simply checking to see if
it is < 0), this is a safe change.
This commit is contained in:
Laine Stump 2010-07-19 19:35:30 -04:00
parent 2ad04f7853
commit ace1a2bac4

View File

@ -116,13 +116,14 @@ virStorageBackendCopyToFD(virStorageVolDefPtr vol,
{ {
int inputfd = -1; int inputfd = -1;
int amtread = -1; int amtread = -1;
int ret = -1; int ret = 0;
unsigned long long remain; unsigned long long remain;
size_t bytes = 1024 * 1024; size_t bytes = 1024 * 1024;
char zerobuf[512]; char zerobuf[512];
char *buf = NULL; char *buf = NULL;
if ((inputfd = open(inputvol->target.path, O_RDONLY)) < 0) { if ((inputfd = open(inputvol->target.path, O_RDONLY)) < 0) {
ret = -errno;
virReportSystemError(errno, virReportSystemError(errno,
_("could not open input path '%s'"), _("could not open input path '%s'"),
inputvol->target.path); inputvol->target.path);
@ -132,6 +133,7 @@ virStorageBackendCopyToFD(virStorageVolDefPtr vol,
bzero(&zerobuf, sizeof(zerobuf)); bzero(&zerobuf, sizeof(zerobuf));
if (VIR_ALLOC_N(buf, bytes) < 0) { if (VIR_ALLOC_N(buf, bytes) < 0) {
ret = -errno;
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
@ -145,6 +147,7 @@ virStorageBackendCopyToFD(virStorageVolDefPtr vol,
bytes = remain; bytes = remain;
if ((amtread = saferead(inputfd, buf, bytes)) < 0) { if ((amtread = saferead(inputfd, buf, bytes)) < 0) {
ret = -errno;
virReportSystemError(errno, virReportSystemError(errno,
_("failed reading from file '%s'"), _("failed reading from file '%s'"),
inputvol->target.path); inputvol->target.path);
@ -161,12 +164,14 @@ virStorageBackendCopyToFD(virStorageVolDefPtr vol,
if (is_dest_file && memcmp(buf+offset, zerobuf, interval) == 0) { if (is_dest_file && memcmp(buf+offset, zerobuf, interval) == 0) {
if (lseek(fd, interval, SEEK_CUR) < 0) { if (lseek(fd, interval, SEEK_CUR) < 0) {
ret = -errno;
virReportSystemError(errno, virReportSystemError(errno,
_("cannot extend file '%s'"), _("cannot extend file '%s'"),
vol->target.path); vol->target.path);
goto cleanup; goto cleanup;
} }
} else if (safewrite(fd, buf+offset, interval) < 0) { } else if (safewrite(fd, buf+offset, interval) < 0) {
ret = -errno;
virReportSystemError(errno, virReportSystemError(errno,
_("failed writing to file '%s'"), _("failed writing to file '%s'"),
vol->target.path); vol->target.path);
@ -177,6 +182,7 @@ virStorageBackendCopyToFD(virStorageVolDefPtr vol,
} }
if (inputfd != -1 && close(inputfd) < 0) { if (inputfd != -1 && close(inputfd) < 0) {
ret = -errno;
virReportSystemError(errno, virReportSystemError(errno,
_("cannot close file '%s'"), _("cannot close file '%s'"),
inputvol->target.path); inputvol->target.path);
@ -185,7 +191,6 @@ virStorageBackendCopyToFD(virStorageVolDefPtr vol,
inputfd = -1; inputfd = -1;
*total -= remain; *total -= remain;
ret = 0;
cleanup: cleanup:
if (inputfd != -1) if (inputfd != -1)