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