[Libguestfs] [PATCH 2/9] daemon error handling: Remove direct access to errno.

Richard W.M. Jones rjones at redhat.com
Fri Nov 27 18:03:20 UTC 2009


-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into Xen guests.
http://et.redhat.com/~rjones/virt-p2v
-------------- next part --------------
>From f982a590ba92d706425be1bdd40eefb6a61c5b29 Mon Sep 17 00:00:00 2001
From: Richard Jones <rjones at redhat.com>
Date: Fri, 27 Nov 2009 13:53:31 +0000
Subject: [PATCH 2/9] daemon error handling: Remove direct access to errno.

---
 daemon/dir.c      |    4 ++--
 daemon/glob.c     |    6 ++++--
 daemon/guestfsd.c |    2 +-
 daemon/inotify.c  |    2 +-
 daemon/realpath.c |    7 +++----
 daemon/tar.c      |   46 ++++++++++++++++++++--------------------------
 daemon/upload.c   |   18 ++++++++----------
 7 files changed, 39 insertions(+), 46 deletions(-)

diff --git a/daemon/dir.c b/daemon/dir.c
index a5076b1..ecb9c79 100644
--- a/daemon/dir.c
+++ b/daemon/dir.c
@@ -127,7 +127,7 @@ recursive_mkdir (const char *path)
  again:
   r = mkdir (path, 0777);
   if (r == -1) {
-    if (errno == EEXIST) {	/* Something exists here, might not be a dir. */
+    if (get_errno() == EEXIST) { /* Something exists here, might not be a dir. */
       r = lstat (path, &buf);
       if (r == -1) return -1;
       if (!S_ISDIR (buf.st_mode)) {
@@ -137,7 +137,7 @@ recursive_mkdir (const char *path)
       return 0;			/* OK - directory exists here already. */
     }
 
-    if (!loop && errno == ENOENT) {
+    if (!loop && get_errno() == ENOENT) {
       loop = 1;			/* Stops it looping forever. */
 
       /* If we're at the root, and we failed, just give up. */
diff --git a/daemon/glob.c b/daemon/glob.c
index 4fe76f3..a330534 100644
--- a/daemon/glob.c
+++ b/daemon/glob.c
@@ -45,8 +45,10 @@ do_glob_expand (const char *pattern)
   }
 
   if (r != 0) {
-    if (errno != 0)
-      reply_with_perror ("glob: %s", pattern);
+    errtype err = get_error ();
+
+    if (err)
+      reply_with_perror_with_err (err, "glob: %s", pattern);
     else
       reply_with_error ("glob failed: %s", pattern);
     return NULL;
diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c
index bb972f2..c3af8c5 100644
--- a/daemon/guestfsd.c
+++ b/daemon/guestfsd.c
@@ -1021,7 +1021,7 @@ device_name_translation (char *device, const char *func)
     return 0;
   }
 
-  if (errno != ENXIO && errno != ENOENT) {
+  if (get_errno() != ENXIO && get_errno() != ENOENT) {
   error:
     reply_with_perror ("%s: %s", func, device);
     return -1;
diff --git a/daemon/inotify.c b/daemon/inotify.c
index 24ce76e..749b8b7 100644
--- a/daemon/inotify.c
+++ b/daemon/inotify.c
@@ -224,7 +224,7 @@ do_inotify_read (void)
     r = read (inotify_fd, inotify_buf + inotify_posn,
               sizeof (inotify_buf) - inotify_posn);
     if (r == -1) {
-      if (errno == EWOULDBLOCK || errno == EAGAIN) /* End of list. */
+      if (get_errno() == EWOULDBLOCK || get_errno() == EAGAIN) /* End of list. */
         break;
       reply_with_perror ("read");
       goto error;
diff --git a/daemon/realpath.c b/daemon/realpath.c
index e6c81ef..f49e0c5 100644
--- a/daemon/realpath.c
+++ b/daemon/realpath.c
@@ -165,14 +165,13 @@ do_case_sensitive_path (const char *path)
 
     /* Is it a directory?  Try going into it. */
     fd2 = openat (fd_cwd, d->d_name, O_RDONLY | O_DIRECTORY);
-    int err = errno;
+    int err = get_error ();
     close (fd_cwd);
     fd_cwd = fd2;
-    errno = err;
     if (fd_cwd == -1) {
       /* ENOTDIR is OK provided we've reached the end of the path. */
-      if (errno != ENOTDIR) {
-        reply_with_perror ("openat: %s", d->d_name);
+      if (get_errno() != ENOTDIR) {
+        reply_with_perror_with_err (err, "openat: %s", d->d_name);
         goto error;
       }
 
diff --git a/daemon/tar.c b/daemon/tar.c
index c3bdcf7..56d291d 100644
--- a/daemon/tar.c
+++ b/daemon/tar.c
@@ -38,7 +38,8 @@ fwrite_cb (void *fp_ptr, const void *buf, int len)
 int
 do_tar_in (const char *dir)
 {
-  int err, r;
+  errtype err;
+  int r;
   FILE *fp;
   char *cmd;
 
@@ -50,10 +51,9 @@ do_tar_in (const char *dir)
 
   /* "tar -C /sysroot%s -xf -" but we have to quote the dir. */
   if (asprintf_nowarn (&cmd, "tar -C %R -xf -", dir) == -1) {
-    err = errno;
+    err = get_error ();
     cancel_receive ();
-    errno = err;
-    reply_with_perror ("asprintf");
+    reply_with_perror_with_err (err, "asprintf");
     return -1;
   }
 
@@ -62,10 +62,9 @@ do_tar_in (const char *dir)
 
   fp = popen (cmd, "w");
   if (fp == NULL) {
-    err = errno;
+    err = get_error ();
     cancel_receive ();
-    errno = err;
-    reply_with_perror ("%s", cmd);
+    reply_with_perror_with_err (err, "%s", cmd);
     free (cmd);
     return -1;
   }
@@ -73,10 +72,9 @@ do_tar_in (const char *dir)
 
   r = receive_file (fwrite_cb, &fp);
   if (r == -1) {		/* write error */
-    err = errno;
+    err = get_error ();
     cancel_receive ();
-    errno = err;
-    reply_with_perror ("write: %s", dir);
+    reply_with_perror_with_err (err, "write: %s", dir);
     pclose (fp);
     return -1;
   }
@@ -87,10 +85,9 @@ do_tar_in (const char *dir)
   }
 
   if (pclose (fp) != 0) {
-    err = errno;
+    err = get_error ();
     cancel_receive ();
-    errno = err;
-    reply_with_perror ("pclose: %s", dir);
+    reply_with_perror_with_err (err, "pclose: %s", dir);
     return -1;
   }
 
@@ -159,7 +156,8 @@ do_tar_out (const char *dir)
 int
 do_tgz_in (const char *dir)
 {
-  int err, r;
+  errtype err;
+  int r;
   FILE *fp;
   char *cmd;
 
@@ -171,10 +169,9 @@ do_tgz_in (const char *dir)
 
   /* "tar -C /sysroot%s -zxf -" but we have to quote the dir. */
   if (asprintf_nowarn (&cmd, "tar -C %R -zxf -", dir) == -1) {
-    err = errno;
+    err = get_error ();
     cancel_receive ();
-    errno = err;
-    reply_with_perror ("asprintf");
+    reply_with_perror_with_err (err, "asprintf");
     return -1;
   }
 
@@ -183,10 +180,9 @@ do_tgz_in (const char *dir)
 
   fp = popen (cmd, "w");
   if (fp == NULL) {
-    err = errno;
+    err = get_error ();
     cancel_receive ();
-    errno = err;
-    reply_with_perror ("%s", cmd);
+    reply_with_perror_with_err (err, "%s", cmd);
     free (cmd);
     return -1;
   }
@@ -194,10 +190,9 @@ do_tgz_in (const char *dir)
 
   r = receive_file (fwrite_cb, &fp);
   if (r == -1) {		/* write error */
-    err = errno;
+    err = get_error ();
     cancel_receive ();
-    errno = err;
-    reply_with_perror ("write: %s", dir);
+    reply_with_perror_with_err (err, "write: %s", dir);
     pclose (fp);
     return -1;
   }
@@ -208,10 +203,9 @@ do_tgz_in (const char *dir)
   }
 
   if (pclose (fp) != 0) {
-    err = errno;
+    err = get_error ();
     cancel_receive ();
-    errno = err;
-    reply_with_perror ("pclose: %s", dir);
+    reply_with_perror_with_err (err, "pclose: %s", dir);
     return -1;
   }
 
diff --git a/daemon/upload.c b/daemon/upload.c
index fdb8654..04aa21d 100644
--- a/daemon/upload.c
+++ b/daemon/upload.c
@@ -38,7 +38,8 @@ write_cb (void *fd_ptr, const void *buf, int len)
 int
 do_upload (const char *filename)
 {
-  int err, fd, r, is_dev;
+  errtype err;
+  int fd, r, is_dev;
 
   is_dev = STRPREFIX (filename, "/dev/");
   if (!is_dev) {
@@ -53,19 +54,17 @@ do_upload (const char *filename)
   fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0666);
   if (!is_dev) CHROOT_OUT;
   if (fd == -1) {
-    err = errno;
+    err = get_error ();
     cancel_receive ();
-    errno = err;
-    reply_with_perror ("%s", filename);
+    reply_with_perror_with_err (err, "%s", filename);
     return -1;
   }
 
   r = receive_file (write_cb, &fd);
   if (r == -1) {		/* write error */
-    err = errno;
+    err = get_error ();
     cancel_receive ();
-    errno = err;
-    reply_with_perror ("write: %s", filename);
+    reply_with_perror_with_err (err, "write: %s", filename);
     close (fd);
     return -1;
   }
@@ -76,10 +75,9 @@ do_upload (const char *filename)
   }
 
   if (close (fd) == -1) {
-    err = errno;
+    err = get_error ();
     cancel_receive ();
-    errno = err;
-    reply_with_perror ("close: %s", filename);
+    reply_with_perror_with_err (err, "close: %s", filename);
     return -1;
   }
 
-- 
1.6.5.2



More information about the Libguestfs mailing list