forked from qt-creator/qt-creator
Wizards: Keep file permissions intact when generating files
Useful e.g. for wizards that create scripts that should be executable. Change-Id: I0437e81008a70137d1c9d24ae562409297f2b734 Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -12,6 +12,8 @@
|
|||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
using namespace Utils;
|
using namespace Utils;
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
@@ -34,7 +36,12 @@ class GeneratedFilePrivate : public QSharedData
|
|||||||
public:
|
public:
|
||||||
GeneratedFilePrivate() = default;
|
GeneratedFilePrivate() = default;
|
||||||
explicit GeneratedFilePrivate(const FilePath &path);
|
explicit GeneratedFilePrivate(const FilePath &path);
|
||||||
|
|
||||||
|
bool writeContents(QString *errorMessage) const;
|
||||||
|
bool writePermissions(QString *errorMessage) const;
|
||||||
|
|
||||||
FilePath path;
|
FilePath path;
|
||||||
|
std::optional<QFile::Permissions> permissions;
|
||||||
QByteArray contents;
|
QByteArray contents;
|
||||||
Id editorId;
|
Id editorId;
|
||||||
bool binary = false;
|
bool binary = false;
|
||||||
@@ -93,6 +100,16 @@ void GeneratedFile::setFilePath(const FilePath &p)
|
|||||||
m_d->path = p;
|
m_d->path = p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GeneratedFile::setPermissions(QFileDevice::Permissions permissions)
|
||||||
|
{
|
||||||
|
m_d->permissions = permissions;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<QFileDevice::Permissions> GeneratedFile::permissions() const
|
||||||
|
{
|
||||||
|
return m_d->permissions;
|
||||||
|
}
|
||||||
|
|
||||||
QString GeneratedFile::contents() const
|
QString GeneratedFile::contents() const
|
||||||
{
|
{
|
||||||
return QString::fromUtf8(m_d->contents);
|
return QString::fromUtf8(m_d->contents);
|
||||||
@@ -133,6 +150,33 @@ void GeneratedFile::setEditorId(Id id)
|
|||||||
m_d->editorId = id;
|
m_d->editorId = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool GeneratedFilePrivate::writeContents(QString *errorMessage) const
|
||||||
|
{
|
||||||
|
if (binary) {
|
||||||
|
QIODevice::OpenMode flags = QIODevice::WriteOnly | QIODevice::Truncate;
|
||||||
|
FileSaver saver(path, flags);
|
||||||
|
saver.write(contents);
|
||||||
|
return saver.finalize(errorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
TextFileFormat format;
|
||||||
|
format.codec = EditorManager::defaultTextCodec();
|
||||||
|
format.lineTerminationMode = EditorManager::defaultLineEnding();
|
||||||
|
return format.writeFile(path, QString::fromUtf8(contents), errorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GeneratedFilePrivate::writePermissions(QString *errorMessage) const
|
||||||
|
{
|
||||||
|
if (!permissions)
|
||||||
|
return true;
|
||||||
|
if (!path.setPermissions(*permissions)) {
|
||||||
|
if (errorMessage)
|
||||||
|
*errorMessage = Tr::tr("Failed to set permissions.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool GeneratedFile::write(QString *errorMessage) const
|
bool GeneratedFile::write(QString *errorMessage) const
|
||||||
{
|
{
|
||||||
// Ensure the directory
|
// Ensure the directory
|
||||||
@@ -146,17 +190,9 @@ bool GeneratedFile::write(QString *errorMessage) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write out
|
// Write out
|
||||||
if (isBinary()) {
|
if (!m_d->writeContents(errorMessage))
|
||||||
QIODevice::OpenMode flags = QIODevice::WriteOnly | QIODevice::Truncate;
|
return false;
|
||||||
Utils::FileSaver saver(m_d->path, flags);
|
return m_d->writePermissions(errorMessage);
|
||||||
saver.write(m_d->contents);
|
|
||||||
return saver.finalize(errorMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
Utils::TextFileFormat format;
|
|
||||||
format.codec = EditorManager::defaultTextCodec();
|
|
||||||
format.lineTerminationMode = EditorManager::defaultLineEnding();
|
|
||||||
return format.writeFile(m_d->path, contents(), errorMessage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GeneratedFile::Attributes GeneratedFile::attributes() const
|
GeneratedFile::Attributes GeneratedFile::attributes() const
|
||||||
|
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include <utils/id.h>
|
#include <utils/id.h>
|
||||||
|
|
||||||
|
#include <QFile>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QSharedDataPointer>
|
#include <QSharedDataPointer>
|
||||||
|
|
||||||
@@ -44,6 +45,9 @@ public:
|
|||||||
void setFilePath(const Utils::FilePath &p);
|
void setFilePath(const Utils::FilePath &p);
|
||||||
Utils::FilePath filePath() const;
|
Utils::FilePath filePath() const;
|
||||||
|
|
||||||
|
void setPermissions(QFile::Permissions permissions);
|
||||||
|
std::optional<QFile::Permissions> permissions() const;
|
||||||
|
|
||||||
// Contents of the file (UTF8)
|
// Contents of the file (UTF8)
|
||||||
QString contents() const;
|
QString contents() const;
|
||||||
void setContents(const QString &c);
|
void setContents(const QString &c);
|
||||||
|
@@ -163,6 +163,7 @@ Core::GeneratedFile JsonWizardFileGenerator::generateFile(const File &file,
|
|||||||
return Core::GeneratedFile();
|
return Core::GeneratedFile();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
gf.setPermissions(file.source.permissions());
|
||||||
}
|
}
|
||||||
|
|
||||||
Core::GeneratedFile::Attributes attributes;
|
Core::GeneratedFile::Attributes attributes;
|
||||||
|
Reference in New Issue
Block a user