secureswitools/swisistools/source/interpretsislib/utils_posix.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 #include<errno.h>
       
    20 #include<fstream>
       
    21 #include<iostream>
       
    22 #include<sstream>
       
    23 
       
    24 #include"utility_interface.h"
       
    25 
       
    26 bool FileExists(const std::wstring& aFile)
       
    27 {
       
    28 	return true;
       
    29 }
       
    30 bool RemoveFile(const std::wstring& aFile)
       
    31 {
       
    32 	return true;
       
    33 }
       
    34 bool CreateFile(const std::wstring& aFile)
       
    35 {
       
    36 	return true;
       
    37 }
       
    38 
       
    39 int GetStat(const std::wstring& aFile, struct stat* s)
       
    40 {
       
    41 	std::string str;
       
    42  	return stat(Ucs2ToUtf8(aFile, str).c_str(), s);
       
    43 }
       
    44 
       
    45 void GetDirContents(const std::wstring& path, 
       
    46 					std::list<std::wstring>& contents)
       
    47 {
       
    48 
       
    49 	std::string utfString;
       
    50 	DIR* currDir =  opendir(Ucs2ToUtf8(path, utfString).c_str());
       
    51 	
       
    52 	while (currDir)
       
    53 	{
       
    54 		dirent* currElem = readdir(currDir);
       
    55 		if (currElem == 0)
       
    56 		{
       
    57 			closedir(currDir);
       
    58 			currDir = 0;
       
    59 		}
       
    60 		else
       
    61 		{
       
    62 			std::wstring ucsString;
       
    63 			contents.push_back(Utf8ToUcs2(currElem->d_name, ucsString));
       
    64 		}
       
    65 	}
       
    66 }
       
    67 
       
    68 bool IsDirectory(std::wstring& aStr)
       
    69 {
       
    70     bool ret = false;
       
    71     //
       
    72     if ( aStr.length() > 0 )
       
    73         {
       
    74 	    struct stat x;
       
    75         //
       
    76 	    int err = GetStat(aStr,&x );
       
    77         //
       
    78         if ( err != 0 )
       
    79             {
       
    80             wchar_t lastChar = aStr[ aStr.length() - 1 ];
       
    81 
       
    82             if ( lastChar == L'\\' || lastChar == '/' )
       
    83                 {
       
    84                 // Try again, but without the trailing backslash
       
    85                 std::wstring path = aStr.substr( 0, aStr.length() - 1 );
       
    86                 err = GetStat( path, &x );
       
    87                 if ( err == 0 && (x.st_mode & S_IFDIR) > 0 )
       
    88                     {
       
    89                     aStr = path;
       
    90                     }
       
    91                 }
       
    92             }
       
    93         //
       
    94         ret = ( err == 0 && (x.st_mode & S_IFDIR) > 0 );
       
    95         }
       
    96     //
       
    97 	return ret;
       
    98 }
       
    99 
       
   100 
       
   101 bool MakeDir(const std::wstring& aDir)
       
   102 {
       
   103 	// This function makes all the directories in a subdirectory hierarchy.
       
   104   	// If the first character in aDir is a backslash, then we assume it refers
       
   105   	// to the root of a drive, hence we start the index below at 1 to skip over
       
   106   	// this initial root directory.
       
   107     size_t index = aDir.find(L':', 0);
       
   108     if(std::wstring::npos == index)
       
   109     	{
       
   110     	index = 0;
       
   111     	}
       
   112     else
       
   113     	{// Skip creation of root directory
       
   114     	index = aDir.find(L'\\', index);
       
   115     	}
       
   116 	do
       
   117 		{
       
   118     	index += 1;
       
   119 		// Try to make each directory in the path. If ERR_ALREADY_EXISTS is returned
       
   120   	  	// then this is okay. Other errors are fatal.
       
   121 		index = aDir.find(L'\\', index);
       
   122 		std::wstring dir = aDir.substr( 0, index );
       
   123 		if(dir == L".")
       
   124 			{
       
   125 			continue;
       
   126 			}
       
   127 		if (!CreateDir(dir.c_str()) && GetErrorValue()  != ERROR_ALREADY_EXISTS)
       
   128             {
       
   129 			int lastErr = GetErrorValue();
       
   130   	  	    (void) lastErr;
       
   131 			return false;
       
   132             }
       
   133 
       
   134 		} while (index != std::wstring::npos);
       
   135 
       
   136     return true;
       
   137 	}
       
   138 
       
   139 
       
   140 bool OpenFile(const std::wstring& aFile, std::fstream& aStream,
       
   141 			  std::ios_base::open_mode aMode)
       
   142 {
       
   143 	std::string s;
       
   144 	aStream.open(Ucs2ToUtf8(aFile, s).c_str(), aMode);
       
   145 	return aStream.good();
       
   146 }
       
   147 
       
   148 
       
   149 int GetAugmentationsNumber(const std::wstring& aFile)
       
   150 {
       
   151 	for (int index = 1; index < 0xFFFFFFFF; index++)
       
   152 	{
       
   153 		std::wstringstream s2;
       
   154 		s2 << std::hex << index;
       
   155 
       
   156 		// e.g. 12345678_x.sis
       
   157 		std::wstring fileName = aFile + s2.str() + L".sis";
       
   158 
       
   159 		if ( !FileExists(fileName) )
       
   160 		{
       
   161 			// return the next available index
       
   162 			return index;
       
   163 		}
       
   164 	}
       
   165 
       
   166 	return 0;
       
   167 }
       
   168 
       
   169 
       
   170 bool CheckSisRegistryDirPresent(const std::wstring& aDrivePath, TUint32 aUid)
       
   171 {
       
   172     std::wstring ret = StringUtils::MakePathFromSID( aDrivePath + L"/sys/install/sisregistry/", aUid );
       
   173 
       
   174 	return IsDirectory(ret);
       
   175 }
       
   176 
       
   177 void RemoveHashForFile(const std::wstring& aFile, const int aDriveLetter, const std::wstring& aPath)
       
   178 {
       
   179 	std::wstring hashdir = L"$:\\sys\\hash\\";
       
   180 	std::wstring basename = aFile.substr( aFile.rfind( KDirectorySeparator ) + 1) ;
       
   181 	if (basename.size() == 0)
       
   182 	{
       
   183 		basename = aFile.substr(aFile.rfind(L"\\"));
       
   184 	}
       
   185 
       
   186 	hashdir[0] = aDriveLetter;
       
   187 	std::wstring hashFile = aPath + L"\\sys\\hash\\" + basename;
       
   188 	if (FileExists(hashFile))
       
   189 	{
       
   190 		RemoveFile(hashFile);
       
   191 	}
       
   192 }