forked from qt-creator/qt-creator
Utils: Implement FilePath::rename()
And uses it in CMake's fileapi reader. Change-Id: I9e719aa4b253eaca17c6b304eab5e7268fcfab29 Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -1415,6 +1415,15 @@ bool FilePath::copyFile(const FilePath &target) const
|
|||||||
return QFile::copy(path(), target.path());
|
return QFile::copy(path(), target.path());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool FilePath::renameFile(const FilePath &target) const
|
||||||
|
{
|
||||||
|
if (needsDevice()) {
|
||||||
|
QTC_ASSERT(s_deviceHooks.renameFile, return false);
|
||||||
|
return s_deviceHooks.renameFile(*this, target);
|
||||||
|
}
|
||||||
|
return QFile::rename(path(), target.path());
|
||||||
|
}
|
||||||
|
|
||||||
QTextStream &operator<<(QTextStream &s, const FilePath &fn)
|
QTextStream &operator<<(QTextStream &s, const FilePath &fn)
|
||||||
{
|
{
|
||||||
return s << fn.toString();
|
return s << fn.toString();
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ public:
|
|||||||
std::function<bool(const FilePath &)> exists;
|
std::function<bool(const FilePath &)> exists;
|
||||||
std::function<bool(const FilePath &)> removeFile;
|
std::function<bool(const FilePath &)> removeFile;
|
||||||
std::function<bool(const FilePath &, const FilePath &)> copyFile;
|
std::function<bool(const FilePath &, const FilePath &)> copyFile;
|
||||||
|
std::function<bool(const FilePath &, const FilePath &)> renameFile;
|
||||||
std::function<FilePath(const FilePath &)> searchInPath;
|
std::function<FilePath(const FilePath &)> searchInPath;
|
||||||
std::function<QList<FilePath>(const FilePath &, const QStringList &, QDir::Filters)> dirEntries;
|
std::function<QList<FilePath>(const FilePath &, const QStringList &, QDir::Filters)> dirEntries;
|
||||||
std::function<QByteArray(const FilePath &, int)> fileContents;
|
std::function<QByteArray(const FilePath &, int)> fileContents;
|
||||||
@@ -167,6 +168,7 @@ public:
|
|||||||
QFile::Permissions permissions() const;
|
QFile::Permissions permissions() const;
|
||||||
bool removeFile() const;
|
bool removeFile() const;
|
||||||
bool copyFile(const FilePath &target) const;
|
bool copyFile(const FilePath &target) const;
|
||||||
|
bool renameFile(const FilePath &target) const;
|
||||||
|
|
||||||
Qt::CaseSensitivity caseSensitivity() const;
|
Qt::CaseSensitivity caseSensitivity() const;
|
||||||
|
|
||||||
|
|||||||
@@ -311,8 +311,7 @@ void FileApiReader::makeBackupConfiguration(bool store)
|
|||||||
if (reply.exists()) {
|
if (reply.exists()) {
|
||||||
if (replyPrev.exists())
|
if (replyPrev.exists())
|
||||||
FileUtils::removeRecursively(replyPrev);
|
FileUtils::removeRecursively(replyPrev);
|
||||||
QDir dir;
|
if (!reply.renameFile(replyPrev))
|
||||||
if (!dir.rename(reply.toString(), replyPrev.toString()))
|
|
||||||
Core::MessageManager::writeFlashing(tr("Failed to rename %1 to %2.")
|
Core::MessageManager::writeFlashing(tr("Failed to rename %1 to %2.")
|
||||||
.arg(reply.toString(), replyPrev.toString()));
|
.arg(reply.toString(), replyPrev.toString()));
|
||||||
|
|
||||||
|
|||||||
@@ -887,6 +887,23 @@ bool DockerDevice::copyFile(const FilePath &filePath, const FilePath &target) co
|
|||||||
return exitCode == 0;
|
return exitCode == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool DockerDevice::renameFile(const FilePath &filePath, const FilePath &target) const
|
||||||
|
{
|
||||||
|
QTC_ASSERT(handlesFile(filePath), return false);
|
||||||
|
QTC_ASSERT(handlesFile(target), return false);
|
||||||
|
tryCreateLocalFileAccess();
|
||||||
|
if (hasLocalFileAccess()) {
|
||||||
|
const FilePath localAccess = mapToLocalAccess(filePath);
|
||||||
|
const FilePath localTarget = mapToLocalAccess(target);
|
||||||
|
const bool res = localAccess.renameFile(localTarget);
|
||||||
|
LOG("Move " << filePath.toUserOutput() << localAccess.toUserOutput() << localTarget << res);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
const CommandLine cmd("mv", {filePath.path(), target.path()});
|
||||||
|
const int exitCode = d->runSynchronously(cmd);
|
||||||
|
return exitCode == 0;
|
||||||
|
}
|
||||||
|
|
||||||
QDateTime DockerDevice::lastModified(const FilePath &filePath) const
|
QDateTime DockerDevice::lastModified(const FilePath &filePath) const
|
||||||
{
|
{
|
||||||
QTC_ASSERT(handlesFile(filePath), return {});
|
QTC_ASSERT(handlesFile(filePath), return {});
|
||||||
|
|||||||
@@ -83,6 +83,7 @@ public:
|
|||||||
bool exists(const Utils::FilePath &filePath) const override;
|
bool exists(const Utils::FilePath &filePath) const override;
|
||||||
bool removeFile(const Utils::FilePath &filePath) const override;
|
bool removeFile(const Utils::FilePath &filePath) const override;
|
||||||
bool copyFile(const Utils::FilePath &filePath, const Utils::FilePath &target) const override;
|
bool copyFile(const Utils::FilePath &filePath, const Utils::FilePath &target) const override;
|
||||||
|
bool renameFile(const Utils::FilePath &filePath, const Utils::FilePath &target) const override;
|
||||||
Utils::FilePath searchInPath(const Utils::FilePath &filePath) const override;
|
Utils::FilePath searchInPath(const Utils::FilePath &filePath) const override;
|
||||||
QList<Utils::FilePath> directoryEntries(const Utils::FilePath &filePath,
|
QList<Utils::FilePath> directoryEntries(const Utils::FilePath &filePath,
|
||||||
const QStringList &nameFilters,
|
const QStringList &nameFilters,
|
||||||
|
|||||||
@@ -435,6 +435,12 @@ DeviceManager::DeviceManager(bool isInstance) : d(std::make_unique<DeviceManager
|
|||||||
return device->copyFile(filePath, target);
|
return device->copyFile(filePath, target);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
deviceHooks.renameFile = [](const FilePath &filePath, const FilePath &target) {
|
||||||
|
auto device = DeviceManager::deviceForPath(filePath);
|
||||||
|
QTC_ASSERT(device, return false);
|
||||||
|
return device->renameFile(filePath, target);
|
||||||
|
};
|
||||||
|
|
||||||
deviceHooks.searchInPath = [](const FilePath &filePath) {
|
deviceHooks.searchInPath = [](const FilePath &filePath) {
|
||||||
auto device = DeviceManager::deviceForPath(filePath);
|
auto device = DeviceManager::deviceForPath(filePath);
|
||||||
QTC_ASSERT(device, return FilePath{});
|
QTC_ASSERT(device, return FilePath{});
|
||||||
|
|||||||
@@ -289,6 +289,14 @@ bool IDevice::copyFile(const FilePath &filePath, const FilePath &target) const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool IDevice::renameFile(const FilePath &filePath, const FilePath &target) const
|
||||||
|
{
|
||||||
|
Q_UNUSED(filePath);
|
||||||
|
Q_UNUSED(target);
|
||||||
|
QTC_CHECK(false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
FilePath IDevice::searchInPath(const FilePath &filePath) const
|
FilePath IDevice::searchInPath(const FilePath &filePath) const
|
||||||
{
|
{
|
||||||
return Environment::systemEnvironment().searchInPath(filePath.path());
|
return Environment::systemEnvironment().searchInPath(filePath.path());
|
||||||
|
|||||||
@@ -246,6 +246,7 @@ public:
|
|||||||
virtual bool exists(const Utils::FilePath &filePath) const;
|
virtual bool exists(const Utils::FilePath &filePath) const;
|
||||||
virtual bool removeFile(const Utils::FilePath &filePath) const;
|
virtual bool removeFile(const Utils::FilePath &filePath) const;
|
||||||
virtual bool copyFile(const Utils::FilePath &filePath, const Utils::FilePath &target) const;
|
virtual bool copyFile(const Utils::FilePath &filePath, const Utils::FilePath &target) const;
|
||||||
|
virtual bool renameFile(const Utils::FilePath &filePath, const Utils::FilePath &target) const;
|
||||||
virtual Utils::FilePath searchInPath(const Utils::FilePath &filePath) const;
|
virtual Utils::FilePath searchInPath(const Utils::FilePath &filePath) const;
|
||||||
virtual QList<Utils::FilePath> directoryEntries(const Utils::FilePath &filePath,
|
virtual QList<Utils::FilePath> directoryEntries(const Utils::FilePath &filePath,
|
||||||
const QStringList &nameFilters,
|
const QStringList &nameFilters,
|
||||||
|
|||||||
Reference in New Issue
Block a user