Пример #1
0
int ZipFunc::replace(const DZStrW &d, const DZStrW &s)
{
#ifndef UNICODE
    int             r;
    HINSTANCE       hKernal;
    MoveWithProgress mover;
#endif

    if (Verbose)
        Notify(IVERBOSE, _T("replace '%s' with '%s'"), d.c_str(), s.c_str());
    else
      CB->UserCB(zacTick);  // take a little time

    if (d.IsEmpty() || s.IsEmpty())
    {
        diag(_T("in replace - missing filename"));
		return DZ_ERM_TEMP_FAILED;
    }
#ifdef UNICODE
	if (!MoveFileWithProgress(s.c_str(), d.c_str(), MoveProgress, this,
                  MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING))
    {
	   int re = DZ_ERM_TEMP_FAILED;
		Notify((unsigned)re, _T(" replace: Move failed -[%s]"),  SysMsg().c_str());
        return re;
    }
    return DZ_ERR_GOOD;
#else
    hKernal = LoadLibrary("kernel32.dll");
    if (hKernal == NULL)
        return replaceOrig(d, s);
    mover = (MoveWithProgress) GetProcAddressA(hKernal, "MoveFileWithProgressA");
    if (mover == NULL)
    {
        FreeLibrary(hKernal);
        return replaceOrig(d, s);
    }
    r = DZ_ERR_GOOD;

    CB->UserXItem(100, 2, _T("Copying Temporary File"));
    if (!((mover)(s, d, MoveProgress, this,
                  MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING)))
    {
        r = DZ_ERM_TEMP_FAILED;
        Notify(DZ_ERM_TEMP_FAILED, _T(" replace: Move failed [%s]"), SysMsg());
    }

    FreeLibrary(hKernal);
    return r;
#endif
}
Пример #2
0
unsigned __stdcall _OpFileMoveThread(void *pVoid) {

  OPERATIONSTRUCT os  = *reinterpret_cast<POPERATIONSTRUCT>(pVoid);
  OPCALLBACKDATA ocd;
  TSTRINGLISTIT it    = os.Files.begin();
  UINT uiCurFile      = 1U;
  bool bAskOverwrite  = true;
  BOOL bCancelOp      = FALSE;
  TSTRING tsTemp;

 /*
  * If the destination directory is invalid,
  * it's probably been moved, or renamed
  * since its association with the application.
  * Prompt the user to see if they'd like to 
  * create it, or abort.
  */
  if (FALSE == PathIsDirectory(os.tsDest.c_str())) {

    INT_PTR iChoice = PrintMsgBox(os.hWnd, _T("extFileCopy")
                                  _T(" : Directory not found"),
                                  MB_YESNO | MB_ICONWARNING,
                                  _T("The destination directory:\n\n")
                                  _T("%s\n\nhas either been moved")
                                  _T(" or renamed since its association")
                                  _T(" with extFileCopy. Would you like")
                                  _T(" to create it?"),
                                  os.tsDest.c_str());
            
    switch (iChoice) {

      case IDYES:
      {
       /*
        * Attempt to create the directory.
        */
        if (FALSE == CreateDirectory(os.tsDest.c_str(), NULL)) {

         /*
          * We've failed miserably.
          * Can't copy any files to a non-existant
          * directory; done.
          */
          PrintMsgBox(os.hWnd, _T("extFileCopy : ERROR"),
                      MB_OK | MB_ICONSTOP,
                      _T("An error occurred while attempting")
                      _T(" to create the directory:\n\n%s.\n\n")
                      _T("The directory will be disassociated with")
                      _T("extFileCopy.\n\n")
                      _T("Win32 error: %d"),
                      os.tsDest.c_str(),
                      GetLastError());

          RegUtil_RemAssociatedDirectory(ExtensionFromList(os.Files), os.tsDest);

          goto mtFinish;
        }

      }
      break;

      case IDNO:
      {
       /*
        * User doesn't want to bother creating
        * the directory; We have to remove
        * the association in the registry.
        */
        PrintMsgBox(os.hWnd, _T("extFileCopy : Removing association"),
                    MB_OK | MB_ICONWARNING,
                    _T("The inaccessible directory:\n\n%s\n\nwill now")
                    _T(" be disassociated with extFileCopy."),
                    os.tsDest.c_str());

        RegUtil_RemAssociatedDirectory(ExtensionFromList(os.Files), os.tsDest);

        goto mtFinish;
      }
      break;

    }

  }

  os.pDlg->SetCancelPtr(&bCancelOp);

 /*
  * Set up the dialog UI
  */
  if (os.bCopy) {

    os.pDlg->SetWindowText(_T("Copying ..."));

  } else {

    os.pDlg->SetWindowText(_T("Moving ..."));

  }

  os.pDlg->SetElapsedTime(0);

  for(; it != os.Files.end(); it++) {

   /*
    * Check if the operation was cancelled
    * by the user
    */
    os.pMutex->Lock();

    if (TRUE == bCancelOp) {

      os.pMutex->Unlock();
      goto mtFinish;
    }

    os.pMutex->Unlock();

   /*
    * Update overall progress
    */
    INT64 iOverallPct = ((uiCurFile * 100) / os.Files.size());

    sprintf(tsTemp, _T("Overall Progress (file %d of %d) : %I64d%%"),
            uiCurFile++, os.Files.size(), iOverallPct);

    os.pDlg->SetOverallProgress(iOverallPct, tsTemp);

   /*
    * Update current file name
    */
    TSTRING tsShortFile = GetFileFromPath((*it));
    TSTRING tsDisplay   = tsShortFile;

   /*
    * Truncate file name if necessary
    */
    tsDisplay = FileGetCompactName(tsDisplay, PD_MAXCURFILE);
    
    sprintf(tsTemp, _T("Current file '%s' : %I64d%%"),
            tsDisplay.c_str(), 0LL);

    os.pDlg->SetCurrentProgress(0, tsTemp);

   /*
    * Reset transfer rate
    */
    os.pDlg->SetTransferRate(_T("Transfer Rate: calculating..."));
            
   /* 
    * If the resulting destination path is > MAX_PATH,
    * we've got to skip this file and tell the user.
    */
    TSTRING tsDest = PathConcatFile(os.tsDest, tsShortFile);

    if (MAX_PATH < tsDest.size()) {

      PrintMsgBox(os.hWnd, _T("extFileCopy : Can't copy file"),
                  MB_OK | MB_ICONWARNING,
                  _T("The following destination path:\n\n")
                  _T("%s\n\nis longer than MAX_PATH characters, and")
                  _T(" cannot be created."), tsDest.c_str());

      continue;

    }

   /*
    * Make sure the file doesn't exist; if it does,
    * we've got to prompt the user to overwrite.
    */
    if (-1 != GetFileAttributes(tsDest.c_str())) {

     /*
      * Yeah, the file exists.
      */
      if (bAskOverwrite) {

        OverwriteDlg dlg((*it), tsDest, 1 < os.Files.size());

        if (!dlg.DoModal(static_cast<HINSTANCE>(g_hModule),
                         MAKEINTRESOURCE(IDD_OVERWRITE),
                         os.pDlg->m_hWnd))
        {
          
          MessageBox(os.pDlg->m_hWnd, _T("A fatal internal error has occurred. Cannot continue.\n"),
                     _T("extFileCopy : ERROR"), MB_OK | MB_ICONSTOP);

          os.pDlg->CleanUp();
          _endthreadex(1);

        }

       /*
        * Find out what the user chose
        */
        switch (dlg.Result()) {

          case YES:
           /*
            * Ok to overwrite, but don't touch
            * bAskOverwrite.
            */            
          break;

          case YESTOALL:
           /*
            * Ok to overwrite ALL files.
            */
            bAskOverwrite = false;
          break;

          case NO:
           /*
            * Don't overwrite this file;
            * instead skip to the next.
            */
            continue;
          break;

          case CANCEL:
           /*
            * Forget it. We're outta here.
            */
            os.pMutex->Lock();
            bCancelOp = TRUE;
            os.pMutex->Unlock();
            goto mtFinish;
          break;

        }

      }

    }

   /*
    * File doesn't exist, or we don't
    * really care.  Continue normally
    */
    TSTRING tsCurFile = FileGetCompactName(tsShortFile.c_str(), PD_MAXCURFILE);

    ocd.pbCancel    = &bCancelOp;
    ocd.dwStartTime = (2 == uiCurFile) ? GetTickCount() : ocd.dwStartTime;
    ocd.dwLastTime  = (2 == uiCurFile) ? ocd.dwStartTime : ocd.dwLastTime;
    ocd.dwLastRate  = GetTickCount();
    ocd.iLastPct    = 0LL;
    ocd.iThroughput = 0LL;
    ocd.pDlg        = os.pDlg;
    ocd.pMutex      = os.pMutex;
    ocd.szCurFile   = tsCurFile.c_str();

    if (os.bCopy) {

     /*
      * This is a copy operation
      */
      if (0 == CopyFileEx((*it).c_str(), tsDest.c_str(), _Callback,
                           &ocd, &bCancelOp, 0UL))
      {

       /*
        * The copy operation has failed!
        * Let's tell the user, and then
        * if there's more than one file
        * being copied, ask the user if
        * they'd like to continue with
        * the next file. (If the error
        * didn't result from user abortion)
        */
        if (ERROR_REQUEST_ABORTED != GetLastError()) {

          INT_PTR iUser = ErrorPrompt(os.pDlg->m_hWnd, (*it), tsDest, 
                                    (1 < os.Files.size()) ? true : false, os.bCopy);

          if (1 < os.Files.size()) {

            if (IDYES == iUser) {

            /*
              * User wishes to try the next file.
              */
              continue;

            } else if (IDNO == iUser) {

              break;

            }

          } else {

            continue;

          }

        }

      }

    } else {

     /*
      * This is a move operation
      */
      if (0 == MoveFileWithProgress((*it).c_str(), tsDest.c_str(), _Callback,
                                    &ocd, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING
                                    | MOVEFILE_WRITE_THROUGH))
      {

       /*
        * The move operation has failed!
        * Let's tell the user, and then
        * if there's more than one file
        * being copied, ask the user if
        * they'd like to continue with
        * the next file. (If the error
        * didn't result from user abortion)
        */
        if (ERROR_REQUEST_ABORTED != GetLastError()) {

          INT_PTR iUser = ErrorPrompt(os.pDlg->m_hWnd, (*it), tsDest,
                                      (1 < os.Files.size()) ? true : false, os.bCopy);

          if (1 < os.Files.size()) {

            if (IDYES == iUser) {

            /*
              * User wishes to try the next file.
              */
              continue;

            } else if (IDNO == iUser) {

              break;

            }

          } else {

            continue;

          }

        }

      }

    }

  }

mtFinish:

  os.pDlg->CleanUp();
  _endthreadex(0U);
  return 0U;
}