]> arthur.barton.de Git - netatalk.git/blob - include/atalk/errchk.h
Fix data corruption bug
[netatalk.git] / include / atalk / errchk.h
1 /*
2    Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8  
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13  */
14
15 #ifndef ERRCHECK_H
16 #define ERRCHECK_H
17
18 #define EC_INIT int ret = 0
19 #define EC_STATUS(a) ret = (a)
20 #define EC_FAIL do { ret = -1; goto cleanup; } while (0)
21 #define EC_FAIL_LOG(a, ...)                     \
22     do {               \
23         LOG(log_error, logtype_default, a, __VA_ARGS__);   \
24         ret = -1;      \
25         goto cleanup;  \
26     } while (0)
27 #define EC_CLEANUP cleanup
28 #define EC_EXIT return ret
29
30 /* 
31  * Check out doc/DEVELOPER for more infos.
32  *
33  * We have these macros:
34  * EC_ZERO, EC_ZERO_LOG, EC_ZERO_LOGSTR, EC_ZERO_LOG_ERR, EC_ZERO_CUSTOM
35  * EC_NEG1, EC_NEG1_LOG, EC_NEG1_LOGSTR, EC_NEG1_LOG_ERR, EC_NEG1_CUSTOM
36  * EC_NULL, EC_NULL_LOG, EC_NULL_LOGSTR, EC_NULL_LOG_ERR, EC_NULL_CUSTOM
37  *
38  * A boileplate function template is:
39
40    int func(void)
41    {
42        EC_INIT;
43
44        ...your code here...
45
46        EC_STATUS(0);
47
48    EC_CLEANUP:
49        EC_EXIT;
50    }
51  */
52
53 /* check for return val 0 which is ok, every other is an error, prints errno */
54 #define EC_ZERO_LOG(a)                                                  \
55     do {                                                                \
56         if ((a) != 0) {                                                 \
57             LOG(log_error, logtype_default, "%s failed: %s", #a, strerror(errno)); \
58             ret = -1;                                                   \
59             goto cleanup;                                               \
60         }                                                               \
61     } while (0)
62
63 #define EC_ZERO_LOGSTR(a, b, ...)                                       \
64     do {                                                                \
65         if ((a) != 0) {                                                 \
66             LOG(log_error, logtype_default, b, __VA_ARGS__);            \
67             ret = -1;                                                   \
68             goto cleanup;                                               \
69         }                                                               \
70     } while (0)
71
72 #define EC_ZERO_LOG_ERR(a, b)                                           \
73     do {                                                                \
74         if ((a) != 0) {                                                 \
75             LOG(log_error, logtype_default, "%s failed: %s", #a, strerror(errno)); \
76             ret = (b);                                                  \
77             goto cleanup;                                               \
78         }                                                               \
79     } while (0)
80
81 #define EC_ZERO(a)                              \
82     do {                                        \
83         if ((a) != 0) {                         \
84             ret = -1;                           \
85             goto cleanup;                       \
86         }                                       \
87     } while (0)
88
89 #define EC_ZERO_ERR(a,b )                       \
90     do {                                        \
91         if ((a) != 0) {                         \
92             ret = b;                            \
93             goto cleanup;                       \
94         }                                       \
95     } while (0)
96
97 /* check for return val 0 which is ok, every other is an error, prints errno */
98 #define EC_NEG1_LOG(a)                                                  \
99     do {                                                                \
100         if ((a) == -1) {                                                \
101             LOG(log_error, logtype_default, "%s failed: %s", #a, strerror(errno)); \
102             ret = -1;                                                   \
103             goto cleanup;                                               \
104         }                                                               \
105     } while (0)
106
107 #define EC_NEG1_LOGSTR(a, b, ...)                               \
108     do {                                                        \
109         if ((a) == -1) {                                        \
110             LOG(log_error, logtype_default, b, __VA_ARGS__);    \
111             ret = -1;                                           \
112             goto cleanup;                                       \
113         }                                                       \
114     } while (0)
115
116 #define EC_NEG1_LOG_ERR(a, b)                                           \
117     do {                                                                \
118         if ((a) == -1) {                                                \
119             LOG(log_error, logtype_default, "%s failed: %s", #a, strerror(errno)); \
120             ret = b;                                                    \
121             goto cleanup;                                               \
122         }                                                               \
123     } while (0)
124
125 #define EC_NEG1(a)                              \
126     do {                                        \
127         if ((a) == -1) {                        \
128             ret = -1;                           \
129             goto cleanup;                       \
130         }                                       \
131     } while (0)
132
133 /* check for return val != NULL, prints errno */
134 #define EC_NULL_LOG(a)                                                  \
135     do {                                                                \
136         if ((a) == NULL) {                                              \
137             LOG(log_error, logtype_default, "%s failed: %s", #a, strerror(errno)); \
138             ret = -1;                                                   \
139             goto cleanup;                                               \
140         }                                                               \
141     } while (0)
142
143 #define EC_NULL_LOGSTR(a, b, ...)                                       \
144     do {                                                                \
145         if ((a) == NULL) {                                              \
146             LOG(log_error, logtype_default, b , __VA_ARGS__);           \
147             ret = -1;                                                   \
148             goto cleanup;                                               \
149         } \
150     } while (0)
151
152 #define EC_NULL_LOG_ERR(a, b)                                           \
153     do {                                                                \
154         if ((a) == NULL) {                                              \
155             LOG(log_error, logtype_default, "%s failed: %s", #a, strerror(errno)); \
156             ret = b;                                                    \
157             goto cleanup;                                               \
158         }                                                               \
159     } while (0)
160
161 #define EC_NULL(a)                              \
162     do {                                        \
163         if ((a) == NULL) {                      \
164             ret = -1;                           \
165             goto cleanup;                       \
166         }                                       \
167     } while (0)
168
169 #endif /* ERRCHECK_H */