CQtDeployer/Deploy/filemanager.cpp

333 lines
9.7 KiB
C++
Raw Normal View History

2019-09-04 12:01:33 +03:00
#include "filemanager.h"
2019-09-03 18:15:05 +03:00
#include <QDir>
#include <quasarapp.h>
#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();
QStringList deployedFiles = settings->getValue(targetDir, {}).toStringList();
2019-09-03 19:36:06 +03:00
_deployedFiles.clear();
2019-09-04 12:01:33 +03:00
_deployedFiles.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;
}
if (QuasarAppUtils::Params::isEndable("always-overwrite") &&
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);
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;
}
}
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-04 12:01:33 +03:00
bool FileManager::smartCopyFile(const QString &file, const QString &target,
2019-09-03 18:15:05 +03:00
const QString& targetDir, QStringList *mask) {
if (file.contains(targetDir)) {
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;
};
};
} else {
if (!copyFile(file, target, mask)) {
qCritical() << "not copy target to bin dir " << file;
return false;
};
}
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;
}
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);
}
2019-09-04 17:07:38 +03:00
;
2019-09-03 18:15:05 +03:00
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-04 12:01:33 +03:00
void FileManager::copyFiles(const QStringList &files, const QString& targetDir) {
2019-09-03 18:15:05 +03:00
for (auto file : files) {
QFileInfo target(file);
auto targetPath = targetDir + QDir::separator() + "lib";
if (target.completeSuffix().contains("dll", Qt::CaseInsensitive) ||
target.completeSuffix().contains("exe", Qt::CaseInsensitive)) {
targetPath = targetDir;
}
if (!smartCopyFile(file, targetPath, targetDir)) {
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;
}