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
|
#include "ScanFileOrFolder.h"
#include "TransferThread.h"
#include <QtGlobal>
#include <regex>
#include "../../../cpp11addition.h"
#ifdef Q_OS_WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#endif
#ifdef WIDESTRING
std::wstring ScanFileOrFolder::text_slash=L"/";
std::wstring ScanFileOrFolder::text_antislash=L"\\";
std::wstring ScanFileOrFolder::text_dot=L".";
#else
std::string ScanFileOrFolder::text_slash="/";
std::string ScanFileOrFolder::text_antislash="\\";
std::string ScanFileOrFolder::text_dot=".";
#endif
ScanFileOrFolder::ScanFileOrFolder(const Ultracopier::CopyMode &mode) :
moveTheWholeFolder(false),
stopIt(false),
stopped(false),
folderExistsAction(FolderExistsAction::FolderExists_NotSet),
fileErrorAction(FileErrorAction::FileError_NotSet),
checkDestinationExists(false),
copyListOrder(false),
#ifdef ULTRACOPIER_PLUGIN_RSYNC
rsync(false),
#endif
mode(Ultracopier::CopyMode::Copy),
reloadTheNewFilters(false),
haveFilters(false)
{
#ifdef ULTRACOPIER_PLUGIN_RSYNC
rsync = false;
#endif
moveTheWholeFolder = true;
stopped = true;
stopIt = false;
this->mode = mode;
folder_isolation = std::regex("^(.*/)?([^/]+)/$");
setObjectName(QStringLiteral("ScanFileOrFolder"));
#ifdef Q_OS_WIN32
QString userName;
DWORD size=255;
WCHAR * userNameW=new WCHAR[size];
if(GetUserNameW(userNameW,&size))
{
#ifdef WIDESTRING
blackList.push_back(INTERNALTYPEPATH(L"C:/Users/")+userNameW+L"/AppData/Roaming/");
blackList.push_back(INTERNALTYPEPATH(L"C:\\Users\\")+userNameW+L"\\AppData\\Roaming\\");
#else
blackList.push_back(INTERNALTYPEPATH("C:/Users/")+userNameW+"/AppData/Roaming/");
blackList.push_back(INTERNALTYPEPATH("C:\\Users\\")+userNameW+"\\AppData\\Roaming\\");
#endif
}
delete userNameW;
#endif
}
ScanFileOrFolder::~ScanFileOrFolder()
{
stop();
quit();
wait();
}
bool ScanFileOrFolder::isFinished() const
{
return stopped;
}
void ScanFileOrFolder::addToList(const std::vector<INTERNALTYPEPATH>& sources,const INTERNALTYPEPATH& destination)
{
stopIt=false;
this->sources=parseWildcardSources(sources);
this->destination=destination;
#ifdef WIDESTRING
QFileInfo destinationInfo(QString::fromStdWString(this->destination));
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"check symblink: "+destinationInfo.absoluteFilePath().toStdString()+", destination: "+TransferThread::internalStringTostring(destination));
#ifdef WIDESTRING
while(TransferThread::is_symlink(destinationInfo.absoluteFilePath().toStdWString()))
#else
while(TransferThread::is_symlink(destinationInfo.absoluteFilePath().toStdString()))
#endif
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"resolv destination to: "+destinationInfo.symLinkTarget().toStdString());
if(QFileInfo(destinationInfo.symLinkTarget()).isAbsolute())
this->destination=destinationInfo.symLinkTarget().toStdWString();
else
this->destination=destinationInfo.absolutePath().toStdWString()+text_slash+destinationInfo.symLinkTarget().toStdWString();
destinationInfo.setFile(QString::fromStdWString(this->destination));
}
if(sources.size()>1 || QFileInfo(QString::fromStdWString(destination)).isDir())
/* Disabled because the separator transformation product bug
* if(!destination.endsWith(QDir::separator()))
this->destination+=QDir::separator();*/
if(!stringEndsWith(destination,'/') && !stringEndsWith(destination,'\\'))
this->destination+=text_slash;//put unix separator because it's transformed into that's under windows too
#else
QFileInfo destinationInfo(QString::fromStdString(this->destination));
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"check symblink: "+destinationInfo.absoluteFilePath().toStdString());
while(destinationInfo.isSymLink())
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"resolv destination to: "+destinationInfo.symLinkTarget().toStdString());
if(QFileInfo(destinationInfo.symLinkTarget()).isAbsolute())
this->destination=destinationInfo.symLinkTarget().toStdString();
else
this->destination=destinationInfo.absolutePath().toStdString()+text_slash+destinationInfo.symLinkTarget().toStdString();
destinationInfo.setFile(QString::fromStdString(this->destination));
}
if(sources.size()>1 || QFileInfo(QString::fromStdString(destination)).isDir())
/* Disabled because the separator transformation product bug
* if(!destination.endsWith(QDir::separator()))
this->destination+=QDir::separator();*/
if(!stringEndsWith(destination,'/') && !stringEndsWith(destination,'\\'))
this->destination+=text_slash;//put unix separator because it's transformed into that's under windows too
#endif
//ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"addToList("+stringimplode(sources,";")+","+this->destination+")");
}
std::vector<INTERNALTYPEPATH> ScanFileOrFolder::parseWildcardSources(const std::vector<INTERNALTYPEPATH> &sources) const
{
std::regex splitFolder("[/\\\\]");
std::vector<INTERNALTYPEPATH> returnList;
unsigned int index=0;
while(index<(unsigned int)sources.size())
{
std::string sourceAt=TransferThread::internalStringTostring(sources.at(index));
if(sourceAt.find("*") != std::string::npos)
{
std::vector<std::string> toParse=stringregexsplit(sourceAt,splitFolder);
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"before wildcard parse: "+sourceAt+", toParse: "+stringimplode(toParse,", "));
std::vector<std::vector<std::string> > recomposedSource;
{
std::vector<std::string> t;
t.push_back("");
recomposedSource.push_back(t);
}
while(toParse.size()>0)
{
if(toParse.front().find("*") != std::string::npos)
{
std::string toParseFirst=toParse.front();
if(toParseFirst.empty())
toParseFirst=TransferThread::internalStringTostring(text_slash);
std::vector<std::vector<std::string> > newRecomposedSource;
stringreplaceAll(toParseFirst,"*","[^/\\\\]*");
std::regex toResolv=std::regex(toParseFirst);
unsigned int index_recomposedSource=0;
while(index_recomposedSource<recomposedSource.size())//parse each url part
{
std::string fileInfo(stringimplode(recomposedSource.at(index_recomposedSource),TransferThread::internalStringTostring(text_slash)));
std::vector<TransferThread::dirent_uc> list;
if(TransferThread::is_dir(fileInfo.c_str()))
{
if(TransferThread::entryInfoList(TransferThread::stringToInternalString(fileInfo),list))
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"list the folder: "+fileInfo+", with the wildcard: "+toParseFirst);
unsigned int index_fileList=0;
while(index_fileList<list.size())
{
const std::string &fileName=TransferThread::internalStringTostring(list.at(index_fileList).d_name);
if(std::regex_match(fileName,toResolv))
{
std::vector<std::string> tempList=recomposedSource.at(index_recomposedSource);
tempList.push_back(fileName);
newRecomposedSource.push_back(tempList);
}
index_fileList++;
}
}
}
index_recomposedSource++;
}
recomposedSource=newRecomposedSource;
}
else
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"add toParse: "+stringimplode(toParse,TransferThread::internalStringTostring(text_slash)));
unsigned int index_recomposedSource=0;
while(index_recomposedSource<recomposedSource.size())
{
recomposedSource[index_recomposedSource].push_back(toParse.front());
if(!QFileInfo(QString::fromStdString(stringimplode(recomposedSource.at(index_recomposedSource),TransferThread::internalStringTostring(text_slash)))).exists())
recomposedSource.erase(recomposedSource.cbegin()+index_recomposedSource);
else
index_recomposedSource++;
}
}
toParse.erase(toParse.cbegin());
}
unsigned int index_recomposedSource=0;
while(index_recomposedSource<recomposedSource.size())
{
returnList.push_back(TransferThread::stringToInternalString(stringimplode(recomposedSource.at(index_recomposedSource),TransferThread::internalStringTostring(text_slash))));
index_recomposedSource++;
}
}
else
returnList.push_back(TransferThread::stringToInternalString(sourceAt));
index++;
}
return returnList;
}
void ScanFileOrFolder::setFilters(const std::vector<Filters_rules> &include, const std::vector<Filters_rules> &exclude)
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
QMutexLocker lock(&filtersMutex);
this->include_send=include;
this->exclude_send=exclude;
reloadTheNewFilters=true;
haveFilters=include_send.size()>0 || exclude_send.size()>0;
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"haveFilters: "+std::to_string(haveFilters)+", include_send.size(): "+std::to_string(include_send.size())+", exclude_send.size(): "+std::to_string(exclude_send.size()));
}
//set action if Folder are same or exists
void ScanFileOrFolder::setFolderExistsAction(const FolderExistsAction &action, const std::string &newName)
{
this->newName=TransferThread::stringToInternalString(newName);
folderExistsAction=action;
waitOneAction.release();
}
//set action if error
void ScanFileOrFolder::setFolderErrorAction(const FileErrorAction &action)
{
fileErrorAction=action;
waitOneAction.release();
}
void ScanFileOrFolder::stop()
{
stopIt=true;
waitOneAction.release();
}
void ScanFileOrFolder::run()
{
stopped=false;
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,
"start the listing with destination: "+TransferThread::internalStringTostring(destination)+", mode: "+std::to_string(mode));
#ifdef Q_OS_UNIX
destination=resolvDestination(destination);
#endif
#ifdef WIDESTRING
stringreplaceAll(destination,L"\\",L"/");
#else
stringreplaceAll(destination,"\\","/");
#endif
if(stopIt)
{
stopped=true;
return;
}
if(fileErrorAction==FileError_Skip)
{
stopped=true;
return;
}
unsigned int sourceIndex=0;
while(sourceIndex<sources.size())
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"size source to list: "+std::to_string(sourceIndex)+TransferThread::internalStringTostring(text_slash)+std::to_string(sources.size()));
if(stopIt)
{
stopped=true;
return;
}
INTERNALTYPEPATH source=sources.at(sourceIndex);
#ifdef WIDESTRING
stringreplaceAll(source,L"\\",L"/");
#else
stringreplaceAll(source,"\\","/");
#endif
if(TransferThread::is_dir(source))
{
/* Bad way; when you copy c:\source\folder into d:\destination, you wait it create the folder d:\destination\folder
//listFolder(source.absoluteFilePath()+QDir::separator(),destination);
listFolder(source.absoluteFilePath()+text_slash,destination);//put unix separator because it's transformed into that's under windows too
*/
//put unix separator because it's transformed into that's under windows too
INTERNALTYPEPATH tempString=destination;
if(!stringEndsWith(tempString,text_slash) && !stringEndsWith(tempString,text_antislash))
tempString+=text_slash;
tempString+=TransferThread::resolvedName(source);
if(moveTheWholeFolder && mode==Ultracopier::Move && !QFileInfo(
#ifdef WIDESTRING
QString::fromStdWString(tempString)
#else
QString::fromStdString(tempString)
#endif
).exists() &&
driveManagement.isSameDrive(TransferThread::internalStringTostring(source),TransferThread::internalStringTostring(tempString)))
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"tempString: move and not exists: "+TransferThread::internalStringTostring(tempString));
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"do real move: "+TransferThread::internalStringTostring(source)+" to "+TransferThread::internalStringTostring(tempString));
emit addToRealMove(source,tempString);
}
else
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"tempString: "+TransferThread::internalStringTostring(tempString)+" normal listing, blacklist size: "+std::to_string(blackList.size()));
//ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"tempString: "+TransferThread::internalStringTostring(tempString)+" normal listing");
if(stringEndsWith(source,'/'))
source.erase(source.end()-1);
if(stringEndsWith(tempString,'/'))
tempString.erase(tempString.end()-1);
listFolder(source,tempString);
}
}
else
{
INTERNALTYPEPATH destinationFinish=destination;
if(stringEndsWith(destinationFinish,'/') || stringEndsWith(destinationFinish,'\\'))
destinationFinish.pop_back();
destinationFinish+=text_slash;
do
{
fileErrorAction=FileError_NotSet;
if(isBlackListed(destination))
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"isBlackListed: "+TransferThread::internalStringTostring(destination));
emit errorOnFolder(destination,tr("Blacklisted folder").toStdString(),ErrorType_Folder);
waitOneAction.acquire();
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,std::string("actionNum: ")+std::to_string((int)fileErrorAction));
}
} while(fileErrorAction==FileError_Retry);
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"source: "+TransferThread::internalStringTostring(source)+" is file or symblink, is_file: "+std::to_string(TransferThread::is_file(source)));
emit fileTransfer(source,destinationFinish+TransferThread::resolvedName(source),mode);
}
sourceIndex++;
}
stopped=true;
if(stopIt)
return;
emit finishedTheListing();
}
#ifdef Q_OS_UNIX
INTERNALTYPEPATH ScanFileOrFolder::resolvDestination(const INTERNALTYPEPATH &destination)
{
INTERNALTYPEPATH temp(destination);
std::vector<char> buf(512);
ssize_t nbytes=0;
do {
buf.resize(buf.size()*2);
nbytes=readlink(TransferThread::internalStringTostring(destination).c_str(), buf.data(), buf.size());
} while (nbytes == (ssize_t)buf.size());
if (nbytes!=-1)
buf.resize(nbytes);
while(nbytes!=-1) {
temp=FSabsolutePath(temp);
if(!stringEndsWith(destination,'/')
#ifdef Q_OS_WIN32
&& !stringEndsWith(destination,'\\')
#endif
)
temp+=TransferThread::stringToInternalString("/");
temp+=TransferThread::stringToInternalString(std::string(buf.data(), buf.size()));
/// \todo change for pure c++ code
#ifdef WIDESTRING
temp=QFileInfo(QString::fromStdWString(temp)).absoluteFilePath().toStdWString();
#else
temp=QFileInfo(QString::fromStdString(temp)).absoluteFilePath().toStdString();
#endif
do {
buf.resize(buf.size() * 2);
nbytes=readlink(TransferThread::internalStringTostring(temp).c_str(), buf.data(), buf.size());
} while (nbytes == (ssize_t)buf.size());
if (nbytes!=-1)
buf.resize(nbytes);
}
return temp;
}
#endif
bool ScanFileOrFolder::isBlackListed(const INTERNALTYPEPATH &path)
{
int index=0;
int size=blackList.size();
INTERNALTYPEPATH path2=path;
while(index<size)
{
#ifdef WIDESTRING
stringreplaceAll(path2,L"\\",L"/");
#else
stringreplaceAll(path2,"\\","/");
#endif
if(stringStartWith(path2,blackList.at(index)))
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,TransferThread::internalStringTostring(path)+" start with: "+TransferThread::internalStringTostring(blackList.at(index)));
return true;
}
else
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,TransferThread::internalStringTostring(path)+" not start with: "+TransferThread::internalStringTostring(blackList.at(index)));
index++;
}
return false;
}
void ScanFileOrFolder::listFolder(INTERNALTYPEPATH source,INTERNALTYPEPATH destination)
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"source: "+TransferThread::internalStringTostring(source)+
", destination: "+TransferThread::internalStringTostring(destination)
);
if(stopIt)
return;
#ifdef Q_OS_UNIX
destination=resolvDestination(destination);
#endif
if(stopIt)
return;
if(fileErrorAction==FileError_Skip)
return;
//if is same
#ifdef ULTRACOPIER_PLUGIN_DEBUG
#ifdef WIDESTRING
if(destination.find(L"//") != std::wstring::npos)
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"path destination contains error");
if(source.find(L"//") != std::wstring::npos)
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"path source contains error");
#else
if(destination.find("//") != std::string::npos)
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"path destination contains error");
if(source.find("//") != std::string::npos)
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"path source contains error");
#endif
#endif
if(source==destination)
{
emit folderAlreadyExists(source,destination,true);
waitOneAction.acquire();
INTERNALTYPEPATH destinationSuffixPath;
switch(folderExistsAction)
{
case FolderExists_Merge:
break;
case FolderExists_Skip:
return;
break;
case FolderExists_Rename:
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"destination before rename: "+TransferThread::internalStringTostring(destination));
if(newName.empty())
{
//ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"pattern: "+folder_isolation.str());
//resolv the new name
destinationSuffixPath=TransferThread::resolvedName(destination);
int num=1;
do
{
if(num==1)
{
if(firstRenamingRule.empty())
#ifdef WIDESTRING
destinationSuffixPath=tr("%1 - copy").arg(QString::fromStdWString(TransferThread::resolvedName(destination))).toStdWString();
#else
destinationSuffixPath=tr("%1 - copy").arg(QString::fromStdString(TransferThread::resolvedName(destination))).toStdString();
#endif
else
destinationSuffixPath=TransferThread::stringToInternalString(firstRenamingRule);
}
else
{
if(otherRenamingRule.empty())
#ifdef WIDESTRING
destinationSuffixPath=tr("%1 - copy (%2)").arg(QString::fromStdWString(TransferThread::resolvedName(destination))).arg(num).toStdWString();
#else
destinationSuffixPath=tr("%1 - copy (%2)").arg(QString::fromStdString(TransferThread::resolvedName(destination))).arg(num).toStdString();
#endif
else
{
destinationSuffixPath=TransferThread::stringToInternalString(otherRenamingRule);
#ifdef WIDESTRING
stringreplaceAll(destinationSuffixPath,L"%number%",std::to_wstring(num));
#else
stringreplaceAll(destinationSuffixPath,"%number%",std::to_string(num));
#endif
}
}
#ifdef WIDESTRING
stringreplaceAll(destinationSuffixPath,L"%name%",TransferThread::resolvedName(destination));
#else
stringreplaceAll(destinationSuffixPath,"%name%",TransferThread::resolvedName(destination));
#endif
num++;
{
std::string::size_type n=destination.rfind('/');
if(n == std::string::npos)
n=destination.rfind('.');
else
n=destination.rfind(n,'.');
if(n == std::string::npos)
{
destination=FSabsolutePath(destination);
if(!stringEndsWith(destination,'/')
#ifdef Q_OS_WIN32
&& !stringEndsWith(destination,'\\')
#endif
)
destination+=text_slash;
destination+=destinationSuffixPath;
}
else
{
destination=FSabsolutePath(destination);
if(!stringEndsWith(destination,'/')
#ifdef Q_OS_WIN32
&& !stringEndsWith(destination,'\\')
#endif
)
destination+=text_slash;
destination+=destinationSuffixPath+TransferThread::stringToInternalString(".")+destination.substr(n);
}
}
}
while(TransferThread::exists(destination));
}
else
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"use new name: "+TransferThread::internalStringTostring(newName));
destinationSuffixPath = newName;
}
destination=FSabsolutePath(destination);
if(!stringEndsWith(destination,'/')
#ifdef Q_OS_WIN32
&& !stringEndsWith(destination,'\\')
#endif
)
destination+=text_slash;
destination+=destinationSuffixPath;
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"destination after rename: "+TransferThread::internalStringTostring(destination));
break;
default:
return;
break;
}
}
#ifdef ULTRACOPIER_PLUGIN_DEBUG
#ifdef WIDESTRING
if(destination.find(L"//") != std::wstring::npos)
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"path destination contains error");
if(source.find(L"//") != std::wstring::npos)
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"path source contains error");
#else
if(destination.find("//") != std::string::npos)
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"path destination contains error");
if(source.find("//") != std::string::npos)
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"path source contains error");
#endif
#endif
//check if destination exists
if(checkDestinationExists)
{
if(TransferThread::exists(destination))
{
emit folderAlreadyExists(source,destination,false);
waitOneAction.acquire();
INTERNALTYPEPATH destinationSuffixPath;
switch(folderExistsAction)
{
case FolderExists_Merge:
break;
case FolderExists_Skip:
return;
break;
case FolderExists_Rename:
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"destination before rename: "+TransferThread::internalStringTostring(destination));
if(newName.empty())
{
//resolv the new name
int num=1;
INTERNALTYPEPATH tempdestination;
do
{
if(num==1)
{
if(firstRenamingRule.empty())
#ifdef WIDESTRING
destinationSuffixPath=tr("%name% - copy").toStdWString();
#else
destinationSuffixPath=tr("%name% - copy").toStdString();
#endif
else
destinationSuffixPath=TransferThread::stringToInternalString(firstRenamingRule);
}
else
{
if(otherRenamingRule.empty())
#ifdef WIDESTRING
destinationSuffixPath=tr("%name% - copy (%number%)").toStdWString();
#else
destinationSuffixPath=tr("%name% - copy (%number%)").toStdString();
#endif
else
destinationSuffixPath=TransferThread::stringToInternalString(otherRenamingRule);
#ifdef WIDESTRING
stringreplaceAll(destinationSuffixPath,L"%number%",std::to_wstring(num));
#else
stringreplaceAll(destinationSuffixPath,"%number%",std::to_string(num));
#endif
}
#ifdef WIDESTRING
stringreplaceAll(destinationSuffixPath,L"%name%",TransferThread::resolvedName(destination));
#else
stringreplaceAll(destinationSuffixPath,"%name%",TransferThread::resolvedName(destination));
#endif
tempdestination=FSabsolutePath(destination);
if(!stringEndsWith(destination,'/')
#ifdef Q_OS_WIN32
&& !stringEndsWith(destination,'\\')
#endif
)
tempdestination+=text_slash;
tempdestination+=destinationSuffixPath;
num++;
}
while(TransferThread::exists(tempdestination));
}
else
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"use new name: "+TransferThread::internalStringTostring(newName));
destinationSuffixPath = newName;
}
{
std::string::size_type n=destination.rfind('/');
if(n == std::string::npos)
n=destination.rfind('.');
else
n=destination.rfind(n,'.');
if(n == std::string::npos)
{
destination=FSabsolutePath(destination);
if(!stringEndsWith(destination,'/')
#ifdef Q_OS_WIN32
&& !stringEndsWith(destination,'\\')
#endif
)
destination+=text_slash;
destination+=destinationSuffixPath;
}
else
{
destination=FSabsolutePath(destination);
if(!stringEndsWith(destination,'/')
#ifdef Q_OS_WIN32
&& !stringEndsWith(destination,'\\')
#endif
)
destination+=text_slash;
destination+=destinationSuffixPath+
TransferThread::stringToInternalString(".")+destination.substr(n);
}
}
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"destination after rename: "+TransferThread::internalStringTostring(destination));
break;
default:
return;
break;
}
}
else
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"checkDestinationExists but stat failed");
}
if(stopIt)
return;
std::vector<TransferThread::dirent_uc> entryList;
do
{
fileErrorAction=FileError_NotSet;
if(isBlackListed(destination))
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"isBlackListed: "+TransferThread::internalStringTostring(destination));
emit errorOnFolder(destination,tr("Blacklisted folder").toStdString(),ErrorType_Folder);
waitOneAction.acquire();
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,std::string("actionNum: ")+std::to_string((int)fileErrorAction));
}
else if(!TransferThread::entryInfoList(source,entryList))
{
#ifdef Q_OS_UNIX
int saveerrno=errno;
const std::string &errorStr=strerror(saveerrno);
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Problem with the folder "+TransferThread::internalStringTostring(source)+" to read: "+std::to_string(saveerrno));
emit errorOnFolder(source,tr("Problem with folder read").toStdString()+": "+errorStr);
#else
const std::string &errorStr=TransferThread::GetLastErrorStdStr();
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Problem with the folder "+TransferThread::internalStringTostring(source)+" to read: "+errorStr);
emit errorOnFolder(source,tr("Problem with folder read").toStdString()+": "+errorStr);
#endif
waitOneAction.acquire();
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"actionNum: "+std::to_string(fileErrorAction));
}
} while(fileErrorAction==FileError_Retry);
if(copyListOrder)
std::sort(entryList.begin(), entryList.end(), [](TransferThread::dirent_uc a, TransferThread::dirent_uc b) {
return a.d_name<b.d_name;
});
if(stopIt)
return;
#ifdef ULTRACOPIER_PLUGIN_DEBUG
#ifdef WIDESTRING
if(destination.find(L"//") != std::wstring::npos)
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"path destination contains error");
if(source.find(L"//") != std::wstring::npos)
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"path source contains error");
#else
if(destination.find("//") != std::string::npos)
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"path destination contains error");
if(source.find("//") != std::string::npos)
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"path source contains error");
#endif
#endif
const unsigned int sizeEntryList=entryList.size();
emit newFolderListing(TransferThread::internalStringTostring(source));
if(mode!=Ultracopier::Move)
emit addToMkPath(source,destination,sizeEntryList);
for(unsigned int index=0;index<sizeEntryList;++index)
{
const TransferThread::dirent_uc &fileInfo=entryList.at(index);
if(stopIt)
return;
if(haveFilters)
{
if(reloadTheNewFilters)
{
QMutexLocker lock(&filtersMutex);
QCoreApplication::processEvents(QEventLoop::AllEvents);
reloadTheNewFilters=false;
this->include=this->include_send;
this->exclude=this->exclude_send;
}
const INTERNALTYPEPATH &fileName=fileInfo.d_name;
if(fileInfo.isFolder)
{
bool excluded=false,included=(include.size()==0);
unsigned int filters_index=0;
while(filters_index<exclude.size())
{
if(exclude.at(filters_index).apply_on==ApplyOn_folder || exclude.at(filters_index).apply_on==ApplyOn_fileAndFolder)
{
if(std::regex_match(TransferThread::internalStringTostring(fileName),exclude.at(filters_index).regex))
{
excluded=true;
break;
}
}
filters_index++;
}
if(excluded)
{}
else
{
filters_index=0;
while(filters_index<include.size())
{
if(include.at(filters_index).apply_on==ApplyOn_folder || include.at(filters_index).apply_on==ApplyOn_fileAndFolder)
{
if(std::regex_match(TransferThread::internalStringTostring(fileName),include.at(filters_index).regex))
{
included=true;
break;
}
}
filters_index++;
}
if(!included)
{}
else
{
INTERNALTYPEPATH fullSource=source;
if(!stringEndsWith(fullSource,'/'))
fullSource+='/';
fullSource+=fileName;
INTERNALTYPEPATH fullDestination=destination;
if(!stringEndsWith(fullDestination,'/'))
fullDestination+='/';
fullDestination+=fileName;
listFolder(fullSource,fullDestination);
}
}
}
else
{
bool excluded=false,included=(include.size()==0);
unsigned int filters_index=0;
while(filters_index<exclude.size())
{
if(exclude.at(filters_index).apply_on==ApplyOn_file || exclude.at(filters_index).apply_on==ApplyOn_fileAndFolder)
{
if(std::regex_match(TransferThread::internalStringTostring(fileName),exclude.at(filters_index).regex))
{
excluded=true;
break;
}
}
filters_index++;
}
if(excluded)
{}
else
{
filters_index=0;
while(filters_index<include.size())
{
if(include.at(filters_index).apply_on==ApplyOn_file || include.at(filters_index).apply_on==ApplyOn_fileAndFolder)
{
if(std::regex_match(TransferThread::internalStringTostring(fileName),include.at(filters_index).regex))
{
included=true;
break;
}
}
filters_index++;
}
if(!included)
{}
else
{
INTERNALTYPEPATH fullSource=source;
if(!stringEndsWith(fullSource,'/'))
fullSource+='/';
fullSource+=fileName;
#ifndef ULTRACOPIER_PLUGIN_RSYNC
#ifdef Q_OS_WIN32
emit fileTransferWithInode(fullSource,destination+TransferThread::stringToInternalString("/")+fileName,mode,fileInfo);
#else
emit fileTransfer(fullSource,destination+TransferThread::stringToInternalString("/")+fileName,mode);
#endif
#else
{
bool sendToTransfer=false;
if(!rsync)
sendToTransfer=true;
else if(!QFile::exists(destination.absoluteFilePath()+"/"+fileInfo.fileName()))
sendToTransfer=true;
else if(fileInfo.lastModified()!=QFileInfo(destination.absoluteFilePath()+"/"+fileInfo.fileName()).lastModified())
sendToTransfer=true;
if(sendToTransfer)
#ifdef Q_OS_WIN32
emit fileTransferWithInode(fileInfo.absoluteFilePath(),destination.absoluteFilePath()+"/"+fileInfo.fileName(),mode,fileInfo);
#else
emit fileTransfer(fileInfo.absoluteFilePath(),destination.absoluteFilePath()+"/"+fileInfo.fileName(),mode);
#endif
}
#endif
}
}
}
}
else
{
const INTERNALTYPEPATH fileName(fileInfo.d_name);
if(fileInfo.isFolder)//possible wait time here
{
//listFolder(source,destination,suffixPath+fileInfo.fileName()+QDir::separator());
INTERNALTYPEPATH fullSource=source;
if(!stringEndsWith(fullSource,'/'))
fullSource+='/';
fullSource+=fileName;
INTERNALTYPEPATH fullDestination=destination;
if(!stringEndsWith(fullDestination,'/'))
fullDestination+='/';
fullDestination+=fileName;
listFolder(fullSource,fullDestination);
}
else
{
INTERNALTYPEPATH fullSource=source;
if(!stringEndsWith(fullSource,'/'))
fullSource+='/';
fullSource+=fileName;
#ifndef ULTRACOPIER_PLUGIN_RSYNC
#ifdef Q_OS_WIN32
emit fileTransferWithInode(fullSource,destination+TransferThread::stringToInternalString("/")+fileName,mode,fileInfo);
#else
emit fileTransfer(fullSource,destination+TransferThread::stringToInternalString("/")+fileName,mode);
#endif
#else
{
bool sendToTransfer=false;
if(!rsync)
sendToTransfer=true;
else if(!QFile::exists(destination.absoluteFilePath()+"/"+fileInfo.fileName()))
sendToTransfer=true;
else if(fileInfo.lastModified()!=QFileInfo(destination.absoluteFilePath()+"/"+fileInfo.fileName()).lastModified())
sendToTransfer=true;
if(sendToTransfer)
#ifdef Q_OS_WIN32
emit fileTransferWithInode(fileInfo.absoluteFilePath(),destination.absoluteFilePath()+"/"+fileInfo.fileName(),mode,fileInfo);
#else
emit fileTransfer(fileInfo.absoluteFilePath(),destination.absoluteFilePath()+"/"+fileInfo.fileName(),mode);
#endif
}
#endif
}
}
}
#ifdef ULTRACOPIER_PLUGIN_RSYNC
if(rsync)
{
//check the reverse path here
QFileInfoList entryListDestination;
if(copyListOrder)
entryListDestination=QDir(destination.absoluteFilePath()).entryInfoList(QDir::AllEntries|QDir::NoDotAndDotDot|QDir::Hidden|QDir::System,QDir::DirsFirst|QDir::Name|QDir::IgnoreCase);//possible wait time here
else
entryListDestination=QDir(destination.absoluteFilePath()).entryInfoList(QDir::AllEntries|QDir::NoDotAndDotDot|QDir::Hidden|QDir::System);//possible wait time here
int sizeEntryListDestination=entryListDestination.size();
int index=0;
for (int indexDestination=0;indexDestination<sizeEntryListDestination;++indexDestination)
{
index=0;
while(index<sizeEntryList)
{
if(entryListDestination.at(indexDestination).fileName()==entryList.at(index).fileName())
break;
index++;
}
if(index==sizeEntryList)
{
//then not found, need be remove
emit addToRmForRsync(entryListDestination.at(indexDestination));
}
}
return;
}
#endif
if(mode==Ultracopier::Move)
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"source: "+TransferThread::internalStringTostring(source)+", sizeEntryList: "+std::to_string(sizeEntryList));
emit addToMovePath(source,destination,sizeEntryList);
}
else// if(keepDate or keep permition, perfer alwasy send it)
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"source: "+TransferThread::internalStringTostring(source)+", sizeEntryList: "+std::to_string(sizeEntryList));
emit addToKeepAttributePath(source,destination,sizeEntryList);
}
}
//set if need check if the destination exists
void ScanFileOrFolder::setCheckDestinationFolderExists(const bool checkDestinationFolderExists)
{
this->checkDestinationExists=checkDestinationFolderExists;
}
void ScanFileOrFolder::setRenamingRules(const std::string &firstRenamingRule, const std::string &otherRenamingRule)
{
this->firstRenamingRule=firstRenamingRule;
this->otherRenamingRule=otherRenamingRule;
}
void ScanFileOrFolder::setMoveTheWholeFolder(const bool &moveTheWholeFolder)
{
this->moveTheWholeFolder=moveTheWholeFolder;
}
void ScanFileOrFolder::setFollowTheStrictOrder(const bool &order)
{
this->copyListOrder=order;
}
#ifdef ULTRACOPIER_PLUGIN_RSYNC
/// \brief set rsync
void ScanFileOrFolder::setRsync(const bool rsync)
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"set rsync: "+std::to_string(rsync));
this->rsync=rsync;
}
#endif
void ScanFileOrFolder::set_updateMount()
{
driveManagement.tryUpdate();
}
|