persistentstorage/dbms/SPConv/cn_util.cpp
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     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 // TSPConvUtil class
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <f32file.h>
       
    19 #include <e32cons.h>
       
    20 #include "D32Assert.h"
       
    21 #include "cn_util.h"
       
    22 #include "cn_main.h"
       
    23 #include "cn_cmdparse.h"
       
    24 
       
    25 TBool TSPConvUtil::iPromptOnError = ETrue;
       
    26 
       
    27 _LIT(KTxt0, "Program information");
       
    28 _LIT(KTxt1, "EDbSpConv version 1.0\n");
       
    29 _LIT(KTxt2, "Symbian OS text policy file/binary policy file converter\n");
       
    30 _LIT(KTxt3, "Copyright (c) 2004 Symbian Software Ltd. All rights reserved.\n");
       
    31 _LIT(KTxt4, "\nUsage:\n");
       
    32 _LIT(KTxt5, "EDbSpConv /h\n");
       
    33 _LIT(KTxt6, "EDbSpConv /f=<text file> /b=[path]\\<uid>[.spd] [/s]\n");
       
    34 _LIT(KTxt7, "\nWhere:\n");
       
    35 _LIT(KTxt8, "/h          - displays program information\n");
       
    36 _LIT(KTxt9, "<text file> - security policy text file, including path\n");
       
    37 _LIT(KTxt10,"<uid>[.spd] - security policy binary file\n");
       
    38 _LIT(KTxt11,"[path]      - optional - binary policy file path.\n");
       
    39 _LIT(KTxt12,"              By default the file will be placed in the current directory.\n");
       
    40 _LIT(KTxt13,"<uid>       - database format uid - hex\n");
       
    41 _LIT(KTxt14,"[/s]        - optional - does not wait for a button pressing in case\n");
       
    42 _LIT(KTxt15,"              of an error (when an error message is displayed).\n");
       
    43 
       
    44 /**
       
    45 Prints program information.
       
    46 */
       
    47 static void PrintInfoL()
       
    48 	{
       
    49 	CConsoleBase* con = Console::NewL(KTxt0, TSize(KConsFullScreen, KConsFullScreen));
       
    50 	CleanupStack::PushL(con);
       
    51 	con->Printf(KTxt1);
       
    52 	con->Printf(KTxt2);
       
    53 	con->Printf(KTxt3);
       
    54 	con->Printf(KTxt4);
       
    55 	con->Printf(KTxt5);
       
    56 	con->Printf(KTxt6);
       
    57 	con->Printf(KTxt7);
       
    58 	con->Printf(KTxt8);
       
    59 	con->Printf(KTxt9);
       
    60 	con->Printf(KTxt10);
       
    61 	con->Printf(KTxt11);
       
    62 	con->Printf(KTxt12);
       
    63 	con->Printf(KTxt13);
       
    64 	con->Printf(KTxt14);
       
    65 	con->Printf(KTxt15);
       
    66 	con->Getch();
       
    67 	CleanupStack::PopAndDestroy(con);
       
    68 	}
       
    69 
       
    70 /**
       
    71 Parses command line arguments and fills TCmdLinePrm structure with the parsed data.
       
    72 A a result of method's execution, the following items will be stored in aCmdLinePrm:
       
    73 requested action: (BIN->TXT) or (TXT->BIN), text policy file path and
       
    74 binary policy file path.
       
    75 If there is a "/s" command line argument and there are parsing errors, the error message
       
    76 will be stored in epocwind.out file only. By default (no "/s" command) the error messages 
       
    77 will be displayed on the screen and stored in epocwind.out file.
       
    78 @param aCmdLineParser A reference to a command line argument parser instance.
       
    79 @param aCmdLinePrm A reference to a TCmdLinePrm instance. Output parameter. The parsed
       
    80                    command line arguments will be stored there.
       
    81 @leave KErrArgument Not enough command line arguments, bad argument, non-recognizable argument.
       
    82 @leave KErrNotFound Missing command line argument.
       
    83 */
       
    84 void TSPConvUtil::ParseL(const CCommandLineArguments& aCmdLineParser, 
       
    85 						 TCmdLinePrm& aCmdLinePrm)
       
    86 	{
       
    87 	aCmdLinePrm.Zero();
       
    88 	TInt prmCnt = aCmdLineParser.Count();
       
    89 	if(prmCnt < 2)
       
    90 		{//Print program information and exit
       
    91 		::PrintInfoL();
       
    92 		User::Exit(KErrNone);
       
    93 		}
       
    94 	else
       
    95 		{
       
    96 		//The comand is fixed explicitly, because now there is only one conversion:
       
    97 		//from text policy file to binary policy file.
       
    98 		aCmdLinePrm.iAction = TCmdLinePrm::ETxt2Bin;
       
    99 		for(TInt i=0;i<prmCnt;++i)
       
   100 			{
       
   101 			TPtrC arg = aCmdLineParser.Arg(i);
       
   102 			_LIT(KPrintInfoCmd, "/H");
       
   103 			_LIT(KSuppressPromptCmd, "/S");
       
   104 			_LIT(KTextFilePrm, "/F=");
       
   105 			_LIT(KBinFilePrm, "/B=");
       
   106 			TInt pos;
       
   107 			if(arg == KPrintInfoCmd)
       
   108 				{//Print program information and exit
       
   109 				::PrintInfoL();
       
   110 				User::Exit(KErrNone);
       
   111 				}
       
   112 			else if(arg == KSuppressPromptCmd)
       
   113 				{
       
   114 				TSPConvUtil::iPromptOnError = EFalse;
       
   115 				}
       
   116 			if((pos = arg.Find(KTextFilePrm)) != KErrNotFound)
       
   117 				{
       
   118 				if(pos != 0)
       
   119 					{
       
   120 					__LEAVE(KErrArgument);
       
   121 					}
       
   122 				aCmdLinePrm.iTxtFile.Copy(arg.Right(arg.Length() - KTextFilePrm().Length()));
       
   123 				}
       
   124 			else if((pos = arg.Find(KBinFilePrm)) != KErrNotFound)
       
   125 				{
       
   126 				if(pos != 0)
       
   127 					{
       
   128 					__LEAVE(KErrArgument);
       
   129 					}
       
   130 				aCmdLinePrm.iBinFile.Copy(arg.Right(arg.Length() - KBinFilePrm().Length()));
       
   131 				}
       
   132 			}//end of - for(TInt i=0;i<prmCnt;++i)
       
   133 		}//end of else - if(prmCnt < 2)
       
   134 	if(aCmdLinePrm.iAction == TCmdLinePrm::ENone)
       
   135 		{
       
   136 		_LIT(KText, "Invalid command line arguments, use \"/h\" option for help\n");
       
   137 		TSPConvUtil::Print(KText);
       
   138 		__LEAVE(KErrArgument);
       
   139 		}
       
   140 	if(aCmdLinePrm.iTxtFile.Length() == 0)
       
   141 		{
       
   142 		_LIT(KText, "No text file, use \"/h\" option for help\n");
       
   143 		TSPConvUtil::Print(KText);
       
   144 		__LEAVE(KErrArgument);
       
   145 		}
       
   146 	if(aCmdLinePrm.iBinFile.Length() == 0)
       
   147 		{
       
   148 		_LIT(KText, "No UID, use \"/h\" option for help\n");
       
   149 		TSPConvUtil::Print(KText);
       
   150 		__LEAVE(KErrArgument);
       
   151 		}
       
   152 	}
       
   153 
       
   154 /**
       
   155 The method checks if the file path (aFile parameter) exists.
       
   156 @param aFs File server session.
       
   157 @param aFile File path.
       
   158 @return ETrue - file exists, EFalse - file not found. EFalse may also reflect some kind of
       
   159         file system error.
       
   160 */
       
   161 TBool TSPConvUtil::FileExists(RFs& aFs, const TDesC& aFile)
       
   162 	{
       
   163 	TEntry fileEntry;
       
   164 	return aFs.Entry(aFile, fileEntry) == KErrNone;
       
   165 	}
       
   166 
       
   167 /**
       
   168 The method checks and constructs full binary policy file path from aFile parameter.
       
   169 @param aFile The expected format is:
       
   170              1) <drive:>\<path>\<UID string>
       
   171              2) <drive:>\<path>\<UID string>.spd
       
   172              3) <UID string>
       
   173              4) <UID string>.spd
       
   174 			 '/' symbol might be used as a directory separator as well.
       
   175 			 Output parameter - the created binary policy file path will be stored there.
       
   176 @leave KErrArgument Bad format of aFile input parameter.
       
   177 */
       
   178 void TSPConvUtil::ConstructBinFileNameL(TDes& aFile)
       
   179 	{
       
   180 	//Replace all '/' in aFile with '\', otherwise TParse won't work properly.
       
   181 	TInt pos;
       
   182 	while((pos = aFile.Locate('/')) != KErrNotFound)
       
   183 		{
       
   184 		aFile[pos] = '\\';
       
   185 		}
       
   186 	TParse fileNameParser;
       
   187 	__LEAVE_IF_ERROR(fileNameParser.Set(aFile, NULL, NULL));
       
   188 	TPtrC fileName = fileNameParser.Name();
       
   189 	TUid dbUid;
       
   190 	TLex lex(fileName);
       
   191 	if(lex.Val(*(TUint32*)&dbUid, EHex) == KErrNone && lex.Eos())
       
   192 		{
       
   193 		if(dbUid != KNullUid)
       
   194 			{
       
   195 			_LIT(KExt, ".SPD");
       
   196 			TPtrC fileExt = fileNameParser.Ext();
       
   197 			if(fileExt.Length() == 0)
       
   198 				{
       
   199 				aFile.Append(KExt);
       
   200 				}
       
   201 			else if(fileExt != KExt)
       
   202 				{
       
   203 				_LIT(KText, "Invalid \"UID\" file extension: \"%S\"\n");
       
   204 				TSPConvUtil::Print(KText, fileExt);
       
   205 				__LEAVE(KErrArgument);
       
   206 				}
       
   207 			return;
       
   208 			}
       
   209 		}
       
   210 	_LIT(KText, "Invalid \"UID\" file: \"%S\"\n");
       
   211 	TSPConvUtil::Print(KText, aFile);
       
   212 	__LEAVE(KErrArgument);
       
   213 	}
       
   214 
       
   215 /**
       
   216 The method extracts the UID from aFile parameter, which is expected to represent 
       
   217 binary security policy file path.
       
   218 The method asserts that the extracted UID is not KNullUid.
       
   219 @param aFile Binary policy file path
       
   220 @leave System-wide error codes from file name parsing or KErrNoMemory (from the parser creation).
       
   221 */
       
   222 TUid TSPConvUtil::UidFromFileNameL(const TDesC& aFile)
       
   223 	{
       
   224 	TParse* parser = new (ELeave) TParse;
       
   225 	CleanupStack::PushL(parser);
       
   226 	__LEAVE_IF_ERROR(parser->Set(aFile, NULL, NULL));
       
   227 	TPtrC fileName = parser->Name();
       
   228 	TUid domainUid;
       
   229 	TLex lex(fileName);
       
   230 	if(lex.Val(*(TUint32*)&domainUid, EHex) == KErrNone && lex.Eos() && domainUid != KNullUid)
       
   231 		{
       
   232 		}
       
   233 	CleanupStack::PopAndDestroy(parser);
       
   234 	__ASSERT(domainUid != KNullUid);
       
   235 	return domainUid;
       
   236 	}
       
   237 
       
   238 /**
       
   239 The method prints aText string to epocwnd.out file and on the screen and waits for
       
   240 a button pressing.
       
   241 If "/s" command line argument is presented, the method won't wait for a button pressing.
       
   242 @param aText The text which has to be printed.
       
   243 */
       
   244 void TSPConvUtil::Print(const TDesC& aText)
       
   245 	{
       
   246 	RDebug::Print(aText);
       
   247 	if(TSPConvUtil::iPromptOnError)
       
   248 		{
       
   249 		RNotifier notify;
       
   250 		TInt err = notify.Connect();
       
   251 		if(err == KErrNone)
       
   252 			{
       
   253 			TRequestStatus stat;
       
   254 			TInt but;
       
   255 			_LIT(KNotify,"EDBSPConv");
       
   256 			_LIT(KContinue,"Continue");
       
   257 			notify.Notify(KNotify, aText, KContinue, KNullDesC, but, stat);
       
   258 			User::WaitForRequest(stat);
       
   259 			notify.Close();
       
   260 			}
       
   261 		else
       
   262 			{
       
   263 			RDebug::Print(_L("Error=%d connecting notifier session!\n"), err);
       
   264 			}
       
   265 		}
       
   266 	User::InfoPrint(aText);
       
   267 	}
       
   268 
       
   269 /**
       
   270 The method outputs a formatted text to epocwnd.out file and on the screen using aFormat 
       
   271 parameter as a format string and aNumber as a number which has to be printed out with 
       
   272 the supplied format string.
       
   273 If "/s" command line argument is presented, the method won't wait for a button pressing.
       
   274 @param aFormat The number format string.
       
   275 @param aNumber The number, which has to be printed together with the text. There must be a "%d"
       
   276                format specification somewhere in aFormat parameter.
       
   277 */
       
   278 void TSPConvUtil::Print(const TDesC& aFormat, TInt aNumber)
       
   279 	{
       
   280 	TBuf<300> buf;
       
   281 	buf.Format(aFormat, aNumber);
       
   282 	TSPConvUtil::Print(buf);
       
   283 	}
       
   284 
       
   285 /**
       
   286 The method outputs a formatted text to epocwnd.out file and on the screen using aFormat 
       
   287 parameter as a format string and aText as a text which has to be printed out with the 
       
   288 supplied format string.
       
   289 If "/s" command line argument is presented, the method won't wait for a button pressing.
       
   290 @param aFormat The number format string.
       
   291 @param aText The text, which has to be formatted. There must be a "%S"
       
   292                format specification somewhere in aFormat parameter.
       
   293 */
       
   294 void TSPConvUtil::Print(const TDesC& aFormat, const TDesC& aText)
       
   295 	{
       
   296 	TBuf<300> buf;
       
   297 	buf.Format(aFormat, &aText);
       
   298 	TSPConvUtil::Print(buf);
       
   299 	}
       
   300 
       
   301 /**
       
   302 The method outputs a formatted text to epocwnd.out file and on the screen using aFormat 
       
   303 parameter as a format string and aText1 and aText2 as texts which have to be printed 
       
   304 out with the supplied format string.
       
   305 If "/s" command line argument is presented, the method won't wait for a button pressing.
       
   306 @param aFormat The number format string.
       
   307 @param aText1 The text, which has to be printed out. There must be a "%S"
       
   308                format specification somewhere in aFormat parameter.
       
   309 @param aText2 The text, which has to be printed out. There must be a "%S"
       
   310                format specification somewhere in aFormat parameter.
       
   311 */
       
   312 void TSPConvUtil::Print(const TDesC& aFormat, const TDesC& aText1, const TDesC& aText2)
       
   313 	{
       
   314 	TBuf<500> buf;
       
   315 	buf.Format(aFormat, &aText1, &aText2);
       
   316 	TSPConvUtil::Print(buf);
       
   317 	}
       
   318