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 Qt Linguist of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
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 General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include "ioutils.h" |
30 | |
31 | #include <qdir.h> |
32 | #include <qfile.h> |
33 | #include <qregexp.h> |
34 | |
35 | #ifdef Q_OS_WIN |
36 | # include <windows.h> |
37 | #else |
38 | # include <sys/types.h> |
39 | # include <sys/stat.h> |
40 | # include <unistd.h> |
41 | # include <utime.h> |
42 | # include <fcntl.h> |
43 | # include <errno.h> |
44 | #endif |
45 | |
46 | #define fL1S(s) QString::fromLatin1(s) |
47 | |
48 | QT_BEGIN_NAMESPACE |
49 | |
50 | using namespace QMakeInternal; |
51 | |
52 | IoUtils::FileType IoUtils::fileType(const QString &fileName) |
53 | { |
54 | Q_ASSERT(fileName.isEmpty() || isAbsolutePath(fileName)); |
55 | #ifdef Q_OS_WIN |
56 | DWORD attr = GetFileAttributesW((WCHAR*)fileName.utf16()); |
57 | if (attr == INVALID_FILE_ATTRIBUTES) |
58 | return FileNotFound; |
59 | return (attr & FILE_ATTRIBUTE_DIRECTORY) ? FileIsDir : FileIsRegular; |
60 | #else |
61 | struct ::stat st; |
62 | if (::stat(file: fileName.toLocal8Bit().constData(), buf: &st)) |
63 | return FileNotFound; |
64 | return S_ISDIR(st.st_mode) ? FileIsDir : S_ISREG(st.st_mode) ? FileIsRegular : FileNotFound; |
65 | #endif |
66 | } |
67 | |
68 | bool IoUtils::isRelativePath(const QString &path) |
69 | { |
70 | #ifdef QMAKE_BUILTIN_PRFS |
71 | if (path.startsWith(s: QLatin1String(":/" ))) |
72 | return false; |
73 | #endif |
74 | #ifdef Q_OS_WIN |
75 | // Unlike QFileInfo, this considers only paths with both a drive prefix and |
76 | // a subsequent (back-)slash absolute: |
77 | if (path.length() >= 3 && path.at(1) == QLatin1Char(':') && path.at(0).isLetter() |
78 | && (path.at(2) == QLatin1Char('/') || path.at(2) == QLatin1Char('\\'))) { |
79 | return false; |
80 | } |
81 | // (... unless, of course, they're UNC, which qmake fails on anyway) |
82 | #else |
83 | if (path.startsWith(c: QLatin1Char('/'))) |
84 | return false; |
85 | #endif // Q_OS_WIN |
86 | return true; |
87 | } |
88 | |
89 | QStringRef IoUtils::pathName(const QString &fileName) |
90 | { |
91 | return fileName.leftRef(n: fileName.lastIndexOf(c: QLatin1Char('/')) + 1); |
92 | } |
93 | |
94 | QStringRef IoUtils::fileName(const QString &fileName) |
95 | { |
96 | return fileName.midRef(position: fileName.lastIndexOf(c: QLatin1Char('/')) + 1); |
97 | } |
98 | |
99 | QString IoUtils::resolvePath(const QString &baseDir, const QString &fileName) |
100 | { |
101 | if (fileName.isEmpty()) |
102 | return QString(); |
103 | if (isAbsolutePath(fileName)) |
104 | return QDir::cleanPath(path: fileName); |
105 | #ifdef Q_OS_WIN // Add drive to otherwise-absolute path: |
106 | if (fileName.at(0).unicode() == '/' || fileName.at(0).unicode() == '\\') { |
107 | Q_ASSERT_X(isAbsolutePath(baseDir), "IoUtils::resolvePath" , qUtf8Printable(baseDir)); |
108 | return QDir::cleanPath(baseDir.left(2) + fileName); |
109 | } |
110 | #endif // Q_OS_WIN |
111 | return QDir::cleanPath(path: baseDir + QLatin1Char('/') + fileName); |
112 | } |
113 | |
114 | inline static |
115 | bool isSpecialChar(ushort c, const uchar (&iqm)[16]) |
116 | { |
117 | if ((c < sizeof(iqm) * 8) && (iqm[c / 8] & (1 << (c & 7)))) |
118 | return true; |
119 | return false; |
120 | } |
121 | |
122 | inline static |
123 | bool hasSpecialChars(const QString &arg, const uchar (&iqm)[16]) |
124 | { |
125 | for (int x = arg.length() - 1; x >= 0; --x) { |
126 | if (isSpecialChar(c: arg.unicode()[x].unicode(), iqm)) |
127 | return true; |
128 | } |
129 | return false; |
130 | } |
131 | |
132 | QString IoUtils::shellQuoteUnix(const QString &arg) |
133 | { |
134 | // Chars that should be quoted (TM). This includes: |
135 | static const uchar iqm[] = { |
136 | 0xff, 0xff, 0xff, 0xff, 0xdf, 0x07, 0x00, 0xd8, |
137 | 0x00, 0x00, 0x00, 0x38, 0x01, 0x00, 0x00, 0x78 |
138 | }; // 0-32 \'"$`<>|;&(){}*?#!~[] |
139 | |
140 | if (!arg.length()) |
141 | return QString::fromLatin1(str: "''" ); |
142 | |
143 | QString ret(arg); |
144 | if (hasSpecialChars(arg: ret, iqm)) { |
145 | ret.replace(c: QLatin1Char('\''), after: QLatin1String("'\\''" )); |
146 | ret.prepend(c: QLatin1Char('\'')); |
147 | ret.append(c: QLatin1Char('\'')); |
148 | } |
149 | return ret; |
150 | } |
151 | |
152 | QString IoUtils::shellQuoteWin(const QString &arg) |
153 | { |
154 | // Chars that should be quoted (TM). This includes: |
155 | // - control chars & space |
156 | // - the shell meta chars "&()<>^| |
157 | // - the potential separators ,;= |
158 | static const uchar iqm[] = { |
159 | 0xff, 0xff, 0xff, 0xff, 0x45, 0x13, 0x00, 0x78, |
160 | 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x10 |
161 | }; |
162 | // Shell meta chars that need escaping. |
163 | static const uchar ism[] = { |
164 | 0x00, 0x00, 0x00, 0x00, 0x40, 0x03, 0x00, 0x50, |
165 | 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x10 |
166 | }; // &()<>^| |
167 | |
168 | if (!arg.length()) |
169 | return QString::fromLatin1(str: "\"\"" ); |
170 | |
171 | QString ret(arg); |
172 | if (hasSpecialChars(arg: ret, iqm)) { |
173 | // The process-level standard quoting allows escaping quotes with backslashes (note |
174 | // that backslashes don't escape themselves, unless they are followed by a quote). |
175 | // Consequently, quotes are escaped and their preceding backslashes are doubled. |
176 | ret.replace(rx: QRegExp(QLatin1String("(\\\\*)\"" )), after: QLatin1String("\\1\\1\\\"" )); |
177 | // Trailing backslashes must be doubled as well, as they are followed by a quote. |
178 | ret.replace(rx: QRegExp(QLatin1String("(\\\\+)$" )), after: QLatin1String("\\1\\1" )); |
179 | // However, the shell also interprets the command, and no backslash-escaping exists |
180 | // there - a quote always toggles the quoting state, but is nonetheless passed down |
181 | // to the called process verbatim. In the unquoted state, the circumflex escapes |
182 | // meta chars (including itself and quotes), and is removed from the command. |
183 | bool quoted = true; |
184 | for (int i = 0; i < ret.length(); i++) { |
185 | QChar c = ret.unicode()[i]; |
186 | if (c.unicode() == '"') |
187 | quoted = !quoted; |
188 | else if (!quoted && isSpecialChar(c: c.unicode(), iqm: ism)) |
189 | ret.insert(i: i++, c: QLatin1Char('^')); |
190 | } |
191 | if (!quoted) |
192 | ret.append(c: QLatin1Char('^')); |
193 | ret.append(c: QLatin1Char('"')); |
194 | ret.prepend(c: QLatin1Char('"')); |
195 | } |
196 | return ret; |
197 | } |
198 | |
199 | #if defined(PROEVALUATOR_FULL) |
200 | |
201 | # if defined(Q_OS_WIN) |
202 | static QString windowsErrorCode() |
203 | { |
204 | wchar_t *string = nullptr; |
205 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, |
206 | NULL, |
207 | GetLastError(), |
208 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
209 | (LPWSTR)&string, |
210 | 0, |
211 | NULL); |
212 | QString ret = QString::fromWCharArray(string); |
213 | LocalFree((HLOCAL)string); |
214 | return ret.trimmed(); |
215 | } |
216 | # endif |
217 | |
218 | bool IoUtils::touchFile(const QString &targetFileName, const QString &referenceFileName, QString *errorString) |
219 | { |
220 | # ifdef Q_OS_UNIX |
221 | struct stat st; |
222 | if (stat(referenceFileName.toLocal8Bit().constData(), &st)) { |
223 | *errorString = fL1S("Cannot stat() reference file %1: %2." ).arg(referenceFileName, fL1S(strerror(errno))); |
224 | return false; |
225 | } |
226 | # if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L |
227 | const struct timespec times[2] = { { 0, UTIME_NOW }, st.st_mtim }; |
228 | const bool utimeError = utimensat(AT_FDCWD, targetFileName.toLocal8Bit().constData(), times, 0) < 0; |
229 | # else |
230 | struct utimbuf utb; |
231 | utb.actime = time(0); |
232 | utb.modtime = st.st_mtime; |
233 | const bool utimeError= utime(targetFileName.toLocal8Bit().constData(), &utb) < 0; |
234 | # endif |
235 | if (utimeError) { |
236 | *errorString = fL1S("Cannot touch %1: %2." ).arg(targetFileName, fL1S(strerror(errno))); |
237 | return false; |
238 | } |
239 | # else |
240 | HANDLE rHand = CreateFile((wchar_t*)referenceFileName.utf16(), |
241 | GENERIC_READ, FILE_SHARE_READ, |
242 | NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
243 | if (rHand == INVALID_HANDLE_VALUE) { |
244 | *errorString = fL1S("Cannot open reference file %1: %2" ).arg(referenceFileName, windowsErrorCode()); |
245 | return false; |
246 | } |
247 | FILETIME ft; |
248 | GetFileTime(rHand, NULL, NULL, &ft); |
249 | CloseHandle(rHand); |
250 | HANDLE wHand = CreateFile((wchar_t*)targetFileName.utf16(), |
251 | GENERIC_WRITE, FILE_SHARE_READ, |
252 | NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
253 | if (wHand == INVALID_HANDLE_VALUE) { |
254 | *errorString = fL1S("Cannot open %1: %2" ).arg(targetFileName, windowsErrorCode()); |
255 | return false; |
256 | } |
257 | SetFileTime(wHand, NULL, NULL, &ft); |
258 | CloseHandle(wHand); |
259 | # endif |
260 | return true; |
261 | } |
262 | |
263 | #if defined(QT_BUILD_QMAKE) && defined(Q_OS_UNIX) |
264 | bool IoUtils::readLinkTarget(const QString &symlinkPath, QString *target) |
265 | { |
266 | const QByteArray localSymlinkPath = QFile::encodeName(symlinkPath); |
267 | # if defined(__GLIBC__) && !defined(PATH_MAX) |
268 | # define PATH_CHUNK_SIZE 256 |
269 | char *s = 0; |
270 | int len = -1; |
271 | int size = PATH_CHUNK_SIZE; |
272 | |
273 | forever { |
274 | s = (char *)::realloc(s, size); |
275 | len = ::readlink(localSymlinkPath.constData(), s, size); |
276 | if (len < 0) { |
277 | ::free(s); |
278 | break; |
279 | } |
280 | if (len < size) |
281 | break; |
282 | size *= 2; |
283 | } |
284 | # else |
285 | char s[PATH_MAX+1]; |
286 | int len = readlink(localSymlinkPath.constData(), s, PATH_MAX); |
287 | # endif |
288 | if (len <= 0) |
289 | return false; |
290 | *target = QFile::decodeName(QByteArray(s, len)); |
291 | # if defined(__GLIBC__) && !defined(PATH_MAX) |
292 | ::free(s); |
293 | # endif |
294 | return true; |
295 | } |
296 | #endif |
297 | |
298 | #endif // PROEVALUATOR_FULL |
299 | |
300 | QT_END_NAMESPACE |
301 | |