Sync with trunk (r48545)
[reactos.git] / dll / win32 / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "wine/unicode.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
42
43 static BOOL check_language(DWORD lang1, LPCWSTR lang2, DWORD attributes)
44 {
45 DWORD langdword;
46
47 if (!lang2 || lang2[0]==0)
48 return TRUE;
49
50 langdword = atoiW(lang2);
51
52 if (attributes & msidbUpgradeAttributesLanguagesExclusive)
53 return (lang1 != langdword);
54 else
55 return (lang1 == langdword);
56 }
57
58 static void append_productcode(MSIPACKAGE* package, LPCWSTR action_property,
59 LPCWSTR productid)
60 {
61 LPWSTR prop;
62 LPWSTR newprop;
63 DWORD len;
64 UINT r;
65
66 prop = msi_dup_property(package->db, action_property );
67 if (prop)
68 len = strlenW(prop);
69 else
70 len = 0;
71
72 /*separator*/
73 len ++;
74
75 len += strlenW(productid);
76
77 /*null*/
78 len++;
79
80 newprop = msi_alloc( len*sizeof(WCHAR) );
81
82 if (prop)
83 {
84 strcpyW(newprop,prop);
85 strcatW(newprop,szSemiColon);
86 }
87 else
88 newprop[0] = 0;
89 strcatW(newprop,productid);
90
91 r = msi_set_property( package->db, action_property, newprop );
92 if (r == ERROR_SUCCESS && !strcmpW( action_property, cszSourceDir ))
93 msi_reset_folders( package, TRUE );
94
95 TRACE("Found Related Product... %s now %s\n",
96 debugstr_w(action_property), debugstr_w(newprop));
97
98 msi_free( prop );
99 msi_free( newprop );
100 }
101
102 static UINT ITERATE_FindRelatedProducts(MSIRECORD *rec, LPVOID param)
103 {
104 MSIPACKAGE *package = param;
105 WCHAR product[GUID_SIZE];
106 DWORD index = 0;
107 DWORD attributes = 0;
108 DWORD sz = GUID_SIZE;
109 LPCWSTR upgrade_code;
110 HKEY hkey = 0;
111 UINT rc = ERROR_SUCCESS;
112 MSIRECORD *uirow;
113
114 upgrade_code = MSI_RecordGetString(rec,1);
115
116 rc = MSIREG_OpenUpgradeCodesKey(upgrade_code, &hkey, FALSE);
117 if (rc != ERROR_SUCCESS)
118 return ERROR_SUCCESS;
119
120 uirow = MSI_CreateRecord(1);
121 attributes = MSI_RecordGetInteger(rec,5);
122
123 while (rc == ERROR_SUCCESS)
124 {
125 rc = RegEnumValueW(hkey, index, product, &sz, NULL, NULL, NULL, NULL);
126 TRACE("Looking at (%i) %s\n",index,debugstr_w(product));
127 if (rc == ERROR_SUCCESS)
128 {
129 WCHAR productid[GUID_SIZE];
130 LPCWSTR ver;
131 LPCWSTR language;
132 LPCWSTR action_property;
133 DWORD check = 0x00000000;
134 DWORD comp_ver = 0x00000000;
135 DWORD sz = 0x100;
136 HKEY hukey;
137 INT r;
138
139 unsquash_guid(product, productid);
140 rc = MSIREG_OpenProductKey(productid, NULL, package->Context,
141 &hukey, FALSE);
142 if (rc != ERROR_SUCCESS)
143 {
144 rc = ERROR_SUCCESS;
145 index ++;
146 continue;
147 }
148
149 sz = sizeof(DWORD);
150 RegQueryValueExW(hukey, INSTALLPROPERTY_VERSIONW, NULL, NULL,
151 (LPBYTE)&check, &sz);
152 /* check min */
153 ver = MSI_RecordGetString(rec,2);
154 if (ver)
155 {
156 comp_ver = msi_version_str_to_dword(ver);
157 r = check - comp_ver;
158 if (r < 0 || (r == 0 && !(attributes & msidbUpgradeAttributesVersionMinInclusive)))
159 {
160 RegCloseKey(hukey);
161 index ++;
162 continue;
163 }
164 }
165
166 /* check max */
167 ver = MSI_RecordGetString(rec,3);
168 if (ver)
169 {
170 comp_ver = msi_version_str_to_dword(ver);
171 r = check - comp_ver;
172 if (r > 0 || (r == 0 && !(attributes & msidbUpgradeAttributesVersionMaxInclusive)))
173 {
174 RegCloseKey(hukey);
175 index ++;
176 continue;
177 }
178 }
179
180 /* check language*/
181 sz = sizeof(DWORD);
182 RegQueryValueExW(hukey, INSTALLPROPERTY_LANGUAGEW, NULL, NULL,
183 (LPBYTE)&check, &sz);
184 RegCloseKey(hukey);
185 language = MSI_RecordGetString(rec,4);
186 TRACE("Checking languages %x 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 MSI_RecordSetStringW(uirow, 1, productid);
197 ui_actiondata(package, szFindRelatedProducts, uirow);
198 }
199 index ++;
200 }
201 RegCloseKey(hkey);
202 msiobj_release( &uirow->hdr);
203
204 return ERROR_SUCCESS;
205 }
206
207 UINT ACTION_FindRelatedProducts(MSIPACKAGE *package)
208 {
209 static const WCHAR Query[] =
210 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',
211 ' ','`','U','p','g','r','a','d','e','`',0};
212 UINT rc = ERROR_SUCCESS;
213 MSIQUERY *view;
214
215 if (msi_get_property_int(package->db, szInstalled, 0))
216 {
217 TRACE("Skipping FindRelatedProducts action: product already installed\n");
218 return ERROR_SUCCESS;
219 }
220
221 if (check_unique_action(package, szFindRelatedProducts))
222 {
223 TRACE("Skipping FindRelatedProducts action: already done in UI sequence\n");
224 return ERROR_SUCCESS;
225 }
226 else
227 register_unique_action(package, szFindRelatedProducts);
228
229 rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
230 if (rc != ERROR_SUCCESS)
231 return ERROR_SUCCESS;
232
233 rc = MSI_IterateRecords(view, NULL, ITERATE_FindRelatedProducts, package);
234 msiobj_release(&view->hdr);
235
236 return rc;
237 }