secureswitools/swisistools/source/interpretsislib/utils_win32.cpp
changeset 0 ba25891c3a9e
child 24 5cc91383ab1e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2006-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 the License "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 
       
    20 #define WIN32_LEAN_AND_MEAN 1
       
    21 
       
    22 #ifdef _MSC_VER
       
    23 #pragma warning (disable: 4786)
       
    24 #endif // _MSC_VER
       
    25 
       
    26 #include <windows.h>
       
    27 #include <fstream>
       
    28 #include <stdio.h>
       
    29 #include <io.h>
       
    30 #include <fcntl.h>
       
    31 #include <iostream>
       
    32 #include <sstream>
       
    33 #include <sys/stat.h>
       
    34 
       
    35 #include "logger.h"
       
    36 #include "stringutils.h"
       
    37 #include "is_utils.h"
       
    38 
       
    39 
       
    40 int GetStat(const std::wstring& aFile, struct stat* s)
       
    41 {
       
    42 	return _wstat(aFile.c_str(), reinterpret_cast<struct _stat*>(s));
       
    43 }
       
    44 
       
    45 bool IsDirectory(std::wstring& aStr)
       
    46 {
       
    47     bool ret = false;
       
    48     //
       
    49     if ( aStr.length() > 0 )
       
    50         {
       
    51 	    struct stat x;
       
    52         //
       
    53 	    int err = GetStat(aStr,&x );
       
    54         //
       
    55         if ( err != 0 )
       
    56             {
       
    57             wchar_t lastChar = aStr[ aStr.length() - 1 ];
       
    58 
       
    59             if ( lastChar == L'\\' || lastChar == '/' )
       
    60                 {
       
    61                 // Try again, but without the trailing backslash
       
    62                 std::wstring path = aStr.substr( 0, aStr.length() - 1 );
       
    63                 err = GetStat( path, &x );
       
    64                 if ( err == 0 && (x.st_mode & S_IFDIR) > 0 )
       
    65                     {
       
    66                     aStr = path;
       
    67                     }
       
    68                 }
       
    69             }
       
    70         //
       
    71         ret = ( err == 0 && (x.st_mode & S_IFDIR) > 0 );
       
    72         }
       
    73     //
       
    74 	return ret;
       
    75 }
       
    76 
       
    77 
       
    78 bool FileExists(const std::wstring& aFile)
       
    79 {
       
    80 	struct stat x;
       
    81 	int err = GetStat(aFile,&x );
       
    82 	return err == 0; 
       
    83 }
       
    84 
       
    85 
       
    86 bool RemoveFile(const std::wstring& aFile)
       
    87 {
       
    88 	return _wunlink(aFile.c_str()) == 0;
       
    89 }
       
    90 
       
    91 void RemoveHashForFile(const std::wstring& aFile, const int aDriveLetter, const std::wstring& aPath)
       
    92 {
       
    93 	std::wstring hashdir = L"$:\\sys\\hash\\";
       
    94 	std::wstring basename = aFile.substr( aFile.rfind( KDirectorySeparator ) + 1) ;
       
    95 	if (basename.size() == 0)
       
    96 	{
       
    97 		basename = aFile.substr(aFile.rfind(L"\\"));
       
    98 	}
       
    99 
       
   100 	hashdir[0] = aDriveLetter;
       
   101 	std::wstring hashFile = aPath + L"\\sys\\hash\\" + basename;
       
   102 	if (FileExists(hashFile))
       
   103 	{
       
   104 		RemoveFile(hashFile);
       
   105 	}
       
   106 }
       
   107 
       
   108 bool OpenFile(const std::wstring& aFile, std::fstream& aStream,
       
   109 			  std::ios_base::open_mode aMode)
       
   110 {
       
   111 	std::string s;
       
   112 	aStream.open(Ucs2ToUtf8(aFile, s).c_str(), aMode);
       
   113 	return aStream.good();
       
   114 }
       
   115 
       
   116 bool MakeDir(const std::wstring& aDir)
       
   117 {
       
   118 	// This function makes all the directories in a subdirectory hierarchy.
       
   119   	// If the first character in aDir is a backslash, then we assume it refers
       
   120   	// to the root of a drive, hence we start the index below at 1 to skip over
       
   121   	// this initial root directory.
       
   122     size_t index = aDir.find(L':', 0);
       
   123     if(std::wstring::npos == index)
       
   124     	{
       
   125     	index = 0;
       
   126     	}
       
   127     else
       
   128     	{// Skip creation of root directory
       
   129     	index = aDir.find(L'\\', index);
       
   130     	}
       
   131 	do
       
   132 		{
       
   133     	index += 1;
       
   134 		// Try to make each directory in the path. If ERR_ALREADY_EXISTS is returned
       
   135   	  	// then this is okay. Other errors are fatal.
       
   136 		index = aDir.find(L'\\', index);
       
   137 		std::wstring dir = aDir.substr( 0, index );
       
   138 		if(dir == L".")
       
   139 			{
       
   140 			continue;
       
   141 			}
       
   142 		if (!CreateDirectory(dir.c_str(),0) && GetLastError() != ERROR_ALREADY_EXISTS)
       
   143             {
       
   144 			int lastErr = GetLastError();
       
   145   	  	    (void) lastErr;
       
   146 			return false;
       
   147             }
       
   148 
       
   149 		} while (index != std::wstring::npos);
       
   150 
       
   151     return true;
       
   152 	}
       
   153 
       
   154 #ifndef _MSC_VER
       
   155 
       
   156 void GetDirContents(const std::wstring& path, 
       
   157 					std::list<std::wstring>& contents)
       
   158 	{
       
   159 	_WDIR* currDir =  _wopendir(path.c_str());
       
   160 	if(currDir == NULL)
       
   161 		{
       
   162 		return;
       
   163 		}
       
   164 	
       
   165 	_wdirent* currElem = _wreaddir(currDir);
       
   166 	
       
   167 	while (currElem)
       
   168 		{
       
   169 		contents.push_back(currElem->d_name);
       
   170 		currElem = _wreaddir(currDir);
       
   171 		}
       
   172 	_wclosedir(currDir);
       
   173 	}
       
   174 
       
   175 #else
       
   176 
       
   177 // missing structures for the posix directory calls 
       
   178 struct dirent 
       
   179 {
       
   180 	wchar_t* d_name;
       
   181 };
       
   182 
       
   183 struct DIR 
       
   184 {
       
   185 	WIN32_FIND_DATA iWinData;
       
   186 	HANDLE          iHandle;
       
   187 	dirent          iEntry;
       
   188 	std::wstring     iName;	
       
   189 };
       
   190 
       
   191 DIR* opendir(const wchar_t* dirname) 
       
   192 	{
       
   193 	static DIR dir;
       
   194 	
       
   195 	std::wstring s(dirname);
       
   196 	std::wstring wildcard = strchr("/\\", s[s.length()-1]) ? L"*" : L"/*";
       
   197 	dir.iName = s.append(wildcard);
       
   198 	dir.iHandle = FindFirstFile(dir.iName.c_str(), &dir.iWinData);
       
   199 	if (dir.iHandle != INVALID_HANDLE_VALUE )
       
   200 		return &dir;
       
   201 	else
       
   202 		return 0;
       
   203 	};
       
   204 
       
   205 
       
   206 dirent* readdir(DIR* aDir)
       
   207 {
       
   208 	int result = FindNextFile(aDir->iHandle, &aDir->iWinData);
       
   209 	if (result != 0)
       
   210 	{
       
   211 		aDir->iEntry.d_name = aDir->iWinData.cFileName;
       
   212 		return &aDir->iEntry;
       
   213 	}
       
   214 	return 0;
       
   215 }
       
   216 
       
   217 void closedir(DIR* aDir)
       
   218 {
       
   219 	FindClose(aDir->iHandle);
       
   220 	aDir = 0;
       
   221 }
       
   222 
       
   223 
       
   224 void GetDirContents(const std::wstring& path, 
       
   225 					std::list<std::wstring>& contents)
       
   226 {
       
   227 
       
   228 	DIR* currDir =  opendir(path.c_str());
       
   229 	
       
   230 	while (currDir)
       
   231 	{
       
   232 		dirent* currElem = readdir(currDir);
       
   233 		if (currElem == 0)
       
   234 		{
       
   235 			closedir(currDir);
       
   236 			currDir = 0;
       
   237 		}
       
   238 		else
       
   239 		{
       
   240 			contents.push_back(currElem->d_name);
       
   241 		}
       
   242 	}
       
   243 }
       
   244 
       
   245 #endif
       
   246 
       
   247 
       
   248 
       
   249 bool CheckSisRegistryDirPresent(const std::wstring& aDrivePath, TUint32 aUid)
       
   250 {
       
   251     std::wstring ret = StringUtils::MakePathFromSID( aDrivePath + L"/sys/install/sisregistry/", aUid );
       
   252 
       
   253 	return IsDirectory(ret);
       
   254 }
       
   255 
       
   256 
       
   257 int GetAugmentationsNumber(const std::wstring& aFile)
       
   258 {
       
   259 	for (int index = 1; index < 0xFFFFFFFF; index++)
       
   260 	{
       
   261 		std::wstringstream s2;
       
   262 		s2 << std::hex << index;
       
   263 
       
   264 		// e.g. 12345678_x.sis
       
   265 		std::wstring fileName = aFile + s2.str() + L".sis";
       
   266 
       
   267 		if ( !FileExists(fileName) )
       
   268 		{
       
   269 			// return the next available index
       
   270 			return index;
       
   271 		}
       
   272 	}
       
   273 
       
   274 	return 0;
       
   275 }
       
   276 std::string Utils::wstring2string (const std::wstring& aWide)
       
   277 	{
       
   278 	int max = WideCharToMultiByte(CP_OEMCP,0,aWide.c_str(),aWide.length(),0,0,0,0);
       
   279 	std::string reply;
       
   280 	if (max > 0 )
       
   281 		{
       
   282 		char* buffer = new char [max];
       
   283 		try
       
   284 			{
       
   285 			WideCharToMultiByte(CP_OEMCP,0,aWide.c_str(),aWide.length(),buffer,max,0,0);
       
   286 			reply = std::string (buffer, max);
       
   287 			}
       
   288 		catch (...)
       
   289 			{
       
   290 			}
       
   291 		delete [] buffer;
       
   292 		}
       
   293 	return reply;
       
   294 	}
       
   295 
       
   296 std::wstring Utils::string2wstring (const std::string& aNarrow)
       
   297 	{
       
   298 	int max = MultiByteToWideChar(CP_OEMCP,0,aNarrow.c_str(),aNarrow.length(),0,0);
       
   299 	std::wstring reply;
       
   300 	if (max > 0 )
       
   301 		{
       
   302 		wchar_t* buffer = new wchar_t [max];
       
   303 		try
       
   304 			{
       
   305 			MultiByteToWideChar(CP_OEMCP,0,aNarrow.c_str(),aNarrow.length(),buffer,max);
       
   306 			reply = std::wstring (buffer, max);
       
   307 			}
       
   308 		catch (...)
       
   309 			{
       
   310 			}
       
   311 		delete [] buffer;
       
   312 		}
       
   313 	return reply;
       
   314 	}
       
   315 
       
   316 const std::wstring Utils::IntegerToWideString(int aInt)
       
   317 	{
       
   318 	std::wstringstream wstream;
       
   319 	wstream << aInt;
       
   320 	return wstream.str();
       
   321 	}
       
   322 
       
   323 std::wstring Utils::Int64ToWideString(TInt64 aInt)
       
   324 	{
       
   325 	wchar_t wint[20];
       
   326 	
       
   327 #ifdef _MSC_VER
       
   328 	swprintf(wint, L"%I64u", aInt);
       
   329 #else
       
   330 	swprintf(wint, 20, L"%I64u", aInt);
       
   331 #endif // _MSC_VER
       
   332 	
       
   333 	std::wstring strInt64(wint);
       
   334 	return strInt64;
       
   335 	}
       
   336 
       
   337 int Utils::WideStringToInteger(const std::wstring& aWideString)
       
   338 	{
       
   339 	unsigned long int value=0;
       
   340 	std::wstringstream str(aWideString);
       
   341 	str >> value;
       
   342 	return value;
       
   343 	}