quartz/tests: Remove misplaced ok() call.
[wine] / dlls / fusion / asmcache.c
1 /*
2  * IAssemblyCache implementation
3  *
4  * Copyright 2008 James Hawkins
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 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winver.h"
30 #include "wincrypt.h"
31 #include "winreg.h"
32 #include "shlwapi.h"
33 #include "dbghelp.h"
34 #include "ole2.h"
35 #include "fusion.h"
36 #include "corerror.h"
37
38 #include "fusionpriv.h"
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(fusion);
43
44 static const WCHAR cache_mutex_nameW[] =
45     {'_','_','W','I','N','E','_','F','U','S','I','O','N','_','C','A','C','H','E','_','M','U','T','E','X','_','_',0};
46
47 static BOOL create_full_path(LPCWSTR path)
48 {
49     LPWSTR new_path;
50     BOOL ret = TRUE;
51     int len;
52
53     new_path = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1) * sizeof(WCHAR));
54     if (!new_path)
55         return FALSE;
56
57     strcpyW(new_path, path);
58
59     while ((len = strlenW(new_path)) && new_path[len - 1] == '\\')
60         new_path[len - 1] = 0;
61
62     while (!CreateDirectoryW(new_path, NULL))
63     {
64         LPWSTR slash;
65         DWORD last_error = GetLastError();
66
67         if(last_error == ERROR_ALREADY_EXISTS)
68             break;
69
70         if(last_error != ERROR_PATH_NOT_FOUND)
71         {
72             ret = FALSE;
73             break;
74         }
75
76         if(!(slash = strrchrW(new_path, '\\')))
77         {
78             ret = FALSE;
79             break;
80         }
81
82         len = slash - new_path;
83         new_path[len] = 0;
84         if(!create_full_path(new_path))
85         {
86             ret = FALSE;
87             break;
88         }
89
90         new_path[len] = '\\';
91     }
92
93     HeapFree(GetProcessHeap(), 0, new_path);
94     return ret;
95 }
96
97 static BOOL get_assembly_directory(LPWSTR dir, DWORD size, BYTE architecture)
98 {
99     static const WCHAR gac[] = {'\\','a','s','s','e','m','b','l','y','\\','G','A','C',0};
100
101     static const WCHAR msil[] = {'_','M','S','I','L',0};
102     static const WCHAR x86[] = {'_','3','2',0};
103     static const WCHAR amd64[] = {'_','6','4',0};
104
105     GetWindowsDirectoryW(dir, size);
106     strcatW(dir, gac);
107
108     switch (architecture)
109     {
110         case peMSIL:
111             strcatW(dir, msil);
112             break;
113
114         case peI386:
115             strcatW(dir, x86);
116             break;
117
118         case peAMD64:
119             strcatW(dir, amd64);
120             break;
121     }
122
123     return TRUE;
124 }
125
126 /* IAssemblyCache */
127
128 typedef struct {
129     IAssemblyCache IAssemblyCache_iface;
130
131     LONG ref;
132     HANDLE lock;
133 } IAssemblyCacheImpl;
134
135 static inline IAssemblyCacheImpl *impl_from_IAssemblyCache(IAssemblyCache *iface)
136 {
137     return CONTAINING_RECORD(iface, IAssemblyCacheImpl, IAssemblyCache_iface);
138 }
139
140 static HRESULT WINAPI IAssemblyCacheImpl_QueryInterface(IAssemblyCache *iface,
141                                                         REFIID riid, LPVOID *ppobj)
142 {
143     IAssemblyCacheImpl *This = impl_from_IAssemblyCache(iface);
144
145     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
146
147     *ppobj = NULL;
148
149     if (IsEqualIID(riid, &IID_IUnknown) ||
150         IsEqualIID(riid, &IID_IAssemblyCache))
151     {
152         IUnknown_AddRef(iface);
153         *ppobj = This;
154         return S_OK;
155     }
156
157     WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
158     return E_NOINTERFACE;
159 }
160
161 static ULONG WINAPI IAssemblyCacheImpl_AddRef(IAssemblyCache *iface)
162 {
163     IAssemblyCacheImpl *This = impl_from_IAssemblyCache(iface);
164     ULONG refCount = InterlockedIncrement(&This->ref);
165
166     TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
167
168     return refCount;
169 }
170
171 static ULONG WINAPI IAssemblyCacheImpl_Release(IAssemblyCache *iface)
172 {
173     IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
174     ULONG refCount = InterlockedDecrement( &cache->ref );
175
176     TRACE("(%p)->(ref before = %u)\n", cache, refCount + 1);
177
178     if (!refCount)
179     {
180         CloseHandle( cache->lock );
181         HeapFree( GetProcessHeap(), 0, cache );
182     }
183     return refCount;
184 }
185
186 static void cache_lock( IAssemblyCacheImpl *cache )
187 {
188     WaitForSingleObject( cache->lock, INFINITE );
189 }
190
191 static void cache_unlock( IAssemblyCacheImpl *cache )
192 {
193     ReleaseMutex( cache->lock );
194 }
195
196 static HRESULT WINAPI IAssemblyCacheImpl_UninstallAssembly(IAssemblyCache *iface,
197                                                            DWORD dwFlags,
198                                                            LPCWSTR pszAssemblyName,
199                                                            LPCFUSION_INSTALL_REFERENCE pRefData,
200                                                            ULONG *pulDisposition)
201 {
202     HRESULT hr;
203     IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
204     IAssemblyName *asmname, *next = NULL;
205     IAssemblyEnum *asmenum = NULL;
206     WCHAR *p, *path = NULL;
207     ULONG disp;
208     DWORD len;
209
210     TRACE("(%p, 0%08x, %s, %p, %p)\n", iface, dwFlags,
211           debugstr_w(pszAssemblyName), pRefData, pulDisposition);
212
213     if (pRefData)
214     {
215         FIXME("application reference not supported\n");
216         return E_NOTIMPL;
217     }
218     hr = CreateAssemblyNameObject( &asmname, pszAssemblyName, CANOF_PARSE_DISPLAY_NAME, NULL );
219     if (FAILED( hr ))
220         return hr;
221
222     cache_lock( cache );
223
224     hr = CreateAssemblyEnum( &asmenum, NULL, asmname, ASM_CACHE_GAC, NULL );
225     if (FAILED( hr ))
226         goto done;
227
228     hr = IAssemblyEnum_GetNextAssembly( asmenum, NULL, &next, 0 );
229     if (hr == S_FALSE)
230     {
231         if (pulDisposition)
232             *pulDisposition = IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED;
233         goto done;
234     }
235     hr = IAssemblyName_GetPath( next, NULL, &len );
236     if (hr != HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER ))
237         goto done;
238
239     if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
240     {
241         hr = E_OUTOFMEMORY;
242         goto done;
243     }
244     hr = IAssemblyName_GetPath( next, path, &len );
245     if (FAILED( hr ))
246         goto done;
247
248     if (DeleteFileW( path ))
249     {
250         if ((p = strrchrW( path, '\\' )))
251         {
252             *p = 0;
253             RemoveDirectoryW( path );
254             if ((p = strrchrW( path, '\\' )))
255             {
256                 *p = 0;
257                 RemoveDirectoryW( path );
258             }
259         }
260         disp = IASSEMBLYCACHE_UNINSTALL_DISPOSITION_UNINSTALLED;
261         hr = S_OK;
262     }
263     else
264     {
265         disp = IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED;
266         hr = S_FALSE;
267     }
268     if (pulDisposition) *pulDisposition = disp;
269
270 done:
271     IAssemblyName_Release( asmname );
272     if (next) IAssemblyName_Release( next );
273     if (asmenum) IAssemblyEnum_Release( asmenum );
274     HeapFree( GetProcessHeap(), 0, path );
275     cache_unlock( cache );
276     return hr;
277 }
278
279 static HRESULT WINAPI IAssemblyCacheImpl_QueryAssemblyInfo(IAssemblyCache *iface,
280                                                            DWORD dwFlags,
281                                                            LPCWSTR pszAssemblyName,
282                                                            ASSEMBLY_INFO *pAsmInfo)
283 {
284     IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
285     IAssemblyName *asmname, *next = NULL;
286     IAssemblyEnum *asmenum = NULL;
287     HRESULT hr;
288
289     TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
290           debugstr_w(pszAssemblyName), pAsmInfo);
291
292     if (pAsmInfo)
293     {
294         if (pAsmInfo->cbAssemblyInfo == 0)
295             pAsmInfo->cbAssemblyInfo = sizeof(ASSEMBLY_INFO);
296         else if (pAsmInfo->cbAssemblyInfo != sizeof(ASSEMBLY_INFO))
297             return E_INVALIDARG;
298     }
299
300     hr = CreateAssemblyNameObject(&asmname, pszAssemblyName,
301                                   CANOF_PARSE_DISPLAY_NAME, NULL);
302     if (FAILED(hr))
303         return hr;
304
305     cache_lock( cache );
306
307     hr = CreateAssemblyEnum(&asmenum, NULL, asmname, ASM_CACHE_GAC, NULL);
308     if (FAILED(hr))
309         goto done;
310
311     for (;;)
312     {
313         hr = IAssemblyEnum_GetNextAssembly(asmenum, NULL, &next, 0);
314         if (hr != S_OK)
315         {
316             hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
317             goto done;
318         }
319         hr = IAssemblyName_IsEqual(asmname, next, ASM_CMPF_IL_ALL);
320         if (hr == S_OK) break;
321     }
322
323     if (!pAsmInfo)
324         goto done;
325
326     hr = IAssemblyName_GetPath(next, pAsmInfo->pszCurrentAssemblyPathBuf, &pAsmInfo->cchBuf);
327
328     pAsmInfo->dwAssemblyFlags = ASSEMBLYINFO_FLAG_INSTALLED;
329
330 done:
331     IAssemblyName_Release(asmname);
332     if (next) IAssemblyName_Release(next);
333     if (asmenum) IAssemblyEnum_Release(asmenum);
334     cache_unlock( cache );
335     return hr;
336 }
337
338 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyCacheItem(IAssemblyCache *iface,
339                                                                  DWORD dwFlags,
340                                                                  PVOID pvReserved,
341                                                                  IAssemblyCacheItem **ppAsmItem,
342                                                                  LPCWSTR pszAssemblyName)
343 {
344     FIXME("(%p, %d, %p, %p, %s) stub!\n", iface, dwFlags, pvReserved,
345           ppAsmItem, debugstr_w(pszAssemblyName));
346
347     return E_NOTIMPL;
348 }
349
350 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyScavenger(IAssemblyCache *iface,
351                                                                  IUnknown **ppUnkReserved)
352 {
353     FIXME("(%p, %p) stub!\n", iface, ppUnkReserved);
354     return E_NOTIMPL;
355 }
356
357 static HRESULT WINAPI IAssemblyCacheImpl_InstallAssembly(IAssemblyCache *iface,
358                                                          DWORD dwFlags,
359                                                          LPCWSTR pszManifestFilePath,
360                                                          LPCFUSION_INSTALL_REFERENCE pRefData)
361 {
362     static const WCHAR format[] = {'%','s','\\','%','s','\\','%','s','_','_','%','s','\\',0};
363     static const WCHAR ext_exe[] = {'.','e','x','e',0};
364     static const WCHAR ext_dll[] = {'.','d','l','l',0};
365     IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
366     ASSEMBLY *assembly;
367     LPWSTR filename;
368     LPWSTR name = NULL;
369     LPWSTR token = NULL;
370     LPWSTR version = NULL;
371     LPWSTR asmpath = NULL;
372     WCHAR path[MAX_PATH];
373     WCHAR asmdir[MAX_PATH];
374     LPWSTR ext;
375     HRESULT hr;
376
377     TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
378           debugstr_w(pszManifestFilePath), pRefData);
379
380     if (!pszManifestFilePath || !*pszManifestFilePath)
381         return E_INVALIDARG;
382
383     if (!(ext = strrchrW(pszManifestFilePath, '.')))
384         return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
385
386     if (lstrcmpiW(ext, ext_exe) && lstrcmpiW(ext, ext_dll))
387         return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
388
389     if (GetFileAttributesW(pszManifestFilePath) == INVALID_FILE_ATTRIBUTES)
390         return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
391
392     hr = assembly_create(&assembly, pszManifestFilePath);
393     if (FAILED(hr))
394     {
395         hr = COR_E_ASSEMBLYEXPECTED;
396         goto done;
397     }
398
399     hr = assembly_get_name(assembly, &name);
400     if (FAILED(hr))
401         goto done;
402
403     hr = assembly_get_pubkey_token(assembly, &token);
404     if (FAILED(hr))
405         goto done;
406
407     hr = assembly_get_version(assembly, &version);
408     if (FAILED(hr))
409         goto done;
410
411     cache_lock( cache );
412
413     get_assembly_directory(asmdir, MAX_PATH, assembly_get_architecture(assembly));
414
415     sprintfW(path, format, asmdir, name, version, token);
416
417     create_full_path(path);
418
419     hr = assembly_get_path(assembly, &asmpath);
420     if (FAILED(hr))
421         goto done;
422
423     filename = PathFindFileNameW(asmpath);
424
425     strcatW(path, filename);
426     if (!CopyFileW(asmpath, path, FALSE))
427         hr = HRESULT_FROM_WIN32(GetLastError());
428
429 done:
430     HeapFree(GetProcessHeap(), 0, name);
431     HeapFree(GetProcessHeap(), 0, token);
432     HeapFree(GetProcessHeap(), 0, version);
433     HeapFree(GetProcessHeap(), 0, asmpath);
434     assembly_release(assembly);
435     cache_unlock( cache );
436     return hr;
437 }
438
439 static const IAssemblyCacheVtbl AssemblyCacheVtbl = {
440     IAssemblyCacheImpl_QueryInterface,
441     IAssemblyCacheImpl_AddRef,
442     IAssemblyCacheImpl_Release,
443     IAssemblyCacheImpl_UninstallAssembly,
444     IAssemblyCacheImpl_QueryAssemblyInfo,
445     IAssemblyCacheImpl_CreateAssemblyCacheItem,
446     IAssemblyCacheImpl_CreateAssemblyScavenger,
447     IAssemblyCacheImpl_InstallAssembly
448 };
449
450 /******************************************************************
451  *  CreateAssemblyCache   (FUSION.@)
452  */
453 HRESULT WINAPI CreateAssemblyCache(IAssemblyCache **ppAsmCache, DWORD dwReserved)
454 {
455     IAssemblyCacheImpl *cache;
456
457     TRACE("(%p, %d)\n", ppAsmCache, dwReserved);
458
459     if (!ppAsmCache)
460         return E_INVALIDARG;
461
462     *ppAsmCache = NULL;
463
464     cache = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyCacheImpl));
465     if (!cache)
466         return E_OUTOFMEMORY;
467
468     cache->IAssemblyCache_iface.lpVtbl = &AssemblyCacheVtbl;
469     cache->ref = 1;
470     cache->lock = CreateMutexW( NULL, FALSE, cache_mutex_nameW );
471     if (!cache->lock)
472     {
473         HeapFree( GetProcessHeap(), 0, cache );
474         return HRESULT_FROM_WIN32( GetLastError() );
475     }
476     *ppAsmCache = &cache->IAssemblyCache_iface;
477     return S_OK;
478 }
479
480 /* IAssemblyCacheItem */
481
482 typedef struct {
483     IAssemblyCacheItem IAssemblyCacheItem_iface;
484
485     LONG ref;
486 } IAssemblyCacheItemImpl;
487
488 static inline IAssemblyCacheItemImpl *impl_from_IAssemblyCacheItem(IAssemblyCacheItem *iface)
489 {
490     return CONTAINING_RECORD(iface, IAssemblyCacheItemImpl, IAssemblyCacheItem_iface);
491 }
492
493 static HRESULT WINAPI IAssemblyCacheItemImpl_QueryInterface(IAssemblyCacheItem *iface,
494                                                             REFIID riid, LPVOID *ppobj)
495 {
496     IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
497
498     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
499
500     *ppobj = NULL;
501
502     if (IsEqualIID(riid, &IID_IUnknown) ||
503         IsEqualIID(riid, &IID_IAssemblyCacheItem))
504     {
505         IUnknown_AddRef(iface);
506         *ppobj = This;
507         return S_OK;
508     }
509
510     WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
511     return E_NOINTERFACE;
512 }
513
514 static ULONG WINAPI IAssemblyCacheItemImpl_AddRef(IAssemblyCacheItem *iface)
515 {
516     IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
517     ULONG refCount = InterlockedIncrement(&This->ref);
518
519     TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
520
521     return refCount;
522 }
523
524 static ULONG WINAPI IAssemblyCacheItemImpl_Release(IAssemblyCacheItem *iface)
525 {
526     IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
527     ULONG refCount = InterlockedDecrement(&This->ref);
528
529     TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
530
531     if (!refCount)
532         HeapFree(GetProcessHeap(), 0, This);
533
534     return refCount;
535 }
536
537 static HRESULT WINAPI IAssemblyCacheItemImpl_CreateStream(IAssemblyCacheItem *iface,
538                                                         DWORD dwFlags,
539                                                         LPCWSTR pszStreamName,
540                                                         DWORD dwFormat,
541                                                         DWORD dwFormatFlags,
542                                                         IStream **ppIStream,
543                                                         ULARGE_INTEGER *puliMaxSize)
544 {
545     FIXME("(%p, %d, %s, %d, %d, %p, %p) stub!\n", iface, dwFlags,
546           debugstr_w(pszStreamName), dwFormat, dwFormatFlags, ppIStream, puliMaxSize);
547
548     return E_NOTIMPL;
549 }
550
551 static HRESULT WINAPI IAssemblyCacheItemImpl_Commit(IAssemblyCacheItem *iface,
552                                                   DWORD dwFlags,
553                                                   ULONG *pulDisposition)
554 {
555     FIXME("(%p, %d, %p) stub!\n", iface, dwFlags, pulDisposition);
556     return E_NOTIMPL;
557 }
558
559 static HRESULT WINAPI IAssemblyCacheItemImpl_AbortItem(IAssemblyCacheItem *iface)
560 {
561     FIXME("(%p) stub!\n", iface);
562     return E_NOTIMPL;
563 }
564
565 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl = {
566     IAssemblyCacheItemImpl_QueryInterface,
567     IAssemblyCacheItemImpl_AddRef,
568     IAssemblyCacheItemImpl_Release,
569     IAssemblyCacheItemImpl_CreateStream,
570     IAssemblyCacheItemImpl_Commit,
571     IAssemblyCacheItemImpl_AbortItem
572 };