messagingfw/msgtest/tools/copylogs/src/CopyLogs.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2000-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 <f32file.h>
       
    17 #include <e32cons.h>
       
    18 
       
    19 LOCAL_C TBool AskL(const TDesC& aSrcFile, const TDesC& aDstFile)
       
    20 	{
       
    21 	const TInt KMaxLineSize = 256;
       
    22 
       
    23 	// Open connection to notify server
       
    24 	RNotifier notifier;
       
    25 	User::LeaveIfError(notifier.Connect());
       
    26 	CleanupClosePushL(notifier);
       
    27 
       
    28 	// Generate line1
       
    29 	TBuf<KMaxLineSize> line1;
       
    30 	_LIT(KLine1, "Copy file: '%S'");
       
    31 	line1.Format(KLine1, &aSrcFile);
       
    32 
       
    33 	// Generate line 2
       
    34 	TBuf<KMaxLineSize> line2;
       
    35 	_LIT(KLine2, "To: '%S'?");	
       
    36 	line2.Format(KLine2, &aDstFile);
       
    37 
       
    38 	// Button text
       
    39 	_LIT(KYesButton, "Yes");
       
    40 	_LIT(KNoButton, "No");
       
    41 
       
    42 	// Display the dialog
       
    43 	TInt button;
       
    44 	TRequestStatus status;
       
    45 	notifier.Notify(line1, line2, KNoButton, KYesButton, button, status);
       
    46 	User::WaitForRequest(status);
       
    47 
       
    48 	CleanupStack::PopAndDestroy(); // notifier
       
    49 
       
    50 	// Did they click yes?
       
    51 	return button == 1;
       
    52 	}
       
    53 
       
    54 LOCAL_C void CopyFileL(RFs& aFs, const TDesC& aSrcFile, const TDesC& aDstFile)
       
    55 	{
       
    56 	// Buffer size
       
    57 	const TInt KBufferSize = 1;
       
    58 
       
    59 	// Check if want to copy the file
       
    60 	if (!AskL(aSrcFile, aDstFile))
       
    61 		return;
       
    62 
       
    63 	// Get source file size
       
    64 	TEntry entry;
       
    65 	User::LeaveIfError(aFs.Entry(aSrcFile, entry));
       
    66 
       
    67 	// Create a buffer to hold file data
       
    68 	HBufC8* contents = HBufC8::NewLC(entry.iSize);
       
    69 
       
    70 	// Make sure destination directory exists
       
    71 	TInt error = aFs.MkDirAll(aDstFile);
       
    72 	if (error != KErrAlreadyExists)
       
    73 		User::LeaveIfError(error);
       
    74 
       
    75 	// Create progress window
       
    76 	_LIT(KProgressTitle, "Copy Progress");
       
    77 	CConsoleBase* console = Console::NewL(KProgressTitle, TSize(KConsFullScreen, KConsFullScreen));
       
    78 	CleanupStack::PushL(console);
       
    79 
       
    80 	// Read in the data
       
    81 	TInt pos = 0;
       
    82 	while(pos < entry.iSize)
       
    83 		{
       
    84 		TBuf8<KBufferSize> buf;
       
    85 		User::LeaveIfError(aFs.ReadFileSection(aSrcFile, pos, buf, Min(KBufferSize, entry.iSize - pos)));
       
    86 
       
    87 		contents->Des().Append(buf);
       
    88 		pos += KBufferSize;
       
    89 
       
    90 		// Display Progress
       
    91 		if (pos % (1<<10) == 0)
       
    92 			console->Printf(_L("Copied %dk of %dk\n"), pos>>10, entry.iSize>>10);
       
    93 		}
       
    94 
       
    95 	CleanupStack::PopAndDestroy(); // console
       
    96 
       
    97 	// Open the file
       
    98 	RFile file;
       
    99 	User::LeaveIfError(file.Replace(aFs, aDstFile, EFileShareExclusive | EFileWrite));
       
   100 	CleanupClosePushL(file);
       
   101 
       
   102 	// Dump the data to file
       
   103 	User::LeaveIfError(file.Write(*contents));
       
   104 	User::LeaveIfError(file.Flush());
       
   105 
       
   106 	CleanupStack::PopAndDestroy(2); // file, contents
       
   107 	}
       
   108 
       
   109 LOCAL_C void CopyDirL(RFs& aFs, const TDesC& aSrcDir, const TDesC& aDstDir)
       
   110 	{
       
   111 	// Generate file specification
       
   112 	TParse spec;
       
   113 	User::LeaveIfError(spec.Set(_L("*"), &aSrcDir, NULL));
       
   114 
       
   115 	TFindFile find(aFs);
       
   116 	CDir* dir;
       
   117 	
       
   118 	// Find the files
       
   119 	TInt error = find.FindWildByPath(spec.FullName(), NULL, dir);
       
   120 	if (error == KErrNone)
       
   121 		{
       
   122 		CleanupStack::PushL(dir);
       
   123 
       
   124 		// Iterate through all the files
       
   125 		for(TInt i = 0; i < dir->Count(); i++)
       
   126 			{
       
   127 			if (!(*dir)[i].IsDir())
       
   128 				{
       
   129 				// Generate source path
       
   130 				TParse srcParse;
       
   131 				User::LeaveIfError(srcParse.Set((*dir)[i].iName, &aSrcDir, NULL));
       
   132 
       
   133 				// Generate destination path
       
   134 				TParse dstParse;
       
   135 				User::LeaveIfError(dstParse.Set((*dir)[i].iName, &aDstDir, NULL));
       
   136 
       
   137 				// Copy the file
       
   138 				CopyFileL(aFs, srcParse.FullName(), dstParse.FullName());
       
   139 				}
       
   140 			}
       
   141 
       
   142 		CleanupStack::PopAndDestroy(); // dir
       
   143 		}
       
   144 
       
   145 	// Did an error occur?
       
   146 	if (error != KErrNotFound)
       
   147 		User::LeaveIfError(error);
       
   148 	}
       
   149 
       
   150 LOCAL_C void CopyLogsL(RFs& aFs, const TDesC& aSrcDir, const TDesC& aDstDir)
       
   151 	{
       
   152 	// Copy any files from root folder
       
   153 	CopyDirL(aFs, aSrcDir, aDstDir);
       
   154 
       
   155 	// Setup directory scan
       
   156 	CDirScan* scan = CDirScan::NewLC(aFs);
       
   157 	scan->SetScanDataL(aSrcDir, KEntryAttDir|KEntryAttMatchExclusive, ESortNone, CDirScan::EScanUpTree);
       
   158 
       
   159 	// Iterate through all the directories
       
   160 	FOREVER
       
   161 		{
       
   162 		// Get next directory list
       
   163 		CDir* dir = NULL;
       
   164 		TRAPD(error, scan->NextL(dir));
       
   165 		delete dir;
       
   166 
       
   167 		if (!(error == KErrNone && dir))
       
   168 			break;
       
   169 
       
   170 		// Copy the directory
       
   171 		CopyDirL(aFs, scan->FullPath(), aDstDir);
       
   172 		};
       
   173 
       
   174 	CleanupStack::PopAndDestroy(); // scan
       
   175 	}
       
   176 
       
   177 LOCAL_C void DoMainL()
       
   178 	{
       
   179 	// Create scheduler
       
   180 	CActiveScheduler* scheduler = new(ELeave)CActiveScheduler;
       
   181 	CleanupStack::PushL(scheduler);
       
   182 	CActiveScheduler::Install(scheduler);
       
   183 
       
   184 	// Create file session
       
   185 	RFs fs;
       
   186 	User::LeaveIfError(fs.Connect());
       
   187 	CleanupClosePushL(fs);
       
   188 
       
   189 	_LIT(KSrcLogFolder, "c:\\logs\\");
       
   190 
       
   191 	// Copy the logs
       
   192 	CopyLogsL(fs, KSrcLogFolder(), RProcess().FileName());
       
   193 
       
   194 	CleanupStack::PopAndDestroy(2); // fs, scheduler
       
   195 	}
       
   196 
       
   197 GLDEF_C TInt E32Main()
       
   198 	{
       
   199 	__UHEAP_MARK;
       
   200 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   201 	__ASSERT_ALWAYS(cleanup, User::Invariant());
       
   202 	TRAPD(error, DoMainL());		
       
   203 	delete cleanup;
       
   204 	__UHEAP_MARKEND;
       
   205 	return(KErrNone);
       
   206 	}
       
   207 
       
   208 
       
   209