Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions deepin-devicemanager/src/DeviceManager/DeviceStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,41 @@ using namespace DDLog;
#define DISK_SCALE_1024 1024
#define DISK_SCALE_1000 1000

// MANFID: 十六进制字符串 -> 厂商名
static const QMap<QString, QString> MANFID_TABLE = {
{"000001", "Panasonic"},
{"000002", "Toshiba"},
{"000003", "SanDisk"},
{"00001b", "Samsung"},
{"00001d", "ADATA"},
{"000027", "Phison"},
{"000028", "Lexar"},
{"000031", "Silicon Power"},
{"000041", "Kingston"},
{"000074", "Transcend Information"},
{"000076", "Patriot Memory"},
{"000082", "Sony"},
{"00009c", "Angelbird / Hoodman"}
};

// OEMID: 十六进制字符串 -> 厂商名
static const QMap<QString, QString> OEMID_TABLE = {
{"3432", "Kingston"},
{"4144", "ADATA"},
{"4245", "Lexar / Angelbird / Hoodman"},
{"4a45", "Transcend Information"},
{"4a54", "Sony"},
{"4a60", "Transcend Information"},
{"5041", "Panasonic"},
{"5048", "Phison"},
{"5054", "SanDisk"},
{"5344", "SanDisk"},
{"534d", "Samsung"},
{"534f", "Angelbird / Hoodman"},
{"5350", "Silicon Power"},
{"544d", "Toshiba"}
};

DeviceStorage::DeviceStorage()
: DeviceBaseInfo()
, m_Model("")
Expand Down Expand Up @@ -275,6 +310,35 @@ bool DeviceStorage::setHwinfoInfo(const QMap<QString, QString> &mapInfo)
setAttribute(mapInfo, "VID_PID", m_VID_PID);
m_PhysID = m_VID_PID;

if (m_Driver.contains("mmcblk")) {
QString sysfsDeviceLink = mapInfo.value("SysFS Device Link");

if (!sysfsDeviceLink.isEmpty())
{
if (!sysfsDeviceLink.startsWith("/sys/"))
{
sysfsDeviceLink = "/sys" + sysfsDeviceLink;
}

QString err;
QString name = Common::safeReadSysFsFile(sysfsDeviceLink, "name", err);
if (err.isEmpty())
m_Name = name;

QString manfIdRaw = Common::safeReadSysFsFile(sysfsDeviceLink, "manfid", err);
if (err.isEmpty()) {
m_Vendor = getManfName(manfIdRaw);
}

QString oemIdRaw = Common::safeReadSysFsFile(sysfsDeviceLink, "oemid", err);
if (err.isEmpty()) {
if (m_Vendor.isEmpty()) {
m_Vendor = getOemName(oemIdRaw);
}
}
}
}

getOtherMapInfo(mapInfo);
return true;
}
Expand Down Expand Up @@ -844,3 +908,19 @@ void DeviceStorage::getInfoFromsmartctl(const QMap<QString, QString> &mapInfo)
m_Vendor = "Longsys";
}
}

// 通过manfid获取厂商名称
QString DeviceStorage::getManfName(const QString &rawManfId)
{
QString cleanId = rawManfId.trimmed().toLower();
cleanId.remove("0x"); // 去除0x前缀
return MANFID_TABLE.value(cleanId, rawManfId);
}

// 通过oemid获取厂商名称
QString DeviceStorage::getOemName(const QString &rawOemId)
{
QString cleanId = rawOemId.trimmed().toLower();
cleanId.remove("0x");
return OEMID_TABLE.value(cleanId, rawOemId);
}
2 changes: 2 additions & 0 deletions deepin-devicemanager/src/DeviceManager/DeviceStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ class DeviceStorage: public DeviceBaseInfo

const QString &mediaType() const;

QString getManfName(const QString &rawManfId);
QString getOemName(const QString &rawOemId);
protected:

/**
Expand Down
56 changes: 56 additions & 0 deletions deepin-devicemanager/src/commonfunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@

// 其它头文件
#include <QString>
#include <QMap>

Check warning on line 15 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QMap> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 15 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QMap> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QProcess>

Check warning on line 16 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QProcess> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 16 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QProcess> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFile>

Check warning on line 17 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFile> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 17 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QFile> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFileInfo>

Check warning on line 18 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFileInfo> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 18 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QFileInfo> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDir>

Check warning on line 19 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDir> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 19 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QDir> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QLoggingCategory>

Check warning on line 20 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QLoggingCategory> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 20 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QLoggingCategory> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QRegularExpression>

Check warning on line 21 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QRegularExpression> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 21 in deepin-devicemanager/src/commonfunction.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QRegularExpression> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <sys/utsname.h>

Expand Down Expand Up @@ -405,3 +407,57 @@
}
return count;
}

/**
* 安全读取sysfs下指定文件内容
* @param baseDir 根目录 /sys/xxx/mmc0:0001
* @param fileName 待读取的文件名
* @return 文件原始字符串(去换行空格)
*/
QString Common::safeReadSysFsFile(const QString &baseDir, const QString &fileName, QString &errOut)
{
errOut.clear();

if (!baseDir.startsWith("/sys/") && baseDir != "/sys") {
errOut = QString("baseDir not under /sys: %1").arg(baseDir);
return "";
}
if (baseDir.contains("..")) {
errOut = QString("Path traversal detected in baseDir: %1").arg(baseDir);
return "";
}

if (fileName.contains('/') || fileName.contains("..") || fileName.contains('\\')) {
errOut = QString("Path traversal detected in fileName: %1").arg(fileName);
return "";
}

QFileInfo fullPathInfo(QDir(baseDir), fileName);
QString fullPath = fullPathInfo.canonicalFilePath();

if (!fullPath.startsWith("/sys/"))
{
errOut = QString("Path escape to non-sys dir: %1").arg(fullPath);
return "";
}

if (!fullPathInfo.isFile())
{
errOut = QString("Not a regular file: %1").arg(fullPath);
return "";
}

QFile file(fullPath);
if (!file.open(QIODevice::ReadOnly))
{
errOut = QString("Open failed: %1, err=%2").arg(fullPath, file.errorString());
return "";
}

const qint64 MAX_READ_LEN = 64;
QByteArray rawData = file.read(MAX_READ_LEN);
file.close();

QString content = QString::fromUtf8(rawData).trimmed();
return content;
}
9 changes: 9 additions & 0 deletions deepin-devicemanager/src/commonfunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,14 @@ class Common
static QString formatTotalCache(const QString& perThreadCache, int coreCount);

static int parseSharedCpuCount(const QString &sharedCpuList);

/**
* @brief safeReadSysFsFile: 安全读取 sysfs 下指定文件内容
* @param baseDir 根目录,如 /sys/devices/xxx/mmc0:0001
* @param fileName 待读取的文件名(禁止包含路径分隔符)
* @param errOut 错误输出参数,成功时为空
* @return 文件内容(去换行空格),失败返回空字符串
*/
static QString safeReadSysFsFile(const QString &baseDir, const QString &fileName, QString &errOut);
};
#endif // COMMONFUNCTION_H
Loading