From patchwork Sun Mar 18 18:29:10 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alon Levy X-Patchwork-Id: 147415 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 54BC9B6FB6 for ; Mon, 19 Mar 2012 05:29:47 +1100 (EST) Received: from localhost ([::1]:55748 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S9KrR-00072H-2r for incoming@patchwork.ozlabs.org; Sun, 18 Mar 2012 14:29:45 -0400 Received: from eggs.gnu.org ([208.118.235.92]:60769) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S9Kr4-0006Hm-H0 for qemu-devel@nongnu.org; Sun, 18 Mar 2012 14:29:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1S9Kr2-00031v-Fa for qemu-devel@nongnu.org; Sun, 18 Mar 2012 14:29:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:18421) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S9Kr2-00031j-8B for qemu-devel@nongnu.org; Sun, 18 Mar 2012 14:29:20 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q2IITIpD000414 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Sun, 18 Mar 2012 14:29:18 -0400 Received: from garlic.redhat.com (reserved-200-252.tlv.redhat.com [10.35.200.252] (may be forged)) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q2IITEpB031064; Sun, 18 Mar 2012 14:29:17 -0400 From: Alon Levy To: qemu-devel@nongnu.org, Luiz Capitulino Date: Sun, 18 Mar 2012 19:29:10 +0100 Message-Id: <1332095353-19120-3-git-send-email-alevy@redhat.com> In-Reply-To: <1332095353-19120-1-git-send-email-alevy@redhat.com> References: <1331765714-26209-1-git-send-email-alevy@redhat.com> <1332095353-19120-1-git-send-email-alevy@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 2/5] add qemu_fopen_err X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- 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; } -