A fopen() wrapper that takes an Error argument. Signed-off-by: Luiz Capitulino <lcapitul...@redhat.com> --- cutils.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ qemu-common.h | 3 +++ 2 files changed, 48 insertions(+)
diff --git a/cutils.c b/cutils.c index af308cd..85dbb06 100644 --- a/cutils.c +++ b/cutils.c @@ -26,6 +26,7 @@ #include <math.h> #include "qemu_socket.h" +#include "error.h" void pstrcpy(char *buf, int buf_size, const char *str) { @@ -549,3 +550,47 @@ int qemu_sendv(int sockfd, struct iovec *iov, int len, int iov_offset) return do_sendv_recvv(sockfd, iov, len, iov_offset, 1); } +static void set_open_err(Error **errp, const char *path, int err_nr) +{ + switch (err_nr) { + case EACCES: + error_set(errp, QERR_INVALID_ACCESS); + return; + case EMFILE: + error_set(errp, QERR_TOO_MANY_FILES_PROC); + return; + case ENFILE: + error_set(errp, QERR_TOO_MANY_FILES_SYS); + return; + case ENAMETOOLONG: + error_set(errp, QERR_NAME_TOO_LONG); + return; + case ENOSPC: + error_set(errp, QERR_NO_SPACE); + return; + case EPERM: + error_set(errp, QERR_PERMISSION_DENIED); + return; + case EROFS: + error_set(errp, QERR_READ_ONLY_FS); + return; + case ENOENT: + error_set(errp, QERR_NO_FILE_DIR); + return; + default: + error_set(errp, QERR_OPEN_FILE_FAILED, path); + return; + } +} + +FILE *qemu_fopen_err(const char *path, const char *mode, Error **errp) +{ + FILE *fp; + + fp = fopen(path, mode); + if (!fp) { + set_open_err(errp, path, errno); + } + + return fp; +} diff --git a/qemu-common.h b/qemu-common.h index cccfb42..2cab2a8 100644 --- a/qemu-common.h +++ b/qemu-common.h @@ -4,6 +4,7 @@ #include "compiler.h" #include "config-host.h" +#include "error.h" #if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__) #define WORDS_ALIGNED @@ -208,6 +209,8 @@ int qemu_pipe(int pipefd[2]); int qemu_recvv(int sockfd, struct iovec *iov, int len, int iov_offset); int qemu_sendv(int sockfd, struct iovec *iov, int len, int iov_offset); +FILE *qemu_fopen_err(const char *path, const char *mode, Error **errp); + /* Error handling. */ void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2); -- 1.7.10.2.565.gbd578b5