1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the qmake application of the Qt Toolkit.
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 Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
50
# include <sys/types.h>
51
# include <sys/stat.h>
57
using namespace QMakeInternal;
59
IoUtils::FileType IoUtils::fileType(const QString &fileName)
61
Q_ASSERT(fileName.isEmpty() || isAbsolutePath(fileName));
63
DWORD attr = GetFileAttributesW((WCHAR*)fileName.utf16());
64
if (attr == INVALID_FILE_ATTRIBUTES)
66
return (attr & FILE_ATTRIBUTE_DIRECTORY) ? FileIsDir : FileIsRegular;
69
if (::stat(fileName.toLocal8Bit().constData(), &st))
71
return S_ISDIR(st.st_mode) ? FileIsDir : FileIsRegular;
75
bool IoUtils::isRelativePath(const QString &path)
77
if (path.startsWith(QLatin1Char('/')))
80
if (path.startsWith(QLatin1Char('\\')))
82
// Unlike QFileInfo, this won't accept a relative path with a drive letter.
83
// Such paths result in a royal mess anyway ...
84
if (path.length() >= 3 && path.at(1) == QLatin1Char(':') && path.at(0).isLetter()
85
&& (path.at(2) == QLatin1Char('/') || path.at(2) == QLatin1Char('\\')))
91
QStringRef IoUtils::fileName(const QString &fileName)
93
return fileName.midRef(fileName.lastIndexOf(QLatin1Char('/')) + 1);
96
QString IoUtils::resolvePath(const QString &baseDir, const QString &fileName)
98
if (fileName.isEmpty())
100
if (isAbsolutePath(fileName))
101
return QDir::cleanPath(fileName);
102
return QDir::cleanPath(baseDir + QLatin1Char('/') + fileName);
106
bool hasSpecialChars(const QString &arg, const uchar (&iqm)[16])
108
for (int x = arg.length() - 1; x >= 0; --x) {
109
ushort c = arg.unicode()[x].unicode();
110
if ((c < sizeof(iqm) * 8) && (iqm[c / 8] & (1 << (c & 7))))
116
QString IoUtils::shellQuoteUnix(const QString &arg)
118
// Chars that should be quoted (TM). This includes:
119
static const uchar iqm[] = {
120
0xff, 0xff, 0xff, 0xff, 0xdf, 0x07, 0x00, 0xd8,
121
0x00, 0x00, 0x00, 0x38, 0x01, 0x00, 0x00, 0x78
122
}; // 0-32 \'"$`<>|;&(){}*?#!~[]
125
return QString::fromLatin1("\"\"");
128
if (hasSpecialChars(ret, iqm)) {
129
ret.replace(QLatin1Char('\''), QLatin1String("'\\''"));
130
ret.prepend(QLatin1Char('\''));
131
ret.append(QLatin1Char('\''));
136
QString IoUtils::shellQuoteWin(const QString &arg)
138
// Chars that should be quoted (TM). This includes:
139
// - control chars & space
140
// - the shell meta chars "&()<>^|
141
// - the potential separators ,;=
142
static const uchar iqm[] = {
143
0xff, 0xff, 0xff, 0xff, 0x45, 0x13, 0x00, 0x78,
144
0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x10
148
return QString::fromLatin1("\"\"");
151
if (hasSpecialChars(ret, iqm)) {
152
// Quotes are escaped and their preceding backslashes are doubled.
153
// It's impossible to escape anything inside a quoted string on cmd
154
// level, so the outer quoting must be "suspended".
155
ret.replace(QRegExp(QLatin1String("(\\\\*)\"")), QLatin1String("\"\\1\\1\\^\"\""));
156
// The argument must not end with a \ since this would be interpreted
157
// as escaping the quote -- rather put the \ behind the quote: e.g.
158
// rather use "foo"\ than "foo\"
159
int i = ret.length();
160
while (i > 0 && ret.at(i - 1) == QLatin1Char('\\'))
162
ret.insert(i, QLatin1Char('"'));
163
ret.prepend(QLatin1Char('"'));