]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/berrno.h
kes Change Bacula trademark owner from John Walker to Kern Sibbald
[bacula/bacula] / bacula / src / lib / berrno.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2004-2007 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *   Version $Id$
30  *
31  * Kern Sibbald, July MMIV
32  *
33  */
34
35 /*
36  * Extra bits set to interpret errno value differently from errno
37  */
38 #ifdef HAVE_WIN32
39 #define b_errno_win32  (1<<29)        /* user reserved bit */
40 #else
41 #define b_errno_win32  0              /* On Unix/Linix system */
42 #endif
43 #define b_errno_exit   (1<<28)        /* child exited, exit code returned */
44 #define b_errno_signal (1<<27)        /* child died, signal code returned */
45
46 /*
47  * A more generalized way of handling errno that works with Unix, Windows,
48  *  and with Bacula bpipes.
49  *
50  * It works by picking up errno and creating a memory pool buffer
51  *  for editing the message. strerror() does the actual editing, and
52  *  it is thread safe.
53  *
54  * If bit 29 in m_berrno is set then it is a Win32 error, and we
55  *  must do a GetLastError() to get the error code for formatting.
56  * If bit 29 in m_berrno is not set, then it is a Unix errno.
57  *
58  */
59 class berrno : public SMARTALLOC {
60    POOLMEM *m_buf;
61    int m_berrno;
62    void format_win32_message();
63 public:
64    berrno(int pool=PM_EMSG);
65    ~berrno();
66    const char *bstrerror();
67    const char *bstrerror(int errnum);
68    void set_errno(int errnum);
69    int code() { return m_berrno & ~(b_errno_exit|b_errno_signal); }
70    int code(int stat) { return stat & ~(b_errno_exit|b_errno_signal); }
71 };
72
73 /* Constructor */
74 inline berrno::berrno(int pool)
75 {
76    m_berrno = errno;
77    m_buf = get_pool_memory(pool);
78 #ifdef HAVE_WIN32
79    format_win32_message();
80 #endif
81    errno = m_berrno;
82 }
83
84 inline berrno::~berrno()
85 {
86    free_pool_memory(m_buf);
87 }
88
89 inline const char *berrno::bstrerror(int errnum)
90 {
91    m_berrno = errnum;
92    return berrno::bstrerror();
93 }
94
95
96 inline void berrno::set_errno(int errnum)
97 {
98    m_berrno = errnum;
99 }