]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/berrno.h
17Mar06
[bacula/bacula] / bacula / src / lib / berrno.h
1 /*
2  *   Version $Id$
3  *
4  * Kern Sibbald, July MMIV
5  *
6  */
7 /*
8    Copyright (C) 2004-2005 Kern Sibbald
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License
12    version 2 as amended with additional clauses defined in the
13    file LICENSE in the main source directory.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
18    the file LICENSE for additional details.
19
20  */
21
22 #ifdef HAVE_WIN32
23 #define b_errno_win32  (1<<29)        /* user reserved bit */
24 #endif
25 #define b_errno_exit   (1<<28)        /* child exited, exit code returned */
26 #define b_errno_signal (1<<27)        /* child died, signal code returned */
27
28 /*
29  * A more generalized way of handling errno that works with Unix, Windows,
30  *  and with Bacula bpipes.
31  *
32  * It works by picking up errno and creating a memory pool buffer
33  *  for editing the message. strerror() does the actual editing, and
34  *  it is thread safe.
35  *
36  * If bit 29 in berrno_ is set then it is a Win32 error, and we
37  *  must to a GetLastError() to get the error code for formatting.
38  * If bit 29 in berrno_ is not set, then it is a Unix errno.
39  *
40  */
41 class berrno : public SMARTALLOC {
42    POOLMEM *buf_;
43    int berrno_;
44    void format_win32_message();
45 public:
46    berrno(int pool=PM_EMSG);
47    ~berrno();
48    const char *strerror();
49    const char *strerror(int errnum);
50    void set_errno(int errnum);
51 };
52
53 /* Constructor */
54 inline berrno::berrno(int pool)
55 {
56    berrno_ = errno;
57    buf_ = get_pool_memory(pool);
58 #ifdef HAVE_WIN32
59    format_win32_message();
60 #endif
61 }
62
63 inline berrno::~berrno()
64 {
65    free_pool_memory(buf_);
66 }
67
68 inline const char *berrno::strerror(int errnum)
69 {
70    berrno_ = errnum;
71    return berrno::strerror();
72 }
73
74
75 inline void berrno::set_errno(int errnum)
76 {
77    berrno_ = errnum;
78 }