1 | /* |
---|
2 | * The contents of this file are subject to the Mozilla Public |
---|
3 | * License Version 1.1 (the "License"); you may not use this file |
---|
4 | * except in compliance with the License. You may obtain a copy of |
---|
5 | * the License at http://www.mozilla.org/MPL/ |
---|
6 | * |
---|
7 | * Software distributed under the License is distributed on an "AS |
---|
8 | * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
---|
9 | * implied. See the License for the specific language governing |
---|
10 | * rights and limitations under the License. |
---|
11 | * |
---|
12 | * The Original Code is Mozilla Navigator. |
---|
13 | * |
---|
14 | * The Initial Developer of the Original Code is Netscape Communications |
---|
15 | * Corp. Portions created by Netscape Communications Corp. are |
---|
16 | * Copyright (C) 1998, 1999, 2000, 2001 Netscape Communications Corp. All |
---|
17 | * Rights Reserved. |
---|
18 | * |
---|
19 | * Contributor(s): |
---|
20 | * Don Bragg <dbragg@netscape.com> |
---|
21 | */ |
---|
22 | |
---|
23 | #include "InstallCleanup.h" |
---|
24 | #include "InstallCleanupDefines.h" |
---|
25 | #include <windows.h> |
---|
26 | #include <string.h> |
---|
27 | //---------------------------------------------------------------------------- |
---|
28 | // Native Windows file deletion function |
---|
29 | //---------------------------------------------------------------------------- |
---|
30 | int NativeDeleteFile(const char* aFileToDelete) |
---|
31 | { |
---|
32 | if (GetFileAttributes(aFileToDelete) == 0xFFFFFFFF) |
---|
33 | { |
---|
34 | return DONE;// No file to delete, do nothing. |
---|
35 | } |
---|
36 | else |
---|
37 | { |
---|
38 | if(!DeleteFile(aFileToDelete)) |
---|
39 | return TRY_LATER; |
---|
40 | } |
---|
41 | return DONE; |
---|
42 | } |
---|
43 | |
---|
44 | //---------------------------------------------------------------------------- |
---|
45 | // Native Windows file replacement function |
---|
46 | //---------------------------------------------------------------------------- |
---|
47 | int NativeReplaceFile(const char* replacementFile, const char* doomedFile ) |
---|
48 | { |
---|
49 | // replacement file must exist, doomed file doesn't have to |
---|
50 | if (GetFileAttributes(replacementFile) == 0xFFFFFFFF) |
---|
51 | return DONE; |
---|
52 | |
---|
53 | // don't have to do anything if the files are the same |
---|
54 | if (CompareString(LOCALE_SYSTEM_DEFAULT, |
---|
55 | NORM_IGNORECASE | SORT_STRINGSORT, |
---|
56 | replacementFile, -1, |
---|
57 | doomedFile, -1) == CSTR_EQUAL) |
---|
58 | return DONE; |
---|
59 | |
---|
60 | if (!DeleteFile(doomedFile)) |
---|
61 | { |
---|
62 | if (GetFileAttributes(doomedFile) != 0xFFFFFFFF) // file exists |
---|
63 | return TRY_LATER; |
---|
64 | } |
---|
65 | |
---|
66 | // doomedFile is gone move new file into place |
---|
67 | if (!MoveFile(replacementFile, doomedFile)) |
---|
68 | return TRY_LATER; // this shouldn't happen |
---|
69 | |
---|
70 | return DONE; |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | int WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR args, int) |
---|
75 | { |
---|
76 | HREG reg; |
---|
77 | HKEY hkRunOnceHandle; |
---|
78 | DWORD dwDisp; |
---|
79 | bool foundSpecialFile = FALSE; |
---|
80 | |
---|
81 | int status = DONE; |
---|
82 | |
---|
83 | if ( REGERR_OK == NR_StartupRegistry()) |
---|
84 | { |
---|
85 | DWORD charsWritten; |
---|
86 | char appPath[_MAX_PATH]; |
---|
87 | charsWritten = GetModuleFileName(NULL, appPath, _MAX_PATH); |
---|
88 | if (charsWritten > 0) |
---|
89 | { |
---|
90 | char regFilePath[_MAX_PATH]; |
---|
91 | |
---|
92 | strcpy(regFilePath, appPath); |
---|
93 | char* lastSlash = strrchr(regFilePath, '\\'); |
---|
94 | lastSlash++; |
---|
95 | *lastSlash = 0;//strip of the executable name |
---|
96 | strcat(regFilePath, CLEANUP_REGISTRY); //append reg file name |
---|
97 | |
---|
98 | if ( GetFileAttributes(regFilePath) == 0xFFFFFFFF ) // file doesn't exist |
---|
99 | strcpy(regFilePath, ""); // an empty reg file tells RegOpen to get the "default" |
---|
100 | else |
---|
101 | foundSpecialFile = TRUE; |
---|
102 | |
---|
103 | if ( REGERR_OK == NR_RegOpen(regFilePath, ®) ) |
---|
104 | { |
---|
105 | RegCreateKeyEx(HKEY_CURRENT_USER, |
---|
106 | "Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce", |
---|
107 | 0, |
---|
108 | NULL, |
---|
109 | REG_OPTION_NON_VOLATILE, |
---|
110 | KEY_WRITE, |
---|
111 | NULL, |
---|
112 | &hkRunOnceHandle, |
---|
113 | &dwDisp); |
---|
114 | |
---|
115 | LPCTSTR cleanupKeyName = "mozilla_cleanup"; |
---|
116 | |
---|
117 | RegSetValueEx(hkRunOnceHandle, |
---|
118 | cleanupKeyName, |
---|
119 | 0, |
---|
120 | REG_SZ, |
---|
121 | (const unsigned char*)appPath, |
---|
122 | strlen(appPath)); |
---|
123 | |
---|
124 | do |
---|
125 | { |
---|
126 | status = PerformScheduledTasks(reg); |
---|
127 | if (status != DONE) |
---|
128 | Sleep(1000); // Sleep for 1 second |
---|
129 | } while (status == TRY_LATER); |
---|
130 | |
---|
131 | RegDeleteValue(hkRunOnceHandle, cleanupKeyName); |
---|
132 | NR_RegClose(®); |
---|
133 | } |
---|
134 | NR_ShutdownRegistry(); |
---|
135 | DeleteFile(regFilePath); |
---|
136 | } |
---|
137 | } |
---|
138 | return(0); |
---|
139 | } |
---|
140 | |
---|