forked from qt-creator/qt-creator
make write_file() capable of making files (not) executable
Change-Id: I9ca96bc3408160261781697a3471c1f446c86c3a Reviewed-by: Joerg Bornemann <joerg.bornemann@theqtcompany.com> (cherry picked from qtbase/57ca8d2698f1eaf30f1110ba47445c04d2bcd0f7) Reviewed-by: Jake Petroules <jake.petroules@qt.io>
This commit is contained in:
@@ -350,10 +350,10 @@ static QMakeEvaluator::VisitReturn parseJsonInto(const QByteArray &json, const Q
|
|||||||
|
|
||||||
QMakeEvaluator::VisitReturn
|
QMakeEvaluator::VisitReturn
|
||||||
QMakeEvaluator::writeFile(const QString &ctx, const QString &fn, QIODevice::OpenMode mode,
|
QMakeEvaluator::writeFile(const QString &ctx, const QString &fn, QIODevice::OpenMode mode,
|
||||||
const QString &contents)
|
bool exe, const QString &contents)
|
||||||
{
|
{
|
||||||
QString errStr;
|
QString errStr;
|
||||||
if (!m_vfs->writeFile(fn, mode, contents, &errStr)) {
|
if (!m_vfs->writeFile(fn, mode, exe, contents, &errStr)) {
|
||||||
evalError(fL1S("Cannot write %1file %2: %3")
|
evalError(fL1S("Cannot write %1file %2: %3")
|
||||||
.arg(ctx, QDir::toNativeSeparators(fn), errStr));
|
.arg(ctx, QDir::toNativeSeparators(fn), errStr));
|
||||||
return ReturnFalse;
|
return ReturnFalse;
|
||||||
@@ -1541,22 +1541,33 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::evaluateBuiltinConditional(
|
|||||||
}
|
}
|
||||||
case T_WRITE_FILE: {
|
case T_WRITE_FILE: {
|
||||||
if (args.count() > 3) {
|
if (args.count() > 3) {
|
||||||
evalError(fL1S("write_file(name, [content var, [append]]) requires one to three arguments."));
|
evalError(fL1S("write_file(name, [content var, [append] [exe]]) requires one to three arguments."));
|
||||||
return ReturnFalse;
|
return ReturnFalse;
|
||||||
}
|
}
|
||||||
QIODevice::OpenMode mode = QIODevice::Truncate;
|
QIODevice::OpenMode mode = QIODevice::Truncate;
|
||||||
|
bool exe = false;
|
||||||
QString contents;
|
QString contents;
|
||||||
if (args.count() >= 2) {
|
if (args.count() >= 2) {
|
||||||
const ProStringList &vals = values(args.at(1).toKey());
|
const ProStringList &vals = values(args.at(1).toKey());
|
||||||
if (!vals.isEmpty())
|
if (!vals.isEmpty())
|
||||||
contents = vals.join(QLatin1Char('\n')) + QLatin1Char('\n');
|
contents = vals.join(QLatin1Char('\n')) + QLatin1Char('\n');
|
||||||
if (args.count() >= 3)
|
if (args.count() >= 3) {
|
||||||
if (!args.at(2).toQString(m_tmp1).compare(fL1S("append"), Qt::CaseInsensitive))
|
foreach (const ProString &opt, split_value_list(args.at(2).toQString(m_tmp2))) {
|
||||||
mode = QIODevice::Append;
|
opt.toQString(m_tmp3);
|
||||||
|
if (m_tmp3 == QLatin1String("append")) {
|
||||||
|
mode = QIODevice::Append;
|
||||||
|
} else if (m_tmp3 == QLatin1String("exe")) {
|
||||||
|
exe = true;
|
||||||
|
} else {
|
||||||
|
evalError(fL1S("write_file(): invalid flag %1.").arg(m_tmp3));
|
||||||
|
return ReturnFalse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
QString path = resolvePath(args.at(0).toQString(m_tmp1));
|
QString path = resolvePath(args.at(0).toQString(m_tmp1));
|
||||||
path.detach(); // make sure to not leak m_tmp1 into the map of written files.
|
path.detach(); // make sure to not leak m_tmp1 into the map of written files.
|
||||||
return writeFile(QString(), path, mode, contents);
|
return writeFile(QString(), path, mode, exe, contents);
|
||||||
}
|
}
|
||||||
case T_TOUCH: {
|
case T_TOUCH: {
|
||||||
if (args.count() != 2) {
|
if (args.count() != 2) {
|
||||||
@@ -1767,7 +1778,7 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::evaluateBuiltinConditional(
|
|||||||
valuesRef(ProKey("_QMAKE_STASH_")) << ProString(fn);
|
valuesRef(ProKey("_QMAKE_STASH_")) << ProString(fn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return writeFile(fL1S("cache "), fn, QIODevice::Append, varstr);
|
return writeFile(fL1S("cache "), fn, QIODevice::Append, false, varstr);
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
evalError(fL1S("Function '%1' is not implemented.").arg(function.toQString(m_tmp1)));
|
evalError(fL1S("Function '%1' is not implemented.").arg(function.toQString(m_tmp1)));
|
||||||
|
@@ -229,7 +229,7 @@ public:
|
|||||||
QMultiMap<int, ProString> &rootSet) const;
|
QMultiMap<int, ProString> &rootSet) const;
|
||||||
|
|
||||||
VisitReturn writeFile(const QString &ctx, const QString &fn, QIODevice::OpenMode mode,
|
VisitReturn writeFile(const QString &ctx, const QString &fn, QIODevice::OpenMode mode,
|
||||||
const QString &contents);
|
bool exe, const QString &contents);
|
||||||
#ifndef QT_BOOTSTRAPPED
|
#ifndef QT_BOOTSTRAPPED
|
||||||
void runProcess(QProcess *proc, const QString &command) const;
|
void runProcess(QProcess *proc, const QString &command) const;
|
||||||
#endif
|
#endif
|
||||||
|
@@ -44,8 +44,8 @@ QMakeVfs::QMakeVfs()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QMakeVfs::writeFile(const QString &fn, QIODevice::OpenMode mode, const QString &contents,
|
bool QMakeVfs::writeFile(const QString &fn, QIODevice::OpenMode mode, bool exe,
|
||||||
QString *errStr)
|
const QString &contents, QString *errStr)
|
||||||
{
|
{
|
||||||
#ifndef PROEVALUATOR_FULL
|
#ifndef PROEVALUATOR_FULL
|
||||||
# ifdef PROEVALUATOR_THREAD_SAFE
|
# ifdef PROEVALUATOR_THREAD_SAFE
|
||||||
@@ -57,6 +57,7 @@ bool QMakeVfs::writeFile(const QString &fn, QIODevice::OpenMode mode, const QStr
|
|||||||
else
|
else
|
||||||
*cont = contents;
|
*cont = contents;
|
||||||
Q_UNUSED(errStr)
|
Q_UNUSED(errStr)
|
||||||
|
Q_UNUSED(exe)
|
||||||
return true;
|
return true;
|
||||||
#else
|
#else
|
||||||
QFileInfo qfi(fn);
|
QFileInfo qfi(fn);
|
||||||
@@ -67,8 +68,16 @@ bool QMakeVfs::writeFile(const QString &fn, QIODevice::OpenMode mode, const QStr
|
|||||||
QByteArray bytes = contents.toLocal8Bit();
|
QByteArray bytes = contents.toLocal8Bit();
|
||||||
QFile cfile(fn);
|
QFile cfile(fn);
|
||||||
if (!(mode & QIODevice::Append) && cfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
if (!(mode & QIODevice::Append) && cfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||||
if (cfile.readAll() == bytes)
|
if (cfile.readAll() == bytes) {
|
||||||
|
if (exe) {
|
||||||
|
cfile.setPermissions(cfile.permissions()
|
||||||
|
| QFile::ExeUser | QFile::ExeGroup | QFile::ExeOther);
|
||||||
|
} else {
|
||||||
|
cfile.setPermissions(cfile.permissions()
|
||||||
|
& ~(QFile::ExeUser | QFile::ExeGroup | QFile::ExeOther));
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
cfile.close();
|
cfile.close();
|
||||||
}
|
}
|
||||||
if (!cfile.open(mode | QIODevice::WriteOnly | QIODevice::Text)) {
|
if (!cfile.open(mode | QIODevice::WriteOnly | QIODevice::Text)) {
|
||||||
@@ -81,6 +90,9 @@ bool QMakeVfs::writeFile(const QString &fn, QIODevice::OpenMode mode, const QStr
|
|||||||
*errStr = cfile.errorString();
|
*errStr = cfile.errorString();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (exe)
|
||||||
|
cfile.setPermissions(cfile.permissions()
|
||||||
|
| QFile::ExeUser | QFile::ExeGroup | QFile::ExeOther);
|
||||||
return true;
|
return true;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@@ -43,7 +43,7 @@ class QMAKE_EXPORT QMakeVfs
|
|||||||
public:
|
public:
|
||||||
QMakeVfs();
|
QMakeVfs();
|
||||||
|
|
||||||
bool writeFile(const QString &fn, QIODevice::OpenMode mode, const QString &contents, QString *errStr);
|
bool writeFile(const QString &fn, QIODevice::OpenMode mode, bool exe, const QString &contents, QString *errStr);
|
||||||
bool readFile(const QString &fn, QString *contents, QString *errStr);
|
bool readFile(const QString &fn, QString *contents, QString *errStr);
|
||||||
bool exists(const QString &fn);
|
bool exists(const QString &fn);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user