msi: The File_Setup field in the ODBCDriver and ODBCTranslator tables is optional.
[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 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
65     prop = msi_dup_property(package, action_property );
66     if (prop)
67         len = strlenW(prop);
68     else
69         len = 0;
70
71     /*separator*/
72     len ++;
73
74     len += strlenW(productid);
75
76     /*null*/
77     len++;
78
79     newprop = msi_alloc( len*sizeof(WCHAR) );
80
81     if (prop)
82     {
83         strcpyW(newprop,prop);
84         strcatW(newprop,szSemiColon);
85     }
86     else
87         newprop[0] = 0;
88     strcatW(newprop,productid);
89
90     MSI_SetPropertyW(package, action_property, newprop);
91     TRACE("Found Related Product... %s now %s\n",debugstr_w(action_property),
92                     debugstr_w(newprop));
93     msi_free( prop );
94     msi_free( newprop );
95 }
96
97 static UINT ITERATE_FindRelatedProducts(MSIRECORD *rec, LPVOID param)
98 {
99     MSIPACKAGE *package = param;
100     WCHAR product[GUID_SIZE];
101     DWORD index = 0;
102     DWORD attributes = 0;
103     DWORD sz = GUID_SIZE;
104     LPCWSTR upgrade_code;
105     HKEY hkey = 0;
106     UINT rc = ERROR_SUCCESS;
107     MSIRECORD *uirow;
108
109     upgrade_code = MSI_RecordGetString(rec,1);
110
111     rc = MSIREG_OpenUpgradeCodesKey(upgrade_code, &hkey, FALSE);
112     if (rc != ERROR_SUCCESS)
113         return ERROR_SUCCESS;
114
115     uirow = MSI_CreateRecord(1);
116     attributes = MSI_RecordGetInteger(rec,5);
117
118     while (rc == ERROR_SUCCESS)
119     {
120         rc = RegEnumValueW(hkey, index, product, &sz, NULL, NULL, NULL, NULL);
121         TRACE("Looking at (%i) %s\n",index,debugstr_w(product));
122         if (rc == ERROR_SUCCESS)
123         {
124             WCHAR productid[GUID_SIZE];
125             LPCWSTR ver;
126             LPCWSTR language;
127             LPCWSTR action_property;
128             DWORD check = 0x00000000;
129             DWORD comp_ver = 0x00000000;
130             DWORD sz = 0x100;
131             HKEY hukey;
132             INT r;
133
134             unsquash_guid(product, productid);
135             rc = MSIREG_OpenProductKey(productid, NULL, package->Context,
136                                        &hukey, FALSE);
137             if (rc != ERROR_SUCCESS)
138             {
139                 rc = ERROR_SUCCESS;
140                 index ++;
141                 continue;
142             }
143
144             sz = sizeof(DWORD);
145             RegQueryValueExW(hukey, INSTALLPROPERTY_VERSIONW, NULL, NULL,
146                     (LPBYTE)&check, &sz);
147             /* check min */
148             ver = MSI_RecordGetString(rec,2);
149             comp_ver = msi_version_str_to_dword(ver);
150             r = check - comp_ver; 
151             if (r < 0 || (r == 0 && !(attributes &
152                                     msidbUpgradeAttributesVersionMinInclusive)))
153             {
154                 RegCloseKey(hukey);
155                 index ++;
156                 continue;
157             }
158
159             /* check max */
160             ver = MSI_RecordGetString(rec,3);
161             comp_ver = msi_version_str_to_dword(ver);
162             r = check - comp_ver;
163             if (r > 0 || (r == 0 && !(attributes & 
164                                     msidbUpgradeAttributesVersionMaxInclusive)))
165             {
166                 RegCloseKey(hukey);
167                 index ++;
168                 continue;
169             }
170
171             /* check language*/
172             sz = sizeof(DWORD);
173             RegQueryValueExW(hukey, INSTALLPROPERTY_LANGUAGEW, NULL, NULL,
174                     (LPBYTE)&check, &sz);
175             RegCloseKey(hukey);
176             language = MSI_RecordGetString(rec,4);
177             TRACE("Checking languages %x and %s\n", check, 
178                             debugstr_w(language));
179             if (!check_language(check, language, attributes))
180             {
181                 index ++;
182                 continue;
183             }
184
185             action_property = MSI_RecordGetString(rec,7);
186             append_productcode(package,action_property,productid);
187             ui_actiondata(package,szFindRelatedProducts,uirow);
188         }
189         index ++;
190     }
191     RegCloseKey(hkey);
192     msiobj_release( &uirow->hdr);
193     
194     return ERROR_SUCCESS;
195 }
196
197 UINT ACTION_FindRelatedProducts(MSIPACKAGE *package)
198 {
199     static const WCHAR Query[] = 
200         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',
201          ' ','`','U','p','g','r','a','d','e','`',0};
202     UINT rc = ERROR_SUCCESS;
203     MSIQUERY *view;
204
205     if (check_unique_action(package,szFindRelatedProducts))
206     {
207         TRACE("Skipping FindRelatedProducts action: already done on client side\n");
208         return ERROR_SUCCESS;
209     }
210     else
211         register_unique_action(package,szFindRelatedProducts);
212
213     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
214     if (rc != ERROR_SUCCESS)
215         return ERROR_SUCCESS;
216     
217     rc = MSI_IterateRecords(view, NULL, ITERATE_FindRelatedProducts, package);
218     msiobj_release(&view->hdr);
219     
220     return rc;
221 }