CQtDeployer/Deploy/filemanager.cpp

369 lines
11 KiB
C++
Raw Normal View History

2019-09-23 16:46:57 +03:00
/*
2019-12-08 13:57:20 +03:00
* Copyright (C) 2018-2020 QuasarApp.
2019-09-23 16:46:57 +03:00
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
2019-09-04 12:01:33 +03:00
#include "filemanager.h"
2019-09-03 18:15:05 +03:00
#include <QDir>
#include <quasarapp.h>
2019-09-25 12:32:05 +03:00
#include "configparser.h"
2019-09-03 18:15:05 +03:00
#include "deploycore.h"
2019-09-03 19:36:06 +03:00
#include <QProcess>
2019-09-03 18:15:05 +03:00
#include <fstream>
2019-09-04 12:01:33 +03:00
FileManager::FileManager() {
2019-09-03 18:15:05 +03:00
}
2019-09-04 12:01:33 +03:00
bool FileManager::initDir(const QString &path) {
2019-09-03 18:15:05 +03:00
if (!QFileInfo::exists(path)) {
2019-09-03 19:36:06 +03:00
addToDeployed(path);
2019-09-03 18:15:05 +03:00
if (!QDir().mkpath(path)) {
return false;
}
}
return true;
}
2019-09-04 12:01:33 +03:00
QSet<QString> FileManager::getDeployedFiles() const {
2019-09-03 19:36:06 +03:00
return _deployedFiles;
}
2019-09-04 12:01:33 +03:00
QStringList FileManager::getDeployedFilesStringList() const {
2019-09-03 19:36:06 +03:00
return _deployedFiles.toList();
}
2019-09-04 12:01:33 +03:00
void FileManager::loadDeployemendFiles(const QString &targetDir) {
auto settings = QuasarAppUtils::Settings::get();
2019-09-28 16:44:39 +03:00
if (targetDir.isEmpty())
return;
2019-09-23 17:59:36 +03:00
QStringList deployedFiles = settings->getValue(targetDir, "").toStringList();
2019-09-04 12:01:33 +03:00
// _deployedFiles.clear();
_deployedFiles.unite(QSet<QString>::fromList(deployedFiles));
2019-09-03 19:36:06 +03:00
}
2019-09-04 12:01:33 +03:00
bool FileManager::addToDeployed(const QString& path) {
2019-09-03 19:36:06 +03:00
auto info = QFileInfo(path);
if (info.isFile() || !info.exists()) {
_deployedFiles += info.absoluteFilePath();
auto completeSufix = info.completeSuffix();
if (info.isFile() && (completeSufix.isEmpty() || completeSufix.toLower() == "run"
|| completeSufix.toLower() == "sh")) {
if (!QFile::setPermissions(path, static_cast<QFile::Permission>(0x7777))) {
QuasarAppUtils::Params::verboseLog("permishens set fail", QuasarAppUtils::Warning);
}
}
}
return true;
}
2019-09-04 12:01:33 +03:00
void FileManager::saveDeploymendFiles(const QString& targetDir) {
auto settings = QuasarAppUtils::Settings::get();
settings->setValue(targetDir, getDeployedFilesStringList());
}
bool FileManager::strip(const QString &dir) const {
2019-09-03 19:36:06 +03:00
#ifdef Q_OS_WIN
Q_UNUSED(dir);
return true;
#else
QFileInfo info(dir);
2019-09-03 18:15:05 +03:00
2019-09-03 19:36:06 +03:00
if (!info.exists()) {
QuasarAppUtils::Params::verboseLog("dir not exits!");
return false;
}
if (info.isDir()) {
QDir d(dir);
auto list = d.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
bool res = false;
for (auto &&i : list) {
res = strip(i.absoluteFilePath()) || res;
}
return res;
} else {
auto sufix = info.completeSuffix();
if (!sufix.contains("so") && !sufix.contains("dll")) {
2019-09-04 12:01:33 +03:00
return true;
2019-09-03 19:36:06 +03:00
}
QProcess P;
P.setProgram("strip");
P.setArguments(QStringList() << info.absoluteFilePath());
P.start();
if (!P.waitForStarted())
return false;
if (!P.waitForFinished())
return false;
return P.exitCode() == 0;
}
#endif
}
2019-09-04 12:01:33 +03:00
bool FileManager::fileActionPrivate(const QString &file, const QString &target,
2019-09-03 19:36:06 +03:00
QStringList *masks, bool isMove) {
auto info = QFileInfo(file);
2019-09-03 18:15:05 +03:00
bool copy = !masks;
if (masks) {
for (auto mask : *masks) {
if (info.absoluteFilePath().contains(mask)) {
copy = true;
break;
}
}
}
if (!copy) {
QuasarAppUtils::Params::verboseLog(((isMove)? "skip move :": "skip copy :" + file));
return false;
}
auto name = info.fileName();
info.setFile(target + QDir::separator() + name);
if (!initDir(info.absolutePath())) {
return false;
}
if (QFileInfo(file).absoluteFilePath() ==
QFileInfo(target + QDir::separator() + name).absoluteFilePath()) {
return true;
}
2019-09-12 09:38:30 +03:00
if (!QuasarAppUtils::Params::isEndable("noOverwrite") &&
2019-09-03 18:15:05 +03:00
info.exists() && !removeFile( target + QDir::separator() + name)) {
return false;
}
qInfo() << ((isMove)? "move :": "copy :") << file;
QFile sourceFile(file);
if (!((isMove)?
sourceFile.rename(target + QDir::separator() + name):
sourceFile.copy(target + QDir::separator() + name))) {
QuasarAppUtils::Params::verboseLog("Qt Operation fail " + file + " >> " + target + QDir::separator() + name +
" Qt error: " + sourceFile.errorString(),
QuasarAppUtils::Warning);
2019-09-12 09:38:30 +03:00
if (!(QuasarAppUtils::Params::isEndable("noOverwrite") &&
QFileInfo(target + QDir::separator() + name).exists())) {
std::ifstream src(file.toStdString(),
std::ios::binary);
std::ofstream dst((target + QDir::separator() + name).toStdString(),
std::ios::binary);
dst << src.rdbuf();
if (!QFileInfo::exists(target + QDir::separator() + name)) {
QuasarAppUtils::Params::verboseLog("std Operation fail file not copied. "
"Сheck if you have access to the target dir",
QuasarAppUtils::Error);
return false;
}
} else {
if (QFileInfo(target + QDir::separator() + name).exists()) {
qInfo() << target + QDir::separator() + name << " already exists!";
return true;
}
2019-09-03 18:15:05 +03:00
return false;
2019-09-03 18:15:05 +03:00
}
}
2019-09-03 19:36:06 +03:00
addToDeployed(target + QDir::separator() + name);
2019-09-03 18:15:05 +03:00
return true;
}
2019-09-04 12:01:33 +03:00
bool FileManager::removeFile(const QString &file) {
2019-09-03 18:15:05 +03:00
return removeFile(QFileInfo (file));
}
2019-09-25 12:32:05 +03:00
bool FileManager::smartCopyFile(const QString &file, const QString &target, QStringList *mask) {
auto config = DeployCore::_config;
2019-12-12 22:50:04 +03:00
if (file.contains(config->getTargetDir())) {
2019-09-03 18:15:05 +03:00
if (!moveFile(file, target, mask)) {
QuasarAppUtils::Params::verboseLog(" file not moved! try copy");
if (!copyFile(file, target, mask)) {
qCritical() << "not copy target to bin dir " << file;
return false;
}
}
2019-09-03 18:15:05 +03:00
} else {
if (!copyFile(file, target, mask)) {
qCritical() << "not copy target to bin dir " << file;
return false;
}
2019-09-03 18:15:05 +03:00
}
return true;
}
2019-09-04 12:01:33 +03:00
bool FileManager::moveFile(const QString &file, const QString &target, QStringList *masks) {
2019-09-03 18:15:05 +03:00
return fileActionPrivate(file, target, masks, true);
}
2019-09-04 12:01:33 +03:00
bool FileManager::copyFolder(const QString &from, const QString &to, const QStringList &filter,
2019-09-03 18:15:05 +03:00
QStringList *listOfCopiedItems, QStringList *mask) {
QDir fromDir(from);
auto list = fromDir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries);
for (auto &&item : list) {
if (QFileInfo(item).isDir()) {
copyFolder(item.absoluteFilePath(), to + "/" + item.fileName(), filter, listOfCopiedItems, mask);
} else {
QString skipFilter = "";
for (auto && i: filter) {
if (item.fileName().contains(i)) {
skipFilter = i;
break;
}
}
if (!skipFilter.isEmpty()) {
QuasarAppUtils::Params::verboseLog(
item.absoluteFilePath() + " ignored by filter " + skipFilter,
QuasarAppUtils::VerboseLvl::Info);
continue;
}
2019-10-15 13:35:50 +03:00
auto config = DeployCore::_config;
LibInfo info;
info.setName(item.fileName());
info.setPath(item.absolutePath());
2019-10-17 15:42:44 +03:00
info.setPlatform(GeneralFile);
2019-10-15 13:35:50 +03:00
if (config)
if (auto rule = config->ignoreList.isIgnore(info)) {
QuasarAppUtils::Params::verboseLog(
item.absoluteFilePath() + " ignored by rule " + rule->label,
QuasarAppUtils::VerboseLvl::Info);
continue;
}
2019-09-03 18:15:05 +03:00
if (!copyFile(item.absoluteFilePath(), to , mask)) {
QuasarAppUtils::Params::verboseLog(
"not copied file " + to + "/" + item.fileName(),
QuasarAppUtils::VerboseLvl::Warning);
continue;
}
if (listOfCopiedItems) {
*listOfCopiedItems << to + "/" + item.fileName();
}
}
}
return true;
}
2019-09-04 12:01:33 +03:00
void FileManager::clear(const QString& targetDir, bool force) {
2019-09-03 18:15:05 +03:00
qInfo() << "clear start!";
if (force) {
qInfo() << "clear force! " << targetDir;
if (QDir(targetDir).removeRecursively()) {
return;
}
QuasarAppUtils::Params::verboseLog("Remove target Dir fail, try remove old deployemend files",
QuasarAppUtils::Warning);
}
QMap<int, QFileInfo> sortedOldData;
2019-09-03 19:36:06 +03:00
for (auto& i : _deployedFiles) {
2019-09-03 18:15:05 +03:00
sortedOldData.insertMulti(i.size(), QFileInfo(i));
}
for (auto it = sortedOldData.end(); it != sortedOldData.begin(); --it) {
auto index = it - 1;
2019-09-04 17:07:38 +03:00
if (!index.value().exists()) {
continue;
}
2019-09-03 18:15:05 +03:00
if (index.value().isFile()) {
if (removeFile(index.value())) {
qInfo() << "Remove " << index.value().absoluteFilePath() << " becouse it is deployed file";
}
} else {
QDir qdir(index.value().absoluteFilePath());
if (!qdir.entryList(QDir::NoDotAndDotDot | QDir::AllEntries).count()) {
qdir.removeRecursively();
qInfo() << "Remove " << index.value().absoluteFilePath() << " becouse it is empty";
}
}
}
2019-09-03 19:36:06 +03:00
_deployedFiles.clear();
2019-09-03 18:15:05 +03:00
}
2019-09-25 12:32:05 +03:00
void FileManager::copyLibs(const QStringList &files) {
auto config = DeployCore::_config;
2019-09-03 18:15:05 +03:00
for (auto file : files) {
QFileInfo target(file);
2019-09-25 12:32:05 +03:00
if (!smartCopyFile(file, DeployCore::_config->targetDir + config->distroStruct.getLibOutDir())) {
2019-09-03 18:15:05 +03:00
QuasarAppUtils::Params::verboseLog(file + " not copied");
}
}
}
2019-09-04 12:01:33 +03:00
bool FileManager::copyFile(const QString &file, const QString &target,
2019-09-03 18:15:05 +03:00
QStringList *masks) {
return fileActionPrivate(file, target, masks, false);
}
2019-09-04 12:01:33 +03:00
bool FileManager::removeFile(const QFileInfo &file) {
2019-09-03 18:15:05 +03:00
if (!QFile::remove(file.absoluteFilePath())) {
QuasarAppUtils::Params::verboseLog("Qt Operation fail (remove file) " + file.absoluteFilePath(),
QuasarAppUtils::Warning);
if (remove(file.absoluteFilePath().toLatin1())) {
QuasarAppUtils::Params::verboseLog("std Operation fail file not removed." + file.absoluteFilePath(),
QuasarAppUtils::Error);
return false;
}
}
return true;
}