Back to index

lightning-sunbird  0.9+nobinonly
InstallCleanupWin.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Navigator.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corp.
00018  * Portions created by the Initial Developer are Copyright (C) 1998
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Don Bragg <dbragg@netscape.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "InstallCleanup.h"
00039 #include "InstallCleanupDefines.h"
00040 #include <windows.h>
00041 #include <string.h>
00042 //----------------------------------------------------------------------------
00043 // Native Windows file deletion function
00044 //----------------------------------------------------------------------------
00045 int NativeDeleteFile(const char* aFileToDelete)
00046 {
00047     if (GetFileAttributes(aFileToDelete) == 0xFFFFFFFF)
00048     {
00049       return DONE;// No file to delete, do nothing.
00050     }
00051     else 
00052     {
00053         if(!DeleteFile(aFileToDelete))
00054           return TRY_LATER;
00055     }
00056     return DONE;
00057 }
00058 
00059 //----------------------------------------------------------------------------
00060 // Native Windows file replacement function
00061 //----------------------------------------------------------------------------
00062 int NativeReplaceFile(const char* replacementFile, const char* doomedFile )
00063 {
00064     // replacement file must exist, doomed file doesn't have to
00065     if (GetFileAttributes(replacementFile) == 0xFFFFFFFF)
00066         return DONE;
00067 
00068     // don't have to do anything if the files are the same
00069     if (CompareString(LOCALE_SYSTEM_DEFAULT,
00070                       NORM_IGNORECASE | SORT_STRINGSORT,
00071                       replacementFile, -1,
00072                       doomedFile, -1) == CSTR_EQUAL)
00073         return DONE;
00074 
00075     if (!DeleteFile(doomedFile))
00076     {
00077         if (GetFileAttributes(doomedFile) != 0xFFFFFFFF) // file exists          
00078             return TRY_LATER;
00079     }
00080     
00081     // doomedFile is gone move new file into place
00082     if (!MoveFile(replacementFile, doomedFile))
00083         return TRY_LATER; // this shouldn't happen
00084 
00085     return DONE;
00086 }
00087 
00088 
00089 int WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR args, int)
00090 {
00091     HREG  reg;
00092     HKEY  hkRunOnceHandle;
00093     DWORD dwDisp;
00094     bool foundSpecialFile = FALSE;
00095 
00096     int status = DONE;
00097 
00098     if ( REGERR_OK == NR_StartupRegistry())
00099     {
00100         DWORD charsWritten;
00101         char appPath[_MAX_PATH];
00102         charsWritten = GetModuleFileName(NULL, appPath, _MAX_PATH);
00103         if (charsWritten > 0)
00104         {
00105             char regFilePath[_MAX_PATH];
00106             
00107             strcpy(regFilePath, appPath);
00108             char* lastSlash = strrchr(regFilePath, '\\');
00109             if (lastSlash) {
00110               // path does contain backslash
00111               lastSlash++;
00112               *lastSlash = 0; // strip off the executable name
00113             } else {
00114               // no backslash in path, just strip off everything
00115               *regFilePath = 0;
00116             }
00117             strcat(regFilePath, CLEANUP_REGISTRY); //append reg file name
00118     
00119             if ( GetFileAttributes(regFilePath) == 0xFFFFFFFF ) // file doesn't exist
00120               strcpy(regFilePath, ""); // an empty reg file tells RegOpen to get the "default"
00121             else
00122               foundSpecialFile = TRUE;
00123 
00124             if ( REGERR_OK == NR_RegOpen(regFilePath, &reg) )
00125             {
00126                 RegCreateKeyEx(HKEY_CURRENT_USER,
00127                                "Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce",
00128                                0,
00129                                NULL,
00130                                REG_OPTION_NON_VOLATILE,
00131                                KEY_WRITE,
00132                                NULL,
00133                                &hkRunOnceHandle,
00134                                &dwDisp);
00135                 
00136                 LPCTSTR cleanupKeyName = "mozilla_cleanup";
00137             
00138                 RegSetValueEx(hkRunOnceHandle,
00139                               cleanupKeyName,
00140                               0,
00141                               REG_SZ,
00142                               (const unsigned char*)appPath,
00143                               strlen(appPath));
00144 
00145                 do 
00146                 {
00147                     status = PerformScheduledTasks(reg);
00148                     if (status != DONE)
00149                         Sleep(1000); // Sleep for 1 second
00150                 } while (status == TRY_LATER);
00151 
00152                 RegDeleteValue(hkRunOnceHandle, cleanupKeyName);
00153                 NR_RegClose(&reg);
00154             }
00155             NR_ShutdownRegistry();
00156             DeleteFile(regFilePath);
00157         }
00158     }
00159     return(0);
00160 }
00161