7a066ca621aeb99f543328605ad9ca7d043d6178
[reactos.git] / reactos / lib / msi / upgrade.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2005 Aric Stewart for CodeWeavers
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 /*
22 * Actions focused on in this module
23 *
24 * FindRelatedProducts
25 * MigrateFeatureStates (TODO)
26 * RemoveExistingProducts (TODO)
27 */
28
29 #include <stdarg.h>
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winerror.h"
34 #include "winreg.h"
35 #include "wine/debug.h"
36 #include "msidefs.h"
37 #include "msipriv.h"
38 #include "winuser.h"
39 #include "action.h"
40 #include "wine/unicode.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(msi);
43
44 extern const WCHAR szFindRelatedProducts[];
45 extern const WCHAR szMigrateFeatureStates[];
46 extern const WCHAR szRemoveExistingProducts[];
47
48 static BOOL check_language(DWORD lang1, LPCWSTR lang2, DWORD attributes)
49 {
50 DWORD langdword;
51
52 if (!lang2 || lang2[0]==0)
53 return TRUE;
54
55 langdword = atoiW(lang2);
56
57 if (attributes & msidbUpgradeAttributesLanguagesExclusive)
58 return (lang1 != langdword);
59 else
60 return (lang1 == langdword);
61 }
62
63 static void append_productcode(MSIPACKAGE* package, LPCWSTR action_property,
64 LPCWSTR productid)
65 {
66 LPWSTR prop;
67 LPWSTR newprop;
68 DWORD len;
69 static const WCHAR separator[] = {';',0};
70
71 prop = load_dynamic_property(package, action_property, NULL);
72 if (prop)
73 len = strlenW(prop);
74 else
75 len = 0;
76
77 /*separator*/
78 len ++;
79
80 len += strlenW(productid);
81
82 /*null*/
83 len++;
84
85 newprop = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
86
87 if (prop)
88 {
89 strcpyW(newprop,prop);
90 strcatW(newprop,separator);
91 }
92 else
93 newprop[0] = 0;
94 strcatW(newprop,productid);
95
96 MSI_SetPropertyW(package, action_property, newprop);
97 TRACE("Found Related Product... %s now %s\n",debugstr_w(action_property),
98 debugstr_w(newprop));
99 HeapFree(GetProcessHeap(),0,prop);
100 HeapFree(GetProcessHeap(),0,newprop);
101 }
102
103 static UINT ITERATE_FindRelatedProducts(MSIRECORD *rec, LPVOID param)
104 {
105 MSIPACKAGE *package = (MSIPACKAGE*)param;
106 WCHAR product[GUID_SIZE];
107 DWORD index = 0;
108 DWORD attributes = 0;
109 DWORD sz = GUID_SIZE;
110 LPCWSTR upgrade_code;
111 HKEY hkey = 0;
112 UINT rc = ERROR_SUCCESS;
113 MSIRECORD *uirow;
114
115 upgrade_code = MSI_RecordGetString(rec,1);
116
117 rc = MSIREG_OpenUpgradeCodesKey(upgrade_code, &hkey, FALSE);
118 if (rc != ERROR_SUCCESS)
119 return ERROR_SUCCESS;
120
121 uirow = MSI_CreateRecord(1);
122 attributes = MSI_RecordGetInteger(rec,5);
123
124 while (rc == ERROR_SUCCESS)
125 {
126 rc = RegEnumValueW(hkey, index, product, &sz, NULL, NULL, NULL, NULL);
127 TRACE("Looking at (%li) %s\n",index,debugstr_w(product));
128 if (rc == ERROR_SUCCESS)
129 {
130 WCHAR productid[GUID_SIZE];
131 LPCWSTR ver;
132 LPCWSTR language;
133 LPCWSTR action_property;
134 DWORD check = 0x00000000;
135 DWORD comp_ver = 0x00000000;
136 DWORD sz = 0x100;
137 HKEY hukey;
138 INT r;
139 static const WCHAR szVersion[] =
140 {'V','e','r','s','i','o','n',0};
141 static const WCHAR szLanguage[] =
142 {'L','a','n','g','u','a','g','e',0};
143
144 unsquash_guid(product,productid);
145 rc = MSIREG_OpenUserProductsKey(productid, &hukey, FALSE);
146 if (rc != ERROR_SUCCESS)
147 {
148 rc = ERROR_SUCCESS;
149 index ++;
150 continue;
151 }
152
153 sz = sizeof(DWORD);
154 RegQueryValueExW(hukey, szVersion, NULL, NULL, (LPBYTE)&check,
155 &sz);
156 /* check min */
157 ver = MSI_RecordGetString(rec,2);
158 comp_ver = build_version_dword(ver);
159 r = check - comp_ver;
160 if (r < 0 || (r == 0 && !(attributes &
161 msidbUpgradeAttributesVersionMinInclusive)))
162 {
163 RegCloseKey(hukey);
164 index ++;
165 continue;
166 }
167
168 /* check max */
169 ver = MSI_RecordGetString(rec,3);
170 comp_ver = build_version_dword(ver);
171 r = check - comp_ver;
172 if (r > 0 || (r == 0 && !(attributes &
173 msidbUpgradeAttributesVersionMaxInclusive)))
174 {
175 RegCloseKey(hukey);
176 index ++;
177 continue;
178 }
179
180 /* check language*/
181 sz = sizeof(DWORD);
182 RegQueryValueExW(hukey, szLanguage, NULL, NULL, (LPBYTE)&check,
183 &sz);
184 RegCloseKey(hukey);
185 language = MSI_RecordGetString(rec,4);
186 TRACE("Checking languages 0x%lx and %s\n", check,
187 debugstr_w(language));
188 if (!check_language(check, language, attributes))
189 {
190 index ++;
191 continue;
192 }
193
194 action_property = MSI_RecordGetString(rec,7);
195 append_productcode(package,action_property,productid);
196 ui_actiondata(package,szFindRelatedProducts,uirow);
197 }
198 index ++;
199 }
200 RegCloseKey(hkey);
201 msiobj_release( &uirow->hdr);
202
203 return ERROR_SUCCESS;
204 }
205
206 UINT ACTION_FindRelatedProducts(MSIPACKAGE *package)
207 {
208 static const WCHAR Query[] =
209 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',
210 ' ','`','U','p','g','r','a','d','e','`',0};
211 UINT rc = ERROR_SUCCESS;
212 MSIQUERY *view;
213
214 if (package->script && package->script->FindRelatedProductsRun)
215 return ERROR_SUCCESS;
216
217 if (package->script)
218 package->script->FindRelatedProductsRun = TRUE;
219
220 rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
221 if (rc != ERROR_SUCCESS)
222 return ERROR_SUCCESS;
223
224 rc = MSI_IterateRecords(view, NULL, ITERATE_FindRelatedProducts, package);
225 msiobj_release(&view->hdr);
226
227 return rc;
228 }