msi: Improve detection of installed local assemblies.
[wine] / dlls / msi / assembly.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2010 Hans Leidekker 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 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wine/debug.h"
28 #include "wine/unicode.h"
29 #include "msipriv.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(msi);
32
33 static HRESULT (WINAPI *pCreateAssemblyCacheNet)( IAssemblyCache **, DWORD );
34 static HRESULT (WINAPI *pCreateAssemblyCacheSxs)( IAssemblyCache **, DWORD );
35 static HRESULT (WINAPI *pLoadLibraryShim)( LPCWSTR, LPCWSTR, LPVOID, HMODULE * );
36
37 static BOOL init_function_pointers( void )
38 {
39     static const WCHAR szFusion[] = {'f','u','s','i','o','n','.','d','l','l',0};
40     HMODULE hfusion, hmscoree, hsxs;
41     HRESULT hr;
42
43     if (pCreateAssemblyCacheNet) return TRUE;
44
45     if (!(hmscoree = LoadLibraryA( "mscoree.dll" )))
46     {
47         WARN("mscoree.dll not available\n");
48         return FALSE;
49     }
50     if (!(pLoadLibraryShim = (void *)GetProcAddress( hmscoree, "LoadLibraryShim" )))
51     {
52         WARN("LoadLibraryShim not available\n");
53         FreeLibrary( hmscoree );
54         return FALSE;
55     }
56     hr = pLoadLibraryShim( szFusion, NULL, NULL, &hfusion );
57     if (FAILED( hr ))
58     {
59         WARN("fusion.dll not available 0x%08x\n", hr);
60         FreeLibrary( hmscoree );
61         return FALSE;
62     }
63     pCreateAssemblyCacheNet = (void *)GetProcAddress( hfusion, "CreateAssemblyCache" );
64     FreeLibrary( hmscoree );
65     if (!(hsxs = LoadLibraryA( "sxs.dll" )))
66     {
67         WARN("sxs.dll not available\n");
68         FreeLibrary( hfusion );
69         return FALSE;
70     }
71     pCreateAssemblyCacheSxs = (void *)GetProcAddress( hsxs, "CreateAssemblyCache" );
72     return TRUE;
73 }
74
75 static BOOL init_assembly_caches( MSIPACKAGE *package )
76 {
77     HRESULT hr;
78
79     if (!init_function_pointers()) return FALSE;
80     if (package->cache_net) return TRUE;
81
82     hr = pCreateAssemblyCacheNet( &package->cache_net, 0 );
83     if (hr != S_OK) return FALSE;
84
85     hr = pCreateAssemblyCacheSxs( &package->cache_sxs, 0 );
86     if (hr != S_OK)
87     {
88         IAssemblyCache_Release( package->cache_net );
89         package->cache_net = NULL;
90         return FALSE;
91     }
92     return TRUE;
93 }
94
95 MSIRECORD *get_assembly_record( MSIPACKAGE *package, const WCHAR *comp )
96 {
97     static const WCHAR query[] = {
98         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
99          '`','M','s','i','A','s','s','e','m','b','l','y','`',' ',
100          'W','H','E','R','E',' ','`','C','o','m','p','o','n','e','n','t','_','`',
101          ' ','=',' ','\'','%','s','\'',0};
102     MSIQUERY *view;
103     MSIRECORD *rec;
104     UINT r;
105
106     r = MSI_OpenQuery( package->db, &view, query, comp );
107     if (r != ERROR_SUCCESS)
108         return NULL;
109
110     r = MSI_ViewExecute( view, NULL );
111     if (r != ERROR_SUCCESS)
112     {
113         msiobj_release( &view->hdr );
114         return NULL;
115     }
116     r = MSI_ViewFetch( view, &rec );
117     if (r != ERROR_SUCCESS)
118     {
119         msiobj_release( &view->hdr );
120         return NULL;
121     }
122     if (!MSI_RecordGetString( rec, 4 ))
123         TRACE("component is a global assembly\n");
124
125     msiobj_release( &view->hdr );
126     return rec;
127 }
128
129 struct assembly_name
130 {
131     WCHAR *type;
132     WCHAR *name;
133     WCHAR *version;
134     WCHAR *culture;
135     WCHAR *token;
136     WCHAR *arch;
137 };
138
139 static UINT get_assembly_name_attribute( MSIRECORD *rec, LPVOID param )
140 {
141     static const WCHAR typeW[] = {'t','y','p','e',0};
142     static const WCHAR nameW[] = {'n','a','m','e',0};
143     static const WCHAR versionW[] = {'v','e','r','s','i','o','n',0};
144     static const WCHAR cultureW[] = {'c','u','l','t','u','r','e',0};
145     static const WCHAR tokenW[] = {'p','u','b','l','i','c','K','e','y','T','o','k','e','n',0};
146     static const WCHAR archW[] = {'p','r','o','c','e','s','s','o','r','A','r','c','h','i','t','e','c','t','u','r','e',0};
147     struct assembly_name *name = param;
148     const WCHAR *attr = MSI_RecordGetString( rec, 2 );
149     WCHAR *value = msi_dup_record_field( rec, 3 );
150
151     if (!strcmpiW( attr, typeW ))
152         name->type = value;
153     else if (!strcmpiW( attr, nameW ))
154         name->name = value;
155     else if (!strcmpiW( attr, versionW ))
156         name->version = value;
157     else if (!strcmpiW( attr, cultureW ))
158         name->culture = value;
159     else if (!strcmpiW( attr, tokenW ))
160         name->token = value;
161     else if (!strcmpiW( attr, archW ))
162         name->arch = value;
163     else
164         msi_free( value );
165
166     return ERROR_SUCCESS;
167 }
168
169 static WCHAR *get_assembly_display_name( MSIDATABASE *db, const WCHAR *comp, MSIASSEMBLY *assembly )
170 {
171     static const WCHAR fmt_netW[] = {
172         '%','s',',',' ','v','e','r','s','i','o','n','=','%','s',',',' ',
173         'c','u','l','t','u','r','e','=','%','s',',',' ',
174         'p','u','b','l','i','c','K','e','y','T','o','k','e','n','=','%','s',0};
175     static const WCHAR fmt_sxsW[] = {
176         '%','s',',',' ','v','e','r','s','i','o','n','=','%','s',',',' ',
177         'p','u','b','l','i','c','K','e','y','T','o','k','e','n','=','%','s',',',' ',
178         'p','r','o','c','e','s','s','o','r','A','r','c','h','i','t','e','c','t','u','r','e','=','%','s',0};
179     static const WCHAR fmt_sxs_localW[] = {
180         '%','s',',',' ','v','e','r','s','i','o','n','=','%','s',',',' ',
181         'c','u','l','t','u','r','e','=','%','s',',',' ',
182         'p','u','b','l','i','c','K','e','y','T','o','k','e','n','=','%','s',0};
183     static const WCHAR queryW[] = {
184         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
185         '`','M','s','i','A','s','s','e','m','b','l','y','N','a','m','e','`',' ',
186         'W','H','E','R','E',' ','`','C','o','m','p','o','n','e','n','t','_','`',
187         ' ','=',' ','\'','%','s','\'',0};
188     struct assembly_name name;
189     WCHAR *display_name = NULL;
190     MSIQUERY *view;
191     int len;
192     UINT r;
193
194     memset( &name, 0, sizeof(name) );
195
196     r = MSI_OpenQuery( db, &view, queryW, comp );
197     if (r != ERROR_SUCCESS)
198         return NULL;
199
200     MSI_IterateRecords( view, NULL, get_assembly_name_attribute, &name );
201     msiobj_release( &view->hdr );
202
203     if (assembly->attributes == msidbAssemblyAttributesWin32)
204     {
205         if (!assembly->application)
206         {
207             if (!name.type || !name.name || !name.version || !name.token || !name.arch)
208             {
209                 WARN("invalid global win32 assembly name\n");
210                 goto done;
211             }
212             len = strlenW( fmt_sxsW );
213             len += strlenW( name.name );
214             len += strlenW( name.version );
215             len += strlenW( name.token );
216             len += strlenW( name.arch );
217             if (!(display_name = msi_alloc( len * sizeof(WCHAR) ))) goto done;
218             sprintfW( display_name, fmt_sxsW, name.name, name.version, name.token, name.arch );
219         }
220         else
221         {
222             if (!name.name || !name.version || !name.culture || !name.token)
223             {
224                 WARN("invalid local win32 assembly name\n");
225                 goto done;
226             }
227             len = strlenW( fmt_sxs_localW );
228             len += strlenW( name.name );
229             len += strlenW( name.version );
230             len += strlenW( name.culture );
231             len += strlenW( name.token );
232             if (!(display_name = msi_alloc( len * sizeof(WCHAR) ))) goto done;
233             sprintfW( display_name, fmt_sxs_localW, name.name, name.version, name.culture, name.token );
234         }
235     }
236     else
237     {
238         if (!name.name || !name.version || !name.culture || !name.token)
239         {
240             WARN("invalid assembly name\n");
241             goto done;
242         }
243         len = strlenW( fmt_netW );
244         len += strlenW( name.name );
245         len += strlenW( name.version );
246         len += strlenW( name.culture );
247         len += strlenW( name.token );
248         if (!(display_name = msi_alloc( len * sizeof(WCHAR) ))) goto done;
249         sprintfW( display_name, fmt_netW, name.name, name.version, name.culture, name.token );
250     }
251
252 done:
253     msi_free( name.type );
254     msi_free( name.name );
255     msi_free( name.version );
256     msi_free( name.culture );
257     msi_free( name.token );
258     msi_free( name.arch );
259
260     return display_name;
261 }
262
263 static BOOL check_assembly_installed( MSIPACKAGE *package, MSIASSEMBLY *assembly )
264 {
265     IAssemblyCache *cache;
266     ASSEMBLY_INFO info;
267     HRESULT hr;
268
269     if (assembly->application)
270     {
271         FIXME("we should probably check the manifest file here\n");
272         if (msi_get_property_int( package->db, szInstalled, 0 )) return TRUE;
273         return FALSE;
274     }
275
276     if (!init_assembly_caches( package ))
277         return FALSE;
278
279     if (assembly->attributes == msidbAssemblyAttributesWin32)
280         cache = package->cache_sxs;
281     else
282         cache = package->cache_net;
283
284     memset( &info, 0, sizeof(info) );
285     info.cbAssemblyInfo = sizeof(info);
286     hr = IAssemblyCache_QueryAssemblyInfo( cache, QUERYASMINFO_FLAG_VALIDATE, assembly->display_name, &info );
287     if (hr != HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER))
288         return FALSE;
289
290     return (info.dwAssemblyFlags == ASSEMBLYINFO_FLAG_INSTALLED);
291 }
292
293 MSIASSEMBLY *load_assembly( MSIPACKAGE *package, MSICOMPONENT *comp )
294 {
295     MSIRECORD *rec;
296     MSIASSEMBLY *a;
297
298     if (!(rec = get_assembly_record( package, comp->Component )))
299         return NULL;
300
301     if (!(a = msi_alloc_zero( sizeof(MSIASSEMBLY) )))
302     {
303         msiobj_release( &rec->hdr );
304         return NULL;
305     }
306     a->feature = strdupW( MSI_RecordGetString( rec, 2 ) );
307     TRACE("feature %s\n", debugstr_w(a->feature));
308
309     a->manifest = strdupW( MSI_RecordGetString( rec, 3 ) );
310     TRACE("manifest %s\n", debugstr_w(a->manifest));
311
312     a->application = strdupW( MSI_RecordGetString( rec, 4 ) );
313     TRACE("application %s\n", debugstr_w(a->application));
314
315     a->attributes = MSI_RecordGetInteger( rec, 5 );
316     TRACE("attributes %u\n", a->attributes);
317
318     if (!(a->display_name = get_assembly_display_name( package->db, comp->Component, a )))
319     {
320         WARN("can't get display name\n");
321         msiobj_release( &rec->hdr );
322         msi_free( a );
323         return NULL;
324     }
325     TRACE("display name %s\n", debugstr_w(a->display_name));
326
327     a->installed = check_assembly_installed( package, a );
328     TRACE("assembly is %s\n", a->installed ? "installed" : "not installed");
329
330     msiobj_release( &rec->hdr );
331     return a;
332 }
333
334 UINT install_assembly( MSIPACKAGE *package, MSICOMPONENT *comp )
335 {
336     HRESULT hr;
337     const WCHAR *manifest;
338     IAssemblyCache *cache;
339     MSIASSEMBLY *assembly = comp->assembly;
340     MSIFEATURE *feature = NULL;
341
342     if (comp->assembly->feature)
343         feature = get_loaded_feature( package, comp->assembly->feature );
344
345     if (assembly->application)
346     {
347         if (feature) feature->Action = INSTALLSTATE_LOCAL;
348         return ERROR_SUCCESS;
349     }
350     if (assembly->attributes == msidbAssemblyAttributesWin32)
351     {
352         if (!assembly->manifest)
353         {
354             WARN("no manifest\n");
355             return ERROR_FUNCTION_FAILED;
356         }
357         manifest = get_loaded_file( package, assembly->manifest )->TargetPath;
358         cache = package->cache_sxs;
359     }
360     else
361     {
362         manifest = get_loaded_file( package, comp->KeyPath )->TargetPath;
363         cache = package->cache_net;
364     }
365     TRACE("installing assembly %s\n", debugstr_w(manifest));
366
367     hr = IAssemblyCache_InstallAssembly( cache, 0, manifest, NULL );
368     if (hr != S_OK)
369     {
370         ERR("Failed to install assembly %s (0x%08x)\n", debugstr_w(manifest), hr);
371         return ERROR_FUNCTION_FAILED;
372     }
373     if (feature) feature->Action = INSTALLSTATE_LOCAL;
374     assembly->installed = TRUE;
375     return ERROR_SUCCESS;
376 }
377
378 UINT ACTION_MsiPublishAssemblies( MSIPACKAGE *package )
379 {
380     MSIRECORD *uirow;
381     MSICOMPONENT *comp;
382
383     LIST_FOR_EACH_ENTRY(comp, &package->components, MSICOMPONENT, entry)
384     {
385         if (!comp->assembly || !comp->Enabled)
386             continue;
387
388         /* FIXME: write assembly registry values */
389
390         uirow = MSI_CreateRecord( 2 );
391         MSI_RecordSetStringW( uirow, 2, comp->assembly->display_name );
392         ui_actiondata( package, szMsiPublishAssemblies, uirow );
393         msiobj_release( &uirow->hdr );
394     }
395     return ERROR_SUCCESS;
396 }