mscoree: Update Wine Mono to 9.2.0.
[wine.git] / dlls / fusion / asmcache.c
blob88805bb354bbf4e8010eea3c8e8041628d71f196
1 /*
2 * IAssemblyCache implementation
4 * Copyright 2008 James Hawkins
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.
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.
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
21 #include <stdarg.h>
22 #include <stdio.h>
24 #define COBJMACROS
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"
38 #include "fusionpriv.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(fusion);
43 typedef struct {
44 IAssemblyCache IAssemblyCache_iface;
46 LONG ref;
47 HANDLE lock;
48 } IAssemblyCacheImpl;
50 typedef struct {
51 IAssemblyCacheItem IAssemblyCacheItem_iface;
53 LONG ref;
54 } IAssemblyCacheItemImpl;
56 static const WCHAR cache_mutex_nameW[] =
57 {'_','_','W','I','N','E','_','F','U','S','I','O','N','_','C','A','C','H','E','_','M','U','T','E','X','_','_',0};
59 static BOOL create_full_path(LPCWSTR path)
61 LPWSTR new_path;
62 BOOL ret = TRUE;
63 int len;
65 if (!(new_path = malloc((lstrlenW(path) + 1) * sizeof(WCHAR)))) return FALSE;
67 lstrcpyW(new_path, path);
69 while ((len = lstrlenW(new_path)) && new_path[len - 1] == '\\')
70 new_path[len - 1] = 0;
72 while (!CreateDirectoryW(new_path, NULL))
74 LPWSTR slash;
75 DWORD last_error = GetLastError();
77 if(last_error == ERROR_ALREADY_EXISTS)
78 break;
80 if(last_error != ERROR_PATH_NOT_FOUND)
82 ret = FALSE;
83 break;
86 if(!(slash = wcsrchr(new_path, '\\')))
88 ret = FALSE;
89 break;
92 len = slash - new_path;
93 new_path[len] = 0;
94 if(!create_full_path(new_path))
96 ret = FALSE;
97 break;
100 new_path[len] = '\\';
103 free(new_path);
104 return ret;
107 static BOOL get_assembly_directory(LPWSTR dir, DWORD size, const char *version, PEKIND architecture)
109 static const WCHAR dotnet[] = {'\\','M','i','c','r','o','s','o','f','t','.','N','E','T','\\',0};
110 static const WCHAR gac[] = {'\\','a','s','s','e','m','b','l','y','\\','G','A','C',0};
111 static const WCHAR msil[] = {'_','M','S','I','L',0};
112 static const WCHAR x86[] = {'_','3','2',0};
113 static const WCHAR amd64[] = {'_','6','4',0};
114 DWORD len = GetWindowsDirectoryW(dir, size);
116 if (!strcmp(version, "v4.0.30319"))
118 lstrcpyW(dir + len, dotnet);
119 len += ARRAY_SIZE(dotnet) - 1;
120 lstrcpyW(dir + len, gac + 1);
121 len += ARRAY_SIZE(gac) - 2;
123 else
125 lstrcpyW(dir + len, gac);
126 len += ARRAY_SIZE(gac) - 1;
128 switch (architecture)
130 case peNone:
131 break;
133 case peMSIL:
134 lstrcpyW(dir + len, msil);
135 break;
137 case peI386:
138 lstrcpyW(dir + len, x86);
139 break;
141 case peAMD64:
142 lstrcpyW(dir + len, amd64);
143 break;
145 default:
146 WARN("unhandled architecture %u\n", architecture);
147 return FALSE;
149 return TRUE;
152 /* IAssemblyCache */
154 static inline IAssemblyCacheImpl *impl_from_IAssemblyCache(IAssemblyCache *iface)
156 return CONTAINING_RECORD(iface, IAssemblyCacheImpl, IAssemblyCache_iface);
159 static HRESULT WINAPI IAssemblyCacheImpl_QueryInterface(IAssemblyCache *iface,
160 REFIID riid, LPVOID *ppobj)
162 IAssemblyCacheImpl *This = impl_from_IAssemblyCache(iface);
164 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
166 *ppobj = NULL;
168 if (IsEqualIID(riid, &IID_IUnknown) ||
169 IsEqualIID(riid, &IID_IAssemblyCache))
171 IAssemblyCache_AddRef(iface);
172 *ppobj = &This->IAssemblyCache_iface;
173 return S_OK;
176 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
177 return E_NOINTERFACE;
180 static ULONG WINAPI IAssemblyCacheImpl_AddRef(IAssemblyCache *iface)
182 IAssemblyCacheImpl *This = impl_from_IAssemblyCache(iface);
183 ULONG refCount = InterlockedIncrement(&This->ref);
185 TRACE("(%p)->(ref before = %lu)\n", This, refCount - 1);
187 return refCount;
190 static ULONG WINAPI IAssemblyCacheImpl_Release(IAssemblyCache *iface)
192 IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
193 ULONG refCount = InterlockedDecrement( &cache->ref );
195 TRACE("(%p)->(ref before = %lu)\n", cache, refCount + 1);
197 if (!refCount)
199 CloseHandle( cache->lock );
200 free( cache );
202 return refCount;
205 static void cache_lock( IAssemblyCacheImpl *cache )
207 WaitForSingleObject( cache->lock, INFINITE );
210 static void cache_unlock( IAssemblyCacheImpl *cache )
212 ReleaseMutex( cache->lock );
215 static HRESULT WINAPI IAssemblyCacheImpl_UninstallAssembly(IAssemblyCache *iface,
216 DWORD dwFlags,
217 LPCWSTR pszAssemblyName,
218 LPCFUSION_INSTALL_REFERENCE pRefData,
219 ULONG *pulDisposition)
221 HRESULT hr;
222 IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
223 IAssemblyName *asmname, *next = NULL;
224 IAssemblyEnum *asmenum = NULL;
225 WCHAR *p, *path = NULL;
226 ULONG disp;
227 DWORD len;
229 TRACE("(%p, 0%08lx, %s, %p, %p)\n", iface, dwFlags,
230 debugstr_w(pszAssemblyName), pRefData, pulDisposition);
232 if (pRefData)
234 FIXME("application reference not supported\n");
235 return E_NOTIMPL;
237 hr = CreateAssemblyNameObject( &asmname, pszAssemblyName, CANOF_PARSE_DISPLAY_NAME, NULL );
238 if (FAILED( hr ))
239 return hr;
241 cache_lock( cache );
243 hr = CreateAssemblyEnum( &asmenum, NULL, asmname, ASM_CACHE_GAC, NULL );
244 if (FAILED( hr ))
245 goto done;
247 hr = IAssemblyEnum_GetNextAssembly( asmenum, NULL, &next, 0 );
248 if (hr == S_FALSE)
250 if (pulDisposition)
251 *pulDisposition = IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED;
252 goto done;
254 hr = IAssemblyName_GetPath( next, NULL, &len );
255 if (hr != HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER ))
256 goto done;
258 if (!(path = malloc( len * sizeof(WCHAR) )))
260 hr = E_OUTOFMEMORY;
261 goto done;
263 hr = IAssemblyName_GetPath( next, path, &len );
264 if (FAILED( hr ))
265 goto done;
267 if (DeleteFileW( path ))
269 if ((p = wcsrchr( path, '\\' )))
271 *p = 0;
272 RemoveDirectoryW( path );
273 if ((p = wcsrchr( path, '\\' )))
275 *p = 0;
276 RemoveDirectoryW( path );
279 disp = IASSEMBLYCACHE_UNINSTALL_DISPOSITION_UNINSTALLED;
280 hr = S_OK;
282 else
284 disp = IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED;
285 hr = S_FALSE;
287 if (pulDisposition) *pulDisposition = disp;
289 done:
290 IAssemblyName_Release( asmname );
291 if (next) IAssemblyName_Release( next );
292 if (asmenum) IAssemblyEnum_Release( asmenum );
293 free( path );
294 cache_unlock( cache );
295 return hr;
298 static HRESULT WINAPI IAssemblyCacheImpl_QueryAssemblyInfo(IAssemblyCache *iface,
299 DWORD dwFlags,
300 LPCWSTR pszAssemblyName,
301 ASSEMBLY_INFO *pAsmInfo)
303 IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
304 IAssemblyName *asmname, *next = NULL;
305 IAssemblyEnum *asmenum = NULL;
306 HRESULT hr;
308 TRACE("(%p, %ld, %s, %p)\n", iface, dwFlags,
309 debugstr_w(pszAssemblyName), pAsmInfo);
311 if (pAsmInfo)
313 if (pAsmInfo->cbAssemblyInfo == 0)
314 pAsmInfo->cbAssemblyInfo = sizeof(ASSEMBLY_INFO);
315 else if (pAsmInfo->cbAssemblyInfo != sizeof(ASSEMBLY_INFO))
316 return E_INVALIDARG;
319 hr = CreateAssemblyNameObject(&asmname, pszAssemblyName,
320 CANOF_PARSE_DISPLAY_NAME, NULL);
321 if (FAILED(hr))
322 return hr;
324 cache_lock( cache );
326 hr = CreateAssemblyEnum(&asmenum, NULL, asmname, ASM_CACHE_GAC, NULL);
327 if (FAILED(hr))
328 goto done;
330 for (;;)
332 hr = IAssemblyEnum_GetNextAssembly(asmenum, NULL, &next, 0);
333 if (hr != S_OK)
335 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
336 goto done;
338 hr = IAssemblyName_IsEqual(asmname, next, ASM_CMPF_IL_ALL);
339 if (hr == S_OK) break;
342 if (!pAsmInfo)
343 goto done;
345 hr = IAssemblyName_GetPath(next, pAsmInfo->pszCurrentAssemblyPathBuf, &pAsmInfo->cchBuf);
347 pAsmInfo->dwAssemblyFlags = ASSEMBLYINFO_FLAG_INSTALLED;
349 done:
350 IAssemblyName_Release(asmname);
351 if (next) IAssemblyName_Release(next);
352 if (asmenum) IAssemblyEnum_Release(asmenum);
353 cache_unlock( cache );
354 return hr;
357 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl;
359 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyCacheItem(IAssemblyCache *iface,
360 DWORD dwFlags,
361 PVOID pvReserved,
362 IAssemblyCacheItem **ppAsmItem,
363 LPCWSTR pszAssemblyName)
365 IAssemblyCacheItemImpl *item;
367 FIXME("(%p, %ld, %p, %p, %s) semi-stub!\n", iface, dwFlags, pvReserved,
368 ppAsmItem, debugstr_w(pszAssemblyName));
370 if (!ppAsmItem)
371 return E_INVALIDARG;
373 *ppAsmItem = NULL;
375 if (!(item = malloc(sizeof(*item)))) return E_OUTOFMEMORY;
377 item->IAssemblyCacheItem_iface.lpVtbl = &AssemblyCacheItemVtbl;
378 item->ref = 1;
380 *ppAsmItem = &item->IAssemblyCacheItem_iface;
381 return S_OK;
384 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyScavenger(IAssemblyCache *iface,
385 IUnknown **ppUnkReserved)
387 FIXME("(%p, %p) stub!\n", iface, ppUnkReserved);
388 return E_NOTIMPL;
391 static HRESULT copy_file( const WCHAR *src_dir, DWORD src_len, const WCHAR *dst_dir, DWORD dst_len,
392 const WCHAR *filename )
394 WCHAR *src_file, *dst_file;
395 DWORD len = lstrlenW( filename );
396 HRESULT hr = S_OK;
398 if (!(src_file = malloc( (src_len + len + 1) * sizeof(WCHAR) )))
399 return E_OUTOFMEMORY;
400 memcpy( src_file, src_dir, src_len * sizeof(WCHAR) );
401 lstrcpyW( src_file + src_len, filename );
403 if (!(dst_file = malloc( (dst_len + len + 1) * sizeof(WCHAR) )))
405 free( src_file );
406 return E_OUTOFMEMORY;
408 memcpy( dst_file, dst_dir, dst_len * sizeof(WCHAR) );
409 lstrcpyW( dst_file + dst_len, filename );
411 if (!CopyFileW( src_file, dst_file, FALSE )) hr = HRESULT_FROM_WIN32( GetLastError() );
412 free( src_file );
413 free( dst_file );
414 return hr;
417 static HRESULT WINAPI IAssemblyCacheImpl_InstallAssembly(IAssemblyCache *iface,
418 DWORD dwFlags,
419 LPCWSTR pszManifestFilePath,
420 LPCFUSION_INSTALL_REFERENCE pRefData)
422 static const WCHAR format[] =
423 {'%','s','\\','%','s','\\','%','s','_','_','%','s','\\',0};
424 static const WCHAR format_v40[] =
425 {'%','s','\\','%','s','\\','v','4','.','0','_','%','s','_','_','%','s','\\',0};
426 static const WCHAR ext_exe[] = {'.','e','x','e',0};
427 static const WCHAR ext_dll[] = {'.','d','l','l',0};
428 IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
429 ASSEMBLY *assembly;
430 const WCHAR *extension, *filename, *src_dir;
431 WCHAR *name = NULL, *token = NULL, *version = NULL, *asmpath = NULL;
432 WCHAR asmdir[MAX_PATH], *p, **external_files = NULL, *dst_dir = NULL;
433 PEKIND architecture;
434 char *clr_version;
435 DWORD i, count = 0, src_len, dst_len = ARRAY_SIZE(format_v40);
436 HRESULT hr;
438 TRACE("(%p, %ld, %s, %p)\n", iface, dwFlags,
439 debugstr_w(pszManifestFilePath), pRefData);
441 if (!pszManifestFilePath || !*pszManifestFilePath)
442 return E_INVALIDARG;
444 if (!(extension = wcsrchr(pszManifestFilePath, '.')))
445 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
447 if (lstrcmpiW(extension, ext_exe) && lstrcmpiW(extension, ext_dll))
448 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
450 if (GetFileAttributesW(pszManifestFilePath) == INVALID_FILE_ATTRIBUTES)
451 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
453 hr = assembly_create(&assembly, pszManifestFilePath);
454 if (FAILED(hr))
456 hr = COR_E_ASSEMBLYEXPECTED;
457 goto done;
460 hr = assembly_get_name(assembly, &name);
461 if (FAILED(hr))
462 goto done;
464 hr = assembly_get_pubkey_token(assembly, &token);
465 if (FAILED(hr))
466 goto done;
468 hr = assembly_get_version(assembly, &version);
469 if (FAILED(hr))
470 goto done;
472 hr = assembly_get_runtime_version(assembly, &clr_version);
473 if (FAILED(hr))
474 goto done;
476 hr = assembly_get_external_files(assembly, &external_files, &count);
477 if (FAILED(hr))
478 goto done;
480 cache_lock( cache );
482 architecture = assembly_get_architecture(assembly);
483 get_assembly_directory(asmdir, MAX_PATH, clr_version, architecture);
485 dst_len += lstrlenW(asmdir) + lstrlenW(name) + lstrlenW(version) + lstrlenW(token);
486 if (!(dst_dir = malloc(dst_len * sizeof(WCHAR))))
488 hr = E_OUTOFMEMORY;
489 goto done;
491 if (!strcmp(clr_version, "v4.0.30319"))
492 dst_len = swprintf(dst_dir, dst_len, format_v40, asmdir, name, version, token);
493 else
494 dst_len = swprintf(dst_dir, dst_len, format, asmdir, name, version, token);
496 create_full_path(dst_dir);
498 hr = assembly_get_path(assembly, &asmpath);
499 if (FAILED(hr))
500 goto done;
502 if ((p = wcsrchr(asmpath, '\\')))
504 filename = p + 1;
505 src_dir = asmpath;
506 src_len = filename - asmpath;
508 else
510 filename = asmpath;
511 src_dir = NULL;
512 src_len = 0;
514 hr = copy_file(src_dir, src_len, dst_dir, dst_len, filename);
515 if (FAILED(hr))
516 goto done;
518 for (i = 0; i < count; i++)
520 hr = copy_file(src_dir, src_len, dst_dir, dst_len, external_files[i]);
521 if (FAILED(hr))
522 break;
525 done:
526 free(name);
527 free(token);
528 free(version);
529 free(asmpath);
530 free(dst_dir);
531 for (i = 0; i < count; i++) free(external_files[i]);
532 free(external_files);
533 assembly_release(assembly);
534 cache_unlock( cache );
535 return hr;
538 static const IAssemblyCacheVtbl AssemblyCacheVtbl = {
539 IAssemblyCacheImpl_QueryInterface,
540 IAssemblyCacheImpl_AddRef,
541 IAssemblyCacheImpl_Release,
542 IAssemblyCacheImpl_UninstallAssembly,
543 IAssemblyCacheImpl_QueryAssemblyInfo,
544 IAssemblyCacheImpl_CreateAssemblyCacheItem,
545 IAssemblyCacheImpl_CreateAssemblyScavenger,
546 IAssemblyCacheImpl_InstallAssembly
549 /******************************************************************
550 * CreateAssemblyCache (FUSION.@)
552 HRESULT WINAPI CreateAssemblyCache(IAssemblyCache **ppAsmCache, DWORD dwReserved)
554 IAssemblyCacheImpl *cache;
556 TRACE("(%p, %ld)\n", ppAsmCache, dwReserved);
558 if (!ppAsmCache)
559 return E_INVALIDARG;
561 *ppAsmCache = NULL;
563 if (!(cache = malloc(sizeof(*cache)))) return E_OUTOFMEMORY;
565 cache->IAssemblyCache_iface.lpVtbl = &AssemblyCacheVtbl;
566 cache->ref = 1;
567 cache->lock = CreateMutexW( NULL, FALSE, cache_mutex_nameW );
568 if (!cache->lock)
570 free( cache );
571 return HRESULT_FROM_WIN32( GetLastError() );
573 *ppAsmCache = &cache->IAssemblyCache_iface;
574 return S_OK;
577 /* IAssemblyCacheItem */
579 static inline IAssemblyCacheItemImpl *impl_from_IAssemblyCacheItem(IAssemblyCacheItem *iface)
581 return CONTAINING_RECORD(iface, IAssemblyCacheItemImpl, IAssemblyCacheItem_iface);
584 static HRESULT WINAPI IAssemblyCacheItemImpl_QueryInterface(IAssemblyCacheItem *iface,
585 REFIID riid, LPVOID *ppobj)
587 IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
589 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
591 *ppobj = NULL;
593 if (IsEqualIID(riid, &IID_IUnknown) ||
594 IsEqualIID(riid, &IID_IAssemblyCacheItem))
596 IAssemblyCacheItem_AddRef(iface);
597 *ppobj = &This->IAssemblyCacheItem_iface;
598 return S_OK;
601 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
602 return E_NOINTERFACE;
605 static ULONG WINAPI IAssemblyCacheItemImpl_AddRef(IAssemblyCacheItem *iface)
607 IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
608 ULONG refCount = InterlockedIncrement(&This->ref);
610 TRACE("(%p)->(ref before = %lu)\n", This, refCount - 1);
612 return refCount;
615 static ULONG WINAPI IAssemblyCacheItemImpl_Release(IAssemblyCacheItem *iface)
617 IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
618 ULONG refCount = InterlockedDecrement(&This->ref);
620 TRACE("(%p)->(ref before = %lu)\n", This, refCount + 1);
622 if (!refCount)
623 free(This);
625 return refCount;
628 static HRESULT WINAPI IAssemblyCacheItemImpl_CreateStream(IAssemblyCacheItem *iface,
629 DWORD dwFlags,
630 LPCWSTR pszStreamName,
631 DWORD dwFormat,
632 DWORD dwFormatFlags,
633 IStream **ppIStream,
634 ULARGE_INTEGER *puliMaxSize)
636 FIXME("(%p, %ld, %s, %ld, %ld, %p, %p) stub!\n", iface, dwFlags,
637 debugstr_w(pszStreamName), dwFormat, dwFormatFlags, ppIStream, puliMaxSize);
639 return E_NOTIMPL;
642 static HRESULT WINAPI IAssemblyCacheItemImpl_Commit(IAssemblyCacheItem *iface,
643 DWORD dwFlags,
644 ULONG *pulDisposition)
646 FIXME("(%p, %ld, %p) stub!\n", iface, dwFlags, pulDisposition);
647 return E_NOTIMPL;
650 static HRESULT WINAPI IAssemblyCacheItemImpl_AbortItem(IAssemblyCacheItem *iface)
652 FIXME("(%p) stub!\n", iface);
653 return E_NOTIMPL;
656 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl = {
657 IAssemblyCacheItemImpl_QueryInterface,
658 IAssemblyCacheItemImpl_AddRef,
659 IAssemblyCacheItemImpl_Release,
660 IAssemblyCacheItemImpl_CreateStream,
661 IAssemblyCacheItemImpl_Commit,
662 IAssemblyCacheItemImpl_AbortItem