diff --git a/application/assets/deepin-logger b/application/assets/deepin-logger index 043fa01c..00c282de 100644 --- a/application/assets/deepin-logger +++ b/application/assets/deepin-logger @@ -4,8 +4,16 @@ para=$* CMDPATTERN="^-" +# 添加当前路径作为命令行最后一个参数,用来解决身份验证提权后,程序获取不到终端当前运行路径的问题。 +para+=" " +para+=$(pwd) + +# 添加当前用户名,解决身份验证提权后,homePath为/root的问题。 +para+=" " +para+=$USER + if [[ "$para" =~ $CMDPATTERN ]]; then pkexec deepin-log-viewer ${para} else deepin-log-viewer -h -fi +fi \ No newline at end of file diff --git a/application/logallexportthread.cpp b/application/logallexportthread.cpp index 9f1df3a0..e22d5ceb 100644 --- a/application/logallexportthread.cpp +++ b/application/logallexportthread.cpp @@ -123,7 +123,7 @@ void LogAllExportThread::run() int tolProcess = nCount + 10; int currentProcess = 1; emit updateTolProcess(tolProcess); - QString tmpPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/tmp/"; + QString tmpPath = Utils::getAppDataPath() + "/tmp/"; QDir dir(tmpPath); //鍒犻櫎涓存椂鐩綍 dir.removeRecursively(); @@ -182,7 +182,7 @@ void LogAllExportThread::run() QProcess procss; procss.setWorkingDirectory(tmpPath); QStringList arg = {"-c"}; - arg.append(QString("zip -r tmp.zip ./*;mv tmp.zip '%1'").arg(m_outfile)); + arg.append(QString("zip -r tmp.zip ./*;mv tmp.zip '%1';chmod 666 '%2'").arg(m_outfile).arg(m_outfile)); procss.start("/bin/bash", arg); procss.waitForFinished(-1); currentProcess += 9; diff --git a/application/logapplicationhelper.cpp b/application/logapplicationhelper.cpp index aaf4fb66..4e2c618b 100644 --- a/application/logapplicationhelper.cpp +++ b/application/logapplicationhelper.cpp @@ -126,7 +126,7 @@ void LogApplicationHelper::initOtherLog() for (QStringList iter : m_other_log_list_temp) { QString path = iter.at(1); if (path.startsWith("~/")) - path.replace(0, 1, QDir::homePath()); + path.replace(0, 1, Utils::homePath); m_other_log_list.append(QStringList() << iter.at(0) << path); } } @@ -160,7 +160,7 @@ void LogApplicationHelper::initCustomLog() for (QString iter : sList) { QString path = iter; if (path.startsWith("~/")) - path.replace(0, 1, QDir::homePath()); + path.replace(0, 1, Utils::homePath); //蹇界暐闈炴枃鏈枃浠跺拰涓嶅瓨鍦ㄧ殑鏂囦欢 if (!QFile::exists(path) || !Utils::isTextFileType(path)) { continue; @@ -189,7 +189,7 @@ void LogApplicationHelper::initCustomLog() for (QString iter : sList2) { QString path = iter; if (path.startsWith("~/")) - path.replace(0, 1, QDir::homePath()); + path.replace(0, 1, Utils::homePath); //蹇界暐闈炴枃鏈枃浠跺拰涓嶅瓨鍦ㄧ殑鏂囦欢 if (!QFile::exists(path) || !Utils::isTextFileType(path)) { continue; @@ -304,7 +304,7 @@ void LogApplicationHelper::createDesktopFiles() */ void LogApplicationHelper::createLogFiles() { - QString homePath = QDir::homePath(); + QString homePath = Utils::homePath; if (homePath.isEmpty()) { return; } diff --git a/application/logbackend.cpp b/application/logbackend.cpp index 9f413dc7..4908328b 100644 --- a/application/logbackend.cpp +++ b/application/logbackend.cpp @@ -39,6 +39,11 @@ LogBackend::LogBackend(QObject *parent) : QObject(parent) getLogTypes(); } +void LogBackend::setCmdWorkDir(const QString &dir) +{ + m_cmdWorkDir = dir; +} + void LogBackend::exportAllLogs(const QString &outDir) { PERF_PRINT_BEGIN("POINT-05", "export all logs"); @@ -53,7 +58,7 @@ void LogBackend::exportAllLogs(const QString &outDir) QString outPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); if (!outDir.isEmpty()) - outPath = outDir; + outPath = QDir::isRelativePath(outDir) ? (m_cmdWorkDir + "/" + outDir) : outDir; // 鑻ユ寚瀹氱洰褰曚笉瀛樺湪锛屽厛鍒涘缓鐩綍 QDir odir(outPath); @@ -62,8 +67,9 @@ void LogBackend::exportAllLogs(const QString &outDir) m_newDir = odir.exists(); if (m_newDir) outPath = odir.absolutePath(); - } else + } else { outPath = odir.absolutePath(); + } m_outPath = outPath; diff --git a/application/logbackend.h b/application/logbackend.h index 6840c0c2..5e9e090b 100644 --- a/application/logbackend.h +++ b/application/logbackend.h @@ -15,6 +15,9 @@ class LogBackend : public QObject ~LogBackend(); explicit LogBackend(QObject *parent = nullptr); + // 璁剧疆鍛戒护琛屽綋鍓嶅伐浣滅洰褰 + void setCmdWorkDir(const QString &dir); + // 瀵煎嚭鍏ㄩ儴鏃ュ織鍒版寚瀹氱洰褰 void exportAllLogs(const QString& outDir = ""); @@ -28,6 +31,7 @@ class LogBackend : public QObject QStringList m_logTypes; bool m_newDir {false}; QString m_outPath {""}; + QString m_cmdWorkDir {""}; }; #endif // LOGBACKEND_H diff --git a/application/logexportthread.cpp b/application/logexportthread.cpp index 43e80d31..d930e22c 100644 --- a/application/logexportthread.cpp +++ b/application/logexportthread.cpp @@ -3351,7 +3351,7 @@ bool LogExportThread::exportToXls(const QString &fileName, const QList &jList) { - QString tmpPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/tmp/"; + QString tmpPath = Utils::getAppDataPath() + "/tmp/"; QDir dir(tmpPath); //鍒犻櫎涓存椂鐩綍 dir.removeRecursively(); diff --git a/application/loglistview.cpp b/application/loglistview.cpp index 5131c579..4a20d389 100644 --- a/application/loglistview.cpp +++ b/application/loglistview.cpp @@ -8,6 +8,7 @@ #include "DebugTimeManager.h" #include "logapplicationhelper.h" #include "dbusproxy/dldbushandler.h" +#include "utils.h" #include #include @@ -449,7 +450,7 @@ void LogListView::showRightMenu(const QPoint &pos, bool isUsePoint) g_openForder->setEnabled(false); } - QString dirPath = QDir::homePath(); + QString dirPath = Utils::homePath; QString _path_ = g_path; //get app path QString path = ""; diff --git a/application/logsettings.cpp b/application/logsettings.cpp index 147e7961..d2671ecc 100644 --- a/application/logsettings.cpp +++ b/application/logsettings.cpp @@ -3,6 +3,7 @@ // SPDX-License-Identifier: GPL-3.0-or-later #include "logsettings.h" +#include "utils.h" #include #include @@ -27,9 +28,9 @@ LogSettings::LogSettings(QObject *parent) m_configPath(""), m_logDirPath("") { - QDir infoPath(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)); + QDir infoPath(Utils::getConfigPath()); if (!infoPath.exists()) { - infoPath.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)); + infoPath.mkpath(Utils::getConfigPath()); } m_configPath = infoPath.filePath("wininfo-config.conf"); diff --git a/application/main.cpp b/application/main.cpp index c14db5d8..b3668f4c 100644 --- a/application/main.cpp +++ b/application/main.cpp @@ -36,6 +36,8 @@ int main(int argc, char *argv[]) // 鍛戒护鍙傛暟澶т簬1锛岃繘琛屽懡浠よ澶勭悊 if (argc > 1) { QCoreApplication a(argc, argv); + a.setOrganizationName("deepin"); + a.setApplicationName("deepin-log-viewer"); QCommandLineOption exportOption(QStringList() << "e" << "export", DApplication::translate("main", "export logs")); @@ -62,11 +64,15 @@ int main(int argc, char *argv[]) } QString outDir = ""; - // 鑻ユ寚瀹氭湁瀵煎嚭鐩綍锛屾寜鎸囧畾鐩綍瀵煎嚭锛屽惁鍒欐寜榛樿璺緞瀵煎嚭 if (!args.isEmpty()) outDir = args.first(); + // 璁剧疆鍛戒护琛屽伐浣滅洰褰 + LogBackend::instance(&a)->setCmdWorkDir(argv[argc - 2]); + // 鏍规嵁褰撳墠鐢ㄦ埛鍚嶈幏鍙栨纭鐩綍璺緞 + Utils::homePath = Utils::getHomePath(argv[argc - 1]); + // 鍏ㄩ儴瀵煎嚭鏃ュ織 LogBackend::instance(&a)->exportAllLogs(outDir); diff --git a/application/structdef.h b/application/structdef.h index a95885b8..a6d21583 100644 --- a/application/structdef.h +++ b/application/structdef.h @@ -7,7 +7,7 @@ #include #include #include - +#include "utils.h" #define DPKG_TABLE_DATA "dpkgItemData" #define XORG_TABLE_DATA "XorgItemData" #define BOOT_TABLE_DATA "bootItemData" @@ -27,7 +27,7 @@ #define BOOT_KLU_TREE_DATA "bootklu" #define DPKG_TREE_DATA "/var/log/dpkg.log" #define XORG_TREE_DATA "/var/log/Xorg.0.log" -#define KWIN_TREE_DATA QDir::homePath() + "/.kwin.log" +#define KWIN_TREE_DATA Utils::homePath + "/.kwin.log" #define BOOT_TREE_DATA "/var/log/boot.log" #define KERN_TREE_DATA "/var/log/kern.log" #define APP_TREE_DATA "application" diff --git a/application/utils.cpp b/application/utils.cpp index 7b6a0744..390a19a9 100644 --- a/application/utils.cpp +++ b/application/utils.cpp @@ -31,6 +31,7 @@ QHash Utils::m_imgCacheHash; QHash Utils::m_fontNameCache; QMap Utils::m_mapAuditType2EventType; int Utils::specialComType = -1; +QString Utils::homePath = QDir::homePath(); Utils::Utils(QObject *parent) : QObject(parent) @@ -55,7 +56,15 @@ QString Utils::getQssContent(const QString &filePath) QString Utils::getConfigPath() { - QDir dir(QDir(QStandardPaths::standardLocations(QStandardPaths::ConfigLocation).first()) + QDir dir(QDir(Utils::homePath + "/.config") + .filePath(qApp->organizationName())); + + return dir.filePath(qApp->applicationName()); +} + +QString Utils::getAppDataPath() +{ + QDir dir(QDir(Utils::homePath + "/.local/share") .filePath(qApp->organizationName())); return dir.filePath(qApp->applicationName()); @@ -320,6 +329,13 @@ QString Utils::getUserNamebyUID(uint uid) return pwd->pw_name; } +QString Utils::getCurrentUserName() +{ + // 鑾峰彇褰撳墠绯荤粺鐢ㄦ埛鍚 + struct passwd* pwd = getpwuid(getuid()); + return pwd->pw_name; +} + bool Utils::isCoredumpctlExist() { bool isCoredumpctlExist = false; @@ -333,3 +349,22 @@ bool Utils::isCoredumpctlExist() } return isCoredumpctlExist; } + +QString Utils::getHomePath(const QString &userName) +{ + QString uName(""); + if (!userName.isEmpty()) + uName = userName; + else + uName = getCurrentUserName(); + + + QProcess *unlock = new QProcess; + unlock->start("sh", QStringList() << "-c" << QString("cat /etc/passwd | grep %1").arg(uName)); + unlock->waitForFinished(); + auto output = unlock->readAllStandardOutput(); + auto str = QString::fromUtf8(output); + str = str.mid(str.indexOf("::") + 2).split(":").first(); + qInfo() << "userName: " << uName << "homePath:" << str; + return str; +} diff --git a/application/utils.h b/application/utils.h index f72244ab..ac2d2f28 100644 --- a/application/utils.h +++ b/application/utils.h @@ -32,6 +32,7 @@ class Utils : public QObject static QString getQssContent(const QString &filePath); static QString getConfigPath(); + static QString getAppDataPath(); static bool isFontMimeType(const QString &filePath); static bool isTextFileType(const QString &filePath); static QString suffixList(); @@ -53,12 +54,15 @@ class Utils : public QObject static QString auditType(const QString& eventType); static double convertToMB(quint64 cap, const int size = 1024); static QString getUserNamebyUID(uint uid); //鏍规嵁uid鑾峰彇鐢ㄦ埛鍚 + static QString getCurrentUserName(); static bool isCoredumpctlExist(); // is coredumpctl installed + static QString getHomePath(const QString &userName = ""); /** * @brief specialComType 鏄惁鏄壒娈婃満鍨嬶紝like huawei * 鍙栧兼湁3绉嶏紙-1,0,>0锛夛紝榛樿涓-1锛堟湭鐭ワ級锛0锛堜笉鏄壒娈婃満鍨嬶級,>0锛堢壒娈婃満鍨嬶級 */ static int specialComType; + static QString homePath; }; #endif diff --git a/tests/src/ut_displaycontent.cpp b/tests/src/ut_displaycontent.cpp index 2aa5d32f..87570d5f 100644 --- a/tests/src/ut_displaycontent.cpp +++ b/tests/src/ut_displaycontent.cpp @@ -1705,7 +1705,7 @@ TEST(DisplayContent_slot_appLogs_UT, DisplayContent_slot_appLogs_UT_001) DisplayContent *p = new DisplayContent(nullptr); EXPECT_NE(p, nullptr); stub.set(ADDR(DisplayContent, generateAppFile), DisplayContent_slot_BtnSelected_UT_generateAppFile); - p->slot_appLogs(1, QDir::homePath() + "/.cache/deepin/deepin-log-viewer/deepin-log-viewer.log"); + p->slot_appLogs(1, Utils::homePath + "/.cache/deepin/deepin-log-viewer/deepin-log-viewer.log"); p->deleteLater(); } diff --git a/tests/src/ut_filtercontent.cpp b/tests/src/ut_filtercontent.cpp index dd9f2c76..7ce8a8c0 100644 --- a/tests/src/ut_filtercontent.cpp +++ b/tests/src/ut_filtercontent.cpp @@ -124,7 +124,7 @@ TEST_F(FilterContent_UT, slot_logCatelogueClicked_UT) m_model.setData(m_model.index(7, 0), QString("bootklu"), Qt::UserRole + 66); m_model.setData(m_model.index(8, 0), QString("/var/log/dnf.log"), Qt::UserRole + 66); m_model.setData(m_model.index(9, 0), QString("dmesg"), Qt::UserRole + 66); - m_model.setData(m_model.index(10, 0), QString(QDir::homePath() + "/.kwin.log"), Qt::UserRole + 66); + m_model.setData(m_model.index(10, 0), QString(Utils::homePath + "/.kwin.log"), Qt::UserRole + 66); for (int i = 0; i < m_model.rowCount(); i++) { QModelIndex modelindex = m_model.index(i, 0, QModelIndex()); m_filter->slot_logCatelogueClicked(modelindex); diff --git a/tests/ut_logviewerplugin/ut_logViewerPlugin.cpp b/tests/ut_logviewerplugin/ut_logViewerPlugin.cpp index 1aed5307..c59a516d 100644 --- a/tests/ut_logviewerplugin/ut_logViewerPlugin.cpp +++ b/tests/ut_logviewerplugin/ut_logViewerPlugin.cpp @@ -42,7 +42,7 @@ TEST_F(LogViewerPlugin_UT, generateAppFile_UT001) stub.set(ADDR(QThread, start), QThread_start); LogViewerPlugin *p = new LogViewerPlugin; - p->generateAppFile(QDir::homePath() + "/.cache/deepin/deepin-log-viewer/deepin-log-viewer.log", ALL, INF, ""); + p->generateAppFile(Utils::homePath + "/.cache/deepin/deepin-log-viewer/deepin-log-viewer.log", ALL, INF, ""); connect(p, &LogViewerPlugin::sigAppData, this, [=](int index, QList iList) { qDebug() << "index:" << index << endl; qDebug() << "appDatas:" << endl;