filebrowser/engine/FBFileOps.cpp
changeset 17 4f2773374eff
child 31 e7a04a6385be
equal deleted inserted replaced
15:e11368ed4880 17:4f2773374eff
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include "FBFileOps.h"
       
    20 #include "FBFileUtils.h"
       
    21 #include "engine.h"
       
    22 #include "FBTraces.h"
       
    23 
       
    24 #ifndef FILEBROWSER_LITE
       
    25   #include "FBFileOpClient.h"
       
    26 #endif  
       
    27 
       
    28 #include <f32file.h>
       
    29 #include <bautils.h>
       
    30 #include <eikenv.h>
       
    31 #include <babackup.h>
       
    32 
       
    33 const TInt KSecureBackupStartDelay = 750000;
       
    34 const TInt KSecureBackupLoopDelay = 100000;
       
    35 const TInt KSecureBackupEndDelay = 200000;
       
    36 const TInt KMaxFileLockAttempts = 3;
       
    37 
       
    38 
       
    39 // ================= MEMBER FUNCTIONS =======================
       
    40 
       
    41 CFileBrowserFileOps* CFileBrowserFileOps::NewL(CEngine* aEngine)
       
    42 	{
       
    43 	CFileBrowserFileOps* self = new(ELeave) CFileBrowserFileOps(aEngine);
       
    44 	CleanupStack::PushL(self);
       
    45 	self->ConstructL();
       
    46 	CleanupStack::Pop();
       
    47 	return self;
       
    48 	}
       
    49 
       
    50 // --------------------------------------------------------------------------------------------
       
    51 
       
    52 CFileBrowserFileOps::CFileBrowserFileOps(CEngine* aEngine) : iEngine(aEngine)
       
    53 	{
       
    54 	}
       
    55 
       
    56 // --------------------------------------------------------------------------------------------
       
    57 
       
    58 void CFileBrowserFileOps::ConstructL()
       
    59 	{
       
    60 	iRecursiveState = EFileOpInvalid;
       
    61 	iSecureBackUpActive = EFalse;
       
    62 	iFileCommandActivatedSecureBackup = EFalse;
       
    63 	
       
    64     User::LeaveIfError(iFs.Connect());
       
    65     iFileMan = CFileMan::NewL( iFs, this );
       
    66 	}
       
    67 
       
    68 // --------------------------------------------------------------------------------------------
       
    69 
       
    70 CFileBrowserFileOps::~CFileBrowserFileOps()
       
    71 	{
       
    72 	if (iSBEClient)
       
    73 	    delete iSBEClient;
       
    74 	
       
    75     #ifndef FILEBROWSER_LITE	
       
    76         if (iFileOpClient)
       
    77             delete iFileOpClient;
       
    78     #endif	
       
    79 
       
    80     delete iFileMan;
       
    81     iFs.Close();    
       
    82     }
       
    83 
       
    84 // --------------------------------------------------------------------------------------------
       
    85 
       
    86 TInt CFileBrowserFileOps::ActivateSecureBackUpViaFileOp()
       
    87     {
       
    88     iFileManObserverResult = MFileManObserver::EContinue;
       
    89     // if already activate by a file command, return ok
       
    90     if (iFileCommandActivatedSecureBackup)
       
    91         return KErrNone;
       
    92     else
       
    93         {
       
    94         // if secure backup is already active, disable it first, because it may in wrong state
       
    95         if (iSecureBackUpActive)
       
    96             DeActivateSecureBackUp();
       
    97         }
       
    98     
       
    99     // try to activate full secure backup
       
   100     TInt err = ActivateSecureBackUp(conn::EBURBackupFull, conn::EBackupBase);
       
   101     
       
   102     if (err == KErrNone)
       
   103         iFileCommandActivatedSecureBackup = ETrue;
       
   104     
       
   105     return err;
       
   106     }
       
   107 
       
   108 // --------------------------------------------------------------------------------------------
       
   109 
       
   110 TInt CFileBrowserFileOps::DeActivateSecureBackUpViaFileOp()
       
   111     {
       
   112     TInt err(KErrGeneral);
       
   113     iFileManObserverResult = MFileManObserver::EContinue;
       
   114     
       
   115     // if activate by a file command, try to reactivate it
       
   116     if (iFileCommandActivatedSecureBackup)
       
   117         {
       
   118         err = DeActivateSecureBackUp();
       
   119         
       
   120         // even if it fails, forget the state
       
   121         iFileCommandActivatedSecureBackup = EFalse;
       
   122         }
       
   123 
       
   124     return err;
       
   125     }    
       
   126     
       
   127 // --------------------------------------------------------------------------------------------
       
   128 
       
   129 TInt CFileBrowserFileOps::ActivateSecureBackUp(conn::TBURPartType aPartType, conn::TBackupIncType aBackupIncType)
       
   130     {
       
   131     iFileManObserverResult = MFileManObserver::EContinue;
       
   132     // check for invalid parameters
       
   133     if (aPartType == conn::EBURNormal || aBackupIncType == conn::ENoBackup)
       
   134         User::Panic(_L("Inv.Usage.SE"), 532);
       
   135     
       
   136     TInt err(KErrNone);
       
   137     
       
   138     if (!iSBEClient)
       
   139         {
       
   140         TRAP(err, iSBEClient = conn::CSBEClient::NewL());
       
   141         if (err != KErrNone)
       
   142             return err;
       
   143         }
       
   144     
       
   145     TDriveList driveList;
       
   146     err = iFs.DriveList(driveList);
       
   147     
       
   148     if (err == KErrNone)
       
   149         {
       
   150         // make sure that the application has a system status to prevent getting shut down events
       
   151         iEngine->EikonEnv()->SetSystem(ETrue);
       
   152     
       
   153         // activating secure back up removes locks from files which respect this fuctionality                
       
   154         TRAP(err, iSBEClient->SetBURModeL(driveList, aPartType, aBackupIncType));
       
   155         
       
   156         if (err == KErrNone)
       
   157             {
       
   158             iSecureBackUpActive = ETrue;
       
   159             User::After(KSecureBackupStartDelay); // a short delay to wait to activate
       
   160             }
       
   161         }
       
   162 
       
   163     return err;
       
   164     }
       
   165 
       
   166 // --------------------------------------------------------------------------------------------
       
   167 
       
   168 TInt CFileBrowserFileOps::DeActivateSecureBackUp()
       
   169     {
       
   170     TInt err(KErrNone);
       
   171     
       
   172     if (!iSBEClient)
       
   173         {
       
   174         TRAP(err, iSBEClient = conn::CSBEClient::NewL());
       
   175         if (err != KErrNone)
       
   176             return err;
       
   177         
       
   178         // make sure that the application has a system status
       
   179         iEngine->EikonEnv()->SetSystem(ETrue);
       
   180         }
       
   181         
       
   182     TDriveList driveList;
       
   183     err = iFs.DriveList(driveList);
       
   184     
       
   185     if (err == KErrNone)
       
   186         {
       
   187         // deactivate secure backup
       
   188         TRAP(err, iSBEClient->SetBURModeL(driveList, conn::EBURNormal, conn::ENoBackup));
       
   189         
       
   190         User::After(KSecureBackupEndDelay); // a short delay to wait to deactivate
       
   191 
       
   192         // system status not needed anymore
       
   193         iEngine->EikonEnv()->SetSystem(EFalse);
       
   194         }
       
   195     
       
   196     iSecureBackUpActive = EFalse;
       
   197 
       
   198     return err;
       
   199     }
       
   200         
       
   201 // --------------------------------------------------------------------------------------------
       
   202 
       
   203 TInt CFileBrowserFileOps::DoFindEntries(const TDesC& aFileName, const TDesC& aPath)
       
   204     {
       
   205     TFindFile fileFinder(iFs);
       
   206     CDir* dir;
       
   207     TInt err = fileFinder.FindWildByPath(aFileName, &aPath, dir);
       
   208 
       
   209     while (err == KErrNone && iFileManObserverResult != MFileManObserver::ECancel)
       
   210         {
       
   211         for (TInt i=0; i<dir->Count(); i++)
       
   212             {
       
   213             TEntry entry = (*dir)[i];
       
   214 
       
   215             if (entry.iName.Length() && aPath.Length())
       
   216                 {
       
   217                 // parse the entry
       
   218                 TParse parsedName;
       
   219                 parsedName.Set(entry.iName, &fileFinder.File(), NULL);
       
   220                 
       
   221                 if (parsedName.Drive().Length() && aPath.Length() && parsedName.Drive()[0] == aPath[0])
       
   222                     {
       
   223                     // get full source path
       
   224                     TFileName fullSourcePath = parsedName.FullName();
       
   225                     if (entry.IsDir())
       
   226                         fullSourcePath.Append(_L("\\"));
       
   227                     
       
   228                     // call the file operation command
       
   229                     switch(iRecursiveState)
       
   230                         {
       
   231                         case EFileOpAttribs:
       
   232                             {
       
   233                             // the same attribs command can be given for both directories and files
       
   234                             FileOpAttribs(fullSourcePath, iUint1, iUint2, iTime1, iUint3);
       
   235                             }
       
   236                             break;
       
   237 
       
   238                         case EFileOpCopy:
       
   239                             {
       
   240                             // calculate length of new entries added to the original source path
       
   241                             TUint newEntriesLength = fullSourcePath.Length() - iBuf1.Length();
       
   242                             
       
   243                             // get pointer description to the rightmost data
       
   244                             TPtr16 newEntriesPtr = fullSourcePath.RightTPtr(newEntriesLength);
       
   245                             
       
   246                             // generate target path
       
   247                             TFileName fullTargetPath = iBuf2;
       
   248                             fullTargetPath.Append(newEntriesPtr);
       
   249                             
       
   250                             if (entry.IsDir())
       
   251                                 {
       
   252                                 // if it is a directory entry, just create it based on the entry's attributes
       
   253                                 FileOpMkDirAll(fullTargetPath, entry.iAtt);
       
   254                                 }
       
   255                             else
       
   256                                 {
       
   257                                 // otherwise copy the file
       
   258                                 FileOpCopy(fullSourcePath, fullTargetPath, iUint1);
       
   259                                 }
       
   260                             }
       
   261                             break;
       
   262 
       
   263                         case EFileOpDelete:
       
   264                             {
       
   265                             if (entry.IsDir())
       
   266                                 {
       
   267                                 // for directories call rmdir    
       
   268                                 FileOpRmDir(fullSourcePath, iUint1);
       
   269                                 }
       
   270                             else
       
   271                                 {
       
   272                                 // for files call the normal file deletion operation    
       
   273                                 FileOpDeleteFile(fullSourcePath, iUint1);
       
   274                                 }
       
   275                             }
       
   276                             break;
       
   277                                                     
       
   278                         default:
       
   279                             User::Panic (_L("FileOpRecurs"), 775);
       
   280                             break;
       
   281                         }                        
       
   282                     }
       
   283                 }
       
   284                 if ( iFileManObserverResult == MFileManObserver::ECancel ) break;
       
   285             }
       
   286 
       
   287         delete dir;
       
   288         dir = NULL;
       
   289         if ( iFileManObserverResult != MFileManObserver::ECancel )
       
   290             {
       
   291             err = fileFinder.FindWild(dir);
       
   292             }
       
   293         }
       
   294 
       
   295     return err;
       
   296     }
       
   297 
       
   298 // --------------------------------------------------------------------------------------------
       
   299 
       
   300 TInt CFileBrowserFileOps::DoFindEntriesRecursiveL(const TDesC& aFileName, const TDesC& aPath)
       
   301 	{
       
   302     TInt err(KErrNone);
       
   303 
       
   304     // it is logical to scan upwards when deleting and changing attributes
       
   305     CDirScan::TScanDirection scanDirection = CDirScan::EScanUpTree;
       
   306     
       
   307     // when copying files, it is more logical to move downwards
       
   308     if (iRecursiveState == EFileOpCopy)
       
   309         scanDirection = CDirScan::EScanDownTree;
       
   310     
       
   311     
       
   312     CDirScan* scan = CDirScan::NewLC(iFs);
       
   313     scan->SetScanDataL(aPath, KEntryAttDir|KEntryAttMatchMask, ESortByName | EAscending | EDirsFirst, scanDirection);
       
   314     CDir* dir = NULL;
       
   315 
       
   316     for(;;)
       
   317         {
       
   318         TRAP(err, scan->NextL(dir));
       
   319         if (!dir  || (err != KErrNone))
       
   320             break;
       
   321 
       
   322         for (TInt i=0; i<dir->Count(); i++)
       
   323             {
       
   324             TEntry entry = (*dir)[i];
       
   325             
       
   326             if (entry.IsDir())
       
   327                 {
       
   328                 TFileName path(scan->FullPath());
       
   329                 
       
   330                 if (path.Length())
       
   331                     {
       
   332                     path.Append(entry.iName);
       
   333                     path.Append(_L("\\"));
       
   334                     DoFindEntries(aFileName, path);
       
   335                     }
       
   336                 }
       
   337             if ( iFileManObserverResult == MFileManObserver::ECancel )
       
   338                 {
       
   339                 break;
       
   340                 }
       
   341             }
       
   342         delete(dir);
       
   343         if ( iFileManObserverResult == MFileManObserver::ECancel )
       
   344             {
       
   345             break;
       
   346             }
       
   347         }
       
   348 
       
   349     CleanupStack::PopAndDestroy(scan);
       
   350     return err;
       
   351     }
       
   352     
       
   353 // --------------------------------------------------------------------------------------------
       
   354 // --------------------------------------------------------------------------------------------
       
   355 
       
   356 TInt CFileBrowserFileOps::Copy(const TFileEntry& aSourceEntry, const TDesC& aTargetFullName, TUint aSwitch, TBool aDeleteSource) 
       
   357     {
       
   358     iOperationError = KErrNone;
       
   359     iFileManObserverResult = MFileManObserver::EContinue;
       
   360     
       
   361     TFileName sourcePath = aSourceEntry.iPath;
       
   362     sourcePath.Append(aSourceEntry.iEntry.iName);
       
   363     
       
   364     TInt err(KErrNone);
       
   365 
       
   366     if (aSourceEntry.iEntry.IsDir() && (aSwitch & CFileMan::ERecurse))
       
   367         {
       
   368         // find all files recursively and run the operation for them
       
   369         iRecursiveState = EFileOpCopy;
       
   370         sourcePath.Append(_L("\\"));
       
   371         
       
   372         TFileName targetPath = aTargetFullName;
       
   373         targetPath.Append(_L("\\"));
       
   374         
       
   375         // remove the recursion flag because we will implement our own recursion
       
   376         TUint newSwitch(aSwitch);
       
   377         newSwitch &= ~CFileMan::ERecurse;
       
   378     
       
   379         iBuf1.Copy(sourcePath);
       
   380         iBuf2.Copy(targetPath);
       
   381         iUint1 = newSwitch;
       
   382         
       
   383         // create initial directory - if it does not succeed, do not even try to continue
       
   384         err = FileOpMkDirAll(targetPath, aSourceEntry.iEntry.iAtt);
       
   385         
       
   386         if (iOperationError == KErrNone)
       
   387             {
       
   388             TRAP(err, DoFindEntries(_L("*"), sourcePath));              // entries under current directory entry
       
   389             if ( iFileManObserverResult != MFileManObserver::ECancel )
       
   390                 {
       
   391                 TRAP(err, DoFindEntriesRecursiveL(_L("*"), sourcePath));    // recursively under directories of current directory entry
       
   392                 }
       
   393             }
       
   394         }
       
   395 
       
   396     else if (aSourceEntry.iEntry.IsDir())
       
   397         {
       
   398         TFileName targetPath = aTargetFullName;
       
   399         targetPath.Append(_L("\\"));
       
   400         
       
   401         // just create a directory based on the file attributes of the source directory
       
   402         err = FileOpMkDirAll(targetPath, aSourceEntry.iEntry.iAtt);
       
   403         }
       
   404         
       
   405     else
       
   406         {
       
   407         // remove a recursion flag if present (this should never happen, but some extra error checking)
       
   408         if (aSwitch & CFileMan::ERecurse)
       
   409             aSwitch &= ~CFileMan::ERecurse;    
       
   410             
       
   411         // do the operation for a file entry
       
   412         err = FileOpCopy(sourcePath, aTargetFullName, aSwitch);
       
   413         }
       
   414     
       
   415         
       
   416     // delete source if needed and copy succeeded without any errors (== move operation)
       
   417     if ( aDeleteSource && iOperationError == KErrNone &&
       
   418          iFileManObserverResult != MFileManObserver::ECancel )
       
   419         {
       
   420         err = Delete(aSourceEntry, aSwitch);    
       
   421         }
       
   422     
       
   423     if ( !iOperationError && iFileManObserverResult == MFileManObserver::ECancel )
       
   424         {
       
   425         iOperationError = KErrCancel;
       
   426         }
       
   427         
       
   428     return iOperationError; 
       
   429     }
       
   430 
       
   431 // --------------------------------------------------------------------------------------------
       
   432 
       
   433 TInt CFileBrowserFileOps::FileOpCopy(const TDesC& aSourceFullName, const TDesC& aTargetFullName, TUint aSwitch) 
       
   434     {
       
   435     TInt err = DoFileOpCopy(aSourceFullName, aTargetFullName, aSwitch);
       
   436 
       
   437     // if locked, unlock the file and retry
       
   438     if (iEngine->Settings().iRemoveFileLocks && err == KErrInUse)
       
   439         {
       
   440         // try to remove the file lock by activating secure backup mode
       
   441         if (ActivateSecureBackUpViaFileOp() == KErrNone)
       
   442             {
       
   443             // try the operation several times
       
   444             for (TInt i=0; i<KMaxFileLockAttempts; i++)
       
   445                 {
       
   446                 err = DoFileOpCopy(aSourceFullName, aTargetFullName, aSwitch);
       
   447                 
       
   448                 if (err != KErrInUse)
       
   449                     break;
       
   450                 else
       
   451                     User::After(KSecureBackupLoopDelay);
       
   452                 }
       
   453             }
       
   454         }
       
   455 
       
   456     // if access denied, then try to remove the target path and try again
       
   457     if (iEngine->Settings().iIgnoreProtectionsAtts && err == KErrAccessDenied && BaflUtils::FileExists(iFs, aTargetFullName))
       
   458         {
       
   459         if (FileOpDeleteFile(aTargetFullName, 0) == KErrNone)
       
   460             {
       
   461             err = DoFileOpCopy(aSourceFullName, aTargetFullName, aSwitch);
       
   462             }
       
   463         }
       
   464 
       
   465     // if the file already exists, it is not an error    
       
   466     if (err == KErrAlreadyExists)
       
   467         err = KErrNone;
       
   468     
       
   469     
       
   470     // if copying from a ROM drive, remove the writing protection flag
       
   471     if (iEngine->Settings().iRemoveROMWriteProrection && err == KErrNone && aSourceFullName.Length() > 3 && (aSourceFullName[0]=='z' || aSourceFullName[0]=='Z'))
       
   472         {
       
   473         FileOpAttribs(aTargetFullName, 0, KEntryAttReadOnly, 0, 0);
       
   474         }
       
   475     
       
   476     
       
   477     // remember the "lowest" error
       
   478     if (err < iOperationError)
       
   479         iOperationError = err;
       
   480 
       
   481     LOGSTRING4("FileBrowser: FileOpCopy %S -> %S, err=%d", &aSourceFullName, &aTargetFullName, err);
       
   482         
       
   483     return err; 
       
   484     }
       
   485             
       
   486 // --------------------------------------------------------------------------------------------
       
   487 
       
   488 TInt CFileBrowserFileOps::DoFileOpCopy(const TDesC& aSourceFullName, const TDesC& aTargetFullName, TUint aSwitch) 
       
   489     {
       
   490     #ifndef FILEBROWSER_LITE
       
   491     if (iEngine->Settings().iBypassPlatformSecurity)
       
   492         {
       
   493         if (!iFileOpClient)
       
   494             iFileOpClient = CFBFileOpClient::NewL();   
       
   495             
       
   496         return iFileOpClient->Copy(aSourceFullName, aTargetFullName, aSwitch);
       
   497         }
       
   498     else
       
   499         {
       
   500     #endif
       
   501         //CAsyncWaiter* waiter = CAsyncWaiter::NewLC();
       
   502         //TInt result = iFileMan->Copy( aSourceFullName, aTargetFullName, aSwitch, waiter->iStatus );
       
   503         //waiter->StartAndWait();
       
   504         //if ( !result ) result = waiter->Result();
       
   505         //CleanupStack::PopAndDestroy( waiter );
       
   506         TInt result = iFileMan->Copy( aSourceFullName, aTargetFullName, aSwitch );
       
   507         return result;
       
   508     #ifndef FILEBROWSER_LITE
       
   509         }    
       
   510     #endif
       
   511     }
       
   512 
       
   513 // --------------------------------------------------------------------------------------------
       
   514 // --------------------------------------------------------------------------------------------
       
   515 
       
   516 TInt CFileBrowserFileOps::Rename(const TFileEntry& aSourceEntry, const TDesC& aNew, TUint aSwitch)
       
   517     {
       
   518     iOperationError = KErrNone;
       
   519     iFileManObserverResult = MFileManObserver::EContinue;
       
   520     
       
   521     TFileName sourcePath = aSourceEntry.iPath;
       
   522     sourcePath.Append(aSourceEntry.iEntry.iName);
       
   523     
       
   524     if (aSourceEntry.iEntry.IsDir())
       
   525         {
       
   526         // do the operation for a directory entry
       
   527         FileOpRename(sourcePath, aNew, aSwitch);
       
   528         }
       
   529     else
       
   530         {
       
   531         // do the operation for a file
       
   532         FileOpRename(sourcePath, aNew, aSwitch);
       
   533         }
       
   534 
       
   535     return iOperationError; 
       
   536     }
       
   537 
       
   538 // --------------------------------------------------------------------------------------------
       
   539 
       
   540 TInt CFileBrowserFileOps::FileOpRename(const TDesC& aName, const TDesC& aNew, TUint aSwitch)
       
   541     {
       
   542     TBool setBackROFlag(EFalse);
       
   543 
       
   544     TInt err = DoFileOpRename(aName, aNew, aSwitch);
       
   545 
       
   546     // if locked, unlock the file and retry
       
   547     if (iEngine->Settings().iRemoveFileLocks && err == KErrInUse)
       
   548         {
       
   549         // try to remove the file lock by activating secure backup mode
       
   550         if (ActivateSecureBackUpViaFileOp() == KErrNone)
       
   551             {
       
   552             // try the operation several times
       
   553             for (TInt i=0; i<KMaxFileLockAttempts; i++)
       
   554                 {
       
   555                 err = DoFileOpRename(aName, aNew, aSwitch);
       
   556                 
       
   557                 if (err != KErrInUse)
       
   558                     break;
       
   559                 else
       
   560                     User::After(KSecureBackupLoopDelay);
       
   561                 }
       
   562             }
       
   563         }
       
   564 
       
   565     // if write protected, remove protection and retry
       
   566     else if (iEngine->Settings().iIgnoreProtectionsAtts && err == KErrAccessDenied)
       
   567         {
       
   568         // remove write protection and try again
       
   569         if (FileOpAttribs(aName, 0, KEntryAttReadOnly, 0, 0) == KErrNone)
       
   570             {
       
   571             err = DoFileOpRename(aName, aNew, aSwitch);
       
   572             
       
   573             setBackROFlag = ETrue;
       
   574             }
       
   575         }
       
   576 
       
   577     // if still access denied, then try to remove the target path and try again
       
   578     if (iEngine->Settings().iIgnoreProtectionsAtts && err == KErrAccessDenied && BaflUtils::FileExists(iFs, aNew))
       
   579         {
       
   580         if (FileOpDeleteFile(aNew, 0) == KErrNone)
       
   581             {
       
   582             err = DoFileOpRename(aName, aNew, aSwitch);
       
   583             }
       
   584         }
       
   585 
       
   586     // set back the read only flag
       
   587     if (setBackROFlag)
       
   588         FileOpAttribs(aName, KEntryAttReadOnly, 0, 0, 0);
       
   589             
       
   590 
       
   591     // remember the "lowest" error
       
   592     if (err < iOperationError)
       
   593         iOperationError = err;
       
   594 
       
   595     LOGSTRING3("FileBrowser: FileOpRename %S, err=%d", &aName, err);
       
   596         
       
   597     return err; 
       
   598     }
       
   599     
       
   600 // --------------------------------------------------------------------------------------------
       
   601 
       
   602 TInt CFileBrowserFileOps::DoFileOpRename(const TDesC& aName, const TDesC& aNew, TUint aSwitch)
       
   603     {
       
   604     #ifndef FILEBROWSER_LITE
       
   605     if (iEngine->Settings().iBypassPlatformSecurity)
       
   606         {
       
   607         if (!iFileOpClient)
       
   608             iFileOpClient = CFBFileOpClient::NewL();   
       
   609 
       
   610         return iFileOpClient->Rename(aName, aNew, aSwitch);
       
   611         }
       
   612     else
       
   613         {
       
   614     #endif
       
   615         return iFileMan->Rename(aName, aNew, aSwitch);
       
   616     #ifndef FILEBROWSER_LITE
       
   617         }    
       
   618     #endif
       
   619     }
       
   620 
       
   621 // --------------------------------------------------------------------------------------------
       
   622 // --------------------------------------------------------------------------------------------
       
   623 
       
   624 TInt CFileBrowserFileOps::Attribs(const TFileEntry& aSourceEntry, TUint aSetMask, TUint aClearMask, const TTime& aTime, TUint aSwitch) 
       
   625     {
       
   626     iOperationError = KErrNone;
       
   627     iFileManObserverResult = MFileManObserver::EContinue;
       
   628     
       
   629     TFileName sourcePath = aSourceEntry.iPath;
       
   630     sourcePath.Append(aSourceEntry.iEntry.iName);
       
   631     
       
   632     TInt err(KErrNone);
       
   633 
       
   634     if (aSourceEntry.iEntry.IsDir() && (aSwitch & CFileMan::ERecurse))
       
   635         {
       
   636         // do the operation for a current directory entry
       
   637         err = FileOpAttribs(sourcePath, aSetMask, aClearMask, aTime, 0);
       
   638 
       
   639         // find all files recursively and run the operation for them
       
   640         iRecursiveState = EFileOpAttribs;
       
   641         sourcePath.Append(_L("\\"));
       
   642     
       
   643         iBuf1.Copy(sourcePath);
       
   644         iUint1 = aSetMask;
       
   645         iUint2 = aClearMask;
       
   646         iTime1 = aTime;
       
   647         iUint3 = 0;
       
   648         
       
   649         TRAP(err, DoFindEntriesRecursiveL(_L("*"), sourcePath));    // recursively under directories of current directory entry
       
   650         TRAP(err, DoFindEntries(_L("*"), sourcePath));              // entries under current directory entry
       
   651         }
       
   652 
       
   653     else if (aSourceEntry.iEntry.IsDir())
       
   654         {
       
   655         //sourcePath.Append(_L("\\"));   // <-- do not apply!
       
   656     
       
   657         // do the operation for a directory entry
       
   658         err = FileOpAttribs(sourcePath, aSetMask, aClearMask, aTime, 0);
       
   659         }
       
   660             
       
   661     else
       
   662         {
       
   663         // do the operation for a file entry
       
   664         err = FileOpAttribs(sourcePath, aSetMask, aClearMask, aTime, 0);
       
   665         }
       
   666 
       
   667     return iOperationError;     
       
   668     }
       
   669 
       
   670 // --------------------------------------------------------------------------------------------
       
   671     
       
   672 TInt CFileBrowserFileOps::FileOpAttribs(const TDesC& aName, TUint aSetMask, TUint aClearMask, const TTime& aTime, TUint aSwitch) 
       
   673     {
       
   674     TInt err = DoFileOpAttribs(aName, aSetMask, aClearMask, aTime, aSwitch);
       
   675 
       
   676     // if locked, unlock the file and retry
       
   677     if (iEngine->Settings().iRemoveFileLocks && err == KErrInUse)
       
   678         {
       
   679         // try to remove the file lock by activating secure backup mode
       
   680         if (ActivateSecureBackUpViaFileOp() == KErrNone)
       
   681             {
       
   682             // try the operation several times
       
   683             for (TInt i=0; i<KMaxFileLockAttempts; i++)
       
   684                 {
       
   685                 err = DoFileOpAttribs(aName, aSetMask, aClearMask, aTime, aSwitch);
       
   686                 
       
   687                 if (err != KErrInUse)
       
   688                     break;
       
   689                 else
       
   690                     User::After(KSecureBackupLoopDelay);
       
   691                 }
       
   692             }
       
   693         }
       
   694 
       
   695     // remember the "lowest" error
       
   696     if (err < iOperationError)
       
   697         iOperationError = err;
       
   698 
       
   699     LOGSTRING3("FileBrowser: FileOpAttribs %S, err=%d", &aName, err);
       
   700         
       
   701     return err;    
       
   702     }
       
   703     
       
   704 // --------------------------------------------------------------------------------------------
       
   705     
       
   706 TInt CFileBrowserFileOps::DoFileOpAttribs(const TDesC& aName, TUint aSetMask, TUint aClearMask, const TTime& aTime, TUint aSwitch) 
       
   707     {
       
   708     #ifndef FILEBROWSER_LITE
       
   709     if (iEngine->Settings().iBypassPlatformSecurity)
       
   710         {
       
   711         if (!iFileOpClient)
       
   712             iFileOpClient = CFBFileOpClient::NewL();   
       
   713 
       
   714         return iFileOpClient->Attribs(aName, aSetMask, aClearMask, aTime, aSwitch);
       
   715         }
       
   716     else
       
   717         {
       
   718     #endif
       
   719         return iFileMan->Attribs(aName, aSetMask, aClearMask, aTime, aSwitch);
       
   720     #ifndef FILEBROWSER_LITE
       
   721         }    
       
   722     #endif
       
   723     }
       
   724 
       
   725 // --------------------------------------------------------------------------------------------
       
   726 // --------------------------------------------------------------------------------------------
       
   727 
       
   728 TInt CFileBrowserFileOps::Delete(const TFileEntry& aSourceEntry, TUint aSwitch) 
       
   729     {
       
   730     iOperationError = KErrNone;
       
   731     iFileManObserverResult = MFileManObserver::EContinue;
       
   732     
       
   733     TFileName sourcePath = aSourceEntry.iPath;
       
   734     sourcePath.Append(aSourceEntry.iEntry.iName);
       
   735     
       
   736     TInt err(KErrNone);
       
   737 
       
   738     if (aSourceEntry.iEntry.IsDir() && (aSwitch & CFileMan::ERecurse))
       
   739         {
       
   740         // find all files recursively and run the operation for them
       
   741         iRecursiveState = EFileOpDelete;
       
   742         sourcePath.Append(_L("\\"));
       
   743     
       
   744         iBuf1.Copy(sourcePath);
       
   745         iUint1 = 0;
       
   746         
       
   747         TRAP(err, DoFindEntriesRecursiveL(_L("*"), sourcePath));    // recursively under directories of current directory entry
       
   748         if ( iFileManObserverResult != MFileManObserver::ECancel )
       
   749             {
       
   750             TRAP(err, DoFindEntries(_L("*"), sourcePath));              // entries under current directory entry
       
   751             }
       
   752         
       
   753         if ( iFileManObserverResult != MFileManObserver::ECancel )
       
   754             {
       
   755             // do the operation for a current directory entry as well
       
   756             err = FileOpRmDir(sourcePath, 0);
       
   757             }
       
   758         }
       
   759 
       
   760     else if (aSourceEntry.iEntry.IsDir())
       
   761         {
       
   762         sourcePath.Append(_L("\\"));
       
   763     
       
   764         // do the operation for a directory entry
       
   765         err = FileOpRmDir(sourcePath, 0);
       
   766         }
       
   767 
       
   768     else
       
   769         {
       
   770         // do the operation for a file entry
       
   771         err = FileOpDeleteFile(sourcePath, 0);
       
   772         }
       
   773     if ( !iOperationError && iFileManObserverResult == MFileManObserver::ECancel )
       
   774         {
       
   775         iOperationError = KErrCancel;
       
   776         }
       
   777     return iOperationError; 
       
   778     }
       
   779 
       
   780 // --------------------------------------------------------------------------------------------
       
   781 
       
   782 TInt CFileBrowserFileOps::FileOpDeleteFile(const TDesC& aName, TUint aSwitch) 
       
   783     {
       
   784     TInt err = DoFileOpDeleteFile(aName, aSwitch);
       
   785 
       
   786     // if locked, unlock the file and retry
       
   787     if (iEngine->Settings().iRemoveFileLocks && err == KErrInUse)
       
   788         {
       
   789         // try to remove the file lock by activating secure backup mode
       
   790         if (ActivateSecureBackUpViaFileOp() == KErrNone)
       
   791             {
       
   792             // try the operation several times
       
   793             for (TInt i=0; i<KMaxFileLockAttempts; i++)
       
   794                 {
       
   795                 err = DoFileOpDeleteFile(aName, aSwitch);
       
   796                 
       
   797                 if (err != KErrInUse)
       
   798                     break;
       
   799                 else
       
   800                     User::After(KSecureBackupLoopDelay);
       
   801                 }
       
   802             }
       
   803         }
       
   804 
       
   805     // if write protected or system file, remove protections and retry
       
   806     else if (iEngine->Settings().iIgnoreProtectionsAtts && (err == KErrAccessDenied || err == KErrNotFound))
       
   807         {
       
   808         // remove protections  and try again
       
   809         if (FileOpAttribs(aName, 0, KEntryAttReadOnly|KEntryAttSystem|KEntryAttHidden, 0, 0) == KErrNone)
       
   810             {
       
   811             err = DoFileOpDeleteFile(aName, aSwitch);
       
   812             }
       
   813         }
       
   814 
       
   815     // remember the "lowest" error
       
   816     if (err < iOperationError)
       
   817         iOperationError = err;
       
   818 
       
   819     LOGSTRING3("FileBrowser: FileOpDeleteFile %S, err=%d", &aName, err);
       
   820         
       
   821     return err; 
       
   822     }
       
   823         
       
   824 // --------------------------------------------------------------------------------------------
       
   825 
       
   826 TInt CFileBrowserFileOps::DoFileOpDeleteFile(const TDesC& aName, TUint aSwitch) 
       
   827     {
       
   828     #ifndef FILEBROWSER_LITE
       
   829     if (iEngine->Settings().iBypassPlatformSecurity)
       
   830         {
       
   831         if (!iFileOpClient)
       
   832             iFileOpClient = CFBFileOpClient::NewL();   
       
   833 
       
   834         return iFileOpClient->Delete(aName, aSwitch);
       
   835         }
       
   836     else
       
   837         {
       
   838     #endif
       
   839 //        CAsyncWaiter* waiter = CAsyncWaiter::NewLC();
       
   840 //        TInt result = iFileMan->Delete( aName, aSwitch, waiter->iStatus );
       
   841 //        waiter->StartAndWait();
       
   842 //        if ( iFileManObserverResult == MFileManObserver::ECancel ) result = KErrCancel;
       
   843 //        if ( !result ) result = waiter->Result();
       
   844 //        CleanupStack::PopAndDestroy( waiter );
       
   845 		TInt result = iFileMan->Delete( aName, aSwitch );
       
   846         return result;
       
   847     #ifndef FILEBROWSER_LITE
       
   848         }    
       
   849     #endif
       
   850     }
       
   851 
       
   852 // --------------------------------------------------------------------------------------------
       
   853 
       
   854 TInt CFileBrowserFileOps::FileOpRmDir(const TDesC& aName, TUint aSwitch) 
       
   855     {
       
   856     TInt err = DoFileOpRmDir(aName, aSwitch);
       
   857 
       
   858     // if write protected or system directory, remove protections and retry
       
   859     if (iEngine->Settings().iIgnoreProtectionsAtts && (err == KErrAccessDenied || err == KErrInUse))
       
   860         {
       
   861         // remove protections and try again
       
   862         if (FileOpAttribs(aName.Left(aName.Length()-1), 0, KEntryAttReadOnly|KEntryAttSystem|KEntryAttHidden, 0, 0) == KErrNone)
       
   863             {
       
   864             err = DoFileOpRmDir(aName, aSwitch);
       
   865             }
       
   866         }
       
   867 
       
   868     // remember the "lowest" error
       
   869     if (err < iOperationError)
       
   870         iOperationError = err;
       
   871 
       
   872     LOGSTRING3("FileBrowser: FileOpRmDir %S, err=%d", &aName, err);
       
   873         
       
   874     return err; 
       
   875     }
       
   876         
       
   877 // --------------------------------------------------------------------------------------------
       
   878 
       
   879 TInt CFileBrowserFileOps::DoFileOpRmDir(const TDesC& aDirName, TUint aSwitch)
       
   880     {
       
   881     #ifndef FILEBROWSER_LITE
       
   882     if (iEngine->Settings().iBypassPlatformSecurity)
       
   883         {
       
   884         if (!iFileOpClient)
       
   885             iFileOpClient = CFBFileOpClient::NewL();   
       
   886 
       
   887         return iFileOpClient->RmDir(aDirName, aSwitch);
       
   888         }
       
   889     else
       
   890         {
       
   891     #endif
       
   892         if ( aSwitch & CFileMan::ERecurse )
       
   893             {
       
   894 //            CAsyncWaiter* waiter = CAsyncWaiter::NewLC();
       
   895 //            TInt result = iFileMan->RmDir( aDirName, waiter->iStatus );
       
   896 //            waiter->StartAndWait();
       
   897 //            if ( iFileManObserverResult == MFileManObserver::ECancel ) result = KErrCancel;
       
   898 //            if ( !result ) result = waiter->Result();
       
   899 //            CleanupStack::PopAndDestroy( waiter);
       
   900             TInt result = iFileMan->RmDir( aDirName );
       
   901             return result;
       
   902             }
       
   903         else
       
   904             return iFs.RmDir(aDirName);    
       
   905     #ifndef FILEBROWSER_LITE
       
   906         }    
       
   907     #endif
       
   908    }
       
   909 
       
   910 // --------------------------------------------------------------------------------------------
       
   911 // --------------------------------------------------------------------------------------------
       
   912 
       
   913 TInt CFileBrowserFileOps::MkDirAll(const TDesC& aPath, TInt aSetAtts, TBool aQuickOperation) 
       
   914     {
       
   915     iFileManObserverResult = MFileManObserver::EContinue;
       
   916     if (aQuickOperation)
       
   917         return DoFileOpMkDirAll(aPath);
       
   918     else
       
   919         return FileOpMkDirAll(aPath, aSetAtts);
       
   920     }
       
   921 
       
   922 // --------------------------------------------------------------------------------------------
       
   923 
       
   924 TInt CFileBrowserFileOps::FileOpMkDirAll(const TDesC& aPath, TInt aSetAtts) 
       
   925     {
       
   926     TInt err = DoFileOpMkDirAll(aPath);
       
   927 
       
   928     // if the directory already exists, it is not an error    
       
   929     if (err == KErrAlreadyExists)
       
   930         err = KErrNone;
       
   931     
       
   932     
       
   933     // set attributes for directory just created
       
   934     if (aSetAtts > 0 && err == KErrNone && aPath.Length() > 3)
       
   935         {
       
   936         // a path has a trailing backslash so it needs to be removed before the call
       
   937         err = FileOpAttribs(aPath.Left(aPath.Length()-1), aSetAtts, 0, 0, 0);
       
   938         }
       
   939     
       
   940 
       
   941     // remember the "lowest" error
       
   942     if (err < iOperationError)
       
   943         iOperationError = err;
       
   944     
       
   945     LOGSTRING3("FileBrowser: FileOpMkDirAll %S, err=%d", &aPath, err);
       
   946     
       
   947     return err;
       
   948     }
       
   949         
       
   950 // --------------------------------------------------------------------------------------------
       
   951 
       
   952 TInt CFileBrowserFileOps::DoFileOpMkDirAll(const TDesC& aPath) 
       
   953     {
       
   954     #ifndef FILEBROWSER_LITE
       
   955     if (iEngine->Settings().iBypassPlatformSecurity)
       
   956         {
       
   957         if (!iFileOpClient)
       
   958             iFileOpClient = CFBFileOpClient::NewL();   
       
   959 
       
   960         return iFileOpClient->MkDirAll(aPath);
       
   961         }
       
   962     else
       
   963         {
       
   964     #endif
       
   965         return iFs.MkDirAll(aPath);
       
   966     #ifndef FILEBROWSER_LITE
       
   967         }    
       
   968     #endif
       
   969     }
       
   970 
       
   971 // --------------------------------------------------------------------------------------------
       
   972 // --------------------------------------------------------------------------------------------
       
   973 
       
   974 TInt CFileBrowserFileOps::CreateEmptyFile(const TDesC& aName) 
       
   975     {
       
   976     return DoFileOpCreateEmptyFile(aName);
       
   977     }
       
   978 
       
   979 // --------------------------------------------------------------------------------------------
       
   980 
       
   981 TInt CFileBrowserFileOps::FileOpCreateEmptyFile(const TDesC& aName) 
       
   982     {
       
   983     TInt err = DoFileOpCreateEmptyFile(aName);
       
   984 
       
   985     // remember the "lowest" error
       
   986     if (err < iOperationError)
       
   987         iOperationError = err;
       
   988     
       
   989     LOGSTRING3("FileBrowser: FileOpCreateEmptyFile %S, err=%d", &aName, err);
       
   990     
       
   991     return err;
       
   992     }
       
   993 
       
   994 // --------------------------------------------------------------------------------------------
       
   995     
       
   996 TInt CFileBrowserFileOps::DoFileOpCreateEmptyFile(const TDesC& aName) 
       
   997     {
       
   998     #ifndef FILEBROWSER_LITE
       
   999     if (iEngine->Settings().iBypassPlatformSecurity)
       
  1000         {
       
  1001         if (!iFileOpClient)
       
  1002             iFileOpClient = CFBFileOpClient::NewL();   
       
  1003 
       
  1004         return iFileOpClient->CreateEmptyFile(aName);
       
  1005         }
       
  1006     else
       
  1007         {
       
  1008     #endif
       
  1009         TInt err(KErrNone);
       
  1010             
       
  1011         RFile newFile;
       
  1012         err = newFile.Create(iFs, aName, EFileShareExclusive);
       
  1013         if (err == KErrNone)
       
  1014             err = newFile.Flush(); 
       
  1015         newFile.Close();
       
  1016         
       
  1017         return err; 
       
  1018     #ifndef FILEBROWSER_LITE
       
  1019         }    
       
  1020     #endif
       
  1021     }
       
  1022 
       
  1023 // --------------------------------------------------------------------------------------------
       
  1024 
       
  1025 TInt CFileBrowserFileOps::DriveSnapShot(TChar aSourceDriveLetter, TChar aTargetDriveLetter)
       
  1026     {
       
  1027     TInt err(KErrNone);
       
  1028     iFileManObserverResult = MFileManObserver::EContinue;
       
  1029     
       
  1030     // remember old settings and force them to be true for this operation
       
  1031     TBool currentRemoveFileLocksValue = iEngine->Settings().iRemoveFileLocks;
       
  1032     TBool currentIgnoreProtectionAttsValue = iEngine->Settings().iIgnoreProtectionsAtts;
       
  1033     TBool currentRemoveROMWriteProrection = iEngine->Settings().iRemoveROMWriteProrection;
       
  1034     iEngine->Settings().iRemoveFileLocks = ETrue;
       
  1035     iEngine->Settings().iIgnoreProtectionsAtts = ETrue;
       
  1036     iEngine->Settings().iRemoveROMWriteProrection = ETrue;
       
  1037 
       
  1038     
       
  1039     TFileName sourceDir;
       
  1040     sourceDir.Append(aSourceDriveLetter);
       
  1041     sourceDir.Append(_L(":"));
       
  1042 
       
  1043     _LIT(KTargetDir, "%c:\\SnapShot_%c_drive");
       
  1044     TFileName targetDir;
       
  1045     targetDir.Format(KTargetDir, TUint(aTargetDriveLetter), TUint(aSourceDriveLetter));            
       
  1046 
       
  1047     // remove any existing content, first get TEntry
       
  1048     TEntry entry;
       
  1049     err = iFs.Entry(targetDir, entry);
       
  1050 
       
  1051     // entry directory exists, delete it
       
  1052     if (err == KErrNone && entry.IsDir())
       
  1053         {
       
  1054         TFileName targetRoot;
       
  1055         targetRoot.Append(aTargetDriveLetter);
       
  1056         targetRoot.Append(_L(":\\"));
       
  1057 
       
  1058         TFileEntry targetEntry;
       
  1059         targetEntry.iPath = targetRoot;
       
  1060         targetEntry.iEntry = entry;
       
  1061 
       
  1062         err = Delete(targetEntry, CFileMan::ERecurse);                
       
  1063         }
       
  1064         
       
  1065     // do not care if removing succeeded or not, just continue with copying    
       
  1066     TEntry fakeEntry;
       
  1067     fakeEntry.iAtt |= KEntryAttDir;
       
  1068 
       
  1069     TFileEntry sourceEntry;
       
  1070     sourceEntry.iPath = sourceDir;
       
  1071     sourceEntry.iEntry = fakeEntry;
       
  1072 
       
  1073     err = Copy(sourceEntry, targetDir, CFileMan::ERecurse|CFileMan::EOverWrite);                
       
  1074 
       
  1075 
       
  1076     // restore back settings
       
  1077     iEngine->Settings().iRemoveFileLocks = currentRemoveFileLocksValue;
       
  1078     iEngine->Settings().iIgnoreProtectionsAtts = currentIgnoreProtectionAttsValue;    
       
  1079     iEngine->Settings().iRemoveROMWriteProrection = currentRemoveROMWriteProrection;    
       
  1080 
       
  1081 
       
  1082     return iOperationError;
       
  1083     }
       
  1084 
       
  1085 // --------------------------------------------------------------------------------------------
       
  1086 
       
  1087 TInt CFileBrowserFileOps::EraseMBR(TUint aDriveNumber) 
       
  1088     {
       
  1089     #ifndef FILEBROWSER_LITE
       
  1090 
       
  1091         if (!iFileOpClient)
       
  1092             iFileOpClient = CFBFileOpClient::NewL();
       
  1093         
       
  1094         return iFileOpClient->EraseMBR(aDriveNumber);
       
  1095 
       
  1096     #else
       
  1097         return KErrNotSupported;
       
  1098     #endif
       
  1099     }
       
  1100 
       
  1101 // --------------------------------------------------------------------------------------------
       
  1102 
       
  1103 TInt CFileBrowserFileOps::PartitionDrive(TUint aDriveNumber, TUint aNumberOfPartitions) 
       
  1104     {
       
  1105     #ifndef FILEBROWSER_LITE
       
  1106 
       
  1107         if (!iFileOpClient)
       
  1108             iFileOpClient = CFBFileOpClient::NewL();
       
  1109         
       
  1110         return iFileOpClient->PartitionDrive(aDriveNumber, aNumberOfPartitions);
       
  1111 
       
  1112     #else
       
  1113         return KErrNotSupported
       
  1114     #endif
       
  1115     }
       
  1116 
       
  1117 // --------------------------------------------------------------------------------------------
       
  1118 
       
  1119 MFileManObserver::TControl CFileBrowserFileOps::NotifyFileManStarted()
       
  1120     {
       
  1121     return iFileManObserverResult;
       
  1122     }
       
  1123 
       
  1124 // --------------------------------------------------------------------------------------------
       
  1125 
       
  1126 MFileManObserver::TControl CFileBrowserFileOps::NotifyFileManOperation()
       
  1127     {
       
  1128     return iFileManObserverResult;
       
  1129     }
       
  1130 // --------------------------------------------------------------------------------------------
       
  1131 
       
  1132 MFileManObserver::TControl CFileBrowserFileOps::NotifyFileManEnded()
       
  1133     {
       
  1134     return iFileManObserverResult;
       
  1135     }
       
  1136 
       
  1137 // --------------------------------------------------------------------------------------------
       
  1138 
       
  1139 void CFileBrowserFileOps::CancelOp()
       
  1140     {
       
  1141 #ifndef FILEBROWSER_LITE
       
  1142 
       
  1143     if ( iEngine->Settings().iBypassPlatformSecurity )
       
  1144         {
       
  1145         if ( !iFileOpClient )
       
  1146             iFileOpClient = CFBFileOpClient::NewL();
       
  1147         
       
  1148         iFileOpClient->CancelOp();
       
  1149         }
       
  1150 
       
  1151     // we need this information even when using iFileOpClient
       
  1152     // to be able to not execute aggregate operations 
       
  1153     iFileManObserverResult = MFileManObserver::ECancel;
       
  1154     
       
  1155 #else
       
  1156     iFileManObserverResult = MFileManObserver::ECancel;
       
  1157 #endif
       
  1158     }
       
  1159 
       
  1160 // End of File