Back to index

lightning-sunbird  0.9+nobinonly
InstallCleanupOS2.cpp
Go to the documentation of this file.
00001 
00002 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00003  *
00004  * ***** BEGIN LICENSE BLOCK *****
00005  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00006  *
00007  * The contents of this file are subject to the Mozilla Public License Version
00008  * 1.1 (the "License"); you may not use this file except in compliance with
00009  * the License. You may obtain a copy of the License at
00010  * http://www.mozilla.org/MPL/
00011  *
00012  * Software distributed under the License is distributed on an "AS IS" basis,
00013  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00014  * for the specific language governing rights and limitations under the
00015  * License.
00016  *
00017  * The Original Code is Mozilla Communicator client code.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   Don Bragg <dbragg@netscape.com>
00026  *   IBM Corp.
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either of the GNU General Public License Version 2 or later (the "GPL"),
00030  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 
00042 #include "InstallCleanup.h"
00043 #include "InstallCleanupDefines.h"
00044 #define INCL_DOSERRORS
00045 #define INCL_DOS
00046 #include <os2.h>
00047 #include <string.h>
00048 #include <sys/stat.h>
00049 //----------------------------------------------------------------------------
00050 // Native OS/2 file deletion function
00051 //----------------------------------------------------------------------------
00052 int NativeDeleteFile(const char* aFileToDelete)
00053 {
00054     struct stat st;
00055     if (stat(aFileToDelete, &st) != 0)
00056     {
00057       return DONE;// No file to delete, do nothing.
00058     }
00059     else 
00060     {
00061         if(DosDelete(aFileToDelete) != NO_ERROR)
00062           return TRY_LATER;
00063     }
00064     return DONE;
00065 }
00066 
00067 //----------------------------------------------------------------------------
00068 // Native OS/2 file replacement function
00069 //----------------------------------------------------------------------------
00070 int NativeReplaceFile(const char* replacementFile, const char* doomedFile )
00071 {
00072     // replacement file must exist, doomed file doesn't have to
00073     struct stat st;
00074     if (stat(replacementFile, &st) != 0)
00075         return DONE;
00076 
00077     // don't have to do anything if the files are the same
00078     if (stricmp(replacementFile, doomedFile) == 0)
00079         return DONE;
00080 
00081     if (DosDelete(doomedFile) != NO_ERROR)
00082     {
00083         if (stat(doomedFile, &st) == 0)
00084             return TRY_LATER;
00085     }
00086     
00087     // doomedFile is gone move new file into place
00088     if (DosMove(replacementFile, doomedFile) != NO_ERROR)
00089         return TRY_LATER; // this shouldn't happen
00090 
00091     return DONE;
00092 }
00093 
00094 int main(int argc, char *argv[], char *envp[])
00095 {
00096     HREG  reg;
00097     BOOL foundSpecialFile = FALSE;
00098     struct stat st;
00099 
00100     int status = DONE;
00101 
00102     if ( REGERR_OK == NR_StartupRegistry())
00103     {
00104         char regFilePath[CCHMAXPATH];
00105 
00106         strcpy(regFilePath, argv[0]);
00107         char* lastSlash = strrchr(regFilePath, '\\');
00108         if (lastSlash) {
00109           // path does contain backslash
00110           lastSlash++;
00111           *lastSlash = 0; // strip off the executable name
00112         } else {
00113           // no backslash in path, just strip off everything
00114           *regFilePath = 0;
00115         }
00116         strcat(regFilePath, CLEANUP_REGISTRY); // append reg file name
00117     
00118         if (stat(regFilePath, &st) != 0)
00119           strcpy(regFilePath, ""); // an empty reg file tells RegOpen to get the "default"
00120         else
00121           foundSpecialFile = TRUE;
00122 
00123         if ( REGERR_OK == NR_RegOpen(regFilePath, &reg) )
00124         {
00125             int numAttempts = 0;
00126             do 
00127             {
00128                 status = PerformScheduledTasks(reg);
00129                 if (status != DONE) {
00130                     DosSleep(1000); // Sleep for 1 second
00131                     numAttempts++;
00132                 }
00133             } while ((status == TRY_LATER) && numAttempts <= 5);
00134 
00135             NR_RegClose(&reg);
00136         }
00137         NR_ShutdownRegistry();
00138         if (status == DONE) {
00139             DosDelete(regFilePath);
00140         }
00141     }
00142     return(0);
00143 }
00144