qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Alon Levy <alevy@redhat.com>
To: qemu-devel@nongnu.org, Luiz Capitulino <lcapitulino@redhat.com>
Subject: [Qemu-devel] [PATCH v3 2/5] add qemu_fopen_err
Date: Sun, 18 Mar 2012 19:29:10 +0100	[thread overview]
Message-ID: <1332095353-19120-3-git-send-email-alevy@redhat.com> (raw)
In-Reply-To: <1332095353-19120-1-git-send-email-alevy@redhat.com>

This adds a helper to conveniently set the correct error based on the
errno after a failed fopen.

The added function is placed in it's own c file to allow libcacard to
not develop dependencies on everything that qerror will bring in, which
includes monitor and half of qemu. I tried to make it as less ugly as I
could, by naming an osdep-no-qerror-obj-y and having that included in
osdep-obj-y, and using only the former for libcacard.

Signed-off-by: Alon Levy <alevy@redhat.com>
---
 Makefile.objs      |    8 +++++---
 libcacard/Makefile |    2 +-
 osdep-qerror.c     |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 osdep-qerror.h     |    8 ++++++++
 osdep.c            |    1 -
 5 files changed, 66 insertions(+), 5 deletions(-)
 create mode 100644 osdep-qerror.c
 create mode 100644 osdep-qerror.h

diff --git a/Makefile.objs b/Makefile.objs
index 226b01d..fb5a73a 100644
--- a/Makefile.objs
+++ b/Makefile.objs
@@ -20,9 +20,11 @@ universal-obj-y += $(qom-obj-y)
 
 #######################################################################
 # oslib-obj-y is code depending on the OS (win32 vs posix)
-oslib-obj-y = osdep.o
-oslib-obj-$(CONFIG_WIN32) += oslib-win32.o qemu-thread-win32.o
-oslib-obj-$(CONFIG_POSIX) += oslib-posix.o qemu-thread-posix.o
+oslib-no-qerror-obj-y = osdep.o
+oslib-no-qerror-obj-$(CONFIG_WIN32) += oslib-win32.o qemu-thread-win32.o
+oslib-no-qerror-obj-$(CONFIG_POSIX) += oslib-posix.o qemu-thread-posix.o
+oslib-obj-y = $(oslib-no-qerror-obj-y)
+oslib-obj-y += osdep-qerror.o
 
 #######################################################################
 # coroutines
diff --git a/libcacard/Makefile b/libcacard/Makefile
index c6a896a..83f483f 100644
--- a/libcacard/Makefile
+++ b/libcacard/Makefile
@@ -8,7 +8,7 @@ libcacard_includedir=$(includedir)/cacard
 $(call set-vpath, $(SRC_PATH):$(libcacard_srcpath))
 
 # objects linked against normal qemu binaries, not compiled with libtool
-QEMU_OBJS=$(addprefix ../,$(oslib-obj-y) qemu-timer-common.o $(trace-obj-y))
+QEMU_OBJS=$(addprefix ../,$(oslib-no-qerror-obj-y) qemu-timer-common.o $(trace-obj-y))
 
 # objects linked into a shared library, built with libtool with -fPIC if required
 QEMU_OBJS_LIB=$(addsuffix .lo,$(basename $(QEMU_OBJS)))
