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