[sword-svn] r82 - trunk/src/installer

bdrake at www.crosswire.org bdrake at www.crosswire.org
Tue Jan 1 10:23:31 MST 2008


Author: bdrake
Date: 2008-01-01 10:23:30 -0700 (Tue, 01 Jan 2008)
New Revision: 82

Modified:
   trunk/src/installer/doInstall.cpp
Log:
Code added to delete files, and arrays formed of .conf file before and after install manager.  Full delet process not yet implemented.  Test for sword.exe now included.

Modified: trunk/src/installer/doInstall.cpp
===================================================================
--- trunk/src/installer/doInstall.cpp	2007-12-28 20:16:16 UTC (rev 81)
+++ trunk/src/installer/doInstall.cpp	2008-01-01 17:23:30 UTC (rev 82)
@@ -3,6 +3,7 @@
 #include <tchar.h>
 #include <rapi.h>
 #include <windows.h>
+#include <fstream>
 
 using namespace SRInstallMGR;
 using namespace System;
@@ -37,16 +38,60 @@
   return buffer;
 }
 
-TCHAR* PrintDirectory(LPWSTR Path, UINT Indent){ // recurse through device from root
-	DWORD          foundCount;
-    LPCE_FIND_DATA findDataArray;
+void copyconfs(WCHAR *confName) {
 	TCHAR wszSrcFile[MAX_PATH];
 	WCHAR tszDestFile[MAX_PATH];
-    WCHAR searchPath[MAX_PATH];
+	BYTE  Buffer[10000];
 	HANDLE hSrc, hDest;
-	BYTE  Buffer[10000];
     DWORD dwNumRead, dwNumWritten;
 
+    wcscpy_s(wszSrcFile, swordPath);
+	wcscat_s(wszSrcFile, TEXT("mods.d\\"));
+    wcscat_s(wszSrcFile, confName);
+	wcscpy_s( tszDestFile, TEXT("mods.d\\"));
+	wcscat_s( tszDestFile, confName);
+    hSrc = CeCreateFile(wszSrcFile, GENERIC_READ, FILE_SHARE_READ,
+        NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+	if (INVALID_HANDLE_VALUE == hSrc) {
+		MessageBox::Show("Sorry, I'm struggling to open a file on your \
+device.", "Process Failed", MessageBoxButtons::OK);
+		Application::Exit();
+	}
+	hDest = CreateFile(tszDestFile, GENERIC_WRITE, FILE_SHARE_READ,
+        NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+	if (INVALID_HANDLE_VALUE == hDest) {
+		MessageBox::Show("Sorry, I can't seem to write files to your PC.\
+Do you have a mods.d directory where you put me?", "Process Failed",
+		MessageBoxButtons::OK);
+		Application::Exit();
+	}	
+	if (CeReadFile(hSrc, &Buffer, sizeof(Buffer), &dwNumRead, NULL)) {
+		if (!WriteFile(hDest, &Buffer, dwNumRead, &dwNumWritten, NULL)) {
+			MessageBox::Show("Sorry, I can't seem to read the files on \
+your mobile device.", "Process Failed", MessageBoxButtons::OK);
+			Application::Exit();
+		}
+	}
+	else {
+		MessageBox::Show("Sorry, I can't read the files on your mobile \
+device.", "Process Failed", MessageBoxButtons::OK);
+		Application::Exit();
+	}
+	if (hSrc){
+		CeCloseHandle(hSrc);
+		hSrc = NULL;
+	}
+	if (hDest){
+		CloseHandle (hDest);
+		hDest = NULL;
+	}
+}
+
+TCHAR* PrintDirectory(LPWSTR Path, UINT Indent){ // recurse through device from root
+	DWORD          foundCount;
+    LPCE_FIND_DATA findDataArray;
+    WCHAR searchPath[MAX_PATH];
+
     wcscpy_s(searchPath, Path);
     wcscat_s(searchPath, L"*");		
     if(!CeFindAllFiles(searchPath, FAF_ATTRIBUTES | FAF_NAME, &foundCount, &findDataArray)) {
@@ -68,7 +113,7 @@
 			int len = strlen (modstr);
 			int swlen = wcslen (swordPath);
 			while (len--) swlen--;
-			swordPath[swlen] = 0;
+			swordPath[swlen] = 0; // global swordPath now set to CE sword dir
 
 		/********************************************************************************
 		// don't want to do anything with files except when they are in MODS.D
@@ -78,47 +123,8 @@
 		// note: have not checked for more than one installation: it is possible for someone
 		// to have installed into two different locations - really! 
 		*********************************************************************************/
-
-		    wcscpy_s(wszSrcFile, Path);
-		    wcscat_s(wszSrcFile, findDataArray[i].cFileName);
-			wcscpy_s( tszDestFile, TEXT("mods.d\\"));
-			wcscat_s( tszDestFile, findDataArray[i].cFileName);
-		    hSrc = CeCreateFile(wszSrcFile, GENERIC_READ, FILE_SHARE_READ,
-                NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
-			if (INVALID_HANDLE_VALUE == hSrc) {
-				MessageBox::Show("Sorry, I'm struggling to open a file on your device.", "Process Failed",
-				MessageBoxButtons::OK);
-				Application::Exit();
-			}
-			hDest = CreateFile(tszDestFile, GENERIC_WRITE, FILE_SHARE_READ,
-                NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
-			if (INVALID_HANDLE_VALUE == hDest) {
-				MessageBox::Show("Sorry, I can't seem to write files to your PC.  Do you have a mods.d directory where you put me?", "Process Failed",
-				MessageBoxButtons::OK);
-				Application::Exit();
-			}	
-			if (CeReadFile(hSrc, &Buffer, sizeof(Buffer), &dwNumRead, NULL)) {
-				if (!WriteFile(hDest, &Buffer, dwNumRead, &dwNumWritten, NULL)) {
-					MessageBox::Show("Sorry, I can't seem to read the files on your mobile device.", "Process Failed",
-							MessageBoxButtons::OK);
-					Application::Exit();
-				}
-			}
-			else {
-				MessageBox::Show("Sorry, I can't read the files on your mobile device.", "Process Failed",
-					MessageBoxButtons::OK);
-				Application::Exit();
-			}
+			copyconfs(findDataArray[i].cFileName);
 		} // else if ((stristr(wstrtostr ......
-		// no longer pointing to a mods.d directory, so close file handles
-		if (hSrc){
-			CeCloseHandle(hSrc);
-			hSrc = NULL;
-		}
-		if (hDest){
-			CloseHandle (hDest);
-			hDest = NULL;
-		}
 	} // for(UINT i = 0; ........
 	if (findDataArray)
         RapiFreeBuffer(findDataArray);
@@ -167,50 +173,131 @@
 	}
 }
 
-// delete function still to be written
-	/*
-	// here is a method to use for removing a single file
-	char FileToDelete[]= "mods.d\\gill.conf";
-	char *f2del = FileToDelete;
-	String^ ptr = System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)f2del); 
-	// I don't have a clue how the above line works, but it does!
-	File::Delete(ptr);
-	*/
+bool fileExists(char *thisFile)
+{
+	//char *thisFile = "sword.exe";
+	std::fstream fin;
+	fin.open(thisFile, std::ios::in);
+	if( fin.is_open()) {
+		fin.close();
+		return true;
+  }
+  fin.close();
+  return false;
+}
 
 
+void getModulePath (LPCWSTR confName, char &outBuffer) {
+	// each .conf file in mods.d comes here with name
+	char longstr[10000];
+	const char *pathstr;
+	char mPath[MAX_PATH];
+	char *modulePath = mPath;
+	BOOL isopen = FALSE;
+	HANDLE modFile;
+	DWORD BytesToRead;
+	DWORD BytesRead = 0;
+	WCHAR namstr[MAX_PATH];
+	WCHAR* name = namstr;
+	wcscpy (name, TEXT("mods.d\\"));
+	wcscat (name, confName);
+	BytesToRead = sizeof(longstr)-1;
+	modFile = CreateFile(name,  GENERIC_READ, FILE_SHARE_READ,
+			NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+	if (INVALID_HANDLE_VALUE == modFile) {
+		MessageBox::Show("Sorry, I can't seem to read files in your mods.d directory.  Do you have a mods.d directory where you put me?", "Process Failed",
+		MessageBoxButtons::OK);
+		Application::Exit();
+		}	
+	// read the entire .conf file into our buffer - biggest I know of
+	// now is a 5k .conf, so I've gone for a 10,000 byte buffer ....
+	isopen = ReadFile(modFile, &longstr, BytesToRead, &BytesRead, NULL);
+	// we ought to check for success 'if isopen' and do something
+	if (modFile){
+		CloseHandle(modFile);
+		modFile = NULL;
+	}
+	pathstr = stristr (longstr, "DataPath=.");
+	if (!pathstr){
+		MessageBox::Show("Failed to make string match for DataPath=.", "Leaving now",
+			MessageBoxButtons::OK);
+			Application::Exit();
+	}
+	pathstr+=10;  // move pointer to end of 'DataPath=.'
+	strcpy (modulePath, "");// this will need to be the swordPath
+	int i = 0;
+	int p = (strlen(modulePath));
+	p--;
+	while (*(pathstr+i) != 10 && *(pathstr+i) != 13) { // some confs have cr/lf and some only lf (why?)
+		*(modulePath+p++) = *(pathstr+i);
+		i++;
+	}
+//		--p;  // if we don't want trailing slash
+	*(modulePath+p) = 0; // at end of trailing slash in pathstring
+	// TODO - lexdicts are handled differently in pathing 
+	char *tmp = modulePath;
+	while (*(tmp++))if (*tmp == 47) *tmp = 92; // change '/' into '\'
+	tmp = &outBuffer;
+	strcpy (tmp, modulePath);
+}
+
+void deleteCEFiles (LPCWSTR CEPath) { // delete all files in mobile device directory then delete directory
+	TCHAR ThisFile[MAX_PATH]; 
+	DWORD foundCount;
+    LPCE_FIND_DATA findDataArray;
+
+    wcscpy_s(ThisFile, CEPath);
+    wcscat_s(ThisFile, L"*");	
+    if(!CeFindAllFiles(ThisFile, FAF_ATTRIBUTES | FAF_NAME, &foundCount, &findDataArray)) {
+	//	_tprintf( TEXT("*** CeFindAllFiles failed. ***\n"));
+		return;
+// TODO needs error MessageBox here and bomb out 
+	}
+    if(!foundCount) return;
+// TODO needs error MessageBox here and bomb out
+    for(UINT i = 0; i < foundCount; i++) {
+			WCHAR newPath[MAX_PATH];
+			wcscpy_s(newPath, CEPath);
+			wcscat_s(newPath, findDataArray[i].cFileName);
+			CeDeleteFile(newPath);
+	} // for(UINT i = 0; i </ 
+	// now delete the directory
+	CeRemoveDirectory(CEPath);
+}
+
+
 const char* doInstall() {	
 	char swPath[MAX_PATH];
 	const char* swordPathString = swPath;
-	HANDLE modFile;
 	HANDLE hFile;                       // Handle to directory
 	HANDLE modDir;
 	WIN32_FIND_DATA FileInformation;    // File information
 	WIN32_FIND_DATA modFileInformation;    // File information
-	DWORD BytesToRead;
-	DWORD BytesRead = 0;
-	BOOL isopen = FALSE;
-	WCHAR namstr[MAX_PATH];
-	WCHAR* name = namstr;
 	char mPath[MAX_PATH];
 	char *modulePath = mPath;
 	char m2Path[MAX_PATH];
 	char *modName = m2Path;
-	char longstr[10000];
-	LPVOID lpBuffer = longstr;
 	TCHAR wszSrcFile[MAX_PATH];
 	WCHAR tszCEDestFile[MAX_PATH];
-	const char *pathstr;
+	WCHAR* oldConfs[1000];  // array of pointers to oldConf files before Install Manager
+	WCHAR* newConfs[1000];  // array of pointers to Conf files after Install Manager session
+	if (fileExists("Sword.exe")) {
+		MessageBox::Show("I'm shutting down because there is a copy \
+of sword.exe in this directory.  You must NOT run this installer \
+in your Sword directory.", "Process Failed", MessageBoxButtons::OK);
+		Application::Exit();
+		}
     RAPIINIT ri = { sizeof(RAPIINIT) };
 	if ( SUCCEEDED(CeRapiInitEx(&ri))) {
 	// wait for 10 seconds for the connection...
 	if ( (WaitForSingleObject(ri.heRapiInit, 10000) == WAIT_OBJECT_0) && 
 		SUCCEEDED(ri.hrRapiInit) ) {
+//deleteCEFiles (TEXT ("\\Storage Card\\sword\\modules\\texts\\rawtext\\phillips\\")); // test only REMOVE
 		// CreateDirectory does not have a problem if the directory 
 		// named exists already
 		Directory::CreateDirectory( "mods.d" );
 		Directory::CreateDirectory( "modules" );
 		TCHAR* swordPath = PrintDirectory( L"\\", 0);
-		// CeRapiUninit(); 
 		if (*swordPath != NULL)
 			swordPathString = wstrtostr(swordPath);
 		// we now have TCHAR *swordPath and char *swordPathString 
@@ -219,15 +306,42 @@
 		// of all the mods.d files that are on the mobile device
 		// plus we definitely have a mods.d and modules directory as 
 		// these are not checked for by InstallManager (It bombs out!).
+		// now put all the mods.d names into an array
+		hFile = FindFirstFile(TEXT("mods.d\\*.conf"), &FileInformation);
+		if(hFile == INVALID_HANDLE_VALUE) {
+				MessageBox::Show("Sorry, I can't seem to open \
+the files in local mods.d directory", "Process Failed",
+						MessageBoxButtons::OK);
+				Application::Exit();
+				}
+		// START this pass ok 
+	int n=0;
+		do {
+	// store the conf names (LPCWSTR FileInformation.cFileName) in an array
+    int len = wcslen(FileInformation.cFileName) + 1;    // how much space is needed
+	TCHAR *newSpace = new WCHAR[len]; // allocate with new
+// copy to new space
+	wcscpy_s (newSpace, len, FileInformation.cFileName);
+    oldConfs[n] = newSpace;            // save pointer
+    n++;
+		}while(FindNextFile(hFile, &FileInformation) == TRUE);
+	oldConfs[n] = 0;
+		if (hFile){
+			FindClose(hFile);
+			hFile = NULL;
+		}
+		// END this pass
 		STARTUPINFO si;
 		PROCESS_INFORMATION pi;
 		ZeroMemory( &si, sizeof(si) );
 		si.cb = sizeof(si);
 		ZeroMemory( &pi, sizeof(pi) );
 		// Start the child process - InstallManager. 
-		if( !CreateProcess( TEXT("InstallManager.exe"), TEXT("InstallManager.exe"), // Command line. 
+		if( !CreateProcess( TEXT("InstallManager.exe"), TEXT(\
+"Install Manager.exe"), // Command line. 
 			NULL, NULL, FALSE,	0, NULL, NULL, &si, &pi )) {
-				MessageBox::Show("Sorry, I could not activate InstallManager", "Process Failed",
+				MessageBox::Show("Sorry, I could not activate \
+InstallManager", "Process Failed",
 						MessageBoxButtons::OK);
 				Application::Exit();
 			}
@@ -236,52 +350,20 @@
 		// Close process and thread handles. 
 		CloseHandle( pi.hProcess );
 		CloseHandle( pi.hThread );
-
+		n=0;
 		hFile = FindFirstFile(TEXT("mods.d\\*.conf"), &FileInformation);
 		if(hFile != INVALID_HANDLE_VALUE)
 		{
-		do {
-			// each .conf file in mods.d comes here with name etc
-			wcscpy ( name, TEXT("mods.d\\"));
-			wcscat (name, FileInformation.cFileName);
-			BytesToRead = sizeof(longstr)-1;
-			modFile = CreateFile(name,  GENERIC_READ, FILE_SHARE_READ,
-					NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
-			if (INVALID_HANDLE_VALUE == modFile) {
-				MessageBox::Show("Sorry, I can't seem to read files in your mods.d directory.  Do you have a mods.d directory where you put me?", "Process Failed",
-				MessageBoxButtons::OK);
-				Application::Exit();
-				return(NULL);
-				}	
-			// read the entire .conf file into our buffer - biggest I know of
-			// now is a 5k .conf, so I've gone for a 10,000 byte buffer ....
-
-			isopen = ReadFile(modFile, &longstr, BytesToRead, &BytesRead, NULL);
-			// we ought to check for success 'if isopen' and do something
-			if (modFile){
-				CloseHandle(modFile);
-				modFile = NULL;
-			}
-			pathstr = stristr (longstr, "DataPath=.");
-			if (!pathstr){
-				MessageBox::Show("Failed to make string match for DataPath=.", "Leaving now",
-					MessageBoxButtons::OK);
-					Application::Exit();
-			}
-			pathstr+=10;  // move pointer to end of 'DataPath=.'
-			strcpy (modulePath, "");// this will need to be the swordPath
-			int i = 0;
-			int p = (strlen(modulePath));
-			p--;
-			while (*(pathstr+i) != 10 && *(pathstr+i) != 13) { // some confs have cr/lf and some only lf (why?)
-				*(modulePath+p++) = *(pathstr+i);
-				i++;
-			}
-	//		--p;  // if we don't want trailing slash
-			*(modulePath+p) = 0; // at end of trailing slash in pathstring
-			// TODO - lexdicts are handled differently in pathing 
-			char *tmp = modulePath;
-			while (*(tmp++))if (*tmp == 47) *tmp = 92; // change '/' into '\'
+		do { // each .conf file in mods.d comes here with name etc
+		// START this pass *NOT* ok 
+			int len = wcslen(FileInformation.cFileName) + 1;    // how much space is needed
+			TCHAR *newSpace = new WCHAR[len]; // allocate with new
+		// copy to new space
+			wcscpy_s (newSpace, len, FileInformation.cFileName);
+			newConfs[n] = newSpace;            // save pointer
+			n++;
+			// END this pass
+			getModulePath (FileInformation.cFileName, *mPath);
 			String^ ptr = System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)modulePath);
 			// I don't have a clue how the above line works, but it does!
 			if (Directory::Exists(ptr)) {  
@@ -293,7 +375,8 @@
 				::MultiByteToWideChar(CP_ACP, NULL, modulePath, -1, wText,len);
 				modDir = FindFirstFile(wText, &modFileInformation);
 				if(modDir == INVALID_HANDLE_VALUE){
-					MessageBox::Show("Sorry, I can't seem to read files from your newly downloaded module.", "Process Failed",
+					MessageBox::Show("Sorry, I can't seem to read files \
+from your newly downloaded module.", "Process Failed",
 					MessageBoxButtons::OK);
 					Application::Exit();
 				}							
@@ -331,15 +414,26 @@
 				}
 				// now copy over the module .conf file
 				wcscpy_s (tszCEDestFile, swordPath);
-				wcscat_s (tszCEDestFile, name);
-				CopyOver (name, tszCEDestFile);
+				wcscat_s (tszCEDestFile, TEXT("mods.d\\"));
+				wcscat_s (tszCEDestFile, FileInformation.cFileName);
+				wcscpy_s (wszSrcFile, TEXT("mods.d\\"));
+				wcscat_s (wszSrcFile, FileInformation.cFileName);
+				CopyOver (wszSrcFile, tszCEDestFile);
+
 			} // if (Directory::Exists(ptr		
 		  }while(FindNextFile(hFile, &FileInformation) == TRUE);
-		}		
-		if (modFile){
-			CloseHandle(modFile);
-			modFile = NULL;
-		}
+		newConfs[n] = 0;
+		} 
+		/*
+		// save this code - it shows char string in MessageBox
+		// debug only code
+n = -1;
+while (newConfs[n++]) {
+			String^ ptr = System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)(wstrtostr(newConfs[n])));
+MessageBox::Show(ptr, "This is the new .conf file", MessageBoxButtons::OK);
+}
+		// end debug code
+		*/
 		if (modDir){
 			FindClose (modDir);
 			modDir = NULL;
@@ -355,21 +449,19 @@
 		return (swordPathString);
 			} // if ( (WaitForSingleObject(ri.heRapiInit .....
     else {
-		MessageBox::Show("Sorry, I could not connect to your mobile device", "CeRapiInit failed",
-			MessageBoxButtons::OK);
+		MessageBox::Show("Sorry, I could not connect to your mobile \
+device", "CeRapiInit failed", MessageBoxButtons::OK);
 		CeRapiUninit();
 		Application::Exit();
 		}
-    // Application::Exit();
-    if ( CeRapiInit() != E_FAIL ) {
+     if ( CeRapiInit() != E_FAIL ) {
 		}
     else {
-		MessageBox::Show("Sorry, I could not connect to your mobile device", "CeRapiInit failed",
-			MessageBoxButtons::OK);
+		MessageBox::Show("Sorry, I could not connect to your mobile \
+device", "CeRapiInit failed", MessageBoxButtons::OK);
 		CeRapiUninit();
 		Application::Exit();
 		}	
-	// Application::Exit();
 	} // if ( SUCCEEDED(CeRapiInitEx
 Application::Exit();
 return(0); // can't get here - but avoids warning




More information about the sword-cvs mailing list