messagingfw/msgsrvnstore/server/src/CMsvCopyStoreOperation.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "MSVSERV.H"
       
    17 #include "MSVAPI.H"
       
    18 #include <cmsvcopystoreoperation.h>
       
    19 
       
    20 // the amount of space used, even if empty (an approximation)
       
    21 const TInt KMinimumSpace=400;
       
    22 
       
    23 _LIT(KIndex, "Index");
       
    24 
       
    25 CMsvCopyStoreOperation::CMsvCopyStoreOperation(const RMessage2& aMessage, CMsvServer& aServer)
       
    26 : CMsvServerOperation(aMessage, aMessage.Int0(), KUidMsvLocalServiceMtm, KMsvLocalServiceIndexEntryId, -1), iDrive(TDriveUnit (aMessage.Int1() )), iServer(aServer)
       
    27 	{
       
    28 	CActiveScheduler::Add(this);
       
    29 	}
       
    30 
       
    31 CMsvCopyStoreOperation* CMsvCopyStoreOperation::NewL(const RMessage2& aMessage, CMsvServer& aServer)
       
    32 	{
       
    33 	CMsvCopyStoreOperation* self=new(ELeave)CMsvCopyStoreOperation(aMessage, aServer);
       
    34 	return self;
       
    35 	}
       
    36 
       
    37 CMsvCopyStoreOperation::~CMsvCopyStoreOperation()
       
    38 	{
       
    39 	Cancel();
       
    40 	delete iDir;
       
    41 	delete iFileMan;
       
    42 	}
       
    43 
       
    44 
       
    45 void CMsvCopyStoreOperation::StartL()
       
    46 	{
       
    47 	__ASSERT_DEBUG(!IsActive() , PanicServer(EMsvCopyStoreReset));
       
    48 
       
    49 	iProgress().iState=TMsvCopyProgress::ENotYetStarted;
       
    50 	iPos=0;
       
    51 
       
    52 	iFileMan = CFileMan::NewL(iServer.FileSession(), this);
       
    53 
       
    54 	CompleteSelf();
       
    55 	}
       
    56 
       
    57 /* Function to retrieve progress of the Copy operation
       
    58 **/
       
    59 const TDesC8& CMsvCopyStoreOperation::Progress()
       
    60 	{
       
    61 	 return iProgress;
       
    62 	}
       
    63 
       
    64 void CMsvCopyStoreOperation::DoCancel()
       
    65 	{
       
    66 	iCopyCancel = ETrue;
       
    67 	Completed(KErrCancel);
       
    68 	}
       
    69 
       
    70 
       
    71 /* Run different operations like LockStore, CopyStore
       
    72    UnlockStore and Completed */
       
    73 
       
    74 void CMsvCopyStoreOperation::RunL()
       
    75 	{
       
    76 	User::LeaveIfError(iStatus.Int());
       
    77 	switch(iProgress().iState)
       
    78 		{
       
    79 		case TMsvCopyProgress::ENotYetStarted:
       
    80 			LockMailStoreL();
       
    81 			iProgress().iState=TMsvCopyProgress::ELock;
       
    82 			break;
       
    83 		case TMsvCopyProgress::ELock:
       
    84 			InitCopyStoreL();
       
    85 			iProgress().iState=TMsvCopyProgress::EInitCopy;
       
    86 			break;
       
    87 
       
    88 		case TMsvCopyProgress::EInitCopy:
       
    89 			iProgress().iState=TMsvCopyProgress::ECopyStore;
       
    90 		//drop through the next case statement
       
    91 		case TMsvCopyProgress::ECopyStore:
       
    92 			if (iPos<iProgress().iTotal)
       
    93 				{
       
    94 				CopySourceL();
       
    95 				}
       
    96 			else
       
    97 			   	{
       
    98 				FinishCopyStoreL();
       
    99 				iProgress().iState=TMsvCopyProgress::EUnlock;
       
   100 			   	}
       
   101 			break;
       
   102 		case TMsvCopyProgress::EUnlock:
       
   103 			Completed(KErrNone);
       
   104 			iProgress().iState=TMsvCopyProgress::ECompleted;
       
   105 			break;
       
   106 		default:
       
   107 			__ASSERT_DEBUG(EFalse,PanicServer(EMsvCopyStoreReset));
       
   108 			Completed(KErrUnknown);
       
   109 			iProgress().iState=TMsvCopyProgress::ECompleted;
       
   110 			break;
       
   111 		}
       
   112 	}
       
   113 
       
   114 
       
   115 /* Lock the Mail store so other operations dont use it while
       
   116    the copy event is in progress
       
   117 */
       
   118 void CMsvCopyStoreOperation::LockMailStoreL()
       
   119 	{
       
   120 	User::LeaveIfError(iServer.Context().IndexAdapter()->ErrorState());
       
   121 	iServer.NotifyChanged(EMsvMediaUnavailable, KMsvNullIndexEntryId, iServer.Drive());
       
   122 	iServer.SetStartupState(EMsvMediaUnavailable);
       
   123 	iServer.Context().IndexAdapter()->SetErrorState(KErrNone);
       
   124 	//Complete yourself
       
   125 	CompleteSelf();
       
   126 	}
       
   127 
       
   128 
       
   129 /* Initialise the copy process by setting the destination path,etc
       
   130 */
       
   131 void CMsvCopyStoreOperation::InitCopyStoreL()
       
   132 	{
       
   133 	//Check if the drive already contains a store,
       
   134 	//if so then unlock the index file and complete
       
   135 	if(MessageServer::DriveContainsStore(iServer.FileSession(),iDrive))
       
   136 		{
       
   137 		User::Leave(KErrAlreadyExists);
       
   138 		}
       
   139 	else
       
   140 		{
       
   141 		//Create the destination path
       
   142 		iDest = iDrive.Name();
       
   143 		iDest.Append(iServer.Context().MessageFolder());
       
   144 
       
   145 		//Delete the drive letters from Messagefolder path
       
   146 		iDest.Delete(2,2);
       
   147 
       
   148 		//check if the target disk has enough disk space
       
   149 		CheckDiskSpaceL();
       
   150 
       
   151 		//Get the directory structure of the source path
       
   152 		delete iDir;
       
   153 		iDir=NULL;
       
   154     	User::LeaveIfError(iServer.FileSession().GetDir(iServer.Context().MessageFolder(), KEntryAttDir|KEntryAttNormal, ESortNone, iDir));
       
   155     	iProgress().iTotal = iDir->Count();
       
   156 
       
   157  		//Check if there is any broken store already existing on iDest
       
   158  		TUint unused;
       
   159 		TInt error=iServer.FileSession().Att(iDest,unused);
       
   160 		if(error == KErrNone)
       
   161 			{
       
   162 			//delete any files that may exist
       
   163 			User::LeaveIfError(iFileMan->RmDir(iDest,iStatus));
       
   164 			SetActive();
       
   165 			}
       
   166 		else
       
   167 			{
       
   168 		    //Complete
       
   169    			CompleteSelf();
       
   170             }
       
   171 		}
       
   172 	}
       
   173 
       
   174 
       
   175 /** Copy Message Store directories from source to destination
       
   176 	except for the Index file, which is copied in the end after
       
   177 	all goes well.
       
   178 */
       
   179 void CMsvCopyStoreOperation::CopySourceL()
       
   180 	{
       
   181 	iCopyCancel = EFalse;
       
   182 	TEntry entry;
       
   183 	TFileName dest=iDest;
       
   184 	entry = (*iDir)[iPos];
       
   185 
       
   186 	if(entry.iName==KIndex)
       
   187     	{
       
   188     	//complete yourself
       
   189 	   	CompleteSelf();
       
   190     	}
       
   191     else
       
   192     	{
       
   193 	    iSrc.Zero();
       
   194     	iSrc.Append(iServer.Context().MessageFolder());
       
   195        	iSrc.Append(entry.iName);
       
   196        	dest.Append(entry.iName);
       
   197 
       
   198        	// Check if the entry is a directory
       
   199     	if(entry.IsDir())
       
   200     		{
       
   201     		dest.Append('\\');
       
   202     		iSrc.Append('\\');
       
   203 
       
   204       		// Get the contents of this directory
       
   205       		CDir *dirList=NULL;
       
   206       		TInt err = iServer.FileSession().GetDir(iSrc, KEntryAttDir, ESortByName, dirList);
       
   207 
       
   208       		// Get the count to check if the directory is empty
       
   209       		TInt count = dirList->Count();
       
   210       		delete dirList;
       
   211 
       
   212       		if(err == KErrNone)
       
   213       			{
       
   214       			// Create an empty directory
       
   215       			User::LeaveIfError(iServer.FileSession().MkDirAll(dest));
       
   216 
       
   217       			// Copy the contents over
       
   218       			if( count != 0 )
       
   219        	     		{
       
   220        	     		User::LeaveIfError(iFileMan->Copy(iSrc,dest,CFileMan::ERecurse,iStatus));
       
   221     				SetActive();
       
   222        	     		}
       
   223        	     	// Directory empty nothing needed
       
   224        	    	else
       
   225        				{
       
   226        				CompleteSelf();
       
   227        				}
       
   228       			}
       
   229       		else
       
   230       			{
       
   231        			User::Leave(err);
       
   232       			}
       
   233     		}
       
   234     	else
       
   235     		{
       
   236     		if(iPos==0)
       
   237     			{
       
   238     			User::LeaveIfError(iServer.FileSession().MkDirAll(dest));
       
   239     			}
       
   240     		User::LeaveIfError(iFileMan->Copy(iSrc,dest,CFileMan::EOverWrite,iStatus));
       
   241     		SetActive();
       
   242     		}
       
   243     	}
       
   244     dest.Zero();
       
   245     iProgress().iCurrent = iPos++;
       
   246     }
       
   247 
       
   248 
       
   249 /** Finish the Copy Process by copying over the index
       
   250 */
       
   251 void CMsvCopyStoreOperation::FinishCopyStoreL()
       
   252 	{
       
   253 	TDriveUnit drive = MessageServer::CurrentDriveL(iServer.FileSession());
       
   254 	
       
   255 	TParse parse;
       
   256 	
       
   257 	TPtrC currDrive(drive.Name());
       
   258 	parse.Set(KMsvDbFile, &currDrive, NULL);
       
   259 	TFileName src = parse.FullName();
       
   260 	
       
   261 	TPtrC newDrive(iDrive.Name());
       
   262 	parse.Set(KMsvDbFile, &newDrive, NULL);
       
   263 	TFileName dest = parse.FullName();
       
   264 	
       
   265 #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
   266 	TRAP_IGNORE(iServer.Context().IndexAdapter()->GetDbAdapter()->DetachDBL(KCurrentDriveId));
       
   267 #else
       
   268 	delete iServer.Context().IndexAdapter()->GetDbAdapter();
       
   269 #endif
       
   270 	
       
   271 	RSqlDatabase::Copy(src, dest);
       
   272 
       
   273 #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
   274 	// Update the drive status of the target 
       
   275 	// drive in the preferred drive list.
       
   276 	iServer.UpdateDriveStatusL(iDrive, EMsvMessageStoreAvailableStatus);
       
   277 #endif
       
   278 
       
   279 	CompleteSelf();
       
   280 	}
       
   281 
       
   282 
       
   283 /** Unlock the Index file so other functions can
       
   284     use it once again
       
   285 */
       
   286 void CMsvCopyStoreOperation::UnlockMailStore()
       
   287 	{
       
   288 #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
   289 	TMsvId maxId;
       
   290 	TDriveNumber driveNum = CMsvPreferredDriveList::GetDriveList()->CurrentDriveNumber();
       
   291 #if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
       
   292 	TRAP_IGNORE(iServer.Context().IndexAdapter()->GetDbAdapter()->AttachDBL(driveNum, KCurrentDriveId, maxId, &iServer.MessageDBAdapter()));
       
   293 #else
       
   294 	TRAP_IGNORE(iServer.Context().IndexAdapter()->GetDbAdapter()->AttachDBL(driveNum, KCurrentDriveId, maxId));
       
   295 #endif
       
   296 #else
       
   297 	TDriveUnit driveUnit;
       
   298 	TRAP_IGNORE(driveUnit = MessageServer::CurrentDriveL(iServer.FileSession()));
       
   299 	TPtrC drive(driveUnit.Name());
       
   300 	TParse parse;
       
   301 	parse.Set(KMsvDbFile, &drive, NULL);
       
   302 	TFileName dest = parse.FullName();
       
   303 	TRAP_IGNORE(iServer.Context().IndexAdapter()->OpenclosedL(dest));
       
   304 #endif		// #if (defined SYMBIAN_MSGS_ENHANCED_REMOVABLE_MEDIA_SUPPORT)
       
   305 
       
   306 	iServer.NotifyChanged(EMsvMediaAvailable, KMsvNullIndexEntryId, iServer.Drive());
       
   307 	iServer.Context().IndexAdapter()->SetErrorState(KErrNone);
       
   308 	iServer.SetStartupState(EMsvNullNotification);
       
   309 	}
       
   310 
       
   311 
       
   312 /** Complete the unlock process and Set yourself active
       
   313 */
       
   314 void CMsvCopyStoreOperation::CompleteSelf()
       
   315 	{
       
   316 	TRequestStatus *status=&iStatus;
       
   317 	iStatus=KRequestPending;
       
   318 	SetActive();
       
   319 	User::RequestComplete(status,KErrNone);
       
   320 	}
       
   321 
       
   322 
       
   323 TInt CMsvCopyStoreOperation::RunError(TInt aError)
       
   324 	{
       
   325 
       
   326 	//Delete any broken store on the target disk
       
   327 	TUint unused;
       
   328 	if (iDest.Length() > 0)
       
   329 		{
       
   330 		if(iServer.FileSession().Att(iDest,unused) == KErrNone)
       
   331 			{
       
   332 			iFileMan->RmDir(iDest);	// ignore error - don't care if fails
       
   333 			}
       
   334 		}
       
   335 
       
   336 	Completed(aError);
       
   337 	return(KErrNone);
       
   338 	}
       
   339 
       
   340 /* Completed */
       
   341 void CMsvCopyStoreOperation::Completed(TInt aError)
       
   342 	{
       
   343 	iProgress().iError=aError;
       
   344 	if(iProgress().iState >= TMsvCopyProgress::ELock)
       
   345 		{
       
   346 		 UnlockMailStore();
       
   347 		}
       
   348 	SetState(EMsvOperationCompleted);
       
   349 	iMessage.Complete(KErrNone);
       
   350 	}
       
   351 
       
   352 /* Check the target disk space */
       
   353 void CMsvCopyStoreOperation::CheckDiskSpaceL()
       
   354 	{
       
   355 	RFs& fs = iServer.FileSession();
       
   356 	TInt64 spaceNeed=KMinimumSpace;
       
   357 	CDirScan* dirScan = CDirScan::NewLC(fs);
       
   358 	CDir* dirList = NULL;
       
   359     
       
   360 	TVolumeIOParamInfo volumeIOInfo;
       
   361 	User::LeaveIfError(fs.VolumeIOParam(TInt(iDrive), volumeIOInfo));
       
   362 	TInt clusterSize = volumeIOInfo.iClusterSize;
       
   363 	dirScan->SetScanDataL(iServer.Context().MessageFolder(), KEntryAttNormal, ESortByName, CDirScan::EScanDownTree);
       
   364 
       
   365 	do{
       
   366 		dirScan->NextL(dirList);
       
   367 		CleanupStack::PushL(dirList);
       
   368 		if(dirList)
       
   369 			{
       
   370 			for(TInt item = 0; item < dirList->Count(); ++item)
       
   371 				{
       
   372 				const TEntry& entry = (*dirList)[item];
       
   373 				spaceNeed+=entry.iSize+clusterSize;
       
   374 				}
       
   375 			}
       
   376 		else
       
   377 			{
       
   378 			spaceNeed+=KMinimumSpace;
       
   379 			}
       
   380          
       
   381 		CleanupStack::PopAndDestroy(dirList);
       
   382 		} while(dirList); 
       
   383 
       
   384 	 CleanupStack::PopAndDestroy(dirScan);
       
   385 	 TVolumeInfo volumeInfo;
       
   386 	 User::LeaveIfError(iServer.FileSession().Volume(volumeInfo, iDrive));
       
   387 	 if(volumeInfo.iFree<spaceNeed)
       
   388 	 	{
       
   389 	 	User::Leave(KErrDiskFull);
       
   390 	 	}
       
   391 	 }
       
   392 /**
       
   393 NotifyFileManOperation is called by the Fileman observer framework after/before each
       
   394 file operation.
       
   395 @param None.
       
   396 @return MFileManObserver::TControl
       
   397         ECancel If the operation is cancelled for one entry.
       
   398         EContinue If the operation needs to be continued.
       
   399 */
       
   400 MFileManObserver::TControl CMsvCopyStoreOperation::NotifyFileManOperation()
       
   401 	{
       
   402 	if(iCopyCancel)
       
   403 		{
       
   404 		return MFileManObserver::ECancel;
       
   405 		}
       
   406 	return MFileManObserver::EContinue;
       
   407 	}
       
   408 
       
   409 /**
       
   410 NotifyFileManEnded is called by the Fileman observer framework to inform the observer
       
   411 that an operation is complete.
       
   412 @param None.
       
   413 @return MFileManObserver::TControl
       
   414         EAbort If the bulk operation is cancelled.
       
   415         EContinue If the operation needs to be continued.
       
   416 
       
   417 */
       
   418 MFileManObserver::TControl CMsvCopyStoreOperation::NotifyFileManEnded()
       
   419 	{
       
   420 	if(iCopyCancel)
       
   421 		{
       
   422 		return MFileManObserver::EAbort;
       
   423 		}
       
   424 	return MFileManObserver::EContinue;
       
   425 	}