]> xenbits.xensource.com Git - libvirt.git/commitdiff
virFileInData: Preserve errno on error
authorMichal Privoznik <mprivozn@redhat.com>
Mon, 8 Oct 2018 15:36:10 +0000 (17:36 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 19 Oct 2018 11:07:49 +0000 (13:07 +0200)
The virFileInData() function should return to the caller if the
current position the passed file is in is a data section or a
hole (and also how long the current section is). At any rate,
upon return from this function (be it successful or not) the
original position in the file is restored. This may mess up with
errno which might have been set earlier. Save the errno into a
local variable so it can be restored for the caller's sake.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
src/util/virfile.c

index e09992e41a24f09a44561b797c889b3c30ce4cf3..f6f01ec1e12ab283328955af96a04378c2f05f80 100644 (file)
@@ -4072,11 +4072,18 @@ virFileInData(int fd,
     ret = 0;
  cleanup:
     /* At any rate, reposition back to where we started. */
-    if (cur != (off_t) -1 &&
-        lseek(fd, cur, SEEK_SET) == (off_t) -1) {
-        virReportSystemError(errno, "%s",
-                             _("unable to restore position in file"));
-        ret = -1;
+    if (cur != (off_t) -1) {
+        int theerrno = errno;
+
+        if (lseek(fd, cur, SEEK_SET) == (off_t) -1) {
+            virReportSystemError(errno, "%s",
+                                 _("unable to restore position in file"));
+            ret = -1;
+            if (theerrno == 0)
+                theerrno = errno;
+        }
+
+        errno = theerrno;
     }
     return ret;
 }