Logo Search packages:      
Sourcecode: ultracopier version File versions  Download package

void copyThread::run (  ) [protected]

Run the copy.

Warning:
The pointer sould be given and thread initilised
See also:
QObjectOfMwindow(), stopTheCopy() and stop()

each action is checked by: retryAction: if(Action()==ERROR) { actionAfterUnlock=-1; emit someSignalNeedAction(); timeCopyElapsed+=tempTimeElapsed.elapsed(); theCurrentStat=copyThreadPaused; if(actionAfterUnlock==-1) controlMutex.wait(&mutexWaitControl); if(stopIt || actionAfterUnlock==ERRORACTION_CLOSE) goto SkipFile; theCurrentStat=copyThreadRunning; tempTimeElapsed.restart(); if(actionAfterUnlock==ERRORACTION_RETRY) goto retryMkpath; else if(actionAfterUnlock==ERRORACTION_SKIP) goto SkipFile; else { DEBUGCONSOLE(10,"copyThread::run","Unknow action at mkpath ("+QStringnumber(actionAfterUnlock)+")"); goto SkipFile; } }

normal run with write thread: void copyThread::run() { while(!CopyList->isEmpty()) { sourceFile.open(QIODevice::ReadOnly); theCurrentThread->openDestination(); set permissions if(RightCopy) destinationFile.setPermissions(sourceFile.permissions()): pre-allocation if(!preallocation) destinationFile.resize(0); else destinationFile.resize(sourceFile.size()); sourceFile.seek(0); theCurrentThread->setFiles(sourceFile,destinationFile,tempItem); do { read one block retryReadThisBlock: positionInSource=sourceFile.pos(); blockArray=sourceFile.read(blockSizeCurrent); write one block if(!blockArray.isEmpty()) { bytesWriten=blockArray.size(); theCurrentThread->addNewBlock(blockArray); totalCopiedSize+=bytesWriten; } } while(!blockArray.isEmpty() && bytesWriten==blockArray.size()); theCurrentThread->endOfSourceDetected(); if(sourceFile.isOpen()) sourceFile.close(); } }

normal run without write thread: void copyThread::run() { while(!theCopyList()) { sourceFile.open(QIODevice::ReadOnly); destinationFile.open(QIODevice::WriteOnly); set permissions if(RightCopy) destinationFile.setPermissions(sourceFile.permissions()); pre-allocation if(!preallocation) destinationFile.resize(0); else destinationFile.resize(sourceFile.size()); sourceFile.seek(0); destinationFile.seek(0); do { read one block blockArray=sourceFile.read(blockSizeCurrent); write one block if(!blockArray.isEmpty()) destinationFile.write(blockArray); } while(!blockArray.isEmpty() && bytesWriten==blockArray.size()); if(sourceFile.isOpen()) sourceFile.close(); if(destinationFile.isOpen()) destinationFile.close(); set the time if no write thread used if(keepDate) changeFileDateTime(destinationFile.fileName(),sourceFile.fileName()); remove source in moving mode if(movingMode) sourceFile.remove(); } }

< For store the block array

< Just for do operation on folder, static method is missing

< Just for do operation on folder, static method is missing

< Number of bytes writen

Note:
if the pointer for the current thread is not null then write thread should be used, then it's to write thread to open and do operation on destination file

Flush the last thread

Definition at line 1066 of file CopyThread.cpp.

References actionAfterUnlock, blockSizeCurrent, checkIfNeedWaitOneWriteThread(), checkIfNeedWaitWriteThread(), clockForTheCopySpeed, copyHadBegin, destinationFile, emptyDestDir, WriteThread::endOfSourceDetected(), errorMessageInCopy, errorOnFile(), errorOnFileAndWait(), fileIsExists(), fileIsSame(), firstScanCur, firstScanTot, flush(), intToQtringSize(), isInPauseOrNot(), keepDate, maxSpeed, movingMode, MultForBigSpeed, MultiThread_ThreadList, MultiThreadCopyList, numberOfBlockCopied, NumberOfFileCopied, NumberOfFileTot, numberOfItemRemoved, preallocation, putFirstFileAtEnd(), queryNewWriteThread(), queryStartThread(), RightCopy, skipCurrentFile(), skipThecurrentFile, sourceDirList, sourceFile, WriteThread::stop(), stopIt, syntetizedStringCurrentFile, syntetizedStringFrom, syntetizedStringOverall, syntetizedStringTo, theCurrentStat, theWriteThreadList, timeCopyElapsed, totalCopiedSize, translationErrorDate, translationErrorDestinationWrite, translationErrorPermissions, translationErrorReadSource, translationErrorRemove, translationErrorResize, translationErrorSourceRead, translationErrorSourceReadWrite, translationErrorWriting, translationOfCopyOf, tryOpenFileInSpecialMode(), useWriteThread, waitInPause, waitNeedAction(), waitNewWriteThread, and writeThreadOperationFinish().

