2013-05-29 18:18:51 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2013-05-29 18:18:51 +00:00
|
|
|
**
|
|
|
|
** This file is part of the qmake application of the Qt Toolkit.
|
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
2013-05-29 18:18:51 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-15 12:36:27 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2013-05-29 18:18:51 +00:00
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2013-05-29 18:18:51 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "qmakevfs.h"
|
|
|
|
|
|
|
|
#include "ioutils.h"
|
|
|
|
using namespace QMakeInternal;
|
|
|
|
|
|
|
|
#include <qdir.h>
|
|
|
|
#include <qfile.h>
|
|
|
|
#include <qfileinfo.h>
|
|
|
|
|
2018-10-02 10:59:24 +00:00
|
|
|
#if QT_CONFIG(textcodec)
|
2016-11-30 17:26:44 +00:00
|
|
|
#include <qtextcodec.h>
|
|
|
|
#endif
|
|
|
|
|
2013-05-29 18:18:51 +00:00
|
|
|
#define fL1S(s) QString::fromLatin1(s)
|
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
QMakeVfs::QMakeVfs()
|
|
|
|
#ifndef PROEVALUATOR_FULL
|
|
|
|
: m_magicMissing(fL1S("missing"))
|
|
|
|
, m_magicExisting(fL1S("existing"))
|
|
|
|
#endif
|
|
|
|
{
|
2018-10-02 10:59:24 +00:00
|
|
|
#if QT_CONFIG(textcodec)
|
2016-11-30 17:26:44 +00:00
|
|
|
m_textCodec = 0;
|
|
|
|
#endif
|
2018-05-24 18:50:32 +00:00
|
|
|
ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
QMakeVfs::~QMakeVfs()
|
|
|
|
{
|
|
|
|
deref();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QMakeVfs::ref()
|
|
|
|
{
|
|
|
|
#ifdef PROEVALUATOR_THREAD_SAFE
|
|
|
|
QMutexLocker locker(&s_mutex);
|
|
|
|
#endif
|
|
|
|
++s_refCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QMakeVfs::deref()
|
|
|
|
{
|
|
|
|
#ifdef PROEVALUATOR_THREAD_SAFE
|
|
|
|
QMutexLocker locker(&s_mutex);
|
|
|
|
#endif
|
|
|
|
if (!--s_refCount) {
|
|
|
|
s_fileIdCounter = 0;
|
|
|
|
s_fileIdMap.clear();
|
|
|
|
s_idFileMap.clear();
|
|
|
|
}
|
2013-05-29 18:18:51 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 17:30:24 +00:00
|
|
|
#ifdef PROPARSER_THREAD_SAFE
|
|
|
|
QMutex QMakeVfs::s_mutex;
|
|
|
|
#endif
|
2018-05-24 18:50:32 +00:00
|
|
|
int QMakeVfs::s_refCount;
|
2016-10-24 17:30:24 +00:00
|
|
|
QAtomicInt QMakeVfs::s_fileIdCounter;
|
|
|
|
QHash<QString, int> QMakeVfs::s_fileIdMap;
|
|
|
|
QHash<int, QString> QMakeVfs::s_idFileMap;
|
|
|
|
|
|
|
|
int QMakeVfs::idForFileName(const QString &fn, VfsFlags flags)
|
|
|
|
{
|
|
|
|
#ifdef PROEVALUATOR_DUAL_VFS
|
|
|
|
{
|
|
|
|
# ifdef PROPARSER_THREAD_SAFE
|
|
|
|
QMutexLocker locker(&m_vmutex);
|
|
|
|
# endif
|
|
|
|
int idx = (flags & VfsCumulative) ? 1 : 0;
|
|
|
|
if (flags & VfsCreate) {
|
|
|
|
int &id = m_virtualFileIdMap[idx][fn];
|
|
|
|
if (!id) {
|
|
|
|
id = ++s_fileIdCounter;
|
|
|
|
m_virtualIdFileMap[id] = fn;
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
int id = m_virtualFileIdMap[idx].value(fn);
|
|
|
|
if (id || (flags & VfsCreatedOnly))
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef PROPARSER_THREAD_SAFE
|
2018-11-27 08:39:56 +00:00
|
|
|
QMutexLocker locker(&s_mutex);
|
2016-10-24 17:30:24 +00:00
|
|
|
#endif
|
2018-11-27 08:39:56 +00:00
|
|
|
if (!(flags & VfsAccessedOnly)) {
|
2016-10-24 17:30:24 +00:00
|
|
|
int &id = s_fileIdMap[fn];
|
|
|
|
if (!id) {
|
|
|
|
id = ++s_fileIdCounter;
|
|
|
|
s_idFileMap[id] = fn;
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
return s_fileIdMap.value(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString QMakeVfs::fileNameForId(int id)
|
|
|
|
{
|
|
|
|
#ifdef PROEVALUATOR_DUAL_VFS
|
|
|
|
{
|
|
|
|
# ifdef PROPARSER_THREAD_SAFE
|
|
|
|
QMutexLocker locker(&m_vmutex);
|
|
|
|
# endif
|
|
|
|
const QString &fn = m_virtualIdFileMap.value(id);
|
|
|
|
if (!fn.isEmpty())
|
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef PROPARSER_THREAD_SAFE
|
|
|
|
QMutexLocker locker(&s_mutex);
|
|
|
|
#endif
|
|
|
|
return s_idFileMap.value(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QMakeVfs::writeFile(int id, QIODevice::OpenMode mode, VfsFlags flags,
|
2015-12-11 11:08:47 +00:00
|
|
|
const QString &contents, QString *errStr)
|
2013-05-29 18:18:51 +00:00
|
|
|
{
|
|
|
|
#ifndef PROEVALUATOR_FULL
|
|
|
|
# ifdef PROEVALUATOR_THREAD_SAFE
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
# endif
|
2016-10-24 17:30:24 +00:00
|
|
|
QString *cont = &m_files[id];
|
|
|
|
Q_UNUSED(flags)
|
2013-05-29 18:18:51 +00:00
|
|
|
if (mode & QIODevice::Append)
|
|
|
|
*cont += contents;
|
|
|
|
else
|
|
|
|
*cont = contents;
|
|
|
|
Q_UNUSED(errStr)
|
|
|
|
return true;
|
|
|
|
#else
|
2016-10-24 17:30:24 +00:00
|
|
|
QFileInfo qfi(fileNameForId(id));
|
2013-05-29 18:18:51 +00:00
|
|
|
if (!QDir::current().mkpath(qfi.path())) {
|
|
|
|
*errStr = fL1S("Cannot create parent directory");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QByteArray bytes = contents.toLocal8Bit();
|
2016-10-24 17:30:24 +00:00
|
|
|
QFile cfile(qfi.filePath());
|
2013-05-29 18:18:51 +00:00
|
|
|
if (!(mode & QIODevice::Append) && cfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
2015-12-11 11:08:47 +00:00
|
|
|
if (cfile.readAll() == bytes) {
|
2016-10-24 17:30:24 +00:00
|
|
|
if (flags & VfsExecutable) {
|
2015-12-11 11:08:47 +00:00
|
|
|
cfile.setPermissions(cfile.permissions()
|
|
|
|
| QFile::ExeUser | QFile::ExeGroup | QFile::ExeOther);
|
|
|
|
} else {
|
|
|
|
cfile.setPermissions(cfile.permissions()
|
|
|
|
& ~(QFile::ExeUser | QFile::ExeGroup | QFile::ExeOther));
|
|
|
|
}
|
2013-05-29 18:18:51 +00:00
|
|
|
return true;
|
2015-12-11 11:08:47 +00:00
|
|
|
}
|
2013-05-29 18:18:51 +00:00
|
|
|
cfile.close();
|
|
|
|
}
|
|
|
|
if (!cfile.open(mode | QIODevice::WriteOnly | QIODevice::Text)) {
|
|
|
|
*errStr = cfile.errorString();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
cfile.write(bytes);
|
|
|
|
cfile.close();
|
|
|
|
if (cfile.error() != QFile::NoError) {
|
|
|
|
*errStr = cfile.errorString();
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-24 17:30:24 +00:00
|
|
|
if (flags & VfsExecutable)
|
2015-12-11 11:08:47 +00:00
|
|
|
cfile.setPermissions(cfile.permissions()
|
|
|
|
| QFile::ExeUser | QFile::ExeGroup | QFile::ExeOther);
|
2013-05-29 18:18:51 +00:00
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-10-24 17:30:24 +00:00
|
|
|
QMakeVfs::ReadResult QMakeVfs::readFile(int id, QString *contents, QString *errStr)
|
2013-05-29 18:18:51 +00:00
|
|
|
{
|
|
|
|
#ifndef PROEVALUATOR_FULL
|
|
|
|
# ifdef PROEVALUATOR_THREAD_SAFE
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
# endif
|
2016-10-24 17:30:24 +00:00
|
|
|
auto it = m_files.constFind(id);
|
2013-05-29 18:18:51 +00:00
|
|
|
if (it != m_files.constEnd()) {
|
|
|
|
if (it->constData() == m_magicMissing.constData()) {
|
|
|
|
*errStr = fL1S("No such file or directory");
|
2016-10-28 18:10:40 +00:00
|
|
|
return ReadNotFound;
|
2013-05-29 18:18:51 +00:00
|
|
|
}
|
|
|
|
if (it->constData() != m_magicExisting.constData()) {
|
|
|
|
*contents = *it;
|
2016-10-28 18:10:40 +00:00
|
|
|
return ReadOk;
|
2013-05-29 18:18:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-10-24 17:30:24 +00:00
|
|
|
QFile file(fileNameForId(id));
|
2013-05-29 18:18:51 +00:00
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
2016-10-28 18:10:40 +00:00
|
|
|
if (!file.exists()) {
|
2013-05-29 18:18:51 +00:00
|
|
|
#ifndef PROEVALUATOR_FULL
|
2016-10-24 17:30:24 +00:00
|
|
|
m_files[id] = m_magicMissing;
|
2013-05-29 18:18:51 +00:00
|
|
|
#endif
|
2016-10-28 18:10:40 +00:00
|
|
|
*errStr = fL1S("No such file or directory");
|
|
|
|
return ReadNotFound;
|
|
|
|
}
|
|
|
|
*errStr = file.errorString();
|
|
|
|
return ReadOtherError;
|
2013-05-29 18:18:51 +00:00
|
|
|
}
|
|
|
|
#ifndef PROEVALUATOR_FULL
|
2016-10-24 17:30:24 +00:00
|
|
|
m_files[id] = m_magicExisting;
|
2013-05-29 18:18:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
QByteArray bcont = file.readAll();
|
|
|
|
if (bcont.startsWith("\xef\xbb\xbf")) {
|
|
|
|
// UTF-8 BOM will cause subtle errors
|
|
|
|
*errStr = fL1S("Unexpected UTF-8 BOM");
|
2016-10-28 18:10:40 +00:00
|
|
|
return ReadOtherError;
|
2013-05-29 18:18:51 +00:00
|
|
|
}
|
2016-11-30 17:26:44 +00:00
|
|
|
*contents =
|
2018-10-02 10:59:24 +00:00
|
|
|
#if QT_CONFIG(textcodec)
|
2016-11-30 17:26:44 +00:00
|
|
|
m_textCodec ? m_textCodec->toUnicode(bcont) :
|
|
|
|
#endif
|
|
|
|
QString::fromLocal8Bit(bcont);
|
2016-10-28 18:10:40 +00:00
|
|
|
return ReadOk;
|
2013-05-29 18:18:51 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 17:30:24 +00:00
|
|
|
bool QMakeVfs::exists(const QString &fn, VfsFlags flags)
|
2013-05-29 18:18:51 +00:00
|
|
|
{
|
|
|
|
#ifndef PROEVALUATOR_FULL
|
|
|
|
# ifdef PROEVALUATOR_THREAD_SAFE
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
# endif
|
2016-10-24 17:30:24 +00:00
|
|
|
int id = idForFileName(fn, flags);
|
|
|
|
auto it = m_files.constFind(id);
|
2013-05-29 18:18:51 +00:00
|
|
|
if (it != m_files.constEnd())
|
|
|
|
return it->constData() != m_magicMissing.constData();
|
2016-10-24 17:30:24 +00:00
|
|
|
#else
|
|
|
|
Q_UNUSED(flags)
|
2013-05-29 18:18:51 +00:00
|
|
|
#endif
|
2016-10-24 12:58:11 +00:00
|
|
|
bool ex = IoUtils::fileType(fn) == IoUtils::FileIsRegular;
|
2013-05-29 18:18:51 +00:00
|
|
|
#ifndef PROEVALUATOR_FULL
|
2016-10-24 17:30:24 +00:00
|
|
|
m_files[id] = ex ? m_magicExisting : m_magicMissing;
|
2013-05-29 18:18:51 +00:00
|
|
|
#endif
|
|
|
|
return ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef PROEVALUATOR_FULL
|
|
|
|
// This should be called when the sources may have changed (e.g., VCS update).
|
|
|
|
void QMakeVfs::invalidateCache()
|
|
|
|
{
|
|
|
|
# ifdef PROEVALUATOR_THREAD_SAFE
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
# endif
|
2016-10-24 17:30:24 +00:00
|
|
|
auto it = m_files.begin(), eit = m_files.end();
|
2013-05-29 18:18:51 +00:00
|
|
|
while (it != eit) {
|
|
|
|
if (it->constData() == m_magicMissing.constData()
|
|
|
|
||it->constData() == m_magicExisting.constData())
|
|
|
|
it = m_files.erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should be called when generated files may have changed (e.g., actual build).
|
|
|
|
void QMakeVfs::invalidateContents()
|
|
|
|
{
|
|
|
|
# ifdef PROEVALUATOR_THREAD_SAFE
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
# endif
|
|
|
|
m_files.clear();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-02 10:59:24 +00:00
|
|
|
#if QT_CONFIG(textcodec)
|
2016-11-30 17:26:44 +00:00
|
|
|
void QMakeVfs::setTextCodec(const QTextCodec *textCodec)
|
|
|
|
{
|
|
|
|
m_textCodec = textCodec;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-05-29 18:18:51 +00:00
|
|
|
QT_END_NAMESPACE
|