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
38 #include "fusionpriv.h"
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(fusion);
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};
47 static BOOL create_full_path(LPCWSTR path)
53 new_path = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1) * sizeof(WCHAR));
57 strcpyW(new_path, path);
59 while ((len = strlenW(new_path)) && new_path[len - 1] == '\\')
60 new_path[len - 1] = 0;
62 while (!CreateDirectoryW(new_path, NULL))
65 DWORD last_error = GetLastError();
67 if(last_error == ERROR_ALREADY_EXISTS)
70 if(last_error != ERROR_PATH_NOT_FOUND)
76 if(!(slash = strrchrW(new_path, '\\')))
82 len = slash - new_path;
84 if(!create_full_path(new_path))
93 HeapFree(GetProcessHeap(), 0, new_path);
97 static BOOL get_assembly_directory(LPWSTR dir, DWORD size, const char *version, PEKIND architecture)
99 static const WCHAR dotnet[] = {'\\','M','i','c','r','o','s','o','f','t','.','N','E','T','\\',0};
100 static const WCHAR gac[] = {'\\','a','s','s','e','m','b','l','y','\\','G','A','C',0};
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 DWORD len = GetWindowsDirectoryW(dir, size);
106 if (!strcmp(version, "v4.0.30319"))
108 strcpyW(dir + len, dotnet);
109 len += sizeof(dotnet)/sizeof(WCHAR) -1;
110 strcpyW(dir + len, gac + 1);
111 len += sizeof(gac)/sizeof(WCHAR) - 2;
115 strcpyW(dir + len, gac);
116 len += sizeof(gac)/sizeof(WCHAR) - 1;
118 switch (architecture)
124 strcpyW(dir + len, msil);
128 strcpyW(dir + len, x86);
132 strcpyW(dir + len, amd64);
136 WARN("unhandled architecture %u\n", architecture);
145 IAssemblyCache IAssemblyCache_iface;
149 } IAssemblyCacheImpl;
151 static inline IAssemblyCacheImpl *impl_from_IAssemblyCache(IAssemblyCache *iface)
153 return CONTAINING_RECORD(iface, IAssemblyCacheImpl, IAssemblyCache_iface);
156 static HRESULT WINAPI IAssemblyCacheImpl_QueryInterface(IAssemblyCache *iface,
157 REFIID riid, LPVOID *ppobj)
159 IAssemblyCacheImpl *This = impl_from_IAssemblyCache(iface);
161 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
165 if (IsEqualIID(riid, &IID_IUnknown) ||
166 IsEqualIID(riid, &IID_IAssemblyCache))
168 IUnknown_AddRef(iface);
173 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
174 return E_NOINTERFACE;
177 static ULONG WINAPI IAssemblyCacheImpl_AddRef(IAssemblyCache *iface)
179 IAssemblyCacheImpl *This = impl_from_IAssemblyCache(iface);
180 ULONG refCount = InterlockedIncrement(&This->ref);
182 TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
187 static ULONG WINAPI IAssemblyCacheImpl_Release(IAssemblyCache *iface)
189 IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
190 ULONG refCount = InterlockedDecrement( &cache->ref );
192 TRACE("(%p)->(ref before = %u)\n", cache, refCount + 1);
196 CloseHandle( cache->lock );
197 HeapFree( GetProcessHeap(), 0, cache );
202 static void cache_lock( IAssemblyCacheImpl *cache )
204 WaitForSingleObject( cache->lock, INFINITE );
207 static void cache_unlock( IAssemblyCacheImpl *cache )
209 ReleaseMutex( cache->lock );
212 static HRESULT WINAPI IAssemblyCacheImpl_UninstallAssembly(IAssemblyCache *iface,
214 LPCWSTR pszAssemblyName,
215 LPCFUSION_INSTALL_REFERENCE pRefData,
216 ULONG *pulDisposition)
219 IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
220 IAssemblyName *asmname, *next = NULL;
221 IAssemblyEnum *asmenum = NULL;
222 WCHAR *p, *path = NULL;
226 TRACE("(%p, 0%08x, %s, %p, %p)\n", iface, dwFlags,
227 debugstr_w(pszAssemblyName), pRefData, pulDisposition);
231 FIXME("application reference not supported\n");
234 hr = CreateAssemblyNameObject( &asmname, pszAssemblyName, CANOF_PARSE_DISPLAY_NAME, NULL );
240 hr = CreateAssemblyEnum( &asmenum, NULL, asmname, ASM_CACHE_GAC, NULL );
244 hr = IAssemblyEnum_GetNextAssembly( asmenum, NULL, &next, 0 );
248 *pulDisposition = IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED;
251 hr = IAssemblyName_GetPath( next, NULL, &len );
252 if (hr != HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER ))
255 if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
260 hr = IAssemblyName_GetPath( next, path, &len );
264 if (DeleteFileW( path ))
266 if ((p = strrchrW( path, '\\' )))
269 RemoveDirectoryW( path );
270 if ((p = strrchrW( path, '\\' )))
273 RemoveDirectoryW( path );
276 disp = IASSEMBLYCACHE_UNINSTALL_DISPOSITION_UNINSTALLED;
281 disp = IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED;
284 if (pulDisposition) *pulDisposition = disp;
287 IAssemblyName_Release( asmname );
288 if (next) IAssemblyName_Release( next );
289 if (asmenum) IAssemblyEnum_Release( asmenum );
290 HeapFree( GetProcessHeap(), 0, path );
291 cache_unlock( cache );
295 static HRESULT WINAPI IAssemblyCacheImpl_QueryAssemblyInfo(IAssemblyCache *iface,
297 LPCWSTR pszAssemblyName,
298 ASSEMBLY_INFO *pAsmInfo)
300 IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
301 IAssemblyName *asmname, *next = NULL;
302 IAssemblyEnum *asmenum = NULL;
305 TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
306 debugstr_w(pszAssemblyName), pAsmInfo);
310 if (pAsmInfo->cbAssemblyInfo == 0)
311 pAsmInfo->cbAssemblyInfo = sizeof(ASSEMBLY_INFO);
312 else if (pAsmInfo->cbAssemblyInfo != sizeof(ASSEMBLY_INFO))
316 hr = CreateAssemblyNameObject(&asmname, pszAssemblyName,
317 CANOF_PARSE_DISPLAY_NAME, NULL);
323 hr = CreateAssemblyEnum(&asmenum, NULL, asmname, ASM_CACHE_GAC, NULL);
329 hr = IAssemblyEnum_GetNextAssembly(asmenum, NULL, &next, 0);
332 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
335 hr = IAssemblyName_IsEqual(asmname, next, ASM_CMPF_IL_ALL);
336 if (hr == S_OK) break;
342 hr = IAssemblyName_GetPath(next, pAsmInfo->pszCurrentAssemblyPathBuf, &pAsmInfo->cchBuf);
344 pAsmInfo->dwAssemblyFlags = ASSEMBLYINFO_FLAG_INSTALLED;
347 IAssemblyName_Release(asmname);
348 if (next) IAssemblyName_Release(next);
349 if (asmenum) IAssemblyEnum_Release(asmenum);
350 cache_unlock( cache );
354 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyCacheItem(IAssemblyCache *iface,
357 IAssemblyCacheItem **ppAsmItem,
358 LPCWSTR pszAssemblyName)
360 FIXME("(%p, %d, %p, %p, %s) stub!\n", iface, dwFlags, pvReserved,
361 ppAsmItem, debugstr_w(pszAssemblyName));
366 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyScavenger(IAssemblyCache *iface,
367 IUnknown **ppUnkReserved)
369 FIXME("(%p, %p) stub!\n", iface, ppUnkReserved);
373 static HRESULT WINAPI IAssemblyCacheImpl_InstallAssembly(IAssemblyCache *iface,
375 LPCWSTR pszManifestFilePath,
376 LPCFUSION_INSTALL_REFERENCE pRefData)
378 static const WCHAR format[] =
379 {'%','s','\\','%','s','\\','%','s','_','_','%','s','\\',0};
380 static const WCHAR format_v40[] =
381 {'%','s','\\','%','s','\\','v','4','.','0','_','%','s','_','_','%','s','\\',0};
382 static const WCHAR ext_exe[] = {'.','e','x','e',0};
383 static const WCHAR ext_dll[] = {'.','d','l','l',0};
384 IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
386 WCHAR *filename, *ext;
387 WCHAR *name = NULL, *token = NULL, *version = NULL, *asmpath = NULL;
388 WCHAR path[MAX_PATH], asmdir[MAX_PATH];
393 TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
394 debugstr_w(pszManifestFilePath), pRefData);
396 if (!pszManifestFilePath || !*pszManifestFilePath)
399 if (!(ext = strrchrW(pszManifestFilePath, '.')))
400 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
402 if (lstrcmpiW(ext, ext_exe) && lstrcmpiW(ext, ext_dll))
403 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
405 if (GetFileAttributesW(pszManifestFilePath) == INVALID_FILE_ATTRIBUTES)
406 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
408 hr = assembly_create(&assembly, pszManifestFilePath);
411 hr = COR_E_ASSEMBLYEXPECTED;
415 hr = assembly_get_name(assembly, &name);
419 hr = assembly_get_pubkey_token(assembly, &token);
423 hr = assembly_get_version(assembly, &version);
427 hr = assembly_get_runtime_version(assembly, &clr_version);
433 architecture = assembly_get_architecture(assembly);
434 get_assembly_directory(asmdir, MAX_PATH, clr_version, architecture);
436 if (!strcmp(clr_version, "v4.0.30319"))
437 sprintfW(path, format_v40, asmdir, name, version, token);
439 sprintfW(path, format, asmdir, name, version, token);
441 create_full_path(path);
443 hr = assembly_get_path(assembly, &asmpath);
447 filename = PathFindFileNameW(asmpath);
449 strcatW(path, filename);
450 if (!CopyFileW(asmpath, path, FALSE))
451 hr = HRESULT_FROM_WIN32(GetLastError());
454 HeapFree(GetProcessHeap(), 0, name);
455 HeapFree(GetProcessHeap(), 0, token);
456 HeapFree(GetProcessHeap(), 0, version);
457 HeapFree(GetProcessHeap(), 0, asmpath);
458 assembly_release(assembly);
459 cache_unlock( cache );
463 static const IAssemblyCacheVtbl AssemblyCacheVtbl = {
464 IAssemblyCacheImpl_QueryInterface,
465 IAssemblyCacheImpl_AddRef,
466 IAssemblyCacheImpl_Release,
467 IAssemblyCacheImpl_UninstallAssembly,
468 IAssemblyCacheImpl_QueryAssemblyInfo,
469 IAssemblyCacheImpl_CreateAssemblyCacheItem,
470 IAssemblyCacheImpl_CreateAssemblyScavenger,
471 IAssemblyCacheImpl_InstallAssembly
474 /******************************************************************
475 * CreateAssemblyCache (FUSION.@)
477 HRESULT WINAPI CreateAssemblyCache(IAssemblyCache **ppAsmCache, DWORD dwReserved)
479 IAssemblyCacheImpl *cache;
481 TRACE("(%p, %d)\n", ppAsmCache, dwReserved);
488 cache = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyCacheImpl));
490 return E_OUTOFMEMORY;
492 cache->IAssemblyCache_iface.lpVtbl = &AssemblyCacheVtbl;
494 cache->lock = CreateMutexW( NULL, FALSE, cache_mutex_nameW );
497 HeapFree( GetProcessHeap(), 0, cache );
498 return HRESULT_FROM_WIN32( GetLastError() );
500 *ppAsmCache = &cache->IAssemblyCache_iface;
504 /* IAssemblyCacheItem */
507 IAssemblyCacheItem IAssemblyCacheItem_iface;
510 } IAssemblyCacheItemImpl;
512 static inline IAssemblyCacheItemImpl *impl_from_IAssemblyCacheItem(IAssemblyCacheItem *iface)
514 return CONTAINING_RECORD(iface, IAssemblyCacheItemImpl, IAssemblyCacheItem_iface);
517 static HRESULT WINAPI IAssemblyCacheItemImpl_QueryInterface(IAssemblyCacheItem *iface,
518 REFIID riid, LPVOID *ppobj)
520 IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
522 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
526 if (IsEqualIID(riid, &IID_IUnknown) ||
527 IsEqualIID(riid, &IID_IAssemblyCacheItem))
529 IUnknown_AddRef(iface);
534 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
535 return E_NOINTERFACE;
538 static ULONG WINAPI IAssemblyCacheItemImpl_AddRef(IAssemblyCacheItem *iface)
540 IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
541 ULONG refCount = InterlockedIncrement(&This->ref);
543 TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
548 static ULONG WINAPI IAssemblyCacheItemImpl_Release(IAssemblyCacheItem *iface)
550 IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
551 ULONG refCount = InterlockedDecrement(&This->ref);
553 TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
556 HeapFree(GetProcessHeap(), 0, This);
561 static HRESULT WINAPI IAssemblyCacheItemImpl_CreateStream(IAssemblyCacheItem *iface,
563 LPCWSTR pszStreamName,
567 ULARGE_INTEGER *puliMaxSize)
569 FIXME("(%p, %d, %s, %d, %d, %p, %p) stub!\n", iface, dwFlags,
570 debugstr_w(pszStreamName), dwFormat, dwFormatFlags, ppIStream, puliMaxSize);
575 static HRESULT WINAPI IAssemblyCacheItemImpl_Commit(IAssemblyCacheItem *iface,
577 ULONG *pulDisposition)
579 FIXME("(%p, %d, %p) stub!\n", iface, dwFlags, pulDisposition);
583 static HRESULT WINAPI IAssemblyCacheItemImpl_AbortItem(IAssemblyCacheItem *iface)
585 FIXME("(%p) stub!\n", iface);
589 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl = {
590 IAssemblyCacheItemImpl_QueryInterface,
591 IAssemblyCacheItemImpl_AddRef,
592 IAssemblyCacheItemImpl_Release,
593 IAssemblyCacheItemImpl_CreateStream,
594 IAssemblyCacheItemImpl_Commit,
595 IAssemblyCacheItemImpl_AbortItem