1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
|
/*
* Copyright (C) Pedram Pourang (aka Tsu Jan) 2018-2021 <tsujan2000@gmail.com>
*
* Arqiver is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Arqiver is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @license GPL-3.0+ <https://spdx.org/licenses/GPL-3.0+.html>
*/
#include "backends.h"
#include <QFile>
#include <QDir>
#include <QUrl>
#include <QDateTime>
#include <QCoreApplication>
#include <QTimer>
#include <QStandardPaths>
#include <QDesktopServices>
#include <QMimeDatabase>
#include <QMimeData>
#include <QRegularExpression>
#ifdef Q_OS_LINUX
#define TAR_CMD "bsdtar"
#else
#define TAR_CMD "tar"
#endif
namespace Arqiver {
Backend::Backend(QObject *parent) : QObject(parent) {
tarCmnd_ = TAR_CMD;
proc_.setProcessChannelMode(QProcess::MergedChannels);
connect(&proc_, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &Backend::procFinished);
connect(&proc_, &QProcess::readyReadStandardOutput, this, &Backend::processData);
connect(&proc_, &QProcess::started, this, &Backend::processStarting);
connect(&proc_, &QProcess::errorOccurred, this, &Backend::onError);
isKilled_ = false;
LIST = false;
isGzip_ = is7z_ = false;
starting7z_ = encryptionQueried_ = encrypted_ = encryptedList_ = false;
}
Backend::~Backend() {
if (!arqiverDir_.isEmpty())
QDir(arqiverDir_).removeRecursively();
}
void Backend::setTarCommand(const QString& cmnd) {
#ifdef Q_OS_LINUX
Q_UNUSED(cmnd);
tarCmnd_ = TAR_CMD;
#else
if (cmnd.isEmpty())
tarCmnd_ = TAR_CMD;
else
tarCmnd_ = cmnd;
#endif
}
QString Backend::getMimeType(const QString &fname) {
QString mimeType, suffix;
int left = fname.indexOf(QLatin1Char('.'));
if (left != -1) {
if (fname.compare("CMakeLists.txt", Qt::CaseInsensitive) == 0) // exception
mimeType = "text/x-cmake";
else {
suffix = fname.right(fname.size() - left - 1);
mimeType = mimeTypes_.value(suffix);
}
}
if (mimeType.isEmpty()) {
QMimeDatabase mimeDatabase;
mimeType = mimeDatabase.mimeTypeForFile(QFileInfo(fname)).name();
if (!suffix.isEmpty())
mimeTypes_.insert(suffix, mimeType);
}
return mimeType;
}
void Backend::loadFile(const QString& path, bool withPassword) {
/* check if the file extraction directory can be made
but don't create it until a file is viewed */
const QString curTime = QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz");
if (!arqiverDir_.isEmpty())
QDir(arqiverDir_).removeRecursively();
QString cache = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
if (!cache.isEmpty()) {
QDir cacheDir(cache);
if (cacheDir.exists()) {
arqiverDir_ = cache + "/" + "arqiver-" + curTime;
}
}
/*
NOTE: So far, bsdtar, gzip and 7z are supported.
Also, password protected 7z archives can be
extracted and created.
7z has two kinds of prompts that we avoid:
password and overwrite prompts.
*/
filepath_ = path;
starting7z_ = false;
if (!withPassword) {
pswrd_.clear();
encryptedPaths_.clear();
encrypted_ = encryptedList_ = encryptionQueried_ = false;
}
QString mt = getMimeType(path);
if (mt == "application/gzip" || mt == "application/x-gzpdf" || mt == "image/svg+xml-compressed") {
isGzip_ = true; is7z_ = false;
}
else if (mt == "application/x-7z-compressed"
|| mt == "application/x-ms-dos-executable" || mt == "application/x-msi"
|| mt == "application/vnd.ms-cab-compressed" || mt == "application/vnd.rar"
|| mt == "application/x-cd-image" || mt == "application/vnd.appimage"
|| mt == "application/x-xz"
|| mt == "application/zstd"
|| mt == "application/x-ace"
|| mt == "application/x-bzip" || mt == "application/x-bzpdf" || mt == "application/x-xzpdf") {
is7z_ = true; isGzip_ = false;
}
else if (mt == "application/x-raw-disk-image") {
/* 7z can't open compressed disk images and bsdtar can't handle uncompressed ones */
QMimeDatabase mimeDatabase;
QString realMt = mimeDatabase.mimeTypeForFile(QFileInfo(path), QMimeDatabase::MatchContent).name();
if (realMt == "application/gzip" || realMt == "application/x-cpio" || realMt == "application/x-xz") {
isGzip_ = is7z_ = false;
}
else {
is7z_ = true; isGzip_ = false;
}
}
else {
isGzip_ = is7z_ = false;
}
tmpfilepath_ = filepath_.section("/", 0, -2) + "/" + ".tmp_arqiver-" + curTime + filepath_.section("/", -1);
fileArgs_.clear();
if (is7z_) {
/*if (path.section("/",-1).section(".",1,-1).split(".").contains("tar"))
fileArgs_ << "-ttar";*/
fileArgs_ << filepath_;
}
else
fileArgs_ << "-f" << filepath_; // add the actual archive path
if (QFile::exists(path))
startList(withPassword);
else {
if (is7z_)
encryptionQueried_ = true; // an empty archive doesn't have encryption (yet)
contents_.clear();
emit loadingFinished();
emit loadingSuccessful();
emit processFinished(true, QString());
}
}
bool Backend::canModify() const {
static QStringList validMimeTypes;
if (validMimeTypes.isEmpty()) {
validMimeTypes << "application/zip" << "application/x-compressed-tar" << "application/x-gzpdf" << "image/svg+xml-compressed" << "application/x-xz-compressed-tar" << "application/x-bzip-compressed-tar" << "application/x-tar" << "application/x-lzma-compressed-tar" << "application/x-zstd-compressed-tar" << "application/x-cpio" << /*".pax" <<*/ "application/x-archive" << /*".shar" <<*/ "application/gzip" << "application/x-7z-compressed";
}
QMimeDatabase mimeDatabase;
QString mimeTypeName = mimeDatabase.mimeTypeForFile(QFileInfo(filepath_)).name();
return validMimeTypes.contains(mimeTypeName);
}
QString Backend::currentFile() const {
return filepath_;
}
QStringList Backend::hierarchy() const {
return contents_.keys();
}
QString Backend::singleRoot() const {
return archiveSingleRoot_;
}
QString Backend::sizeString(const QString& file) const {
if (contents_.contains(file))
return contents_.value(file).at(1);
return QString();
}
double Backend::size(const QString& file) const {
if (!contents_.contains(file))
return -1;
return contents_.value(file).at(1).toDouble();
}
double Backend::csize(const QString& file) const {
if (!contents_.contains(file))
return -1;
if (is7z_)
return contents_.value(file).at(2).toDouble();
return contents_.value(file).at(1).toDouble();
}
bool Backend::isDir(const QString& file) const {
if (!contents_.contains(file))
return false;
if (is7z_)
return contents_.value(file).at(0).startsWith("D");
return contents_.value(file).at(0).startsWith("d");
}
bool Backend::isLink(const QString& file) const {
if (!contents_.contains(file))
return false;
return contents_.value(file).at(0).startsWith("l");
}
QString Backend::linkTo(const QString& file) const {
if (!contents_.contains(file))
return QString();
return contents_.value(file).at(2);
}
static inline void skipExistingFiles(QString& file) {
QString suffix;
int i = 0;
while (QFile::exists(file + suffix)) {
suffix = "-" + QString::number(i);
i++;
}
file += suffix;
}
static inline QString escapedWildCard(const QString& str)
{
QString newStr = str;
newStr.replace("*", "\\*").replace("[", "\\[");
return newStr;
}
void Backend::startAdd(const QStringList& paths, bool absolutePaths) {
keyArgs_.clear();
QStringList filePaths = paths;
/* exclude the archive itself */
if (filePaths.contains(filepath_))
filePaths.removeAll(filepath_);
/* no path should be a parent folder of the archive */
QString parentDir = filepath_.section("/", 0, -2);
for (int i = 0; !filePaths.isEmpty() && i < filePaths.length(); i++) {
if (parentDir.startsWith(filePaths.at(i))) {
filePaths.removeAt(i);
i--;
}
}
if (filePaths.isEmpty()) return;
/* no path should be repeated */
filePaths.removeDuplicates();
QStringList args;
if (isGzip_) {
emit processStarting();
if (QFile::exists(filepath_)) // the overwrite prompt should be already accepted
args << "--to-stdout" << "--force" << filePaths.at(0);
else
args << "--to-stdout" << filePaths.at(0);
tmpProc_.setStandardOutputFile(filepath_);
tmpProc_.start("gzip", args); // "gzip -c (-f) file > archive.gz"
if (tmpProc_.waitForStarted()) {
while (!tmpProc_.waitForFinished(500))
QCoreApplication::processEvents();
}
emit processFinished(tmpProc_.exitCode() == 0, QString());
loadFile(filepath_);
return; // FIXME: Unfortunately, onError() can't be called here
}
if (is7z_) {
if (encryptedList_) {
/* with an encrypted header, password should be given
(but the operation will fail silently if it's incorrect) */
if (pswrd_.isEmpty())
return;
args << "-mhe=on" << "-p" + pswrd_;
}
else if (!pswrd_.isEmpty()) {
/* always add files with encryption if any */
args << "-p" + pswrd_;
encrypted_ = true;
}
args << "a" << fileArgs_ << filePaths;
starting7z_ = true;
keyArgs_ << "a";
proc_.start ("7z", args);
return;
}
/* NOTE: All paths should have the same parent directory.
Check that and put the wrong paths into insertQueue_. */
QString parent = filePaths.at(0).section("/", 0, -2);
insertQueue_.clear();
for (int i = 1; i < filePaths.length(); i++) {
if (filePaths.at(i).section("/", 0, -2) != parent) {
insertQueue_ << filePaths.takeAt(i);
i--;
}
}
args << "-c" << "-a";
args << fileArgs_;
/* now, setup the parent dir */
if (!absolutePaths) {
for (int i = 0; i < filePaths.length(); i++) {
filePaths[i] = filePaths[i].section(parent, 1, -1);
if (filePaths[i].startsWith("/"))
filePaths[i].remove(0, 1);
}
args << "-C" << parent;
}
else
args << "-C" << "/";
args << filePaths;
if (QFile::exists(filepath_)) { // append to the existing archive
skipExistingFiles(tmpfilepath_); // practically not required
args.replaceInStrings(filepath_, tmpfilepath_);
args << "@" + filepath_;
}
keyArgs_ << "-c" << "-a" << "-C";
proc_.start(tarCmnd_, args);
}
void Backend::startRemove(const QStringList& paths) {
keyArgs_.clear();
if (isGzip_) return;
QStringList filePaths = paths;
if (filePaths.contains(filepath_))
filePaths.removeAll(filepath_);
if (contents_.isEmpty() || filePaths.isEmpty() || !QFile::exists(filepath_))
return; // invalid
filePaths.removeDuplicates();
QStringList args;
if (is7z_) {
if (encrypted_)
args << "-p" + pswrd_;
args << "d" << fileArgs_ << filePaths;
starting7z_ = true;
keyArgs_ << "d";
proc_.start("7z", args);
return;
}
args << "-c" << "-a";
args << fileArgs_;
skipExistingFiles(tmpfilepath_); // practically not required
args.replaceInStrings(filepath_, tmpfilepath_);
for (int i = 0; i < filePaths.length(); i++) {
args << "--exclude" << escapedWildCard(filePaths.at(i));
}
args << "@" + filepath_;
keyArgs_ << "-c" << "-a" << "--exclude";
proc_.start(tarCmnd_, args);
}
void Backend::startExtract(const QString& path, const QString& file, bool overwrite, bool preservePaths) {
startExtract(path, QStringList() << file, overwrite, preservePaths);
}
void Backend::startExtract(const QString& path, const QStringList& files, bool overwrite, bool preservePaths) {
keyArgs_.clear();
if (isGzip_) {
/* if the extraction takes place in the same directory, we could do it
in the usual way but the standard output method works in all cases */
/*if (0 && path == filepath_.section("/", 0, -2)) {
proc_.start("gzip", QStringList() << "-d" << "-k" << filepath_);
return;
}*/
emit processStarting();
QString extName = filepath_.section("/", -1);
if (extName.contains(".")) {
extName = extName.section(".", 0, -2);
}
if (extName.isEmpty())
extName = "arqiver-extracted";
if (filepath_.endsWith(".svgz") && !extName.endsWith(".svg"))
extName += ".svg";
extName = path + "/" + extName;
skipExistingFiles(extName);
tmpProc_.setStandardOutputFile(extName);
tmpProc_.start("gzip", QStringList() << "-d" << "--to-stdout" << filepath_); // gzip -d -c archive.gz > file
if (tmpProc_.waitForStarted()) {
while (!tmpProc_.waitForFinished(500))
QCoreApplication::processEvents();
}
emit processFinished(tmpProc_.exitCode() == 0, QString());
emit extractionFinished();
emit extractionSuccessful();
return;
}
QStringList args;
QStringList filesList = files;
filesList.removeAll(QString());
if (!filesList.isEmpty()) {
filesList.removeDuplicates();
/* the paths may contain newlines, which have been escaped and are restored here */
filesList.replaceInStrings(QRegularExpression("(?<!\\\\)\\\\n"), "\n")
.replaceInStrings(QRegularExpression("(?<!\\\\)\\\\t"), "\t");
}
if (is7z_) {
if (filesList.isEmpty())
args << "-aou"; // auto-rename: the archive may contain files with identical names
else if (overwrite)
args << "-aoa"; // overwrite without prompt
else
args << "-aos"; // skip extraction of existing files
if (encrypted_)
args << "-p" + pswrd_;
args << (preservePaths ? "x" : "e") << fileArgs_;
keyArgs_ << "x" << "e";
starting7z_ = true;
}
else {
args << "-x" << "--no-same-owner";
if (!overwrite)
args << "-k";
args << fileArgs_;
if (!filesList.isEmpty()) {
/* If a file comes after its containing folder in the command line,
bsdtar doesn't extract the folder. So, we sort the list and read it inversely. */
filesList.sort();
int N = filesList.length();
for (int i = 0; i < N; i++) {
if (filesList[N - 1 - i].simplified().isEmpty())
continue;
args << "--include" << escapedWildCard(filesList[N - 1 - i])
<< "--strip-components" << QString::number(filesList[N - 1 - i].count("/"));
}
}
keyArgs_ << "-x";
}
QString xPath = path;
bool archiveRootExists(false);
/* prevent overwriting by making an appropriate directory and extracting into it
if the whole archive is going to be extracted (otherwise, overwriting will be handled by mainWin) */
if (filesList.isEmpty()) {
QString archiveSingleRoot = archiveSingleRoot_;
if (!archiveSingleRoot.isEmpty() && archiveSingleRoot.startsWith("."))
archiveSingleRoot.remove(0, 1); // no hidden extraction folder
if (!archiveSingleRoot.isEmpty()) { // is empty with some rpm archives or when an encrypted list isn't known yet
archiveSingleRoot.replace(QRegularExpression("(?<!\\\\)\\\\n"), "\n")
.replace(QRegularExpression("(?<!\\\\)\\\\t"), "\t");
if (QFile::exists(xPath + "/" + archiveSingleRoot)) {
archiveRootExists = true;
QDir dir (xPath);
QString subdirName = archiveSingleRoot;
xPath += "/" + subdirName;
int i = 0;
QString suffix;
while (QFile::exists(xPath + suffix)) {
suffix = "-" + QString::number(i);
++i;
}
xPath += suffix;
subdirName += suffix;
dir.mkdir(subdirName);
}
}
else { // the archive doesn't have a parent dir and isn't a single file
QDir dir(xPath);
QString subdirName = filepath_.section("/", -1);
if (subdirName.contains(".")) {
subdirName = subdirName.section(".", 0, -2);
if (subdirName.endsWith(".tar"))
subdirName = subdirName.section(".", 0, -2);
}
if (subdirName.isEmpty())
subdirName = "arqiver-extracted";
xPath += "/" + subdirName;
int i = 0;
QString suffix;
while (QFile::exists(xPath + suffix)) {
suffix = "-" + QString::number(i);
++i;
}
xPath += suffix;
subdirName += suffix;
dir.mkdir(subdirName);
}
}
if (is7z_) {
args << "-o" + xPath;
if (!filesList.isEmpty())
args << filesList;
proc_.start("7z", args);
}
else {
args << "-C" << xPath;
if (archiveRootExists && contents_.size() > 1)
args << "--strip-components" << "1"; // the parent name is changed
keyArgs_ << "-C";
proc_.start(tarCmnd_, args); // doesn't create xPath if not existing
}
}
bool Backend::isEncryptedPath(const QString& path) const {
if (!is7z_) return false;
return (encryptedList_ || encryptedPaths_.contains(path));
}
bool Backend::isSingleExtracted(const QString& archivePath) const {
if (arqiverDir_.isEmpty()) return false;
QString str = archivePath;
if (str.startsWith("./")) // as with some rpm archives
str.remove(0, 2);
return (!str.isEmpty() && QFile::exists(arqiverDir_ + "/" + str));
}
bool Backend::allChildrenExyracted(const QString& parent) const {
if (arqiverDir_.isEmpty()) return false;
const QStringList files = contents_.keys();
if (isDir(parent) || !files.contains(parent)) {
QString p = parent + "/";
for (const auto &file : files) {
if (file != p && file.startsWith(p)) {
QString str = file;
if (str.startsWith("./")) // as with some rpm archives
str.remove(0, 2);
if (!QFile::exists(arqiverDir_ + "/" + str))
return false;
}
}
}
return true;
}
static inline bool removeDir(const QString &dirName)
{
bool res = true;
QDir dir(dirName);
if (dir.exists()) {
const QFileInfoList infoList = dir.entryInfoList(QDir::Files | QDir::AllDirs
| QDir::NoDotAndDotDot | QDir::System | QDir::Hidden,
QDir::DirsFirst);
for (const QFileInfo& info : infoList) {
if (info.isDir())
res = removeDir(info.absoluteFilePath());
else
res = QFile::remove(info.absoluteFilePath());
if (!res) return res;
}
res = dir.rmdir(dirName);
}
return res;
}
void Backend::removeSingleExtracted(const QString& archivePath) const {
if (!arqiverDir_.isEmpty()) {
const QString filePath = arqiverDir_ + "/" + archivePath;
if (QFile::exists(filePath)) {
QFileInfo info(filePath);
if (info.isDir())
removeDir(filePath);
else
QFile::remove(filePath);
}
}
}
// Returns false only when a password is needed but it's nonempty and wrong.
bool Backend::startViewFile(const QString& path) {
/* the path may contain newlines, which have been escaped and are restored here */
QString realPath(path);
realPath.replace(QRegularExpression("(?<!\\\\)\\\\n"), "\n")
.replace(QRegularExpression("(?<!\\\\)\\\\t"), "\t");
QString parentDir = arqiverDir_;
if (!arqiverDir_.isEmpty()) {
QDir dir(arqiverDir_);
if (realPath.contains("/")) {
parentDir = arqiverDir_ + "/" + realPath.section("/", 0, -2);
dir.mkpath(parentDir); // also creates "dir" if needed
}
else if (!dir.exists())
dir.mkpath(arqiverDir_);
}
QString fileName = (arqiverDir_.isEmpty() ? QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz")
: parentDir + "/")
+ realPath.section("/",-1);
QFile file(fileName);
bool fileExists(file.exists());
if (fileExists && file.size() == static_cast<qint64>(0)) {
file.remove();
fileExists = false;
}
if (!fileExists) {
QStringList args;
if (is7z_) {
args << "-aou"; // the archive may contain files with identical names
if (encrypted_)
args << "-p" + pswrd_;
args << "x" << fileArgs_ << "-o" + arqiverDir_;
args << "-y"; // required with multiple passwords (says yes to the overwrite prompt)
args << realPath;
emit processStarting();
tmpProc_.setStandardOutputFile(QProcess::nullDevice());
tmpProc_.start("7z", args);
if (tmpProc_.waitForStarted()) {
while (!tmpProc_.waitForFinished(500))
QCoreApplication::processEvents();
}
bool res(tmpProc_.exitCode() == 0 || pswrd_.isEmpty());
if (!res)
pswrd_ = QString(); // avoid overwrite prompt if there are more than one password
emit processFinished(tmpProc_.exitCode() == 0, QString());
if (tmpProc_.exitCode() == 0) {
if (!QFileInfo::exists(fileName)) { // handle links
errorMsg(tr("This file is a link but its target does not exist."));
}
else if (!QProcess::startDetached("gio", QStringList() << "open" << fileName)) // "gio" is more reliable
QDesktopServices::openUrl(QUrl::fromLocalFile(fileName));
}
return res;
}
QString cmnd;
if (isGzip_) {
cmnd = "gzip";
args << "-d" << "--to-stdout" << filepath_;
emit processStarting();
tmpProc_.setStandardOutputFile(fileName);
}
else { // bsdtar
cmnd = tarCmnd_;
args << "-x" << "--no-same-owner" << "-k" << fileArgs_
<< "--include" << escapedWildCard(realPath) << "-C" << arqiverDir_;
/* NOTE: The following arguments were also possible with "fileName" as the standard output
(as with Gzip) but symlinks couldn't be handled in that way. */
//args << "-x" << fileArgs_ << "--include" << escapedWildCard(realPath) << "--to-stdout";
emit processStarting();
tmpProc_.setStandardOutputFile(QProcess::nullDevice());
}
tmpProc_.start(cmnd, args);
if (tmpProc_.waitForStarted()) {
while (!tmpProc_.waitForFinished(500))
QCoreApplication::processEvents();
}
emit processFinished(tmpProc_.exitCode() == 0, QString());
if (tmpProc_.exitCode() != 0)
return true;
if (!QFileInfo::exists(fileName)) { // handle links
errorMsg(tr("This file is a link but its target does not exist."));
return true;
}
}
else
emit processFinished(true, QString());
if (!QProcess::startDetached("gio", QStringList() << "open" << fileName)) // "gio" is more reliable
QDesktopServices::openUrl(QUrl::fromLocalFile(fileName));
return true;
}
void Backend::extractTempFiles(const QStringList& paths) {
QStringList realPaths(paths);
realPaths.removeAll(QString());
if (!realPaths.isEmpty()) {
realPaths.removeDuplicates();
realPaths.replaceInStrings(QRegularExpression("(?<!\\\\)\\\\n"), "\n")
.replaceInStrings(QRegularExpression("(?<!\\\\)\\\\t"), "\t");
}
else { // should never happen in practice
emit processFinished(true, QString());
emit tempFilesExtracted(QStringList());
}
QStringList tempFileNames;
if (!arqiverDir_.isEmpty()) {
QDir dir(arqiverDir_);
if (!dir.exists())
dir.mkpath(arqiverDir_);
QList<QString>::iterator it = realPaths.begin();
while (it != realPaths.end()) {
QString realPath = *it;
if (realPath.simplified().isEmpty()) {
it = realPaths.erase(it);
continue;
}
if (realPath.startsWith("./") || realPath == ".") { // as with some rpm archives
realPath.remove(0, 2);
if (realPath.isEmpty()) {
it = realPaths.erase(it);
continue;
}
}
QString parentDir;
if (realPath.contains("/")) {
parentDir = arqiverDir_ + "/" + realPath.section("/", 0, -2);
dir.mkpath(parentDir);
}
else
parentDir = arqiverDir_; // top-level
QString fileName = parentDir + "/" + realPath.section("/",-1);
tempFileNames << fileName;
/* check whether it's already extracted (FIXME: this doesn't cover all symlinks) */
if (QFile::exists(fileName)
&& allChildrenExyracted(*it)) { // realPath may have changed above
it = realPaths.erase(it);
}
else ++it;
}
}
if (!realPaths.isEmpty()) {
QStringList args;
/* Gzip */
if (isGzip_) {
if (tempFileNames.size() == 1) {
args << "-d" << "--to-stdout" << filepath_;
emit processStarting();
tmpProc_.setStandardOutputFile(tempFileNames.at(0));
tmpProc_.start("gzip", args);
if (tmpProc_.waitForStarted()) {
while (!tmpProc_.waitForFinished(500))
QCoreApplication::processEvents();
}
emit processFinished(tmpProc_.exitCode() == 0, QString());
emit tempFilesExtracted(tempFileNames);
}
return;
}
/* 7z */
if (is7z_) {
args << "-aos"; // skip extraction of existing files
if (encrypted_ )
args << "-p" + pswrd_;
args << "x" << fileArgs_ << "-o" + arqiverDir_ << "-y" << realPaths;
emit processStarting();
tmpProc_.setStandardOutputFile(QProcess::nullDevice());
tmpProc_.start("7z", args);
if (tmpProc_.waitForStarted()) {
while (!tmpProc_.waitForFinished(500))
QCoreApplication::processEvents();
}
if (tmpProc_.exitCode() != 0)
pswrd_ = QString();
emit processFinished(tmpProc_.exitCode() == 0, QString());
emit tempFilesExtracted(tempFileNames);
return;
}
/* bsdtar */
args << "-x" << "--no-same-owner" << "-k" << fileArgs_;
/* If a file comes after its containing folder in the command line,
bsdtar doesn't extract the folder. So, we sort the list and read it inversely. */
realPaths.sort();
int N = realPaths.length();
for (int i = 0; i < N; i++)
args << "--include" << escapedWildCard(realPaths[N - 1 - i]);
args << "-C" << arqiverDir_;
emit processStarting();
tmpProc_.setStandardOutputFile(QProcess::nullDevice());
tmpProc_.start(tarCmnd_, args);
if (tmpProc_.waitForStarted()) {
while (!tmpProc_.waitForFinished(500))
QCoreApplication::processEvents();
}
emit processFinished(tmpProc_.exitCode() == 0, QString());
emit tempFilesExtracted(tempFileNames);
}
else { // all paths are already extracted
emit processFinished(true, QString());
emit tempFilesExtracted(tempFileNames);
}
}
void Backend::parseLines(QStringList& lines) {
static bool hasSingleRoot = false;
if (contents_.isEmpty()) {
hasSingleRoot = true;
archiveSingleRoot_ = QString(); // if existent, may mean a parent dir or single file
}
if (is7z_) {
static int attrIndex = 0;
static int cSizeIndex = 0; // end of the compressed size column
static int nameIndex = 0;
if (contents_.isEmpty()) {
attrIndex = cSizeIndex = nameIndex = 0;
}
if (starting7z_) {
/* ignore all p7zip header info */
while (starting7z_ && !lines.isEmpty()) {
if (lines.at(0) == "--")
starting7z_ = false; // found the end of the headers
lines.removeAt(0);
}
}
for (int i = 0; i < lines.length(); i++) {
if (lines.at(i).simplified().isEmpty() || lines.at(i).startsWith("----") || lines.at(i).startsWith(" = "))
continue;
if (LIST) {
QString file;
#if (QT_VERSION >= QT_VERSION_CHECK(5,15,0))
QStringList info = lines.at(i).split(" ",Qt::SkipEmptyParts);
#else
QStringList info = lines.at(i).split(" ",QString::SkipEmptyParts);
#endif
if (info.size() < 2) continue; // invalid line
// Format: [Date, Time, Attr, Size, Compressed, Name]
if (info.size() >= 6 && info.at(2) == "Attr") { // header
attrIndex = lines.at(i).indexOf(info.at(2));
cSizeIndex = lines.at(i).indexOf(info.at(4)) + info.at(4).size();
nameIndex = lines.at(i).indexOf(info.at(5));
continue;
}
const int lineSize = lines.at(i).size();
if (attrIndex <= 0 || cSizeIndex <= 0 || nameIndex < 3
|| attrIndex >= lineSize || cSizeIndex >= lineSize || nameIndex >= lineSize) {
continue; // the header should be read first
}
/* we suppose that the row starts either with Date or with Attr (Date and Time are empty) */
QString attrStr = lines.at(i).mid(attrIndex - 1, 5); // the Attr column has 5 characters (like "....A")
if (!attrStr.contains("."))
continue; // a row that isn't related to a file
bool hasCSize = !lines.at(i).at(cSizeIndex - 1).isSpace();
if (info.size() < 5) {
if (!info.at(0).contains(".")) continue; // should start with Attr (no Date and Time)
file = lines.at(i).right(lineSize - nameIndex);
if (file.isEmpty()) continue;
if (info.size() == 2) { // only Attr and name (as with "application/x-bzip")
if (!contents_.isEmpty()) continue; // invalid line
archiveSingleRoot_ = file.section('/', 0, 0);
QString s, cs;
if (i < lines.length() - 2
&& lines.at(i+1).startsWith("---") // next line is end of table
&& attrIndex + 5 < lines.at(i+2).size()
&& lines.at(i+2).left(attrIndex + 6).simplified().isEmpty()) { // no Attr and nothing before it
#if (QT_VERSION >= QT_VERSION_CHECK(5,15,0))
QStringList infoNext = lines.at(i+2).split(" ",Qt::SkipEmptyParts);
#else
QStringList infoNext = lines.at(i+2).split(" ",QString::SkipEmptyParts);
#endif
// Format of infoNext: [Size(?), Compressed, Number(=1), "files"]
if (!infoNext.isEmpty()) {
if (cSizeIndex < lines.at(i+2).size() && !lines.at(i+2).at(cSizeIndex - 1).isSpace()) {
if (infoNext.size() == 3) {
cs = infoNext.at(0);
s = QString::number(0); // size is missing
}
else if (infoNext.size() == 4)
cs = infoNext.at(1);
}
if (s.isEmpty()) s = infoNext.at(0);
}
}
if (s.isEmpty()) s = QString::number(0);
if (cs.isEmpty()) cs = QString::number(0);
contents_.insert(file, QStringList() << attrStr << s << cs);
return;
}
contents_.insert(file,
QStringList() << attrStr << info.at(1)
<< (hasCSize ? info.at(2) : QString::number(0)));
}
else {
file = lines.at(i).right(lineSize - nameIndex);
if (file.isEmpty()) continue;
if (info.at(0).contains(".")) { // starts with Attr (no Date and Time)
contents_.insert(file,
QStringList() << attrStr << info.at(1)
<< (hasCSize ? info.at(2) : QString::number(0)));
}
else {
contents_.insert(file,
QStringList() << attrStr << info.at(3)
<< (hasCSize ? info.at(4) : QString::number(0)));
}
}
if (hasSingleRoot) {
if (archiveSingleRoot_.isEmpty()) {
archiveSingleRoot_ = file.section('/', 0, 0);
if (archiveSingleRoot_.isEmpty())
hasSingleRoot = false;
}
else if (archiveSingleRoot_ != file.section('/', 0, 0)) {
hasSingleRoot = false;
archiveSingleRoot_ = QString();
}
}
}
}
return;
}
for (int i = 0; i < lines.length(); i++) {
if (isGzip_) {
// lines[i] is: <compressed_size> <uncompressed_size> -33.3% /x/y/z}
#if (QT_VERSION >= QT_VERSION_CHECK(5,15,0))
QStringList info = lines.at(i).split(" ",Qt::SkipEmptyParts);
#else
QStringList info = lines.at(i).split(" ",QString::SkipEmptyParts);
#endif
if (contents_.isEmpty() && info.size() >= 4) {
int indx = lines.at(i).indexOf("% ");
if (indx > -1) {
QString file = lines.at(i).mid(indx + 2).section('/', -1);
if (filepath_.endsWith(".svgz") && file.endsWith(".svgz")) {
/* WARNING: gzip lists the file as svgz again */
file.remove(QRegularExpression("svgz$"));
file += "svg";
}
archiveSingleRoot_ = file.section('/', 0, 0);
contents_.insert(file,
QStringList() << "-rw-r--r--" << info.at(1) << QString()); // [perms, size, linkto]
}
}
return;
}
int indx;
QRegularExpressionMatch match;
if (getMimeType(filepath_) == "application/zip") {
static const QRegularExpression badZipExp("^\\s*x\\s+");
indx = lines.at(i).indexOf(badZipExp, 0 , &match);
if (indx == 0) {
/* ZIP archives may not have all the extra information - just file names */
QString file = lines.at(i).right(lines.at(i).length() - match.capturedLength());
QString perms;
if (file.endsWith("/")) {
perms = "d";
file.chop(1);
}
if (file.isEmpty()) continue; // impossible
if (hasSingleRoot) {
if (archiveSingleRoot_.isEmpty()) {
archiveSingleRoot_ = file.section('/', 0, 0);
if (archiveSingleRoot_.isEmpty())
hasSingleRoot = false;
}
else if (archiveSingleRoot_ != file.section('/', 0, 0)) {
hasSingleRoot = false;
archiveSingleRoot_ = QString();
}
}
contents_.insert (file, QStringList() << perms << "-1" << QString()); // [perms, size, linkto ]
continue;
}
}
// here, lines[i] is like: -rw-r--r-- 0 USER GROUP 32616 Oct 4 2018 PATH
static const QRegularExpression nonPathExp("(\\S+\\s+){7}\\S+ ");
indx = lines.at(i).indexOf(nonPathExp, 0 , &match);
if (indx != 0 || match.capturedLength() == lines.at(i).length())
continue; // invalid line
QStringList info;
#if (QT_VERSION >= QT_VERSION_CHECK(5,15,0))
info << lines.at(i).left(match.capturedLength()).split(" ",Qt::SkipEmptyParts);
#else
info << lines.at(i).left(match.capturedLength()).split(" ",QString::SkipEmptyParts); // 8 elements
#endif
info << lines.at(i).right(lines.at(i).length() - match.capturedLength());
// here, info is like ("-rw-r--r--", "1", "0", "0", "645", "Feb", "5", "2016", "x/y -> /a/b")
QString file = info.at(8);
if (file.endsWith("/"))
file.chop(1);
if (file.isEmpty()) // possible in rare cases (with "application/x-archive", for example)
continue;
QString linkto;
/* see if this file has the "link to" or "->" notation */
if (file.contains(" -> ")) {
linkto = file.section(" -> ", 1, -1);
file = file.section(" -> ", 0, 0);
}
else if (file.contains(" link to ")) {
/* alternate form of a link within a tar archive (not reflected in perms) */
linkto = file.section(" link to ", 1, -1);
file = file.section(" link to ", 0, 0);
if (info.at(0).startsWith("-"))
info[0].replace(0, 1, "l");
}
if (hasSingleRoot) {
if (archiveSingleRoot_.isEmpty()) {
archiveSingleRoot_ = file.section('/', 0, 0);
if (archiveSingleRoot_.isEmpty())
hasSingleRoot = false;
}
else if (archiveSingleRoot_ != file.section('/', 0, 0)) {
hasSingleRoot = false;
archiveSingleRoot_ = QString();
}
}
contents_.insert(file, QStringList() << info.at(0) << info.at(4) << linkto); // [perms, size, linkto ]
}
}
void Backend::startList(bool withPassword) {
contents_.clear();
keyArgs_.clear();
LIST = true;
if (isGzip_) {
keyArgs_ << "-l";
proc_.start("gzip", QStringList() << "-l" << filepath_);
}
else if (is7z_) {
QStringList args;
args << "-p" + (withPassword ? pswrd_ : QString()); // needed for encrypted lists
if (!encryptionQueried_)
args << "-slt"; // query encryption instead of listing
args << "l";
starting7z_ = true;
keyArgs_ << "l";
proc_.start("7z", QStringList() << args << fileArgs_);
}
else {
QStringList args;
args << "-tv";
keyArgs_ << "-tv";
proc_.start(tarCmnd_, QStringList() << args << fileArgs_);
}
}
void Backend::procFinished(int retcode, QProcess::ExitStatus) {
if (isKilled_) {
isKilled_ = false;
if (keyArgs_.contains("l") || keyArgs_.contains("-tv") || keyArgs_.contains("-l")) { // listing
/* reset most variables (the rest will be reset with the next loading) */
LIST = false;
isGzip_ = is7z_ = false;
starting7z_ = encryptionQueried_ = encrypted_ = encryptedList_ = false;
keyArgs_.clear();
contents_.clear();
insertQueue_.clear();
encryptedPaths_.clear();
pswrd_ = archiveSingleRoot_ = result_ = data_ = QString();
emit processFinished(false, tr("Could not read archive"));
return;
}
}
if (is7z_ && !encryptionQueried_ && keyArgs_.contains("l")) {
encryptionQueried_ = true;
if (encryptedList_)
emit encryptedList(filepath_); // its slot should get password and load the file again
else {
/* now, really start listing */
QStringList args;
if (!pswrd_.isEmpty()) // happens only when the list is encrypted
args << "-p" + pswrd_;
args << "l";
starting7z_ = true;
keyArgs_.clear(); keyArgs_ << "l";
proc_.start("7z", QStringList() << args << fileArgs_);
}
return;
}
/* NOTE: processFinished() should be emitted once, in the end */
processData();
LIST = false;
if (is7z_) {
starting7z_ = false;
if (keyArgs_.contains("l")) { // listing
emit loadingFinished();
if (retcode != 0) {
contents_.clear();
result_ = tr("Could not read archive");
}
else if (result_.isEmpty()) {
result_ = tr("Archive Loaded");
emit loadingSuccessful();
}
emit processFinished(retcode == 0, result_);
result_.clear();
}
else if (keyArgs_.contains("a") || keyArgs_.contains("d")) { // addition/removal
result_ = tr("Modification Finished");
emit archivingSuccessful();
if (!encryptedList_) {
/* We want to know which files are encrypted after an addition.
After a deletion, it makes no difference. */
encryptionQueried_ = false;
encryptedPaths_.clear();
}
startList(encryptedList_);
}
else { // extraction
/*if (retcode == 0) {
QStringList args = proc_.arguments();
for (int i = 0; i < args.length(); i++) {
if (args.at(i).startsWith("-o")) //just extracted to a dir - open it now
QProcess::startDetached("xdg-open \"" + args.at(i).section("-o", 1, -1) + "\"");
}
}*/
emit extractionFinished();
if (retcode == 0) {
emit extractionSuccessful();
result_ = tr("Extraction Finished");
}
else if (result_.isEmpty())
result_ = tr("Extraction Failed");
emit processFinished(retcode == 0, result_);
result_.clear();
}
return;
}
if (keyArgs_.contains("-tv") || keyArgs_.contains("-l")) { // listing
if (retcode != 0) {
contents_.clear();
result_ = tr("Could not read archive");
}
else if (result_.isEmpty()) {
emit loadingFinished();
if (retcode == 0) {
result_ = tr("Archive Loaded");
emit loadingSuccessful();
}
if (isGzip_)
emit archivingSuccessful(); // because the created archive is just loaded (-> startAdd)
}
emit processFinished(retcode == 0, result_);
result_.clear();
}
else
{
bool updateList = true;
if (keyArgs_.contains("-x")) { // extraction
updateList = false;
emit extractionFinished();
if (retcode == 0)
{
result_ = tr("Extraction Finished");
emit extractionSuccessful();
/*if (args.count("--include") == 1) {
//Need to find the full path to the (single) extracted file
QString path = args.last() +"/"+ args[ args.indexOf("--include") + 1].section("/", -1);
QFile::setPermissions(path, QFileDevice::ReadOwner);
QProcess::startDetached("xdg-open \"" + path + "\"");
}
else {
//Multi-file extract - open the dir instead
QString dir = args.last();
//Check to see if tar extracted into a new subdir it just created
if (QFile::exists(dir + "/" + filepath_.section("/", -1).section(".", 0, 0))) {
dir = dir + "/" + filepath_.section("/", -1).section(".", 0, 0);
}
QProcess::startDetached("xdg-open \"" + dir + "\""); //just extracted to a dir - open it now
}*/
}
else if (result_.isEmpty())
result_ = tr("Extraction Failed");
}
else if (keyArgs_.contains("-c")) { // addition/removal
if (QFile::exists(tmpfilepath_)) {
if (retcode == 0) {
QFile::remove(filepath_);
QFile::rename(tmpfilepath_, filepath_);
}
else
QFile::remove(tmpfilepath_);
}
result_ = tr("Modification Finished");
if (insertQueue_.isEmpty())
emit archivingSuccessful();
else {
updateList = false;
QTimer::singleShot(0, this, &Backend::startInsertFromQueue);
}
}
if (updateList)
startList();
else {
emit processFinished(retcode == 0, result_);
result_.clear();
}
}
}
void Backend::processData() {
if (is7z_ && !encryptionQueried_) {
if (!encryptedList_) {
QString read = proc_.readAllStandardOutput();
if (read.contains("\nERROR: ")) { // ERROR: FILE_PATH : Can not open encrypted archive. Wrong password?
encryptedList_ = encrypted_ = true;
}
else {
#if (QT_VERSION >= QT_VERSION_CHECK(5,15,0))
const QStringList& items = read.split("\n\n", Qt::SkipEmptyParts);
#else
const QStringList& items = read.split("\n\n", QString::SkipEmptyParts);
#endif
for (const QString& thisItem : items) {
if (thisItem.contains("\nEncrypted = +")) {
/* the archive has an encrypted file but its header isn't encrypted */
#if (QT_VERSION >= QT_VERSION_CHECK(5,15,0))
QStringList lines = thisItem.split("\n", Qt::SkipEmptyParts);
#else
QStringList lines = thisItem.split("\n", QString::SkipEmptyParts);
#endif
if (!lines.isEmpty()) {
QString pathLine;
if (lines.at(0).startsWith("Path = "))
pathLine = lines.at(0);
else if (lines.at(1).startsWith("Path = ")) // the first line consists of dashes
pathLine = lines.at(1);
if (!pathLine.isEmpty())
encryptedPaths_ << pathLine.remove(0, 7);
}
encrypted_ = true;
}
}
}
}
return; // no listing here
}
QString read = data_ + proc_.readAllStandardOutput();
if (read.endsWith("\n"))
data_.clear();
else {
data_ = read.section("\n", -1);
read = read.section("\n", 0, -2);
}
/* NOTE: Because 7x doesn't escape newlines in file names, the archives
that contain such files can't be processed correctly. */
#if (QT_VERSION >= QT_VERSION_CHECK(5,15,0))
QStringList lines = read.split("\n", Qt::SkipEmptyParts);
#else
QStringList lines = read.split("\n", QString::SkipEmptyParts);
#endif
/* Gzip doesn't escape newlines either but that can be worked around */
if (isGzip_ && lines.size() >= 2) {
lines.removeFirst();
QString joined = lines.join('\n');
lines.clear();
lines << joined;
}
if (LIST)
parseLines(lines);
if (is7z_) {
if (read.contains("\nERROR")) { // ERROR: Data Error in encrypted file. Wrong password?
/* while extracting the archive, another file in it had another password */
pswrd_ = QString();
}
emit progressUpdate(-1, QString());
}
else {
QString info;
if (!lines.isEmpty())
info = lines.last();
emit progressUpdate(-1, info);
}
}
void Backend::onError(QProcess::ProcessError error) {
if (error == QProcess::FailedToStart)
emit errorMsg(tr("%1 is missing from your system.\nPlease install it for this kind of archive!")
.arg(proc_.program()));
}
bool Backend::isWorking() { // used only with DND
return (proc_.state() == QProcess::Running
|| tmpProc_.state() == QProcess::Running);
}
void Backend::killProc() {
if (proc_.state() == QProcess::Running) {
isKilled_ = true;
proc_.kill();
}
else if (tmpProc_.state() == QProcess::Running) { // tmpProc_ starts after proc_ is finished
tmpProc_.kill();
}
}
}
|