From 691e9906a615a2f94dad72a3d487c13d765137dc Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Tue, 27 Jul 2004 21:01:50 +0000 Subject: [PATCH] Add new berrno files git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@1494 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/lib/berrno.c | 109 ++++++++++++++++++++++++++++++++++++++++ bacula/src/lib/berrno.h | 68 +++++++++++++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 bacula/src/lib/berrno.c create mode 100644 bacula/src/lib/berrno.h diff --git a/bacula/src/lib/berrno.c b/bacula/src/lib/berrno.c new file mode 100644 index 0000000000..9ec5fbd6bb --- /dev/null +++ b/bacula/src/lib/berrno.c @@ -0,0 +1,109 @@ +/* + * Bacula errno handler + * + * berrno is a simplistic errno handler that works for + * Unix, Win32, and Bacula bpipes. + * + * See berrno.h for how to use berrno. + * + * Kern Sibbald, July MMIV + * + * Version $Id$ + * + */ +/* + Copyright (C) 2004 Kern Sibbald and John Walker + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public + License along with this program; if not, write to the Free + Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + MA 02111-1307, USA. + + */ + +#include "bacula.h" + +const char *berrno::strerror() +{ +#ifdef HAVE_WIN32 + LPVOID msg; + + if (errnum && b_errno_win32) { + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR)&msg; + 0, + NULL); + + pm_strcpy(&buf_, msg); + LocalFree(msg); + return (const char *)buf_; + } +#endif + if (bstrerror(berrno_, buf_, 1024) < 0) { + return "Invalid errno. No error message possible."; + } + return (const char *)buf_; +} + + +#ifdef TEST_PROGRAM + + +struct FILESET { + alist mylist; +}; + +int main() +{ + FILESET *fileset; + char buf[30]; + alist *mlist; + + fileset = (FILESET *)malloc(sizeof(FILESET)); + memset(fileset, 0, sizeof(FILESET)); + fileset->mylist.init(); + + printf("Manual allocation/destruction of list:\n"); + + for (int i=0; i<20; i++) { + sprintf(buf, "This is item %d", i); + fileset->mylist.append(bstrdup(buf)); + } + for (int i=0; i< fileset->mylist.size(); i++) { + printf("Item %d = %s\n", i, (char *)fileset->mylist[i]); + } + fileset->mylist.destroy(); + free(fileset); + + printf("Allocation/destruction using new delete\n"); + mlist = new alist(10); + + for (int i=0; i<20; i++) { + sprintf(buf, "This is item %d", i); + mlist->append(bstrdup(buf)); + } + for (int i=0; i< mlist->size(); i++) { + printf("Item %d = %s\n", i, (char *)mlist->get(i)); + } + + delete mlist; + + + sm_dump(false); + +} +#endif diff --git a/bacula/src/lib/berrno.h b/bacula/src/lib/berrno.h new file mode 100644 index 0000000000..88492e1772 --- /dev/null +++ b/bacula/src/lib/berrno.h @@ -0,0 +1,68 @@ +/* + * Version $Id$ + */ + +/* + Copyright (C) 2004 Kern Sibbald and John Walker + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public + License along with this program; if not, write to the Free + Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + MA 02111-1307, USA. + + Kern Sibbald, July MMIV + + */ + +#ifdef HAVE_WIN32 +#define b_errno_win32 (1<<29) /* user reserved bit */ +#endif + +/* + * A more generalized way of handling errno that works with Unix, Windows, + * and with Bacula bpipes. + * + * It works by picking up errno and creating a memory pool buffer + * for editing the message. strerror() does the actual editing, and + * it is thread safe. + * + * If bit 29 in berrno_ is set then it is a Win32 error, and we + * must to a GetLastError() to get the error code for formatting. + * If bit 29 in berrno_ is not set, then it is a Unix errno. + */ +class berrno { + POOLMEM *buf_; + int berrno_; +public: + berrno(int pool=PM_EMSG); + ~berrno(); + const char *strerror(); + void set_errno(int errnum); +}; + +/* Constructor */ +inline berrno::berrno(int pool) +{ + berrno_ = errno; + buf_ = get_pool_memory(pool); +} + +inline berrno::~berrno() +{ + free_pool_memory(buf_); +} + +inline void berrno::set_errno(int errnum) +{ + berrno_ = errnum; +} -- 2.39.5