msi: Rewrite the PublishFeatures test to only check the registry changes.
[wine] / dlls / 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 extern const WCHAR szFindRelatedProducts[];
44 extern const WCHAR szMigrateFeatureStates[];
45 extern const WCHAR szRemoveExistingProducts[];
46
47 static BOOL check_language(DWORD lang1, LPCWSTR lang2, DWORD attributes)
48 {
49     DWORD langdword;
50
51     if (!lang2 || lang2[0]==0)
52         return TRUE;
53
54     langdword = atoiW(lang2);
55
56     if (attributes & msidbUpgradeAttributesLanguagesExclusive)
57         return (lang1 != langdword);
58     else
59         return (lang1 == langdword);
60 }
61
62 static void append_productcode(MSIPACKAGE* package, LPCWSTR action_property,
63                                LPCWSTR productid)
64 {
65     LPWSTR prop;
66     LPWSTR newprop;
67     DWORD len;
68     static const WCHAR separator[] = {';',0};
69
70     prop = msi_dup_property(package, action_property );
71     if (prop)
72         len = strlenW(prop);
73     else
74         len = 0;
75
76     /*separator*/
77     len ++;
78
79     len += strlenW(productid);
80
81     /*null*/
82     len++;
83
84     newprop = msi_alloc( len*sizeof(WCHAR) );
85
86     if (prop)
87     {
88         strcpyW(newprop,prop);
89         strcatW(newprop,separator);
90     }
91     else
92         newprop[0] = 0;
93     strcatW(newprop,productid);
94
95     MSI_SetPropertyW(package, action_property, newprop);
96     TRACE("Found Related Product... %s now %s\n",debugstr_w(action_property),
97                     debugstr_w(newprop));
98     msi_free( prop );
99     msi_free( newprop );
100 }
101
102 static UINT ITERATE_FindRelatedProducts(MSIRECORD *rec, LPVOID param)
103 {
104     MSIPACKAGE *package = (MSIPACKAGE*)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_OpenUserProductsKey(productid, &hukey, FALSE);
141             if (rc != ERROR_SUCCESS)
142             {
143                 rc = ERROR_SUCCESS;
144                 index ++;
145                 continue;
146             }
147           
148             sz = sizeof(DWORD);
149             RegQueryValueExW(hukey, INSTALLPROPERTY_VERSIONW, NULL, NULL,
150                     (LPBYTE)&check, &sz);
151             /* check min */
152             ver = MSI_RecordGetString(rec,2);
153             comp_ver = msi_version_str_to_dword(ver);
154             r = check - comp_ver; 
155             if (r < 0 || (r == 0 && !(attributes &
156                                     msidbUpgradeAttributesVersionMinInclusive)))
157             {
158                 RegCloseKey(hukey);
159                 index ++;
160                 continue;
161             }
162
163             /* check max */
164             ver = MSI_RecordGetString(rec,3);
165             comp_ver = msi_version_str_to_dword(ver);
166             r = check - comp_ver;
167             if (r > 0 || (r == 0 && !(attributes & 
168                                     msidbUpgradeAttributesVersionMaxInclusive)))
169             {
170                 RegCloseKey(hukey);
171                 index ++;
172                 continue;
173             }
174
175             /* check language*/
176             sz = sizeof(DWORD);
177             RegQueryValueExW(hukey, INSTALLPROPERTY_LANGUAGEW, NULL, NULL,
178                     (LPBYTE)&check, &sz);
179             RegCloseKey(hukey);
180             language = MSI_RecordGetString(rec,4);
181             TRACE("Checking languages %x and %s\n", check, 
182                             debugstr_w(language));
183             if (!check_language(check, language, attributes))
184             {
185                 index ++;
186                 continue;
187             }
188
189             action_property = MSI_RecordGetString(rec,7);
190             append_productcode(package,action_property,productid);
191             ui_actiondata(package,szFindRelatedProducts,uirow);
192         }
193         index ++;
194     }
195     RegCloseKey(hkey);
196     msiobj_release( &uirow->hdr);
197     
198     return ERROR_SUCCESS;
199 }
200
201 UINT ACTION_FindRelatedProducts(MSIPACKAGE *package)
202 {
203     static const WCHAR Query[] = 
204         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',
205          ' ','`','U','p','g','r','a','d','e','`',0};
206     UINT rc = ERROR_SUCCESS;
207     MSIQUERY *view;
208
209     if (check_unique_action(package,szFindRelatedProducts))
210     {
211         TRACE("Skipping FindRelatedProducts action: already done on client side\n");
212         return ERROR_SUCCESS;
213     }
214     else
215         register_unique_action(package,szFindRelatedProducts);
216
217     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
218     if (rc != ERROR_SUCCESS)
219         return ERROR_SUCCESS;
220     
221     rc = MSI_IterateRecords(view, NULL, ITERATE_FindRelatedProducts, package);
222     msiobj_release(&view->hdr);
223     
224     return rc;
225 }