QtBase  v6.3.1
qsystemerror.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include <qglobal.h>
41 #include "qsystemerror_p.h"
42 #include <errno.h>
43 #if defined(Q_CC_MSVC)
44 # include <crtdbg.h>
45 #endif
46 #ifdef Q_OS_WIN
47 # include <qt_windows.h>
48 #endif
49 #ifndef QT_BOOTSTRAPPED
50 # include <qcoreapplication.h>
51 #endif
52 
54 
55 #if !defined(Q_OS_WIN) && QT_CONFIG(thread) && !defined(Q_OS_INTEGRITY) && !defined(Q_OS_QNX) && \
56  defined(_POSIX_THREAD_SAFE_FUNCTIONS) && _POSIX_VERSION >= 200112L
57 namespace {
58  // There are two incompatible versions of strerror_r:
59  // a) the XSI/POSIX.1 version, which returns an int,
60  // indicating success or not
61  // b) the GNU version, which returns a char*, which may or may not
62  // be the beginning of the buffer we used
63  // The GNU libc manpage for strerror_r says you should use the XSI
64  // version in portable code. However, it's impossible to do that if
65  // _GNU_SOURCE is defined so we use C++ overloading to decide what to do
66  // depending on the return type
67  [[maybe_unused]] static inline QString fromstrerror_helper(int, const QByteArray &buf)
68  {
70  }
71  [[maybe_unused]] static inline QString fromstrerror_helper(const char *str, const QByteArray &)
72  {
74  }
75 }
76 #endif
77 
78 #ifdef Q_OS_WIN
79 static QString windowsErrorString(int errorCode)
80 {
81  QString ret;
82  wchar_t *string = nullptr;
83  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
84  NULL,
85  errorCode,
86  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
87  (LPWSTR)&string,
88  0,
89  NULL);
90  ret = QString::fromWCharArray(string);
91  LocalFree((HLOCAL)string);
92 
93  if (ret.isEmpty() && errorCode == ERROR_MOD_NOT_FOUND)
94  ret = QString::fromLatin1("The specified module could not be found.");
95  if (ret.endsWith(QLatin1String("\r\n")))
96  ret.chop(2);
97  if (ret.isEmpty())
98  ret = QString::fromLatin1("Unknown error 0x%1.")
99  .arg(unsigned(errorCode), 8, 16, QLatin1Char('0'));
100  return ret;
101 }
102 #endif
103 
104 static QString standardLibraryErrorString(int errorCode)
105 {
106  const char *s = nullptr;
107  QString ret;
108  switch (errorCode) {
109  case 0:
110  break;
111  case EACCES:
112  s = QT_TRANSLATE_NOOP("QIODevice", "Permission denied");
113  break;
114  case EMFILE:
115  s = QT_TRANSLATE_NOOP("QIODevice", "Too many open files");
116  break;
117  case ENOENT:
118  s = QT_TRANSLATE_NOOP("QIODevice", "No such file or directory");
119  break;
120  case ENOSPC:
121  s = QT_TRANSLATE_NOOP("QIODevice", "No space left on device");
122  break;
123  default: {
124  #if QT_CONFIG(thread) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && _POSIX_VERSION >= 200112L && !defined(Q_OS_INTEGRITY) && !defined(Q_OS_QNX)
126  ret = fromstrerror_helper(strerror_r(errorCode, buf.data(), buf.size()), buf);
127  #else
128  ret = QString::fromLocal8Bit(strerror(errorCode));
129  #endif
130  break; }
131  }
132  if (s) {
133 #ifndef QT_BOOTSTRAPPED
134  ret = QCoreApplication::translate("QIODevice", s);
135 #else
137 #endif
138  }
139  return ret.trimmed();
140 }
141 
142 QString QSystemError::string(ErrorScope errorScope, int errorCode)
143 {
144  switch (errorScope) {
145  case NativeError:
146 #if defined(Q_OS_WIN)
147  return windowsErrorString(errorCode);
148 #endif // else unix: native and standard library are the same
150  return standardLibraryErrorString(errorCode);
151  default:
152  qWarning("invalid error scope");
153  Q_FALLTHROUGH();
154  case NoError:
155  return QLatin1String("No error");
156  }
157 }
158 
160 {
161  return standardLibraryErrorString(errorCode == -1 ? errno : errorCode);
162 }
163 
164 #ifdef Q_OS_WIN
165 QString QSystemError::windowsString(int errorCode)
166 {
167  return windowsErrorString(errorCode == -1 ? GetLastError() : errorCode);
168 }
169 
171 {
172  return windowsErrorString(code == -1 ? GetLastError() : code);
173 }
174 #else
176 {
177  return standardLibraryErrorString(code == -1 ? errno : code);
178 }
179 #endif
180 
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:85
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
Definition: qstring.h:84
The QString class provides a Unicode character string.
Definition: qstring.h:388
static QString fromLatin1(QByteArrayView ba)
Definition: qstring.cpp:5488
static QString fromLocal8Bit(QByteArrayView ba)
Definition: qstring.cpp:5563
static QString fromWCharArray(const wchar_t *string, qsizetype size=-1)
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=QLatin1Char(' ')) const
Definition: qstring.cpp:8318
static Q_CORE_EXPORT QString stdString(int errorCode=-1)
ErrorScope errorScope
static Q_CORE_EXPORT QString string(ErrorScope errorScope, int errorCode)
QString str
[2]
#define NULL
Definition: ftobjs.h:61
constexpr Initialization Uninitialized
Definition: qnamespace.h:1613
#define Q_FALLTHROUGH()
#define qWarning
Definition: qlogging.h:179
GLenum GLuint GLenum GLsizei const GLchar * buf
GLdouble s
[6]
Definition: qopenglext.h:235
QString qt_error_string(int code)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:53
Definition: inftrees.h:24