[Python-checkins] distutils/misc install.c,1.18,1.19

theller@users.sourceforge.net theller@users.sourceforge.net
Tue, 15 Oct 2002 12:41:26 -0700


Update of /cvsroot/python/distutils/misc
In directory usw-pr-cvs1:/tmp/cvs-serv3730

Modified Files:
	install.c 
Log Message:
Better error handling.

Now requires admin rights for installation and uninstallation,
previously it was possible do this without admin rights, but changing
the registry could fail without proper permissions.


Index: install.c
===================================================================
RCS file: /cvsroot/python/distutils/misc/install.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -C2 -d -r1.18 -r1.19
*** install.c	15 Oct 2002 15:11:45 -0000	1.18
--- install.c	15 Oct 2002 19:41:24 -0000	1.19
***************
*** 1322,1326 ****
  }
  
! static void OpenLogfile(char *dir)
  {
      char buffer[_MAX_PATH+1];
--- 1322,1326 ----
  }
  
! static BOOL OpenLogfile(char *dir)
  {
      char buffer[_MAX_PATH+1];
***************
*** 1329,1336 ****
--- 1329,1355 ----
      long result;
      HKEY hKey, hSubkey;
+     char subkey_name[256];
      static char KeyName[] = 
  	"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
      DWORD disposition;
  
+     result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
+ 			  KeyName,
+ 			  0,
+ 			  KEY_CREATE_SUB_KEY,
+ 			  &hKey);
+     if (result != ERROR_SUCCESS) {
+ 	if (result == ERROR_ACCESS_DENIED) {
+ 	    MessageBox(GetFocus(),
+ 		       "You do not seem to have sufficient access rights\n"
+ 		       "on this machine to install this software",
+ 		       NULL,
+ 		       MB_OK | MB_ICONSTOP);
+ 	    return FALSE;
+ 	} else {
+ 	    MessageBox(GetFocus(), KeyName, "Could not open key", MB_OK);
+ 	}
+     }
+ 
      sprintf(buffer, "%s\\%s-wininst.log", dir, meta_name);
      logfile = fopen(buffer, "a");
***************
*** 1343,1360 ****
      fprintf(logfile, "Source: %s\n", modulename);
  
!     result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
! 			  KeyName,
! 			  0,
! 			  KEY_ALL_ACCESS,
! 			  &hKey);
! 
!     if (result != ERROR_SUCCESS)
! 	MessageBox(GetFocus(), "Could not open key", KeyName, MB_OK);
! 
!     sprintf(buffer, "%s-py%d.%d", meta_name, py_major, py_minor);
  
!     result = RegCreateKeyEx(hKey, buffer,
  			    0, NULL, 0,
! 			    KEY_ALL_ACCESS,
  			    NULL,
  			    &hSubkey,
--- 1362,1370 ----
      fprintf(logfile, "Source: %s\n", modulename);
  
!     sprintf(subkey_name, "%s-py%d.%d", meta_name, py_major, py_minor);
  
!     result = RegCreateKeyEx(hKey, subkey_name,
  			    0, NULL, 0,
! 			    KEY_WRITE,
  			    NULL,
  			    &hSubkey,
***************
*** 1362,1382 ****
  
      if (result != ERROR_SUCCESS)
! 	MessageBox(GetFocus(), "Could not create key", buffer, MB_OK);
  
      RegCloseKey(hKey);
  
      if (disposition == REG_CREATED_NEW_KEY)
! 	fprintf(logfile, "020 Reg DB Key: [%s]%s\n", KeyName, buffer);
  
      sprintf(buffer, "Python %d.%d %s", py_major, py_minor, title);
  
!     RegSetValueEx(hSubkey, "DisplayName",
! 		  0,
! 		  REG_SZ,
! 		  buffer,
! 		  strlen(buffer)+1);
  
!     fprintf(logfile, "040 Reg DB Value: [%s]%s=%s\n",
! 	    KeyName, "DisplayName", buffer);
  
      {
--- 1372,1395 ----
  
      if (result != ERROR_SUCCESS)
! 	MessageBox(GetFocus(), subkey_name, "Could not create key", MB_OK);
  
      RegCloseKey(hKey);
  
      if (disposition == REG_CREATED_NEW_KEY)
! 	fprintf(logfile, "020 Reg DB Key: [%s]%s\n", KeyName, subkey_name);
  
      sprintf(buffer, "Python %d.%d %s", py_major, py_minor, title);
  
!     result = RegSetValueEx(hSubkey, "DisplayName",
! 			   0,
! 			   REG_SZ,
! 			   buffer,
! 			   strlen(buffer)+1);
  
!     if (result != ERROR_SUCCESS)
! 	MessageBox(GetFocus(), buffer, "Could not set key value", MB_OK);
! 
!     fprintf(logfile, "040 Reg DB Value: [%s\\%s]%s=%s\n",
! 	    KeyName, subkey_name, "DisplayName", buffer);
  
      {
***************
*** 1390,1402 ****
  		dir, meta_name, dir, meta_name);
  
! 	RegSetValueEx(hSubkey, "UninstallString",
! 		      0,
! 		      REG_SZ,
! 		      buffer,
! 		      strlen(buffer)+1);
  	
! 	fprintf(logfile, "040 Reg DB Value: [%s]%s=%s\n",
! 		KeyName, "UninstallString", buffer);
      }
  }
  
--- 1403,1419 ----
  		dir, meta_name, dir, meta_name);
  