diff --git a/osdep-qerror.c b/osdep-qerror.c
new file mode 100644
index 0000000..6dac984
--- /dev/null
+++ b/osdep-qerror.c
@@ -0,0 +1,52 @@
+#include "qerror.h"
+
+#include "osdep-qerror.h"
+
+/*
+ * Helper to set an Error after a failed fopen.
+ *
+ * Uses errno so it must not be changed by another intermediate call.
+ */
+void qemu_fopen_err(Error **errp, const char *file_name)
+{
+    const char *fmt = NULL;
+
+    switch (errno) {
+    case EACCES:
+        fmt = QERR_EACCES;
+        break;
+    case EINTR:
+        fmt = QERR_EINTR;
+        break;
+    case EEXIST:
+        fmt = QERR_EEXIST;
+        break;
+    case EMFILE:
+        fmt = QERR_OPEN_FILE_EMFILE;
+        break;
+    case ENOSPC:
+        fmt = QERR_ENOSPC;
+        break;
+    case EPERM:
+        fmt = QERR_EPERM;
+        break;
+    case EROFS:
+        fmt = QERR_READ_ONLY;
+        break;
+    case ENOTDIR:
+        fmt = QERR_ENOTDIR;
+        break;
+    case EFBIG:
+        fmt = QERR_EFBIG;
+        break;
+    default:
+        /*
+         * EINVAL and ENOTSUP will result in the default
+         *
+         * ENOENT too, it's used by (for instance) bdrv_create_file for
+         * a different purpose then open (2) so just give a generic error.
+         */
+        fmt = QERR_OPEN_FILE_FAILED;
+    }
+    error_set(errp, fmt, file_name);
+}
diff --git a/osdep-qerror.h b/osdep-qerror.h
new file mode 100644
index 0000000..7320f4a
--- /dev/null
+++ b/osdep-qerror.h
@@ -0,0 +1,8 @@
+#ifndef OSDEP_QERROR_H
+#define OSDEP_QERROR_H
+
+#include "error.h"
+
+void qemu_fopen_err(Error **errp, const char *file_name);
+
+#endif
diff --git a/osdep.c b/osdep.c
index 3e6bada..efdd21c 100644
--- a/osdep.c
+++ b/osdep.c
@@ -241,4 +241,3 @@ ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags)
 
     return total;
 }
-
-- 
1.7.9.3

  parent reply	other threads:[~2012-03-18 18:29 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-14 22:55 [Qemu-devel] [PATCH v2 0/5] screendump qapi convertion Alon Levy
2012-03-14 22:55 ` [Qemu-devel] [PATCH v2 1/5] error: add error_set_file_open_failed Alon Levy
2012-03-15 14:52   ` Luiz Capitulino
2012-03-15 15:21     ` Alon Levy
2012-03-15 16:00       ` Luiz Capitulino
2012-03-14 22:55 ` [Qemu-devel] [PATCH v2 2/5] vga_hw_screen_dump: add Error** param Alon Levy
2012-03-15 14:54   ` Luiz Capitulino
2012-03-15 15:22     ` Alon Levy
2012-03-14 22:55 ` [Qemu-devel] [PATCH v2 3/5] qapi: convert screendump Alon Levy
2012-03-15 14:55   ` Luiz Capitulino
2012-03-15 15:23     ` Alon Levy
2012-03-14 22:55 ` [Qemu-devel] [PATCH v2 4/5] vga: ppm_save(): Return error on failure Alon Levy
2012-03-14 22:55 ` [Qemu-devel] [PATCH v2 5/5] blockdev: use error_set_file_open_failed Alon Levy
2012-03-15 14:56   ` Luiz Capitulino
2012-03-15 15:23     ` Alon Levy
2012-03-18 18:29 ` [Qemu-devel] [PATCH v3 0/5] screendump qapi convertion Alon Levy
2012-03-18 18:29   ` [Qemu-devel] [PATCH v3 1/5] qerror: add error codes for fopen failure Alon Levy
2012-03-23 14:21     ` Luiz Capitulino
2012-03-23 19:53       ` Alon Levy
2012-03-23 20:48         ` Luiz Capitulino
2012-03-26 18:44       ` Alon Levy
2012-03-26 18:51       ` Alon Levy
2012-03-18 18:29   ` Alon Levy [this message]
2012-03-23 14:22     ` [Qemu-devel] [PATCH v3 2/5] add qemu_fopen_err Luiz Capitulino
2012-03-23 19:55       ` Alon Levy
2012-03-18 18:29   ` [Qemu-devel] [PATCH v3 3/5] vga_hw_screen_dump: add Error** param Alon Levy
2012-03-18 18:29   ` [Qemu-devel] [PATCH v3 4/5] qapi: convert screendump Alon Levy
2012-03-23 14:25     ` Luiz Capitulino
2012-03-18 18:29   ` [Qemu-devel] [PATCH v3 5/5] vga: ppm_save(): Return error on failure Alon Levy
2012-03-23 14:27     ` Luiz Capitulino
2012-03-23 19:54       ` Alon Levy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1332095353-19120-3-git-send-email-alevy@redhat.com \
    --to=alevy@redhat.com \
    --cc=lcapitulino@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).