filebrowser/src/FBFileOps.cpp
changeset 0 d6fe6244b863
child 21 aefcba28a3e0
equal deleted inserted replaced
-1:000000000000 0:d6fe6244b863
       
     1 /*
       
     2 * Copyright (c) 2009 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 
       
    19 // INCLUDE FILES
       
    20 #include "FBFileOps.h"
       
    21 #include "FBFileUtils.h"
       
    22 #include "FBModel.h"
       
    23 #include "FBTraces.h"
       
    24 
       
    25 #ifndef FILEBROWSER_LITE
       
    26   #include "FBFileOpClient.h"
       
    27 #endif  
       
    28 
       
    29 #include <f32file.h>
       
    30 #include <bautils.h>
       
    31 #include <eikenv.h>
       
    32 #include <babackup.h>
       
    33 
       
    34 const TInt KSecureBackupStartDelay = 750000;
       
    35 const TInt KSecureBackupLoopDelay = 100000;
       
    36 const TInt KSecureBackupEndDelay = 200000;
       
    37 const TInt KMaxFileLockAttempts = 3;
       
    38 
       
    39 
       
    40 // ================= MEMBER FUNCTIONS =======================
       
    41 
       
    42 CFileBrowserFileOps* CFileBrowserFileOps::NewL(CFileBrowserModel* aModel)
       
    43 	{
       
    44 	CFileBrowserFileOps* self = new(ELeave) CFileBrowserFileOps(aModel);
       
    45 	CleanupStack::PushL(self);
       
    46 	self->ConstructL();
       
    47 	CleanupStack::Pop();
       
    48 	return self;
       
    49 	}
       
    50 
       
    51 // --------------------------------------------------------------------------------------------
       
    52 
       
    53 CFileBrowserFileOps::CFileBrowserFileOps(CFileBrowserModel* aModel) : iModel(aModel)
       
    54 	{
       
    55 	}
       
    56 
       
    57 // --------------------------------------------------------------------------------------------
       
    58 
       
    59 void CFileBrowserFileOps::ConstructL()
       
    60 	{
       
    61 	iRecursiveState = EFileOpInvalid;
       
    62 	iSecureBackUpActive = EFalse;
       
    63 	iFileCommandActivatedSecureBackup = EFalse;
       
    64 	
       
    65     User::LeaveIfError(iFs.Connect());
       
    66     iFileMan = CFileMan::NewL( iFs, this );
       
    67 	}
       
    68 
       
    69 // --------------------------------------------------------------------------------------------
       
    70 
       
    71 CFileBrowserFileOps::~CFileBrowserFileOps()
       
    72 	{
       
    73 	if (iSBEClient)
       
    74 	    delete iSBEClient;
       
    75 	
       
    76     #ifndef FILEBROWSER_LITE	
       
    77         if (iFileOpClient)
       
    78             delete iFileOpClient;
       
    79     #endif	
       
    80 
       
    81     delete iFileMan;
       
    82     iFs.Close();    
       
    83     }
       
    84 
       
    85 // --------------------------------------------------------------------------------------------
       
    86 
       
    87 TInt CFileBrowserFileOps::ActivateSecureBackUpViaFileOp()
       
    88     {
       
    89     iFileManObserverResult = MFileManObserver::EContinue;
       
    90     // if already activate by a file command, return ok
       
    91     if (iFileCommandActivatedSecureBackup)
       
    92         return KErrNone;
       
    93     else
       
    94         {
       
    95         // if secure backup is already active, disable it first, because it may in wrong state
       
    96         if (iSecureBackUpActive)
       
    97             DeActivateSecureBackUp();
       
    98         }
       
    99     
       
   100     // try to activate full secure backup
       
   101     TInt err = ActivateSecureBackUp(conn::EBURBackupFull, conn::EBackupBase);
       
   102     
       
   103     if (err == KErrNone)
       
   104         iFileCommandActivatedSecureBackup = ETrue;
       
   105     
       
   106     return err;
       
   107     }
       
   108 
       
   109 // --------------------------------------------------------------------------------------------
       
   110 
       
   111 TInt CFileBrowserFileOps::DeActivateSecureBackUpViaFileOp()
       
   112     {
       
   113     TInt err(KErrGeneral);
       
   114     iFileManObserverResult = MFileManObserver::EContinue;
       
   115     
       
   116     // if activate by a file command, try to reactivate it
       
   117     if (iFileCommandActivatedSecureBackup)
       
   118         {
       
   119         err = DeActivateSecureBackUp();
       
   120         
       
   121         // even if it fails, forget the state
       
   122         iFileCommandActivatedSecureBackup = EFalse;
       
   123         }
       
   124 
       
   125     return err;
       
   126     }    
       
   127     
       
   128 // --------------------------------------------------------------------------------------------
       
   129 
       
   130 TInt CFileBrowserFileOps::ActivateSecureBackUp(conn::TBURPartType aPartType, conn::TBackupIncType aBackupIncType)
       
   131     {
       
   132     iFileManObserverResult = MFileManObserver::EContinue;
       
   133     // check for invalid parameters
       
   134     if (aPartType == conn::EBURNormal || aBackupIncType == conn::ENoBackup)
       
   135         User::Panic(_L("Inv.Usage.SE"), 532);
       
   136     
       
   137     TInt err(KErrNone);
       
   138     
       
   139     if (!iSBEClient)
       
   140         {
       
   141         TRAP(err, iSBEClient = conn::CSBEClient::NewL());
       
   142         if (err != KErrNone)
       
   143             return err;
       
   144         }
       
   145     
       
   146     TDriveList driveList;
       
   147     err = iFs.DriveList(driveList);
       
   148     
       
   149     if (err == KErrNone)
       
   150         {
       
   151         // make sure that the application has a system status to prevent getting shut down events
       
   152         iModel->EikonEnv()->SetSystem(ETrue);
       
   153     
       
   154         // activating secure back up removes locks from files which respect this fuctionality                
       
   155         TRAP(err, iSBEClient->SetBURModeL(driveList, aPartType, aBackupIncType));
       
   156         
       
   157         if (err == KErrNone)
       
   158             {
       
   159             iSecureBackUpActive = ETrue;
       
   160             User::After(KSecureBackupStartDelay); // a short delay to wait to activate
       
   161             }
       
   162         }
       
   163 
       
   164     return err;
       
   165     }
       
   166 
       
   167 // --------------------------------------------------------------------------------------------
       
   168 
       
   169 TInt CFileBrowserFileOps::DeActivateSecureBackUp()
       
   170     {
       
   171     TInt err(KErrNone);
       
   172     
       
   173     if (!iSBEClient)
       
   174         {
       
   175         TRAP(err, iSBEClient = conn::CSBEClient::NewL());
       
   176         if (err != KErrNone)
       
   177             return err;
       
   178         
       
   179         // make sure that the application has a system status
       
   180         iModel->EikonEnv()->SetSystem(ETrue);
       
   181         }
       
   182         
       
   183     TDriveList driveList;
       
   184     err = iFs.DriveList(driveList);
       
   185     
       
   186     if (err == KErrNone)
       
   187         {
       
   188         // deactivate secure backup
       
   189         TRAP(err, iSBEClient->SetBURModeL(driveList, conn::EBURNormal, conn::ENoBackup));
       
   190         
       
   191         User::After(KSecureBackupEndDelay); // a short delay to wait to deactivate
       
   192 
       
   193         // system status not needed anymore
       
   194         iModel->EikonEnv()->SetSystem(EFalse);
       
   195         }
       
   196     
       
   197     iSecureBackUpActive = EFalse;
       
   198 
       
   199     return err;
       
   200     }
       
   201         
       
   202 // --------------------------------------------------------------------------------------------
       
   203 
       
   204 TInt CFileBrowserFileOps::DoFindEntries(const TDesC& aFileName, const TDesC& aPath)
       
   205     {
       
   206     TFindFile fileFinder(iFs);
       
   207     CDir* dir;
       
   208     TInt err = fileFinder.FindWildByPath(aFileName, &aPath, dir);
       
   209 
       
   210     while (err == KErrNone && iFileManObserverResult != MFileManObserver::ECancel)
       
   211         {
       
   212         for (TInt i=0; i<dir->Count(); i++)
       
   213             {
       
   214             TEntry entry = (*dir)[i];
       
   215 
       
   216             if (entry.iName.Length() && aPath.Length())
       
   217                 {
       
   218                 // parse the entry
       
   219                 TParse parsedName;
       
   220                 parsedName.Set(entry.iName, &fileFinder.File(), NULL);
       
   221                 
       
   222                 if (parsedName.Drive().Length() && aPath.Length() && parsedName.Drive()[0] == aPath[0])
       
   223                     {
       
   224                     // get full source path
       
   225                     TFileName fullSourcePath = parsedName.FullName();
       
   226                     if (entry.IsDir())
       
   227                         fullSourcePath.Append(_L("\\"));
       
   228                     
       
   229                     // call the file operation command
       
   230                     switch(iRecursiveState)
       
   231                         {
       
   232                         case EFileOpAttribs:
       
   233                             {
       
   234                             // the same attribs command can be given for both directories and files
       
   235                             FileOpAttribs(fullSourcePath, iUint1, iUint2, iTime1, iUint3);
       
   236                             }
       
   237                             break;
       
   238 
       
   239                         case EFileOpCopy:
       
   240                             {
       
   241                             // calculate length of new entries added to the original source path
       
   242                             TUint newEntriesLength = fullSourcePath.Length() - iBuf1.Length();
       
   243                             
       
   244                             // get pointer description to the rightmost data
       
   245                             TPtr16 newEntriesPtr = fullSourcePath.RightTPtr(newEntriesLength);
       
   246                             
       
   247                             // generate target path
       
   248                             TFileName fullTargetPath = iBuf2;
       
   249                             fullTargetPath.Append(newEntriesPtr);
       
   250                             
       
   251                             if (entry.IsDir())
       
   252                                 {
       
   253                                 // if it is a directory entry, just create it based on the entry's attributes
       
   254                                 FileOpMkDirAll(fullTargetPath, entry.iAtt);
       
   255                                 }
       
   256                             else
       
   257                                 {
       
   258                                 // otherwise copy the file
       
   259                                 FileOpCopy(fullSourcePath, fullTargetPath, iUint1);
       
   260                                 }
       
   261                             }
       
   262                             break;
       
   263 
       
   264                         case EFileOpDelete:
       
   265                             {
       
   266                             if (entry.IsDir())
       
   267                                 {
       
   268                                 // for directories call rmdir    
       
   269                                 FileOpRmDir(fullSourcePath, iUint1);
       
   270                                 }
       
   271                             else
       
   272                                 {
       
   273                                 // for files call the normal file deletion operation    
       
   274                                 FileOpDeleteFile(fullSourcePath, iUint1);
       
   275                                 }
       
   276                             }
       
   277                             break;
       
   278                                                     
       
   279                         default:
       
   280                             User::Panic (_L("FileOpRecurs"), 775);
       
   281                             break;
       
   282                         }                        
       
   283                     }
       
   284                 }
       
   285                 if ( iFileManObserverResult == MFileManObserver::ECancel ) break;
       
   286             }
       
   287 
       
   288         delete dir;
       
   289         dir = NULL;
       
   290         if ( iFileManObserverResult != MFileManObserver::ECancel )
       
   291             {
       
   292             err = fileFinder.FindWild(dir);
       
   293             }
       
   294         }
       
   295 
       
   296     return err;
       
   297     }
       
   298 
       
   299 // --------------------------------------------------------------------------------------------
       
   300 
       
   301 TInt CFileBrowserFileOps::DoFindEntriesRecursiveL(const TDesC& aFileName, const TDesC& aPath)
       
   302 	{
       
   303     TInt err(KErrNone);
       
   304 
       
   305     // it is logical to scan upwards when deleting and changing attributes
       
   306     CDirScan::TScanDirection scanDirection = CDirScan::EScanUpTree;
       
   307     
       
   308     // when copying files, it is more logical to move downwards
       
   309     if (iRecursiveState == EFileOpCopy)
       
   310         scanDirection = CDirScan::EScanDownTree;
       
   311     
       
   312     
       
   313     CDirScan* scan = CDirScan::NewLC(iFs);
       
   314     scan->SetScanDataL(aPath, KEntryAttDir|KEntryAttMatchMask, ESortByName | EAscending | EDirsFirst, scanDirection);
       
   315     CDir* dir = NULL;
       
   316 
       
   317     for(;;)
       
   318         {
       
   319         TRAP(err, scan->NextL(dir));
       
   320         if (!dir  || (err != KErrNone))
       
   321             break;
       
   322 
       
   323         for (TInt i=0; i<dir->Count(); i++)
       
   324             {
       
   325             TEntry entry = (*dir)[i];
       
   326             
       
   327             if (entry.IsDir())
       
   328                 {
       
   329                 TFileName path(scan->FullPath());
       
   330                 
       
   331                 if (path.Length())
       
   332                     {
       
   333                     path.Append(entry.iName);
       
   334                     path.Append(_L("\\"));
       
   335                     DoFindEntries(aFileName, path);
       
   336                     }
       
   337                 }
       
   338             if ( iFileManObserverResult == MFileManObserver::ECancel )
       
   339                 {
       
   340                 break;
       
   341                 }
       
   342             }
       
   343         delete(dir);
       
   344         if ( iFileManObserverResult == MFileManObserver::ECancel )
       
   345             {
       
   346             break;
       
   347             }
       
   348         }
       
   349 
       
   350     CleanupStack::PopAndDestroy(scan);
       
   351     return err;
       
   352     }
       
   353     
       
   354 // --------------------------------------------------------------------------------------------
       
   355 // --------------------------------------------------------------------------------------------
       
   356 
       
   357 TInt CFileBrowserFileOps::Copy(const TFileEntry& aSourceEntry, const TDesC& aTargetFullName, TUint aSwitch, TBool aDeleteSource) 
       
   358     {
       
   359     iOperationError = KErrNone;
       
   360     iFileManObserverResult = MFileManObserver::EContinue;
       
   361     
       
   362     TFileName sourcePath = aSourceEntry.iPath;
       
   363     sourcePath.Append(aSourceEntry.iEntry.iName);
       
   364     
       
   365     TInt err(KErrNone);
       
   366 
       
   367     if (aSourceEntry.iEntry.IsDir() && (aSwitch & CFileMan::ERecurse))
       
   368         {
       
   369         // find all files recursively and run the operation for them
       
   370         iRecursiveState = EFileOpCopy;
       
   371         sourcePath.Append(_L("\\"));
       
   372         
       
   373         TFileName targetPath = aTargetFullName;
       
   374         targetPath.Append(_L("\\"));
       
   375         
       
   376         // remove the recursion flag because we will implement our own recursion
       
   377         TUint newSwitch(aSwitch);
       
   378         newSwitch &= ~CFileMan::ERecurse;
       
   379     
       
   380         iBuf1.Copy(sourcePath);
       
   381         iBuf2.Copy(targetPath);
       
   382         iUint1 = newSwitch;
       
   383         
       
   384         // create initial directory - if it does not succeed, do not even try to continue
       
   385         err = FileOpMkDirAll(targetPath, aSourceEntry.iEntry.iAtt);
       
   386         
       
   387         if (iOperationError == KErrNone)
       
   388             {
       
   389             TRAP(err, DoFindEntries(_L("*"), sourcePath));              // entries under current directory entry
       
   390             if ( iFileManObserverResult != MFileManObserver::ECancel )
       
   391                 {
       
   392                 TRAP(err, DoFindEntriesRecursiveL(_L("*"), sourcePath));    // recursively under directories of current directory entry
       
   393                 }
       
   394             }
       
   395         }
       
   396 
       
   397     else if (aSourceEntry.iEntry.IsDir())
       
   398         {
       
   399         TFileName targetPath = aTargetFullName;
       
   400         targetPath.Append(_L("\\"));
       
   401         
       
   402         // just create a directory based on the file attributes of the source directory
       
   403         err = FileOpMkDirAll(targetPath, aSourceEntry.iEntry.iAtt);
       
   404         }
       
   405         
       
   406     else
       
   407         {
       
   408         // remove a recursion flag if present (this should never happen, but some extra error checking)
       
   409         if (aSwitch & CFileMan::ERecurse)
       
   410             aSwitch &= ~CFileMan::ERecurse;    
       
   411             
       
   412         // do the operation for a file entry
       
   413         err = FileOpCopy(sourcePath, aTargetFullName, aSwitch);
       
   414         }
       
   415     
       
   416         
       
   417     // delete source if needed and copy succeeded without any errors (== move operation)
       
   418     if ( aDeleteSource && iOperationError == KErrNone &&
       
   419          iFileManObserverResult != MFileManObserver::ECancel )
       
   420         {
       
   421         err = Delete(aSourceEntry, aSwitch);    
       
   422         }
       
   423     
       
   424     if ( !iOperationError && iFileManObserverResult == MFileManObserver::ECancel )
       
   425         {
       
   426         iOperationError = KErrCancel;
       
   427         }
       
   428         
       
   429     return iOperationError; 
       
   430     }
       
   431 
       
   432 // --------------------------------------------------------------------------------------------
       
   433 
       
   434 TInt CFileBrowserFileOps::FileOpCopy(const TDesC& aSourceFullName, const TDesC& aTargetFullName, TUint aSwitch) 
       
   435     {
       
   436     TInt err = DoFileOpCopy(aSourceFullName, aTargetFullName, aSwitch);
       
   437 
       
   438     // if locked, unlock the file and retry
       
   439     if (iModel->Settings().iRemoveFileLocks && err == KErrInUse)
       
   440         {
       
   441         // try to remove the file lock by activating secure backup mode
       
   442         if (ActivateSecureBackUpViaFileOp() == KErrNone)
       
   443             {
       
   444             // try the operation several times
       
   445             for (TInt i=0; i<KMaxFileLockAttempts; i++)
       
   446                 {
       
   447                 err = DoFileOpCopy(aSourceFullName, aTargetFullName, aSwitch);
       
   448                 
       
   449                 if (err != KErrInUse)
       
   450                     break;
       
   451                 else
       
   452                     User::After(KSecureBackupLoopDelay);
       
   453                 }
       
   454             }
       
   455         }
       
   456 
       
   457     // if access denied, then try to remove the target path and try again
       
   458     if (iModel->Settings().iIgnoreProtectionsAtts && err == KErrAccessDenied && BaflUtils::FileExists(iFs, aTargetFullName))
       
   459         {
       
   460         if (FileOpDeleteFile(aTargetFullName, 0) == KErrNone)
       
   461             {
       
   462             err = DoFileOpCopy(aSourceFullName, aTargetFullName, aSwitch);
       
   463             }
       
   464         }
       
   465 
       
   466     // if the file already exists, it is not an error    
       
   467     if (err == KErrAlreadyExists)
       
   468         err = KErrNone;
       
   469     
       
   470     
       
   471     // if copying from a ROM drive, remove the writing protection flag
       
   472     if (iModel->Settings().iRemoveROMWriteProrection && err == KErrNone && aSourceFullName.Length() > 3 && (aSourceFullName[0]=='z' || aSourceFullName[0]=='Z'))
       
   473         {
       
   474         FileOpAttribs(aTargetFullName, 0, KEntryAttReadOnly, 0, 0);
       
   475         }
       
   476     
       
   477     
       
   478     // remember the "lowest" error
       
   479     if (err < iOperationError)
       
   480         iOperationError = err;
       
   481 
       
   482     LOGSTRING4("FileBrowser: FileOpCopy %S -> %S, err=%d", &aSourceFullName, &aTargetFullName, err);
       
   483         
       
   484     return err; 
       
   485     }
       
   486             
       
   487 // --------------------------------------------------------------------------------------------
       
   488 
       
   489 TInt CFileBrowserFileOps::DoFileOpCopy(const TDesC& aSourceFullName, const TDesC& aTargetFullName, TUint aSwitch) 
       
   490     {
       
   491     #ifndef FILEBROWSER_LITE
       
   492     if (iModel->Settings().iBypassPlatformSecurity)
       
   493         {
       
   494         if (!iFileOpClient)
       
   495             iFileOpClient = CFBFileOpClient::NewL();   
       
   496             
       
   497         return iFileOpClient->Copy(aSourceFullName, aTargetFullName, aSwitch);
       
   498         }
       
   499     else
       
   500         {
       
   501     #endif
       
   502         CAsyncWaiter* waiter = CAsyncWaiter::NewLC();
       
   503         TInt result = iFileMan->Copy( aSourceFullName, aTargetFullName, aSwitch, waiter->iStatus );
       
   504         waiter->StartAndWait();
       
   505         if ( !result ) result = waiter->Result();
       
   506         CleanupStack::PopAndDestroy( waiter );
       
   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 (iModel->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 (iModel->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 (iModel->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 (iModel->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 (iModel->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 (iModel->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 (iModel->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 (iModel->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 (iModel->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         return result;
       
   846     #ifndef FILEBROWSER_LITE
       
   847         }    
       
   848     #endif
       
   849     }
       
   850 
       
   851 // --------------------------------------------------------------------------------------------
       
   852 
       
   853 TInt CFileBrowserFileOps::FileOpRmDir(const TDesC& aName, TUint aSwitch) 
       
   854     {
       
   855     TInt err = DoFileOpRmDir(aName, aSwitch);
       
   856 
       
   857     // if write protected or system directory, remove protections and retry
       
   858     if (iModel->Settings().iIgnoreProtectionsAtts && (err == KErrAccessDenied || err == KErrInUse))
       
   859         {
       
   860         // remove protections and try again
       
   861         if (FileOpAttribs(aName.Left(aName.Length()-1), 0, KEntryAttReadOnly|KEntryAttSystem|KEntryAttHidden, 0, 0) == KErrNone)
       
   862             {
       
   863             err = DoFileOpRmDir(aName, aSwitch);
       
   864             }
       
   865         }
       
   866 
       
   867     // remember the "lowest" error
       
   868     if (err < iOperationError)
       
   869         iOperationError = err;
       
   870 
       
   871     LOGSTRING3("FileBrowser: FileOpRmDir %S, err=%d", &aName, err);
       
   872         
       
   873     return err; 
       
   874     }
       
   875         
       
   876 // --------------------------------------------------------------------------------------------
       
   877 
       
   878 TInt CFileBrowserFileOps::DoFileOpRmDir(const TDesC& aDirName, TUint aSwitch)
       
   879     {
       
   880     #ifndef FILEBROWSER_LITE
       
   881     if (iModel->Settings().iBypassPlatformSecurity)
       
   882         {
       
   883         if (!iFileOpClient)
       
   884             iFileOpClient = CFBFileOpClient::NewL();   
       
   885 
       
   886         return iFileOpClient->RmDir(aDirName, aSwitch);
       
   887         }
       
   888     else
       
   889         {
       
   890     #endif
       
   891         if ( aSwitch & CFileMan::ERecurse )
       
   892             {
       
   893             CAsyncWaiter* waiter = CAsyncWaiter::NewLC();
       
   894             TInt result = iFileMan->RmDir( aDirName, waiter->iStatus );
       
   895             waiter->StartAndWait();
       
   896             if ( iFileManObserverResult == MFileManObserver::ECancel ) result = KErrCancel;
       
   897             if ( !result ) result = waiter->Result();
       
   898             CleanupStack::PopAndDestroy( waiter);
       
   899             return result;
       
   900             }
       
   901         else
       
   902             return iFs.RmDir(aDirName);    
       
   903     #ifndef FILEBROWSER_LITE
       
   904         }    
       
   905     #endif
       
   906    }
       
   907 
       
   908 // --------------------------------------------------------------------------------------------
       
   909 // --------------------------------------------------------------------------------------------
       
   910 
       
   911 TInt CFileBrowserFileOps::MkDirAll(const TDesC& aPath, TInt aSetAtts, TBool aQuickOperation) 
       
   912     {
       
   913     iFileManObserverResult = MFileManObserver::EContinue;
       
   914     if (aQuickOperation)
       
   915         return DoFileOpMkDirAll(aPath);
       
   916     else
       
   917         return FileOpMkDirAll(aPath, aSetAtts);
       
   918     }
       
   919 
       
   920 // --------------------------------------------------------------------------------------------
       
   921 
       
   922 TInt CFileBrowserFileOps::FileOpMkDirAll(const TDesC& aPath, TInt aSetAtts) 
       
   923     {
       
   924     TInt err = DoFileOpMkDirAll(aPath);
       
   925 
       
   926     // if the directory already exists, it is not an error    
       
   927     if (err == KErrAlreadyExists)
       
   928         err = KErrNone;
       
   929     
       
   930     
       
   931     // set attributes for directory just created
       
   932     if (aSetAtts > 0 && err == KErrNone && aPath.Length() > 3)
       
   933         {
       
   934         // a path has a trailing backslash so it needs to be removed before the call
       
   935         err = FileOpAttribs(aPath.Left(aPath.Length()-1), aSetAtts, 0, 0, 0);
       
   936         }
       
   937     
       
   938 
       
   939     // remember the "lowest" error
       
   940     if (err < iOperationError)
       
   941         iOperationError = err;
       
   942     
       
   943     LOGSTRING3("FileBrowser: FileOpMkDirAll %S, err=%d", &aPath, err);
       
   944     
       
   945     return err;
       
   946     }
       
   947         
       
   948 // --------------------------------------------------------------------------------------------
       
   949 
       
   950 TInt CFileBrowserFileOps::DoFileOpMkDirAll(const TDesC& aPath) 
       
   951     {
       
   952     #ifndef FILEBROWSER_LITE
       
   953     if (iModel->Settings().iBypassPlatformSecurity)
       
   954         {
       
   955         if (!iFileOpClient)
       
   956             iFileOpClient = CFBFileOpClient::NewL();   
       
   957 
       
   958         return iFileOpClient->MkDirAll(aPath);
       
   959         }
       
   960     else
       
   961         {
       
   962     #endif
       
   963         return iFs.MkDirAll(aPath);
       
   964     #ifndef FILEBROWSER_LITE
       
   965         }    
       
   966     #endif
       
   967     }
       
   968 
       
   969 // --------------------------------------------------------------------------------------------
       
   970 // --------------------------------------------------------------------------------------------
       
   971 
       
   972 TInt CFileBrowserFileOps::CreateEmptyFile(const TDesC& aName) 
       
   973     {
       
   974     return DoFileOpCreateEmptyFile(aName);
       
   975     }
       
   976 
       
   977 // --------------------------------------------------------------------------------------------
       
   978 
       
   979 TInt CFileBrowserFileOps::FileOpCreateEmptyFile(const TDesC& aName) 
       
   980     {
       
   981     TInt err = DoFileOpCreateEmptyFile(aName);
       
   982 
       
   983     // remember the "lowest" error
       
   984     if (err < iOperationError)
       
   985         iOperationError = err;
       
   986     
       
   987     LOGSTRING3("FileBrowser: FileOpCreateEmptyFile %S, err=%d", &aName, err);
       
   988     
       
   989     return err;
       
   990     }
       
   991 
       
   992 // --------------------------------------------------------------------------------------------
       
   993     
       
   994 TInt CFileBrowserFileOps::DoFileOpCreateEmptyFile(const TDesC& aName) 
       
   995     {
       
   996     #ifndef FILEBROWSER_LITE
       
   997     if (iModel->Settings().iBypassPlatformSecurity)
       
   998         {
       
   999         if (!iFileOpClient)
       
  1000             iFileOpClient = CFBFileOpClient::NewL();   
       
  1001 
       
  1002         return iFileOpClient->CreateEmptyFile(aName);
       
  1003         }
       
  1004     else
       
  1005         {
       
  1006     #endif
       
  1007         TInt err(KErrNone);
       
  1008             
       
  1009         RFile newFile;
       
  1010         err = newFile.Create(iFs, aName, EFileShareExclusive);
       
  1011         if (err == KErrNone)
       
  1012             err = newFile.Flush(); 
       
  1013         newFile.Close();
       
  1014         
       
  1015         return err; 
       
  1016     #ifndef FILEBROWSER_LITE
       
  1017         }    
       
  1018     #endif
       
  1019     }
       
  1020 
       
  1021 // --------------------------------------------------------------------------------------------
       
  1022 
       
  1023 TInt CFileBrowserFileOps::DriveSnapShot(TChar aSourceDriveLetter, TChar aTargetDriveLetter)
       
  1024     {
       
  1025     TInt err(KErrNone);
       
  1026     iFileManObserverResult = MFileManObserver::EContinue;
       
  1027     
       
  1028     // remember old settings and force them to be true for this operation
       
  1029     TBool currentRemoveFileLocksValue = iModel->Settings().iRemoveFileLocks;
       
  1030     TBool currentIgnoreProtectionAttsValue = iModel->Settings().iIgnoreProtectionsAtts;
       
  1031     TBool currentRemoveROMWriteProrection = iModel->Settings().iRemoveROMWriteProrection;
       
  1032     iModel->Settings().iRemoveFileLocks = ETrue;
       
  1033     iModel->Settings().iIgnoreProtectionsAtts = ETrue;
       
  1034     iModel->Settings().iRemoveROMWriteProrection = ETrue;
       
  1035 
       
  1036     
       
  1037     TFileName sourceDir;
       
  1038     sourceDir.Append(aSourceDriveLetter);
       
  1039     sourceDir.Append(_L(":"));
       
  1040 
       
  1041     _LIT(KTargetDir, "%c:\\SnapShot_%c_drive");
       
  1042     TFileName targetDir;
       
  1043     targetDir.Format(KTargetDir, TUint(aTargetDriveLetter), TUint(aSourceDriveLetter));            
       
  1044 
       
  1045     // remove any existing content, first get TEntry
       
  1046     TEntry entry;
       
  1047     err = iFs.Entry(targetDir, entry);
       
  1048 
       
  1049     // entry directory exists, delete it
       
  1050     if (err == KErrNone && entry.IsDir())
       
  1051         {
       
  1052         TFileName targetRoot;
       
  1053         targetRoot.Append(aTargetDriveLetter);
       
  1054         targetRoot.Append(_L(":\\"));
       
  1055 
       
  1056         TFileEntry targetEntry;
       
  1057         targetEntry.iPath = targetRoot;
       
  1058         targetEntry.iEntry = entry;
       
  1059 
       
  1060         err = Delete(targetEntry, CFileMan::ERecurse);                
       
  1061         }
       
  1062         
       
  1063     // do not care if removing succeeded or not, just continue with copying    
       
  1064     TEntry fakeEntry;
       
  1065     fakeEntry.iAtt |= KEntryAttDir;
       
  1066 
       
  1067     TFileEntry sourceEntry;
       
  1068     sourceEntry.iPath = sourceDir;
       
  1069     sourceEntry.iEntry = fakeEntry;
       
  1070 
       
  1071     err = Copy(sourceEntry, targetDir, CFileMan::ERecurse|CFileMan::EOverWrite);                
       
  1072 
       
  1073 
       
  1074     // restore back settings
       
  1075     iModel->Settings().iRemoveFileLocks = currentRemoveFileLocksValue;
       
  1076     iModel->Settings().iIgnoreProtectionsAtts = currentIgnoreProtectionAttsValue;    
       
  1077     iModel->Settings().iRemoveROMWriteProrection = currentRemoveROMWriteProrection;    
       
  1078 
       
  1079 
       
  1080     return iOperationError;
       
  1081     }
       
  1082 
       
  1083 // --------------------------------------------------------------------------------------------
       
  1084 
       
  1085 TInt CFileBrowserFileOps::EraseMBR(TUint aDriveNumber) 
       
  1086     {
       
  1087     #ifndef FILEBROWSER_LITE
       
  1088 
       
  1089         if (!iFileOpClient)
       
  1090             iFileOpClient = CFBFileOpClient::NewL();
       
  1091         
       
  1092         return iFileOpClient->EraseMBR(aDriveNumber);
       
  1093 
       
  1094     #else
       
  1095         return KErrNotSupported;
       
  1096     #endif
       
  1097     }
       
  1098 
       
  1099 // --------------------------------------------------------------------------------------------
       
  1100 
       
  1101 TInt CFileBrowserFileOps::PartitionDrive(TUint aDriveNumber, TUint aNumberOfPartitions) 
       
  1102     {
       
  1103     #ifndef FILEBROWSER_LITE
       
  1104 
       
  1105         if (!iFileOpClient)
       
  1106             iFileOpClient = CFBFileOpClient::NewL();
       
  1107         
       
  1108         return iFileOpClient->PartitionDrive(aDriveNumber, aNumberOfPartitions);
       
  1109 
       
  1110     #else
       
  1111         return KErrNotSupported
       
  1112     #endif
       
  1113     }
       
  1114 
       
  1115 // --------------------------------------------------------------------------------------------
       
  1116 
       
  1117 MFileManObserver::TControl CFileBrowserFileOps::NotifyFileManStarted()
       
  1118     {
       
  1119     return iFileManObserverResult;
       
  1120     }
       
  1121 
       
  1122 // --------------------------------------------------------------------------------------------
       
  1123 
       
  1124 MFileManObserver::TControl CFileBrowserFileOps::NotifyFileManOperation()
       
  1125     {
       
  1126     return iFileManObserverResult;
       
  1127     }
       
  1128 // --------------------------------------------------------------------------------------------
       
  1129 
       
  1130 MFileManObserver::TControl CFileBrowserFileOps::NotifyFileManEnded()
       
  1131     {
       
  1132     return iFileManObserverResult;
       
  1133     }
       
  1134 
       
  1135 // --------------------------------------------------------------------------------------------
       
  1136 
       
  1137 void CFileBrowserFileOps::CancelOp()
       
  1138     {
       
  1139 #ifndef FILEBROWSER_LITE
       
  1140 
       
  1141     if ( iModel->Settings().iBypassPlatformSecurity )
       
  1142         {
       
  1143         if ( !iFileOpClient )
       
  1144             iFileOpClient = CFBFileOpClient::NewL();
       
  1145         
       
  1146         iFileOpClient->CancelOp();
       
  1147         }
       
  1148 
       
  1149     // we need this information even when using iFileOpClient
       
  1150     // to be able to not execute aggregate operations 
       
  1151     iFileManObserverResult = MFileManObserver::ECancel;
       
  1152     
       
  1153 #else
       
  1154     iFileManObserverResult = MFileManObserver::ECancel;
       
  1155 #endif
       
  1156     }
       
  1157 
       
  1158 // End of File