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