| 1 | // SPDX-FileCopyrightText: 2015 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-only |
| 4 | |
| 5 | #include "utils.h" |
| 6 | |
| 7 | #include <QtCore> |
| 8 | #include <QString> |
| 9 | |
| 10 | #include <XSys> |
| 11 | #include <dirent.h> |
| 12 | #include <QTextCodec> |
| 13 | #include <QByteArray> |
| 14 | #include <QRegExp> |
| 15 | #include <qglobal.h> |
| 16 | #ifdef Q_OS_WIN32 |
| 17 | #include <Windows.h> |
| 18 | #endif |
| 19 | |
| 20 | static void initQRC() |
| 21 | { |
| 22 | #ifdef Q_OS_LINUX |
| 23 | //Q_INIT_RESOURCE(blob_linux); |
| 24 | #else |
| 25 | Q_INIT_RESOURCE(blob); |
| 26 | #endif |
| 27 | } |
| 28 | |
| 29 | namespace Utils { |
| 30 | |
| 31 | bool isUft8(const QByteArray& byArr) |
| 32 | { |
| 33 | unsigned int nBytes = 0;//UFT8可用1-6个字节编码,ASCII用一个字节 |
| 34 | bool bAllAscii = true; |
| 35 | |
| 36 | for (int i = 0; i < byArr.length(); ++i) { |
| 37 | unsigned char chr = static_cast<unsigned char>(byArr.at(i)); |
| 38 | |
| 39 | //ASCII码高位为0 |
| 40 | if (nBytes == 0 && (chr & 0x80) != 0) { |
| 41 | bAllAscii = false; |
| 42 | } |
| 43 | |
| 44 | if (nBytes == 0) { |
| 45 | //计算连续1的位数 |
| 46 | if (chr >= 0x80) { |
| 47 | if (chr >= 0xFC && chr <= 0xFD) { |
| 48 | nBytes = 6; |
| 49 | } |
| 50 | else if (chr >= 0xF8) { |
| 51 | nBytes = 5; |
| 52 | } |
| 53 | else if (chr >= 0xF0) { |
| 54 | nBytes = 4; |
| 55 | } |
| 56 | else if (chr >= 0xE0) { |
| 57 | nBytes = 3; |
| 58 | } |
| 59 | else if (chr >= 0xC0) { |
| 60 | nBytes = 2; |
| 61 | } |
| 62 | else { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | nBytes--; |
| 67 | } |
| 68 | } |
| 69 | else { |
| 70 | if ((chr & 0xC0) != 0x80) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | nBytes--; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | if (nBytes != 0) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | if (bAllAscii) { //如果全部都是ASCII, 也是UTF8 |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | bool isGBK(const QByteArray& byArr) |
| 90 | { |
| 91 | unsigned int nBytes = 0; |
| 92 | bool bAllAscii = true; |
| 93 | |
| 94 | for (int i = 0; i < byArr.length(); ++i) { |
| 95 | unsigned char chr = static_cast<unsigned char>(byArr.at(i)); |
| 96 | |
| 97 | //ASCII码高位为0 |
| 98 | if ((chr & 0x80) != 0 && nBytes == 0) { |
| 99 | bAllAscii = false; |
| 100 | } |
| 101 | |
| 102 | if (nBytes == 0) { |
| 103 | if (chr >= 0x80) { |
| 104 | if (chr >= 0x81 && chr <= 0xFE) { |
| 105 | nBytes = +2; |
| 106 | } |
| 107 | else { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | nBytes--; |
| 112 | } |
| 113 | } |
| 114 | else { |
| 115 | if (chr < 0x40 || chr>0xFE) { |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | nBytes--; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if (nBytes != 0) {//违返规则 |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | if (bAllAscii){ //如果全部都是ASCII, 也是GBK |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | void loadTranslate() |
| 135 | { |
| 136 | QTranslator *qtTranslator = new QTranslator; |
| 137 | qtTranslator->load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath)); |
| 138 | qApp->installTranslator(qtTranslator); |
| 139 | qtTranslator->deleteLater(); |
| 140 | qtTranslator = nullptr; |
| 141 | |
| 142 | #ifdef Q_OS_WIN |
| 143 | QTranslator *translator = new QTranslator(); |
| 144 | QString tnapplang; |
| 145 | QString tnappcoun; |
| 146 | QString clangcode = ""; |
| 147 | QStringList allappargs = qApp->arguments(); |
| 148 | QList<QPair<QString, QString> > oppairs; |
| 149 | |
| 150 | for (QList<QString>::const_iterator i = allappargs.constBegin(); i < allappargs.constEnd(); ++i) { |
| 151 | if (i->count('=') == 1) { |
| 152 | oppairs.append(QPair<QString, QString> (i->section('=', 0, 0).simplified(), i->section('=', 1, 1).simplified())); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | for (QList<QPair<QString, QString> >::const_iterator i = oppairs.constBegin(); i < oppairs.constEnd(); ++i) { |
| 157 | if (i->first.contains("lang", Qt::CaseInsensitive)) { |
| 158 | clangcode = i->second; |
| 159 | tnapplang = clangcode.left(2); |
| 160 | |
| 161 | if (clangcode.contains('_') && clangcode.size() == 5) { |
| 162 | tnappcoun = clangcode.section('_', -1, -1); |
| 163 | } |
| 164 | |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (clangcode.isEmpty()) { |
| 170 | clangcode = QLocale::system().name(); |
| 171 | tnapplang = clangcode.left(2); |
| 172 | |
| 173 | if (clangcode.contains('_') && clangcode.size() == 5) { |
| 174 | tnappcoun = clangcode.section('_', -1, -1); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | QString tranlateUrl; |
| 179 | if (!tnappcoun.isEmpty()) { |
| 180 | tranlateUrl = QString(":/translations/deepin-boot-maker_%1_%2.qm").arg(tnapplang).arg(tnappcoun); |
| 181 | } |
| 182 | |
| 183 | if (!QFile::exists(tranlateUrl)) { |
| 184 | tranlateUrl = QString(":/translations/deepin-boot-maker_%1.qm").arg(tnapplang); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | if (!QFile::exists(tranlateUrl)) { |
| 189 | tranlateUrl = ":/translations/deepin-boot-maker.qm"; |
| 190 | } |
| 191 | |
| 192 | qDebug() << "locate:" << clangcode; |
| 193 | qDebug() << "load:" << tranlateUrl; |
| 194 | |
| 195 | if (translator->load(tranlateUrl)) { |
| 196 | qApp->installTranslator(translator); |
| 197 | } |
| 198 | #endif |
| 199 | } |
| 200 | |
| 201 | QString UsbShowText(const QString &dev) |
| 202 | { |
| 203 | QString showText; |
| 204 | if (!dev.isEmpty()) { |
| 205 | QString label = XSys::DiskUtil::GetPartitionLabel(dev); |
| 206 | if (label.isEmpty()) { |
| 207 | label = QObject::tr("Removable disk"); |
| 208 | } |
| 209 | #ifdef Q_OS_UNIX |
| 210 | QFileInfo devFile(dev); |
| 211 | showText = QString("%1: %2").arg(devFile.baseName()).arg(label); |
| 212 | #endif |
| 213 | #ifdef Q_OS_WIN32 |
| 214 | showText = QString("(%1:) %2").arg(dev.at(0)).arg(label); |
| 215 | #endif |
| 216 | } |
| 217 | return showText; |
| 218 | } |
| 219 | |
| 220 | void ClearTargetDev(const QString &targetPath) |
| 221 | { |
| 222 | QStringList dirlist; |
| 223 | dirlist.append("/boot/"); |
| 224 | dirlist.append("/EFI/"); |
| 225 | dirlist.append("/casper/"); |
| 226 | dirlist.append("/live/"); |
| 227 | dirlist.append("/dists/"); |
| 228 | dirlist.append("/isolinux/"); |
| 229 | dirlist.append("/pool/"); |
| 230 | dirlist.append("/preseed/"); |
| 231 | dirlist.append("/syslinux/"); |
| 232 | dirlist.append("/oem/"); |
| 233 | |
| 234 | foreach (QString dirname, dirlist) { |
| 235 | QString fullDir = targetPath + dirname; |
| 236 | XSys::FS::RmDir(fullDir); |
| 237 | } |
| 238 | |
| 239 | #ifdef Q_OS_UNIX |
| 240 | XSys::SynExec("sync", ""); |
| 241 | #endif |
| 242 | } |
| 243 | |
| 244 | #ifdef Q_OS_UNIX |
| 245 | QMap<QString, DeviceInfo> CommandDfParse() |
| 246 | { |
| 247 | QProcess df; |
| 248 | df.start("df", QStringList{"-k", "--output=source,used,avail"}); |
| 249 | df.waitForFinished(-1); |
| 250 | |
| 251 | QString dfout = df.readAll(); |
| 252 | |
| 253 | QMap<QString, DeviceInfo> deviceInfos; |
| 254 | foreach (const QString &infoline, dfout.split("\n")) { |
| 255 | QStringList infos = infoline.simplified().split(" "); |
| 256 | |
| 257 | if (infos.size() != 3) { |
| 258 | continue; |
| 259 | } |
| 260 | DeviceInfo devInfo; |
| 261 | devInfo.path = infos.at(0); |
| 262 | if (!devInfo.path.contains("/dev/")) { |
| 263 | continue; |
| 264 | } |
| 265 | devInfo.used = static_cast<quint32>(infos.at(1).toInt() / 1024); |
| 266 | devInfo.total = static_cast<quint32>((infos.at(2).toInt() + infos.at(1).toInt()) / 1024) ; |
| 267 | qDebug() << "device path" << devInfo.path << "used: " << devInfo.used << "total: " << devInfo.total; |
| 268 | deviceInfos.insert(devInfo.path, devInfo); |
| 269 | } |
| 270 | return deviceInfos; |
| 271 | } |
| 272 | |
| 273 | static QByteArray unescapeLimited(const QString &str) |
| 274 | { |
| 275 | bool escape = false; |
| 276 | QByteArray hex(2, '\0'); |
| 277 | QByteArray ret; |
| 278 | QByteArray origin = str.toUtf8(); |
| 279 | for (auto it = origin.constBegin(); it != origin.constEnd();) { |
| 280 | if (escape) { |
| 281 | if (*it != 'x' || it + 2 > origin.constEnd()) |
| 282 | break; |
| 283 | hex[0] = (*(it + 1)); |
| 284 | hex[1] = (*(it + 2)); |
| 285 | ret.append(QByteArray::fromHex(hex)); |
| 286 | escape = false; |
| 287 | it += 3; |
| 288 | } else { |
| 289 | if (*it == '\\') |
| 290 | escape = true; |
| 291 | else |
| 292 | ret.append(*it); |
| 293 | ++it; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | QMap<QString, DeviceInfo> CommandLsblkParse() |
| 301 | { |
| 302 | QProcess lsblk; |
| 303 | lsblk.start("lsblk", QStringList{"-b", "-p", "-P", "-o", "name,label,size,uuid,fstype,type"}); |
| 304 | lsblk.waitForFinished(-1); |
| 305 | QString line; |
| 306 | DeviceInfo info; |
| 307 | QString diskDevPath; |
| 308 | QMap<QString, DeviceInfo> deviceInfos; |
| 309 | // currentPartPath用记录当前的磁盘设备,如sdb |
| 310 | QString currentPartPath = ""; |
| 311 | do { |
| 312 | bool isPart = false; |
| 313 | line = QString::fromUtf8(lsblk.readLine()); |
| 314 | if (line.isEmpty()) |
| 315 | break; |
| 316 | |
| 317 | QString type; |
| 318 | QRegExp reg("NAME=\"(.*)\" LABEL=\"(.*)\" SIZE=\"(.*)\" UUID=\"(.*)\" FSTYPE=\"(.*)\" TYPE=\"(.*)\""); |
| 319 | |
| 320 | if (reg.indexIn(line) >= 0) { |
| 321 | info.path = reg.cap(1); |
| 322 | info.label = reg.cap(2); |
| 323 | info.uuid = reg.cap(4); |
| 324 | info.fstype = reg.cap(5); |
| 325 | type = reg.cap(6); |
| 326 | |
| 327 | if (!type.compare("disk")) { |
| 328 | diskDevPath = info.path; |
| 329 | isPart = false; |
| 330 | } |
| 331 | else if (!type.compare("part")){ |
| 332 | isPart = true; |
| 333 | } else { |
| 334 | diskDevPath = ""; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | QString strLabel = info.label; |
| 339 | |
| 340 | if (info.label.contains("\\x", Qt::CaseInsensitive)) { |
| 341 | QByteArray byArr = unescapeLimited(info.label); |
| 342 | |
| 343 | if (isUft8(byArr)) { |
| 344 | strLabel = QTextCodec::codecForName("UTF-8")->toUnicode(byArr); |
| 345 | } |
| 346 | else if(isGBK(byArr)) { |
| 347 | strLabel = QTextCodec::codecForName("GBK")->toUnicode(byArr); |
| 348 | } |
| 349 | else { |
| 350 | strLabel = QString::fromLocal8Bit(byArr); |
| 351 | } |
| 352 | |
| 353 | if (strLabel.isEmpty()) |
| 354 | strLabel = QObject::tr("Removable disk"); |
| 355 | } |
| 356 | |
| 357 | info.label = strLabel; |
| 358 | // 如果是disk 如sdb1,sdb2。 |
| 359 | if (isPart && !diskDevPath.isEmpty() && (info.path.left(currentPartPath.length()) == currentPartPath)) { |
| 360 | info.isDisk = false; |
| 361 | info.strDev = diskDevPath; |
| 362 | deviceInfos[diskDevPath].children.insert(info.path, info); |
| 363 | } else { // 否则就是 part, 如sdb。 |
| 364 | info.isDisk = true; |
| 365 | info.strDev = ""; |
| 366 | deviceInfos.insert(info.path, info); |
| 367 | // 记录当前是part的情况 |
| 368 | currentPartPath = info.path; |
| 369 | } |
| 370 | } while(true); |
| 371 | |
| 372 | return deviceInfos; |
| 373 | } |
| 374 | #endif |
| 375 | |
| 376 | |
| 377 | bool CheckInstallDisk(const QString &targetDev) |
| 378 | { |
| 379 | qDebug() << "CheckInstallDisk"; |
| 380 | if (XSys::DiskUtil::PF_FAT32 != XSys::DiskUtil::GetPartitionFormat(targetDev)) { |
| 381 | qDebug() << "disk format error " << targetDev; |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | QString targetPath = XSys::DiskUtil::MountPoint(targetDev); |
| 386 | qDebug() << "targetPath: " << targetPath; |
| 387 | QFile test(QDir::toNativeSeparators(targetPath + "/" + "UOS")); |
| 388 | |
| 389 | if (!test.open(QIODevice::ReadWrite)) { |
| 390 | qDebug() << "erro open file: " << test.fileName(); |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | QFile UOS(":src/UOS"); |
| 395 | UOS.open(QIODevice::ReadOnly); |
| 396 | QByteArray data = UOS.readAll(); |
| 397 | |
| 398 | if (data.length() != test.write(data)) { |
| 399 | qDebug() << "erro write file: " << UOS.fileName(); |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | test.close(); |
| 404 | UOS.close(); |
| 405 | test.remove(); |
| 406 | return true; |
| 407 | } |
| 408 | |
| 409 | bool isUsbDisk(const QString &dev) |
| 410 | { |
| 411 | QString out = XSys::FS::TmpFilePath("diskutil_isusb_out"); |
| 412 | XSys::SynExec("bash", QString("-c \" diskutil info %1 > \"%2\" \" ").arg(dev).arg(out)); |
| 413 | QFile outfile(out); |
| 414 | outfile.open(QIODevice::ReadOnly); |
| 415 | QString info = outfile.readAll(); |
| 416 | outfile.close(); |
| 417 | outfile.remove(); |
| 418 | return info.contains(QRegExp("Protocol:\\s+USB")); |
| 419 | } |
| 420 | |
| 421 | QList<DeviceInfo> ListUsbDrives() |
| 422 | { |
| 423 | qDebug() << "ListUsbDrives"; |
| 424 | QList<DeviceInfo> deviceList; |
| 425 | #ifdef Q_OS_WIN32 |
| 426 | QFileInfoList extdrivesList = QDir::drives(); |
| 427 | |
| 428 | for (int i = 0; i < extdrivesList.size(); ++i) { |
| 429 | QString deviceLetter = extdrivesList.at(i).path().toUpper(); |
| 430 | if (QDir::toNativeSeparators(deviceLetter) != QDir::toNativeSeparators(QDir::rootPath().toUpper()) && !QDir::toNativeSeparators(deviceLetter) |
| 431 | .contains("A:") && !QDir::toNativeSeparators(deviceLetter) |
| 432 | .contains("B:")) { |
| 433 | if (GetDriveType(LPWSTR(deviceLetter.utf16())) == 2) { |
| 434 | |
| 435 | DeviceInfo info; |
| 436 | info.path = QDir::toNativeSeparators(deviceLetter); |
| 437 | deviceList.push_back(info); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | #endif |
| 442 | |
| 443 | #ifdef Q_OS_LINUX |
| 444 | QDir devlstdir("/dev/disk/by-id/"); |
| 445 | QFileInfoList usbfileinfoL = devlstdir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files); |
| 446 | QMap<QString, DeviceInfo> dfDeviceInfos = CommandDfParse(); |
| 447 | QMap<QString, DeviceInfo> lsblkDeviceInfos = CommandLsblkParse(); |
| 448 | |
| 449 | QMap<QString, QString> removeDevice; |
| 450 | |
| 451 | for (int i = 0; i < usbfileinfoL.size(); ++i) { |
| 452 | if (usbfileinfoL.at(i).fileName().contains(QRegExp("^usb-\\S{1,}$")) || usbfileinfoL.at(i).fileName().contains(QRegExp("^mmc-\\S{1,}$"))) { |
| 453 | QString path = usbfileinfoL.at(i).canonicalFilePath(); |
| 454 | removeDevice.insert(path, usbfileinfoL.at(i).fileName()); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | for (auto devicePath : lsblkDeviceInfos.keys()) { |
| 459 | if (!removeDevice.contains(devicePath)) { |
| 460 | continue; |
| 461 | } |
| 462 | // find first partition |
| 463 | QString strDiskName = devicePath; |
| 464 | DeviceInfo diskinfo = lsblkDeviceInfos.value(devicePath); |
| 465 | QStringList partitionNames = diskinfo.children.keys(); |
| 466 | |
| 467 | foreach (QString strPartionName, partitionNames) { |
| 468 | bool needformat = true; |
| 469 | DeviceInfo partitionInfo = diskinfo.children.value(strPartionName); |
| 470 | |
| 471 | if (partitionInfo.fstype != "vfat") { |
| 472 | needformat = true; |
| 473 | } |
| 474 | else { |
| 475 | needformat = false; |
| 476 | } |
| 477 | |
| 478 | DeviceInfo dfinfo = dfDeviceInfos.value(strPartionName); |
| 479 | if (partitionInfo.label.isEmpty()) { |
| 480 | partitionInfo.label = partitionInfo.path; |
| 481 | } |
| 482 | |
| 483 | partitionInfo.used = dfinfo.used; |
| 484 | partitionInfo.total = dfinfo.total; |
| 485 | partitionInfo.target = dfinfo.target; |
| 486 | partitionInfo.needFormat = needformat; |
| 487 | deviceList.push_back(partitionInfo); |
| 488 | qDebug() << partitionInfo.path << partitionInfo.used << partitionInfo.total << partitionInfo.target << partitionInfo.needFormat; |
| 489 | } |
| 490 | } |
| 491 | #endif |
| 492 | |
| 493 | #ifdef Q_OS_MAC |
| 494 | QStringList fulldrivelist; |
| 495 | QString out = XSys::FS::TmpFilePath("diskutil_out"); |
| 496 | XSys::SynExec("bash", QString("-c \" diskutil list > \"%1\" \" ").arg(out)); |
| 497 | QFile outfile(out); |
| 498 | outfile.open(QIODevice::ReadOnly); |
| 499 | QString diskutilList = outfile.readAll(); |
| 500 | QStringList usbdevsL = diskutilList.split("\n").filter(QRegExp("(FAT|Microsoft)")).join(" ").split(" ").filter("disk"); |
| 501 | |
| 502 | for (int i = 0; i < usbdevsL.size(); ++i) { |
| 503 | if (isUsbDisk("/dev/" + usbdevsL.at(i))) { |
| 504 | auto path = "/dev/" + usbdevsL.at(i); |
| 505 | fulldrivelist.append(path); |
| 506 | |
| 507 | DeviceInfo info; |
| 508 | info.path = path; |
| 509 | deviceList.push_back(info); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | qDebug() << fulldrivelist; |
| 514 | outfile.close(); |
| 515 | outfile.remove(); |
| 516 | #endif |
| 517 | return deviceList; |
| 518 | } |
| 519 | |
| 520 | void initResource() |
| 521 | { |
| 522 | initQRC(); |
| 523 | } |
| 524 | } |