! 	result = RegSetValueEx(hSubkey, "UninstallString",
! 			       0,
! 			       REG_SZ,
! 			       buffer,
! 			       strlen(buffer)+1);
  	
! 	if (result != ERROR_SUCCESS)
! 	    MessageBox(GetFocus(), buffer, "Could not set key value", MB_OK);
! 
! 	fprintf(logfile, "040 Reg DB Value: [%s\\%s]%s=%s\n",
! 		KeyName, subkey_name, "UninstallString", buffer);
      }
+     return TRUE;
  }
  
***************
*** 1473,1477 ****
  	    python_dir[strlen(python_dir)-1] = '\0';
  	    
! 	    OpenLogfile(python_dir);
  
  /*
--- 1490,1495 ----
  	    python_dir[strlen(python_dir)-1] = '\0';
  	    
! 	    if (!OpenLogfile(python_dir))
! 		break;
  
  /*
***************
*** 1812,1816 ****
  }
  
! void DeleteRegistryKey(char *line)
  {
      char *keyname;
--- 1830,1834 ----
  }
  
! void DeleteRegistryKey(char *string)
  {
      char *keyname;
***************
*** 1818,1821 ****
--- 1836,1843 ----
      char *delim;
      HKEY hKey;
+     long result;
+     char *line;
+ 
+     line = strdup(string); /* so we can change it */
  
      keyname = strchr(line, '[');
***************
*** 1832,1846 ****
  	*delim = '\0';
  
!     RegOpenKeyEx(HKEY_LOCAL_MACHINE,
! 		 keyname,
! 		 0,
! 		 KEY_ALL_ACCESS,
! 		 &hKey);
      
!     RegDeleteKey(hKey, subkeyname);
!     RegCloseKey(hKey);
  }
  
! void DeleteRegistryValue(char *line)
  {
      char *keyname;
--- 1854,1875 ----
  	*delim = '\0';
  
!     result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
! 			  keyname,
! 			  0,
! 			  KEY_WRITE,
! 			  &hKey);
      
!     if (result != ERROR_SUCCESS)
! 	MessageBox(GetFocus(), string, "Could not open key", MB_OK);
!     else {
! 	result = RegDeleteKey(hKey, subkeyname);
! 	if (result != ERROR_SUCCESS)
! 	    MessageBox(GetFocus(), string, "Could not delete key", MB_OK);
! 	RegCloseKey(hKey);
!     }
!     free(line);
  }
  
! void DeleteRegistryValue(char *string)
  {
      char *keyname;
***************
*** 1848,1851 ****
--- 1877,1885 ----
      char *value;
      HKEY hKey;
+     long result;
+     char *line;
+ 
+     line = strdup(string); /* so we can change it */
+ 
  /* Format is 'Reg DB Value: [key]name=value' */
      keyname = strchr(line, '[');
***************
*** 1863,1874 ****
      *value++ = '\0';
  
!     RegOpenKeyEx(HKEY_LOCAL_MACHINE,
! 		 keyname,
! 		 0,
! 		 KEY_ALL_ACCESS,
! 		 &hKey);
! 
!     RegDeleteValue(hKey, valuename);
!     RegCloseKey(hKey);
  }
  
--- 1897,1914 ----
      *value++ = '\0';
  
!     result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
! 			  keyname,
! 			  0,
! 			  KEY_WRITE,
! 			  &hKey);
!     if (result != ERROR_SUCCESS)
! 	MessageBox(GetFocus(), string, "Could not open key", MB_OK);
!     else {
! 	result = RegDeleteValue(hKey, valuename);
! 	if (result != ERROR_SUCCESS)
! 	    MessageBox(GetFocus(), string, "Could not delete value", MB_OK);
! 	RegCloseKey(hKey);
!     }
!     free(line);
  }
  
***************
*** 1980,1983 ****
--- 2020,2045 ----
  		   MB_OK);
  	return 1; /* Error */
+     }
+ 
+     {
+ 	DWORD result;
+ 	HKEY hKey;
+ 	static char KeyName[] = 
+ 	    "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
+ 
+ 	result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
+ 			      KeyName,
+ 			      0,
+ 			      KEY_CREATE_SUB_KEY,
+ 			      &hKey);
+ 	if (result == ERROR_ACCESS_DENIED) {
+ 	    MessageBox(GetFocus(),
+ 		       "You do not seem to have sufficient access rights\n"
+ 		       "on this machine to uninstall this software",
+ 		       NULL,
+ 		       MB_OK | MB_ICONSTOP);
+ 	    return 1; /* Error */
+ 	}
+ 	RegCloseKey(hKey);
      }