MPD: system/Error.hxx Source File

MPD 0.20.15

Error.hxx
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2013-2015 Max Kellermann <max.kellermann@gmail.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef SYSTEM_ERROR_HXX
31 #define SYSTEM_ERROR_HXX
32
33 #include "util/StringUtil.hxx"
34 #include "Compiler.h"
35
36 #include <system_error>
37 #include <utility>
38
39 #include <stdio.h>
40
41 template<typename... Args>
42 static inline std::system_error
43 FormatSystemError(std::error_code code, const char *fmt, Args&&... args)
44 {
45 char buffer[1024];
46 snprintf(buffer, sizeof(buffer), fmt, std::forward<Args>(args)...);
47 return std::system_error(code, buffer);
48 }
49
50 #ifdef _WIN32
51
52 #include <windows.h>
53
54 static inline std::system_error
55 MakeLastError(DWORD code, const char *msg)
56 {
57 return std::system_error(std::error_code(code, std::system_category()),
58 msg);
59 }
60
61 static inline std::system_error
62 MakeLastError(const char *msg)
63 {
64 return MakeLastError(GetLastError(), msg);
65 }
66
67 template<typename... Args>
68 static inline std::system_error
69 FormatLastError(DWORD code, const char *fmt, Args&&... args)
70 {
71 char buffer[512];
72 const auto end = buffer + sizeof(buffer);
73 size_t length = snprintf(buffer, sizeof(buffer) - 128,
74 fmt, std::forward<Args>(args)...);
75 char *p = buffer + length;
76 *p++ = ':';
77 *p++ = ' ';
78
79 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
80 FORMAT_MESSAGE_IGNORE_INSERTS,
81 nullptr, code, 0, p, end - p, nullptr);
82 return MakeLastError(code, buffer);
83 }
84
85 template<typename... Args>
86 static inline std::system_error
87 FormatLastError(const char *fmt, Args&&... args)
88 {
89 return FormatLastError(GetLastError(), fmt,
90 std::forward<Args>(args)...);
91 }
92
93 #endif /* _WIN32 */
94
95 #include <errno.h>
96 #include <string.h>
97
106 static inline const std::error_category &
107 ErrnoCategory()
108 {
109 #ifdef _WIN32
110 /* on Windows, the generic_category() is used for errno
111 values */
112 return std::generic_category();
113 #else
114 /* on POSIX, system_category() appears to be the best
115 choice */
116 return std::system_category();
117 #endif
118 }
119
120 static inline std::system_error
121 MakeErrno(int code, const char *msg)
122 {
123 return std::system_error(std::error_code(code, ErrnoCategory()),
124 msg);
125 }
126
127 static inline std::system_error
128 MakeErrno(const char *msg)
129 {
130 return MakeErrno(errno, msg);
131 }
132
133 template<typename... Args>
134 static inline std::system_error
135 FormatErrno(int code, const char *fmt, Args&&... args)
136 {
137 char buffer[512];
138 snprintf(buffer, sizeof(buffer),
139 fmt, std::forward<Args>(args)...);
140 return MakeErrno(code, buffer);
141 }
142
143 template<typename... Args>
144 static inline std::system_error
145 FormatErrno(const char *fmt, Args&&... args)
146 {
147 return FormatErrno(errno, fmt, std::forward<Args>(args)...);
148 }
149
150 gcc_pure
151 static inline bool
152 IsFileNotFound(const std::system_error &e) noexcept
153 {
154 #ifdef _WIN32
155 return e.code().category() == std::system_category() &&
156 e.code().value() == ERROR_FILE_NOT_FOUND;
157 #else
158 return e.code().category() == ErrnoCategory() &&
159 e.code().value() == ENOENT;
160 #endif
161 }
162
163 gcc_pure
164 static inline bool
165 IsPathNotFound(const std::system_error &e) noexcept
166 {
167 #ifdef _WIN32
168 return e.code().category() == std::system_category() &&
169 e.code().value() == ERROR_PATH_NOT_FOUND;
170 #else
171 return e.code().category() == ErrnoCategory() &&
172 e.code().value() == ENOTDIR;
173 #endif
174 }
175
176 gcc_pure
177 static inline bool
178 IsAccessDenied(const std::system_error &e) noexcept
179 {
180 #ifdef _WIN32
181 return e.code().category() == std::system_category() &&
182 e.code().value() == ERROR_ACCESS_DENIED;
183 #else
184 return e.code().category() == ErrnoCategory() &&
185 e.code().value() == EACCES;
186 #endif
187 }
188
189 #endif
MakeErrno
static std::system_error MakeErrno(int code, const char *msg)
Definition: Error.hxx:121
IsFileNotFound
static gcc_pure bool IsFileNotFound(const std::system_error &e) noexcept
Definition: Error.hxx:152
StringUtil.hxx
ErrnoCategory
static const std::error_category & ErrnoCategory()
Returns the error_category to be used to wrap errno values.
Definition: Error.hxx:107
IsAccessDenied
static gcc_pure bool IsAccessDenied(const std::system_error &e) noexcept
Definition: Error.hxx:178
FormatErrno
static std::system_error FormatErrno(int code, const char *fmt, Args &&... args)
Definition: Error.hxx:135
IsPathNotFound
static gcc_pure bool IsPathNotFound(const std::system_error &e) noexcept
Definition: Error.hxx:165
Compiler.h
e
int e
Definition: Log.hxx:115
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
fmt
const char * fmt
Definition: Client.hxx:231
FormatSystemError
static std::system_error FormatSystemError(std::error_code code, const char *fmt, Args &&... args)
Definition: Error.hxx:43