msi: Reuse temporary executables.
[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 queryW[] = {
180         'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
181         '`','M','s','i','A','s','s','e','m','b','l','y','N','a','m','e','`',' ',
182         'W','H','E','R','E',' ','`','C','o','m','p','o','n','e','n','t','_','`',
183         ' ','=',' ','\'','%','s','\'',0};
184     struct assembly_name name;
185     WCHAR *display_name = NULL;
186     MSIQUERY *view;
187     int len;
188     UINT r;
189
190     memset( &name, 0, sizeof(name) );
191
192     r = MSI_OpenQuery( db, &view, queryW, comp );
193     if (r != ERROR_SUCCESS)
194         return NULL;
195
196     MSI_IterateRecords( view, NULL, get_assembly_name_attribute, &name );
197     msiobj_release( &view->hdr );
198
199     if (assembly->attributes == msidbAssemblyAttributesWin32)
200     {
201         if (!name.type || !name.name || !name.version || !name.token || !name.arch)
202         {
203             WARN("invalid win32 assembly name\n");
204             goto done;
205         }
206         len = strlenW( fmt_sxsW );
207         len += strlenW( name.name );
208         len += strlenW( name.version );
209         len += strlenW( name.token );
210         len += strlenW( name.arch );
211         if (!(display_name = msi_alloc( len * sizeof(WCHAR) ))) goto done;
212         sprintfW( display_name, fmt_sxsW, name.name, name.version, name.token, name.arch );
213     }
214     else
215     {
216         if (!name.name || !name.version || !name.culture || !name.token)
217         {
218             WARN("invalid assembly name\n");
219             goto done;
220         }
221         len = strlenW( fmt_netW );
222         len += strlenW( name.name );
223         len += strlenW( name.version );
224         len += strlenW( name.culture );
225         len += strlenW( name.token );
226         if (!(display_name = msi_alloc( len * sizeof(WCHAR) ))) goto done;
227         sprintfW( display_name, fmt_netW, name.name, name.version, name.culture, name.token );
228     }
229
230 done:
231     msi_free( name.type );
232     msi_free( name.name );
233     msi_free( name.version );
234     msi_free( name.culture );
235     msi_free( name.token );
236     msi_free( name.arch );
237
238     return display_name;
239 }
240
241 static BOOL check_assembly_installed( MSIPACKAGE *package, MSIASSEMBLY *assembly )
242 {
243     IAssemblyCache *cache;
244     ASSEMBLY_INFO info;
245     HRESULT hr;
246
247     if (assembly->application)
248     {
249         /* FIXME: we should probably check the manifest file here */
250         return FALSE;
251     }
252
253     if (!init_assembly_caches( package ))
254         return FALSE;
255
256     if (assembly->attributes == msidbAssemblyAttributesWin32)
257         cache = package->cache_sxs;
258     else
259         cache = package->cache_net;
260
261     memset( &info, 0, sizeof(info) );
262     info.cbAssemblyInfo = sizeof(info);
263     hr = IAssemblyCache_QueryAssemblyInfo( cache, QUERYASMINFO_FLAG_VALIDATE, assembly->display_name, &info );
264     if (hr != S_OK)
265         return FALSE;
266
267     return (info.dwAssemblyFlags == ASSEMBLYINFO_FLAG_INSTALLED);
268 }
269
270 MSIASSEMBLY *load_assembly( MSIPACKAGE *package, MSICOMPONENT *comp )
271 {
272     MSIRECORD *rec;
273     MSIASSEMBLY *a;
274
275     if (!(rec = get_assembly_record( package, comp->Component )))
276         return NULL;
277
278     if (!(a = msi_alloc_zero( sizeof(MSIASSEMBLY) )))
279     {
280         msiobj_release( &rec->hdr );
281         return NULL;
282     }
283     a->feature = strdupW( MSI_RecordGetString( rec, 2 ) );
284     TRACE("feature %s\n", debugstr_w(a->feature));
285
286     a->manifest = strdupW( MSI_RecordGetString( rec, 3 ) );
287     TRACE("manifest %s\n", debugstr_w(a->manifest));
288
289     a->application = strdupW( MSI_RecordGetString( rec, 4 ) );
290     TRACE("application %s\n", debugstr_w(a->application));
291
292     a->attributes = MSI_RecordGetInteger( rec, 5 );
293     TRACE("attributes %u\n", a->attributes);
294
295     if (!(a->display_name = get_assembly_display_name( package->db, comp->Component, a )))
296     {
297         WARN("can't get display name\n");
298         msiobj_release( &rec->hdr );
299         msi_free( a );
300         return NULL;
301     }
302     TRACE("display name %s\n", debugstr_w(a->display_name));
303
304     a->installed = check_assembly_installed( package, a );
305     TRACE("assembly is %s\n", a->installed ? "installed" : "not installed");
306
307     msiobj_release( &rec->hdr );
308     return a;
309 }
310
311 UINT install_assembly( MSIPACKAGE *package, MSICOMPONENT *comp )
312 {
313     HRESULT hr;
314     const WCHAR *manifest;
315     IAssemblyCache *cache;
316     MSIASSEMBLY *assembly = comp->assembly;
317     MSIFEATURE *feature = NULL;
318
319     if (comp->assembly->feature)
320         feature = get_loaded_feature( package, comp->assembly->feature );
321
322     if (assembly->application)
323     {
324         if (feature) feature->Action = INSTALLSTATE_LOCAL;
325         return ERROR_SUCCESS;
326     }
327     if (assembly->attributes == msidbAssemblyAttributesWin32)
328     {
329         if (!assembly->manifest)
330         {
331             WARN("no manifest\n");
332             return ERROR_FUNCTION_FAILED;
333         }
334         manifest = get_loaded_file( package, assembly->manifest )->TargetPath;
335         cache = package->cache_sxs;
336     }
337     else
338     {
339         manifest = get_loaded_file( package, comp->KeyPath )->TargetPath;
340         cache = package->cache_net;
341     }
342     TRACE("installing assembly %s\n", debugstr_w(manifest));
343
344     hr = IAssemblyCache_InstallAssembly( cache, 0, manifest, NULL );
345     if (hr != S_OK)
346     {
347         ERR("Failed to install assembly %s (0x%08x)\n", debugstr_w(manifest), hr);
348         return ERROR_FUNCTION_FAILED;
349     }
350     if (feature) feature->Action = INSTALLSTATE_LOCAL;
351     assembly->installed = TRUE;
352     return ERROR_SUCCESS;
353 }
354
355 UINT ACTION_MsiPublishAssemblies( MSIPACKAGE *package )
356 {
357     MSIRECORD *uirow;
358     MSICOMPONENT *comp;
359
360     LIST_FOR_EACH_ENTRY(comp, &package->components, MSICOMPONENT, entry)
361     {
362         if (!comp->assembly || !comp->Enabled)
363             continue;
364
365         /* FIXME: write assembly registry values */
366
367         uirow = MSI_CreateRecord( 2 );
368         MSI_RecordSetStringW( uirow, 2, comp->assembly->display_name );
369         ui_actiondata( package, szMsiPublishAssemblies, uirow );
370         msiobj_release( &uirow->hdr );
371     }
372     return ERROR_SUCCESS;
373 }