{
      // initialise the variable for copy
      QByteArray        blockArray;       ///< For store the block array
      QDir              destinationFolder;      ///< Just for do operation on folder, static method is missing
      QFileInfo         destintationFileInfo;   ///< Just for do operation on folder, static method is missing
      QString                 destinationFolderPath;
      bool              resultOpen;
      bool              destinationIsOpenNow;
      int               id;

      timeCopyElapsed         = 0;
      qint64 bytesWriten      = 0;              ///< Number of bytes writen
      quint64 CurentCopiedSize= 0;
      theCurrentStat          = copyThread::Running;
      stopIt                  = false;
      errorMessageInCopy      = "";
      stopped                 = false;
      waitInPause       = false;
      theCurrentThread  = NULL;
      currentFileSize         = 0;
      currentFileTotPos = totalCopiedSize;

      emit isInPauseOrNot();
      tempTimeElapsed.start();
      int copyListSize;
      {
            QMutexLocker lock_mutex(&MultiThreadCopyList);
            copyListSize=theCopyList.size();
      }
      #if (DEBUG_ULTRACOPIER>0)
      {
            QMutexLocker lock_mutex(&MultiThreadCopyList);
            if(!copyListSize)
            {
                  DEBUGCONSOLE(70,"copyThread::run","theCopyList.size()==0");//possible if listing of folder is empty
                  if(sourceDirList.isEmpty())
                        DEBUGCONSOLE(10,"copyThread::run","sourceDirList.isEmpty() and copy list is empty! bug?");
                  if(emptyDestDir.isEmpty())
                        DEBUGCONSOLE(10,"copyThread::run","emptyDestDir.isEmpty() and copy list is empty! bug?");
            }
      }
      #endif

      DEBUGCONSOLE(90,"copyThread::run","start the copy loop");
      while(copyListSize && !stopIt)
      {
            //initialise the variable for current file
            actionAfterUnlock = -1;
            copyHadBegin            = false;
            skipThecurrentFile      = false;
            destinationIsOpenNow    = false;
            theCurrentThread  = NULL;
            needRemoveFileToList    = true;

            /**************************
                  extra check
            **************************/
            DEBUGCONSOLE(90,"copyThread::run","start extra check");
            #if (DEBUG_ULTRACOPIER>0)
            if(sourceFile.isOpen())
                  DEBUGCONSOLE(10,"copyThread::run","source: \""+sourceFile.fileName()+"\" is already open");
            if(destinationFile.isOpen())
                  DEBUGCONSOLE(10,"copyThread::run","destination: \""+destinationFile.fileName()+"\" is already open");
            {
                  QMutexLocker lock_mutex(&MultiThreadCopyList);
                  if(theCopyList.first().source.absoluteFilePath().isEmpty())
                  {
                        DEBUGCONSOLE(10,"copyThread::run","The source file path is empty");
                        goto SkipFile;
                  }
                  if(theCopyList.first().destination.absoluteFilePath().isEmpty())
                  {
                        DEBUGCONSOLE(10,"copyThread::run","The destination file path is empty");
                        goto SkipFile;
                  }
                  if(theCopyList.first().destination.absolutePath().isEmpty())
                  {
                        DEBUGCONSOLE(10,"copyThread::run","The destination folder name is empty");
                        goto SkipFile;
                  }
                  for (int i=0;i<theWriteThreadList.size();++i) {
                        if(theWriteThreadList.at(i)->getTheCurrentStat()==WriteThread::Stopped)
                        {
                              if(theWriteThreadList.at(i)->isFinished())
                                    DEBUGCONSOLE(50,"copyThread::run","The thread id "+QString::number(i)+" is finised but present in the list!");
                        }
                  }
            }
            #endif

            //if query to stop, directly go to SkipFile
            if(stopIt)
                  goto SkipFile;

            checkIfNeedWaitWriteThread();

            //set file name
            DEBUGCONSOLE(90,"copyThread::run","load file name");
            {
                  QMutexLocker lock_mutex(&MultiThreadCopyList);
                  //determine if the path source should be resolved or if use protocol
                  if(getMountType(theCopyList.first().source.filePath())!="protocol")
                        sourceFile.setFileName(theCopyList.first().source.absoluteFilePath());
                  else
                        sourceFile.setFileName(theCopyList.first().source.filePath());
                  //determine if the path destination should be resolved or if use protocol
                  QString destinationAbsoluteFilePath;
                  if(getMountType(theCopyList.first().destination.filePath())!="protocol")
                        destinationAbsoluteFilePath=theCopyList.first().destination.absoluteFilePath();
                  else
                        destinationAbsoluteFilePath=theCopyList.first().destination.filePath();
                  //add QDir::separator (/ under unix or \ under windows) to destination and file name of the source if needed
                  if(!destinationAbsoluteFilePath.endsWith('\\') && !destinationAbsoluteFilePath.endsWith('/'))
                  {
                        if(theCopyList.first().destination.isDir())
                              destinationFile.setFileName(destinationAbsoluteFilePath+QDir::separator()+theCopyList.first().source.fileName());
                        else
                              destinationFile.setFileName(destinationAbsoluteFilePath+theCopyList.first().source.fileName());
                  }
                  else
                        destinationFile.setFileName(destinationAbsoluteFilePath+theCopyList.first().source.fileName());
                  //load the current id copied
                  id=theCopyList.first().id;
            }
            //load the object for get informations to the destination
            destintationFileInfo.setFile(destinationFile.fileName());

            //check if write thread in error need be wait
            checkIfNeedWaitWriteThread();

            //if query to stop, directly go to SkipFile
            if(stopIt)
                  goto SkipFile;

            //wait in pause stat if needed
            if(waitInPause)
            {
                  waitNeedAction();
                  emit isInPauseOrNot();
                  if(stopIt)
                        goto SkipFile;
            }

            //if query to stop, directly go to SkipFile
            if(stopIt)
                  goto SkipFile;

            /****************************
                  start FS check
            *****************************/

            //check if both file are not the same
            DEBUGCONSOLE(90,"copyThread::run","check sourceFile.fileName()==destinationFile.fileName()");
            if(sourceFile.fileName()==destinationFile.fileName())
            {
                  actionAfterUnlock=-1;
                  DEBUGCONSOLE(90,"copyThread::run","source and destination are the same!");
                  emit fileIsSame(destinationFile.fileName());
                  waitNeedAction();
                  if(stopIt || actionAfterUnlock==FILEEXIST_ACTION_CANCEL)
                        goto SkipFile;
                  else if(actionAfterUnlock==FILEEXIST_ACTION_RENAME)
                        destinationFile.setFileName(QFileInfo(destinationFile.fileName()).absolutePath()+QDir::separator()+translationOfCopyOf+QFileInfo(destinationFile.fileName()).fileName());
                  else if(actionAfterUnlock==FILEEXIST_ACTION_SKIP)
                        goto SkipFile;
                  else
                  {
                        DEBUGCONSOLE(10,"copyThread::run","Unknow File Exists action when is same! ("+QString::number(actionAfterUnlock)+")");
                        goto SkipFile;
                  }
            }
            DEBUGCONSOLE(50,"copyThread::run","source: \""+sourceFile.fileName()+"\" dest: \""+destinationFile.fileName()+"\"");

            //if query to stop, directly go to SkipFile
            if(stopIt)
                  goto SkipFile;

            DEBUGCONSOLE(90,"copyThread::run","opening the destination...: "+destinationFile.fileName());

            //check if destination exists
            CheckIfExists:
            DEBUGCONSOLE(90,"copyThread::run","check destinationFile.exists()");
            if(destinationFile.exists())
            {
                  actionAfterUnlock=-1;
                  DEBUGCONSOLE(90,"copyThread::run","The destination exists: true");
                  emit fileIsExists(sourceFile.fileName(),destinationFile.fileName());
                  waitNeedAction();
                  switch(actionAfterUnlock)
                  {
                        default:
                        case FILEEXIST_ACTION_CANCEL:
                        case FILEEXIST_ACTION_SKIP:
                              goto SkipFile;
                        case FILEEXIST_ACTION_OVERWRITE_IFNEWER:
                              //check if same then skip the file
                              if(QFileInfo(sourceFile).lastModified()<QFileInfo(destinationFile).lastModified())
                                    goto SkipFile;
                              break;
                        case FILEEXIST_ACTION_OVERWRITE_IFNOTSAME:
                              //check if same then skip the file
                              if(sourceFile.size()==destinationFile.size() && QFileInfo(sourceFile).lastModified()==QFileInfo(destinationFile).lastModified())
                                    goto SkipFile;
                              break;
                        case FILEEXIST_ACTION_RENAME:
                        {
                              QString path=QFileInfo(destinationFile.fileName()).absolutePath();
                              if(!path.endsWith('/') && !path.endsWith('\\'))
                                    path+=QDir::separator();
                              destinationFile.setFileName(path+translationOfCopyOf+QFileInfo(destinationFile.fileName()).fileName());
                              goto CheckIfExists;
                        }
                        case FILEEXIST_ACTION_OVERWRITE:
                              break;
                  }
            }

            DEBUGCONSOLE(90,"copyThread::run","opening the destination...: "+destinationFile.fileName());

            //if query to stop, directly go to SkipFile
            if(stopIt)
                  goto SkipFile;

            //check if destination folder exists, and create it
            destinationFolderPath=destintationFileInfo.absolutePath();
            DEBUGCONSOLE(90,"copyThread::run","destinationFolder: "+destinationFolderPath);
            if(!destinationFolder.exists(destinationFolderPath))
            {
                  retryMkpath:
                  DEBUGCONSOLE(90,"copyThread::run","destintationFileInfo.absolutePath(): "+destintationFileInfo.absolutePath());
                  if(!destinationFolder.mkpath(destinationFolderPath))
                  {
                        DEBUGCONSOLE(70,"copyThread::run","Error while create destination folder");
                        DEBUGCONSOLE(70,"copyThread::run","Note: bug if file exists with same name as the folder");
                        switch(errorOnFileAndWait(ERROR_DEF_ALL,destintationFileInfo.absolutePath(),tr("Error while create destination folder"),0))
                        {
                              case ERRORACTION_RETRY:
                                    goto retryMkpath;
                              case ERRORACTION_SKIP:
                                    skipCurrentFile();
                              case ERRORACTION_CLOSE:
                                    goto SkipFile;
                              case ERRORACTION_ENDOF:
                                    putFirstFileAtEnd();
                                    goto SkipFile;
                              default:
                                    DEBUGCONSOLE(10,"copyThread::run","Unknow action at mkpath ("+QString::number(actionAfterUnlock)+")");
                                    goto SkipFile;
                        }
                  }
                  #if (DEBUG_ULTRACOPIER>0)
                  else
                        DEBUGCONSOLE(90,"copyThread::run","Destination folder created!");
                  #endif
            }
            #if (DEBUG_ULTRACOPIER>0)
            else
                  DEBUGCONSOLE(90,"copyThread::run","Destination folder exists!");
            #endif

            //if query to stop, directly go to SkipFile
            if(stopIt)
                  goto SkipFile;

            //move if on same mount point
            #ifdef ULTRACOPIER_MODE_WINDOWS
            if(movingMode && getMountPoint(destinationFile.fileName())==getMountPoint(sourceFile.fileName()))
            {
                  DEBUGCONSOLE(90,"copyThread::run","getMountPoint(destinationFile.fileName())==getMountPoint(sourceFile.fileName()) in moving mode");
                  retryMoveReal:
                  bool errorFound=false;
                  actionAfterUnlock=-1;
                  if(destinationFile.exists())
                        if(!destinationFile.remove())
                        {
                              errorFound=true;
                              DEBUGCONSOLE(90,"copyThread::run","Unable to move the file on the same partition and remove dest: "+destinationFile.errorString());
                              if(copyListSize>1)
                                    emit errorOnFile(ERROR_DEF_ALL,destinationFile.fileName(),tr("Unable to remove the destination file: ")+destinationFile.errorString(),id);
                              else
                                    emit errorOnFile(ERROR_DEF_NOENDOF,destinationFile.fileName(),tr("Unable to remove the destination file: ")+destinationFile.errorString(),id);
                        }
                  if(!errorFound)
                        if(!sourceFile.rename(destinationFile.fileName()))
                        {
                              errorFound=true;
                              DEBUGCONSOLE(90,"copyThread::run","Unable to move the file on the same partition: "+sourceFile.errorString());
                              if(copyListSize>1)
                                    emit errorOnFile(ERROR_DEF_ALL,destinationFile.fileName(),tr("Unable to remove the destination file: ")+destinationFile.errorString(),id);
                              else
                                    emit errorOnFile(ERROR_DEF_NOENDOF,destinationFile.fileName(),tr("Unable to remove the destination file: ")+destinationFile.errorString(),id);
                        }
                  if(errorFound)
                  {
                        waitNeedAction();
                        switch(actionAfterUnlock)
                        {
                              case ERRORACTION_ENDOF:
                                    putFirstFileAtEnd();
                                    goto SkipFile;
                              case ERRORACTION_RETRY:
                                    goto retryMoveReal;
                              case ERRORACTION_SKIP:
                                    skipCurrentFile();
                              case ERRORACTION_CLOSE:
                                    goto SkipFile;
                              default:
                                    DEBUGCONSOLE(10,"copyThread::run","Unknow action at moving windows ("+QString::number(actionAfterUnlock)+")");
                                    goto SkipFile;
                        }
                  }
                  //file moved skip to the next file
                  goto SkipFile;
            }
            #endif

            //if query to stop, directly go to SkipFile
            if(stopIt)
                  goto SkipFile;

            #ifdef Q_OS_UNIX
            //if symbolic link should be not read the destination file content but just copy the link
            if(QFileInfo(sourceFile).isSymLink())
            {
                  if(destinationFile.exists())
                  {
                        retrySymLinkDestRemove:
                        if(!destinationFile.remove())
                        {
                              DEBUGCONSOLE(70,"copyThread::run","error at the removing the symLink removig: "+destinationFile.errorString());
                              switch(errorOnFileAndWait(ERROR_DEF_ALL,QFileInfo(destinationFile).absoluteFilePath(),destinationFile.errorString(),0))
                              {
                                    case ERRORACTION_RETRY:
                                          goto retrySymLinkDestRemove;
                                    case ERRORACTION_SKIP:
                                          skipCurrentFile();
                                    case ERRORACTION_CLOSE:
                                          goto SkipFile;
                                    case ERRORACTION_ENDOF:
                                          putFirstFileAtEnd();
                                          goto SkipFile;
                                    default:
                                          DEBUGCONSOLE(10,"copyThread::run","Unknow action at SymLink ("+QString::number(actionAfterUnlock)+")");
                                          goto SkipFile;
                              }
                        }
                  }
                  retrySymLink:
                  DEBUGCONSOLE(70,"copyThread::run","QFileInfo(\""+QFileInfo(sourceFile).absoluteFilePath()+"\").symLinkTarget(): "+QFileInfo(sourceFile).symLinkTarget());
                  DEBUGCONSOLE(70,"copyThread::run","destinationFile: "+destinationFile.fileName());
                  if(!QFile::link(QFileInfo(sourceFile).symLinkTarget(),destinationFile.fileName()))
                  {
                        //QFileInfo(sourceFile).symLinkTarget()
                        DEBUGCONSOLE(70,"copyThread::run","error at the symLinkTarget: "+destinationFile.errorString());
                        switch(errorOnFileAndWait(ERROR_DEF_ALL,QFileInfo(destinationFile).absoluteFilePath(),destinationFile.errorString(),0))
                        {
                              case ERRORACTION_RETRY:
                                    goto retrySymLink;
                              case ERRORACTION_SKIP:
                                    skipCurrentFile();
                              case ERRORACTION_CLOSE:
                                    goto SkipFile;
                              case ERRORACTION_ENDOF:
                                    putFirstFileAtEnd();
                                    goto SkipFile;
                              default:
                                    DEBUGCONSOLE(10,"copyThread::run","Unknow action at mkpath ("+QString::number(actionAfterUnlock)+")");
                                    goto SkipFile;
                        }
                  }
            }
            else
            {
            #endif
                  //try open source is read only for copy and read/write for move
                  retryOpenSource:
                  DEBUGCONSOLE(90,"copyThread::run","opening the source...");
                  if(movingMode)
                               resultOpen=tryOpenFileInSpecialMode(&sourceFile,QIODevice::ReadWrite);
                  else
                               resultOpen=tryOpenFileInSpecialMode(&sourceFile,QIODevice::ReadOnly);
                  if(!resultOpen)
                  {
                        actionAfterUnlock=-1;
                        DEBUGCONSOLE(70,"copyThread::run","try: sourceFile.open(): "+sourceFile.errorString());
                        int buttonToActivate=ERROR_DEF_ALL;
                        //desactivate skip of if only one item
                        if(theCopyList.size()<=1)
                              buttonToActivate=ERROR_DEF_NOENDOF;
                        DEBUGCONSOLE(90,"copyThread::run","Unable to remove the destination file, buttonToActivate: "+QString::number(buttonToActivate));
                        if(movingMode)
                              emit errorOnFile(buttonToActivate,QFileInfo(sourceFile).absoluteFilePath(),translationErrorSourceReadWrite+sourceFile.errorString(),id);
                        else
                              emit errorOnFile(buttonToActivate,QFileInfo(sourceFile).absoluteFilePath(),translationErrorSourceRead+sourceFile.errorString(),id);
                        DEBUGCONSOLE(90,"copyThread::run","Error at open source, stop the thread");
                        waitNeedAction();
                        DEBUGCONSOLE(90,"copyThread::run","Error at open source, resume the thread");
                        switch(actionAfterUnlock)
                        {
                              case ERRORACTION_ENDOF:
                                    putFirstFileAtEnd();
                                    goto SkipFile;
                              case ERRORACTION_RETRY:
                                    goto retryOpenSource;
                              case ERRORACTION_SKIP:
                                    skipCurrentFile();
                              case ERRORACTION_CLOSE:
                                    goto SkipFile;
                              default:
                                    DEBUGCONSOLE(10,"copyThread::run","Unknow action at mkpath ("+QString::number(actionAfterUnlock)+")");
                                    goto SkipFile;
                        }
                  }
                  currentFileSize=sourceFile.size();
                  currentFileTotPos=totalCopiedSize;
                  DEBUGCONSOLE(90,"copyThread::run","Source opened and sized to: "+QString::number(sourceFile.size()));

                  DEBUGCONSOLE(90,"copyThread::run","check if need be in pause");
                  //if write thread should be used load it here
                  if(useWriteThread)
                  {
                        DEBUGCONSOLE(90,"copyThread::run","writeThreadSem->available():"+QString::number(writeThreadSem->available()));
                        //block here with semaphore if too many thread is open
                        writeThreadSem->acquire();
                        {
                              //search write thread free
                              QMutexLocker lock_mutex(&MultiThread_ThreadList);
                              for (int i=0;i<theWriteThreadList.size();++i) {
                                    if(theWriteThreadList.at(i)->getTheCurrentStat()==WriteThread::Stopped)
                                    {
                                          //if have been closed by previous copy list finish restart it
                                          if(theWriteThreadList.at(i)->isFinished())
                                          {
                                                DEBUGCONSOLE(50,"copyThread::run","The thread id "+QString::number(i)+" is finised but present in the list! Start it!");
                                                //use signal/slot and mutex for create it in main thread
                                                emit queryStartThread(i);
                                                waitNewWriteThread.acquire();
                                                DEBUGCONSOLE(50,"copyThread::run","The thread id "+QString::number(i)+" is finised but present in the list! Started!");
                                          }
                                          //set the pointer on it for use it
                                          theCurrentThread=theWriteThreadList.at(i);
                                          break;
                                    }
                              }
                        }
                        //check if no other thread is in error
                        checkIfNeedWaitWriteThread();
                        //if no existing thread is loaded, create it
                        if(theCurrentThread==NULL)
                        {
                              DEBUGCONSOLE(50,"copyThread::run","Create new Write thread existing");
                              //use signal/slot and mutex for create it in main thread
                              emit queryNewWriteThread();
                              waitNewWriteThread.acquire();
                              DEBUGCONSOLE(90,"copyThread::run","create write thread done");
                        }
                        #if (DEBUG_ULTRACOPIER>0)
                        {
                              //return number of the current thread and the number of total thread for debug
                              QMutexLocker lock_mutex(&MultiThread_ThreadList);
                              DEBUGCONSOLE(90,"copyThread::run","Use Write thread existing, the number "+QString::number(theWriteThreadList.indexOf(theCurrentThread))+" on "+QString::number(theWriteThreadList.size()));
                        }
                        #endif
                        if(theCurrentThread->isFinished())
                        {
                              stopIt=true;
                              DEBUGCONSOLE(10,"copyThread::run","Write thread existing is not running!");
                        }
                        //for the current thread selected, set the actual options
                        theCurrentThread->setKeepDate(keepDate);
                        theCurrentThread->setPreallocation(preallocation);
                  }

                  //open destination here
                  retryOpenDestination:
                  /** \note if the pointer for the current thread is not null
                    then write thread should be used, then it's to write
                    thread to open and do operation on destination file
                    **/
                  if(theCurrentThread!=NULL)
                  {
                        DEBUGCONSOLE(90,"copyThread::run","theCurrentThread is not NULL");
                        copyItemInternal tempItem;
                        {
                              QMutexLocker lock_mutex(&MultiThreadCopyList);
                              tempItem=theCopyList.first();
                        }
                        DEBUGCONSOLE(50,"copyThread::run","theCurrentThread->setFiles()");
                        theCurrentThread->setFiles(sourceFile.fileName(),destinationFile.fileName(),tempItem);
                        destinationIsOpenNow=theCurrentThread->openDestination();
                  }
                  else
                        destinationIsOpenNow=tryOpenFileInSpecialMode(&destinationFile,QIODevice::WriteOnly);
                  //error management
                  if(!destinationIsOpenNow)
                  {
                        QString error;
                        if(theCurrentThread==NULL)
                              error=destinationFile.errorString();
                        else
                              error=theCurrentThread->errorString();
                        DEBUGCONSOLE(70,"copyThread::run","try: destination->open(): "+error);
                        switch(errorOnFileAndWait(ERROR_DEF_ALL,QFileInfo(destinationFile).absoluteFilePath(),translationErrorDestinationWrite+error,id))
                        {
                              case ERRORACTION_ENDOF:
                                    putFirstFileAtEnd();
                                    if(theCurrentThread!=NULL)
                                          writeThreadOperationFinish();
                                    goto SkipFile;
                              case ERRORACTION_RETRY:
                                    goto retryOpenDestination;
                              case ERRORACTION_SKIP:
                                    if(theCurrentThread!=NULL)
                                          writeThreadOperationFinish();
                                    skipCurrentFile();
                              case ERRORACTION_CLOSE:
                                    goto SkipFile;
                              default:
                                    DEBUGCONSOLE(10,"copyThread::run","Unknow action at mkpath ("+QString::number(actionAfterUnlock)+")");
                                    if(theCurrentThread!=NULL)
                                          writeThreadOperationFinish();
                                    goto SkipFile;
                        }
                  }
                  DEBUGCONSOLE(90,"copyThread::run","Destination open");

                  //set permision, only for real file, because Qt not support for the symLink
                  if(RightCopy)
                  {
                        retrySetPermissions:
                        DEBUGCONSOLE(90,"copyThread::run","set permissions...");
                        if(!destinationFile.setPermissions(sourceFile.permissions()))
                        {
                              DEBUGCONSOLE(90,"copyThread::run",destinationFile.fileName()+", the permissions cannot be modified: "+destinationFile.errorString());
                              switch(errorOnFileAndWait(ERROR_DEF_ALL,QFileInfo(destinationFile).absoluteFilePath(),translationErrorPermissions+destinationFile.errorString(),id))
                              {
                                    case ERRORACTION_ENDOF:
                                          putFirstFileAtEnd();
                                          goto SkipFile;
                                    case ERRORACTION_RETRY:
                                          goto retrySetPermissions;
                                    case ERRORACTION_SKIP:
                                          skipCurrentFile();
                                    case ERRORACTION_CLOSE:
                                          goto SkipFile;
                                    default:
                                          DEBUGCONSOLE(10,"copyThread::run","Unknow action at mkpath ("+QString::number(actionAfterUnlock)+")");
                                          goto SkipFile;
                              }
                        }
                        DEBUGCONSOLE(90,"copyThread::run","set permissions done");
                  }
            #ifdef Q_OS_UNIX
            }
            #endif

            //if query to stop, directly go to SkipFile
            if(stopIt)
                  goto SkipFile;

            #ifdef Q_OS_UNIX
            if(!QFileInfo(sourceFile).isSymLink())
            {
            #endif
                  if(theCurrentThread==NULL && preallocation)
                  {
                        DEBUGCONSOLE(90,"copyThread::run","resize destination to "+QString::number(sourceFile.size())+"...");
                        retryResizeFirst:
                        if(!destinationFile.resize(sourceFile.size()))
                        {
                                        DEBUGCONSOLE(90,"copyThread::run",destinationFile.fileName()+", cannot be resized: "+destinationFile.errorString()+" at: "+QString::number(sourceFile.size()));
                              switch(errorOnFileAndWait(ERROR_DEF_ALL,QFileInfo(destinationFile).absoluteFilePath(),translationErrorResize+destinationFile.errorString(),id))
                              {
                                    case ERRORACTION_ENDOF:
                                          putFirstFileAtEnd();
                                          goto SkipFile;
                                    case ERRORACTION_RETRY:
                                          goto retryResizeFirst;
                                    case ERRORACTION_SKIP:
                                          skipCurrentFile();
                                    case ERRORACTION_CLOSE:
                                          goto SkipFile;
                                    default:
                                          DEBUGCONSOLE(10,"copyThread::run","Unknow action at mkpath ("+QString::number(actionAfterUnlock)+")");
                                          goto SkipFile;
                              }
                        }
                        DEBUGCONSOLE(90,"copyThread::run","resize done");
                  }

                  //if query to stop, directly go to SkipFile
                  if(stopIt)
                        goto SkipFile;

                  DEBUGCONSOLE(90,"copyThread::run","seek...");
                  sourceFile.seek(0);
                  if(theCurrentThread==NULL)
                        destinationFile.seek(0);
                  DEBUGCONSOLE(90,"copyThread::run","seek done");

                  //if query to stop, directly go to SkipFile
                  if(stopIt)
                        goto SkipFile;

                  //source file is ready for the copy
                  numberOfBlockCopied=0;

                  #if (DEBUG_ULTRACOPIER>0)
                  if(maxSpeed>0)
                  {
                        DEBUGCONSOLE(90,"copyThread::run","maxSpeed>0: "+QString::number(maxSpeed));
                        DEBUGCONSOLE(90,"copyThread::run","setInterval at: "+QString::number(clockForTheCopySpeed.interval())+"ms for "+QString::number(MultForBigSpeed)+" block(s).");
                        if(clockForTheCopySpeed.interval()>0)
                              DEBUGCONSOLE(90,"copyThread::run","for speed of: "+QString::number(((blockSizeCurrent*MultForBigSpeed)/clockForTheCopySpeed.interval())/1024)+"MB/s");
                  }
                  #endif

                  //if query to stop, directly go to SkipFile
                  if(stopIt)
                        goto SkipFile;

                  DEBUGCONSOLE(90,"copyThread::run","load current file string for gui update the current file string");
                  syntetizedStringCurrentFile=destintationFileInfo.fileName()+" "+intToQtringSize(sourceFile.size());
                  syntetizedStringTo=destintationFileInfo.absoluteFilePath();
                  syntetizedStringFrom=sourceFile.fileName();

                  qint64 positionInSource;
                  qint64 positionInDestination;
                  copyHadBegin=true;
                  CurentCopiedSize=0;
                  DEBUGCONSOLE(90,"copyThread::run","star copy");
                  do
                  {
                        //wait in pause stat if needed
                        if(!stopIt && waitInPause)
                        {
                              waitNeedAction();
                              if(stopIt)
                                    goto SkipFile;
                        }

                        //read one block
                        retryReadThisBlock:
                        #if (DEBUG_ULTRACOPIER>0)
                        theCurrentWait          = WaitInRead;
                        #endif
                        positionInSource=sourceFile.pos();
                        blockArray=sourceFile.read(blockSizeCurrent);
                        #if (DEBUG_ULTRACOPIER>0)
                        theCurrentWait          = WaitInThread;
                        #endif
                        if(!stopIt && sourceFile.error()!=QFile::NoError)
                        {
                              retrySeekSource:
                              DEBUGCONSOLE(90,"copyThread::run","Error while reading source file: "+sourceFile.errorString());
                              switch(errorOnFileAndWait(ERROR_DEF_ALL,QFileInfo(sourceFile).absoluteFilePath(),translationErrorReadSource+sourceFile.errorString(),id))
                              {
                                    case ERRORACTION_ENDOF:
                                          putFirstFileAtEnd();
                                          goto SkipFile;
                                    case ERRORACTION_RETRY:
                                          if(!sourceFile.seek(positionInSource))
                                                goto retrySeekSource;
                                          goto retryReadThisBlock;
                                    case ERRORACTION_SKIP:
                                          skipCurrentFile();
                                    case ERRORACTION_CLOSE:
                                          goto SkipFile;
                                    default:
                                          DEBUGCONSOLE(10,"copyThread::run","Unknow action at mkpath ("+QString::number(actionAfterUnlock)+")");
                                          goto SkipFile;
                              }
                        }
                        CurentCopiedSize+=blockArray.size();

                        //write one block
                        if(!stopIt && !blockArray.isEmpty())
                        {
                              #if (DEBUG_ULTRACOPIER>0)
                              theCurrentWait          = WaitInWrite;
                              #endif
                              if(theCurrentThread==NULL)
                              {
                                    positionInDestination=destinationFile.pos();
                                    retryWriteThisBlock:
                                    bytesWriten=destinationFile.write(blockArray);
                                    if(destinationFile.error()!=QFile::NoError || bytesWriten!=blockArray.size())
                                    {
                                          retrySeekDestination:
                                          DEBUGCONSOLE(90,"copyThread::run","Error in writing: "+destinationFile.errorString());
                                          switch(errorOnFileAndWait(ERROR_DEF_ALL,QFileInfo(destinationFile).absoluteFilePath(),translationErrorWriting+destinationFile.errorString(),id))
                                          {
                                                case ERRORACTION_ENDOF:
                                                      putFirstFileAtEnd();
                                                      goto SkipFile;
                                                case ERRORACTION_RETRY:
                                                      if(!destinationFile.seek(positionInDestination))
                                                            goto retrySeekDestination;
                                                      goto retryWriteThisBlock;
                                                case ERRORACTION_SKIP:
                                                      skipCurrentFile();
                                                case ERRORACTION_CLOSE:
                                                      goto SkipFile;
                                                default:
                                                      DEBUGCONSOLE(10,"copyThread::run","Unknow action at mkpath ("+QString::number(actionAfterUnlock)+")");
                                                      goto SkipFile;
                                          }
                                    }
                              }
                              else
                              {
                                    bytesWriten=blockArray.size();
                                    theCurrentThread->addNewBlock(blockArray);
                                    checkIfNeedWaitOneWriteThread(theCurrentThread);
                              }
                              #if (DEBUG_ULTRACOPIER>0)
                              theCurrentWait          = WaitInThread;
                              #endif
                              totalCopiedSize+=bytesWriten;
                        }

                        //wait for limitation speed if stop not query
                        if(!stopIt && maxSpeed>0)
                        {
                              numberOfBlockCopied++;
                              if(numberOfBlockCopied>=MultForBigSpeed)
                              {
                                    numberOfBlockCopied=0;
                                    waitNewClockForSpeed.acquire();
                              }
                        }
                  }
                  while(!blockArray.isEmpty() && bytesWriten==blockArray.size() && !stopIt);
                  DEBUGCONSOLE(90,"copyThread::run","blockArray.isEmpty(): "+QString::number(blockArray.isEmpty()));
                  DEBUGCONSOLE(90,"copyThread::run","bytesWriten: "+QString::number(bytesWriten));
                  DEBUGCONSOLE(90,"copyThread::run","blockArray.size(): "+QString::number(blockArray.size()));
                  DEBUGCONSOLE(90,"copyThread::run","stopIt: "+QString::number(stopIt));
                  DEBUGCONSOLE(90,"copyThread::run","blockSizeCurrent: "+QString::number(blockSizeCurrent));
                  currentFileSize         = 0;
                  currentFileTotPos = totalCopiedSize;

                  if(theCurrentThread==NULL && !stopIt)
                  {
                        DEBUGCONSOLE(90,"copyThread::run","resize destination to "+QString::number(sourceFile.size())+"...");
                        retryResizeLast:
                        if(!destinationFile.resize(CurentCopiedSize))
                        {
                                        DEBUGCONSOLE(90,"copyThread::run",destinationFile.fileName()+", cannot be resized: "+destinationFile.errorString()+" at: "+QString::number(CurentCopiedSize));
                              switch(errorOnFileAndWait(ERROR_DEF_ALL,QFileInfo(destinationFile).absoluteFilePath(),translationErrorResize+destinationFile.errorString(),id))
                              {
                                    case ERRORACTION_ENDOF:
                                          putFirstFileAtEnd();
                                          goto SkipFile;
                                    case ERRORACTION_RETRY:
                                          goto retryResizeLast;
                                    case ERRORACTION_SKIP:
                                          skipCurrentFile();
                                    case ERRORACTION_CLOSE:
                                          goto SkipFile;
                                    default:
                                          DEBUGCONSOLE(10,"copyThread::run","Unknow action at mkpath ("+QString::number(actionAfterUnlock)+")");
                                          goto SkipFile;
                              }
                        }
                        DEBUGCONSOLE(90,"copyThread::run","resizing done");
                  }

                  if(theCurrentThread!=NULL)
                  {
                        if(stopIt)
                              theCurrentThread->stop();
                        else
                        {
                              DEBUGCONSOLE(70,"copyThread::run","call theCurrentThread->endOfSourceDetected()");
                              theCurrentThread->endOfSourceDetected();
                        }
                  }
            #ifdef Q_OS_UNIX
            }
            #endif

            //if query to stop, directly go to SkipFile
            if(stopIt)
                  goto SkipFile;

            //close file
            DEBUGCONSOLE(90,"copyThread::run","closing source and destination...");
            if(sourceFile.isOpen())
                  sourceFile.close();
            DEBUGCONSOLE(90,"copyThread::run","closing source done");
            if(theCurrentThread==NULL)
            {
                  if(destinationFile.isOpen())
                  {
                        destinationFile.close();
                        DEBUGCONSOLE(90,"copyThread::run","closing destination done");
                  }
            }

            //set the time if no write thread used
            if(keepDate && theCurrentThread==NULL
            #ifdef Q_OS_UNIX
            && !QFileInfo(sourceFile).isSymLink()
            #endif
            )
            {
                  retrySetDate:
                  DEBUGCONSOLE(90,"copyThread::run","set date...");
                  if(!changeFileDateTime(destinationFile.fileName(),sourceFile.fileName()))
                  {
                        DEBUGCONSOLE(90,"copyThread::run","Date cannot be modified!");
                        switch(errorOnFileAndWait(ERROR_DEF_ALL,QFileInfo(sourceFile).absoluteFilePath(),translationErrorDate,id))
                        {
                              case ERRORACTION_ENDOF:
                                    putFirstFileAtEnd();
                                    goto SkipFile;
                              case ERRORACTION_RETRY:
                                    goto retrySetDate;
                              case ERRORACTION_SKIP:
                                    skipCurrentFile();
                              case ERRORACTION_CLOSE:
                                    goto SkipFile;
                              default:
                                    DEBUGCONSOLE(10,"copyThread::run","Unknow action at mkpath ("+QString::number(actionAfterUnlock)+")");
                                    goto SkipFile;
                        }
                  }
                  DEBUGCONSOLE(90,"copyThread::run","set date done");
            }

            //if query to stop, directly go to SkipFile
            if(stopIt)
                  goto SkipFile;

            //remove source in moving mode
            if(movingMode && theCurrentThread==NULL)
            {
                  retrySourceFile:
                  DEBUGCONSOLE(90,"copyThread::run","try remove sourceFile...");
                  if(destinationFile.exists())
                  {
                        if(!sourceFile.remove())
                        {
                              DEBUGCONSOLE(70,"copyThread::run","sourceFile.errorString():\""+sourceFile.errorString()+"\" while removing the source");
                              switch(errorOnFileAndWait(ERROR_DEF_ALL,QFileInfo(destinationFile).absoluteFilePath(),translationErrorRemove+sourceFile.errorString(),id))
                              {
                                    case ERRORACTION_ENDOF:
                                          putFirstFileAtEnd();
                                          goto SkipFile;
                                    case ERRORACTION_RETRY:
                                          goto retrySourceFile;
                                    case ERRORACTION_SKIP:
                                          skipCurrentFile();
                                    case ERRORACTION_CLOSE:
                                          goto SkipFile;
                                    default:
                                          DEBUGCONSOLE(10,"copyThread::run","Unknow action at mkpath ("+QString::number(actionAfterUnlock)+")");
                                          goto SkipFile;
                              }
                        }
                  }
                  else
                  {
                        DEBUGCONSOLE(10,"copyThread::run","try remove source but destination not exists!");
                  }
                  DEBUGCONSOLE(90,"copyThread::run","try remove sourceFile done");
            }

            DEBUGCONSOLE(90,"copyThread::run","now is in SkipFile point!");

/////////////////////////////////
SkipFile:
/////////////////////////////////

            //close file
            DEBUGCONSOLE(90,"copyThread::run","closing source and destination...");
            if(sourceFile.isOpen())
                  sourceFile.close();
            DEBUGCONSOLE(90,"copyThread::run","closing source done");
            if(copyHadBegin)
            {
                  if(theCurrentThread==NULL)
                  {
                        if(destinationFile.isOpen())
                        {
                              DEBUGCONSOLE(90,"copyThread::run","closing destination done");
                              destinationFile.close();
                        }
                  }
            }

            DEBUGCONSOLE(90,"copyThread::run","At the end of copy, actionAfterUnlock: "+QString::number(actionAfterUnlock));
            DEBUGCONSOLE(90,"copyThread::run","At the end of copy, stopIt: "+QString::number(stopIt));
            if(theCurrentThread==NULL)
            {
                  DEBUGCONSOLE(90,"copyThread::run","destinationFile.exists(): "+QString::number(destinationFile.exists()));
                  DEBUGCONSOLE(90,"copyThread::run","copyHadBegin: "+QString::number(copyHadBegin));
                  if((actionAfterUnlock==ERRORACTION_SKIP || actionAfterUnlock==ERRORACTION_CLOSE || actionAfterUnlock==ERRORACTION_ENDOF || stopIt) && destinationFile.exists()    && sourceFile.exists() && copyHadBegin)
                  {
                        DEBUGCONSOLE(50,"copyThread::run","try remove destination: "+destinationFile.fileName());
                        destinationFile.remove();
                  }
            }
            else
            {
                  if(skipThecurrentFile)
                        theCurrentThread->skipTheCurrentFile();
            }

            if(skipThecurrentFile)
            {
                  stopIt=false;
                  skipThecurrentFile=false;
            }
            if(stopIt && theCurrentThread!=NULL)
                  theCurrentThread->stop();
            //do list operation
            if(needRemoveFileToList)
            {
                  QMutexLocker lock_mutex(&MultiThreadCopyList);
                  copyListSize=theCopyList.size();
                  if(copyListSize)
                  {
                        firstScanCur+=theCopyList.first().size;
                        theCopyList.removeFirst();
                        numberOfItemRemoved++;
                        NumberOfFileCopied++;
                        copyListSize--;
                  }
                  else
                  {
                        DEBUGCONSOLE(70,"copyThread::run","copy list is empty");
                  }
            }
            syntetizedStringOverall="File "+QString::number(NumberOfFileCopied)+"/"+QString::number(NumberOfFileTot)+", Total: "+intToQtringSize(firstScanTot);
                DEBUGCONSOLE(90,"copyThread::run","copyListSize: "+QString::number(copyListSize));
      }
      /// \brief Flush the last thread
      if(!stopIt)
      {
            DEBUGCONSOLE(90,"copyThread::run","flush()");
            for (int i = 0; i < theWriteThreadList.size(); ++i) {
                  theWriteThreadList.at(i)->stopWhenIsFinish(true);
            }
            flush();
      }

      timeCopyElapsed         +=tempTimeElapsed.elapsed();
}

Here is the call graph for this function:


Generated by  Doxygen 1.6.0   Back to index