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, BYTE architecture)
99 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};
105 GetWindowsDirectoryW(dir, size);
108 switch (architecture)
129 IAssemblyCache IAssemblyCache_iface;
133 } IAssemblyCacheImpl;
135 static inline IAssemblyCacheImpl *impl_from_IAssemblyCache(IAssemblyCache *iface)
137 return CONTAINING_RECORD(iface, IAssemblyCacheImpl, IAssemblyCache_iface);
140 static HRESULT WINAPI IAssemblyCacheImpl_QueryInterface(IAssemblyCache *iface,
141 REFIID riid, LPVOID *ppobj)
143 IAssemblyCacheImpl *This = impl_from_IAssemblyCache(iface);
145 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
149 if (IsEqualIID(riid, &IID_IUnknown) ||
150 IsEqualIID(riid, &IID_IAssemblyCache))
152 IUnknown_AddRef(iface);
157 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
158 return E_NOINTERFACE;
161 static ULONG WINAPI IAssemblyCacheImpl_AddRef(IAssemblyCache *iface)
163 IAssemblyCacheImpl *This = impl_from_IAssemblyCache(iface);
164 ULONG refCount = InterlockedIncrement(&This->ref);
166 TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
171 static ULONG WINAPI IAssemblyCacheImpl_Release(IAssemblyCache *iface)
173 IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
174 ULONG refCount = InterlockedDecrement( &cache->ref );
176 TRACE("(%p)->(ref before = %u)\n", cache, refCount + 1);
180 CloseHandle( cache->lock );
181 HeapFree( GetProcessHeap(), 0, cache );
186 static void cache_lock( IAssemblyCacheImpl *cache )
188 WaitForSingleObject( cache->lock, INFINITE );
191 static void cache_unlock( IAssemblyCacheImpl *cache )
193 ReleaseMutex( cache->lock );
196 static HRESULT WINAPI IAssemblyCacheImpl_UninstallAssembly(IAssemblyCache *iface,
198 LPCWSTR pszAssemblyName,
199 LPCFUSION_INSTALL_REFERENCE pRefData,
200 ULONG *pulDisposition)
203 IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
204 IAssemblyName *asmname, *next = NULL;
205 IAssemblyEnum *asmenum = NULL;
206 WCHAR *p, *path = NULL;
210 TRACE("(%p, 0%08x, %s, %p, %p)\n", iface, dwFlags,
211 debugstr_w(pszAssemblyName), pRefData, pulDisposition);
215 FIXME("application reference not supported\n");
218 hr = CreateAssemblyNameObject( &asmname, pszAssemblyName, CANOF_PARSE_DISPLAY_NAME, NULL );
224 hr = CreateAssemblyEnum( &asmenum, NULL, asmname, ASM_CACHE_GAC, NULL );
228 hr = IAssemblyEnum_GetNextAssembly( asmenum, NULL, &next, 0 );
232 *pulDisposition = IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED;
235 hr = IAssemblyName_GetPath( next, NULL, &len );
236 if (hr != HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER ))
239 if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
244 hr = IAssemblyName_GetPath( next, path, &len );
248 if (DeleteFileW( path ))
250 if ((p = strrchrW( path, '\\' )))
253 RemoveDirectoryW( path );
254 if ((p = strrchrW( path, '\\' )))
257 RemoveDirectoryW( path );
260 disp = IASSEMBLYCACHE_UNINSTALL_DISPOSITION_UNINSTALLED;
265 disp = IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED;
268 if (pulDisposition) *pulDisposition = disp;
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 );
279 static HRESULT WINAPI IAssemblyCacheImpl_QueryAssemblyInfo(IAssemblyCache *iface,
281 LPCWSTR pszAssemblyName,
282 ASSEMBLY_INFO *pAsmInfo)
284 IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
285 IAssemblyName *asmname, *next = NULL;
286 IAssemblyEnum *asmenum = NULL;
289 TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
290 debugstr_w(pszAssemblyName), pAsmInfo);
294 if (pAsmInfo->cbAssemblyInfo == 0)
295 pAsmInfo->cbAssemblyInfo = sizeof(ASSEMBLY_INFO);
296 else if (pAsmInfo->cbAssemblyInfo != sizeof(ASSEMBLY_INFO))
300 hr = CreateAssemblyNameObject(&asmname, pszAssemblyName,
301 CANOF_PARSE_DISPLAY_NAME, NULL);
307 hr = CreateAssemblyEnum(&asmenum, NULL, asmname, ASM_CACHE_GAC, NULL);
313 hr = IAssemblyEnum_GetNextAssembly(asmenum, NULL, &next, 0);
316 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
319 hr = IAssemblyName_IsEqual(asmname, next, ASM_CMPF_IL_ALL);
320 if (hr == S_OK) break;
326 hr = IAssemblyName_GetPath(next, pAsmInfo->pszCurrentAssemblyPathBuf, &pAsmInfo->cchBuf);
328 pAsmInfo->dwAssemblyFlags = ASSEMBLYINFO_FLAG_INSTALLED;
331 IAssemblyName_Release(asmname);
332 if (next) IAssemblyName_Release(next);
333 if (asmenum) IAssemblyEnum_Release(asmenum);
334 cache_unlock( cache );
338 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyCacheItem(IAssemblyCache *iface,
341 IAssemblyCacheItem **ppAsmItem,
342 LPCWSTR pszAssemblyName)
344 FIXME("(%p, %d, %p, %p, %s) stub!\n", iface, dwFlags, pvReserved,
345 ppAsmItem, debugstr_w(pszAssemblyName));
350 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyScavenger(IAssemblyCache *iface,
351 IUnknown **ppUnkReserved)
353 FIXME("(%p, %p) stub!\n", iface, ppUnkReserved);
357 static HRESULT WINAPI IAssemblyCacheImpl_InstallAssembly(IAssemblyCache *iface,
359 LPCWSTR pszManifestFilePath,
360 LPCFUSION_INSTALL_REFERENCE pRefData)
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);
370 LPWSTR version = NULL;
371 LPWSTR asmpath = NULL;
372 WCHAR path[MAX_PATH];
373 WCHAR asmdir[MAX_PATH];
377 TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
378 debugstr_w(pszManifestFilePath), pRefData);
380 if (!pszManifestFilePath || !*pszManifestFilePath)
383 if (!(ext = strrchrW(pszManifestFilePath, '.')))
384 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
386 if (lstrcmpiW(ext, ext_exe) && lstrcmpiW(ext, ext_dll))
387 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
389 if (GetFileAttributesW(pszManifestFilePath) == INVALID_FILE_ATTRIBUTES)
390 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
392 hr = assembly_create(&assembly, pszManifestFilePath);
395 hr = COR_E_ASSEMBLYEXPECTED;
399 hr = assembly_get_name(assembly, &name);
403 hr = assembly_get_pubkey_token(assembly, &token);
407 hr = assembly_get_version(assembly, &version);
413 get_assembly_directory(asmdir, MAX_PATH, assembly_get_architecture(assembly));
415 sprintfW(path, format, asmdir, name, version, token);
417 create_full_path(path);
419 hr = assembly_get_path(assembly, &asmpath);
423 filename = PathFindFileNameW(asmpath);
425 strcatW(path, filename);
426 if (!CopyFileW(asmpath, path, FALSE))
427 hr = HRESULT_FROM_WIN32(GetLastError());
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 );
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
450 /******************************************************************
451 * CreateAssemblyCache (FUSION.@)
453 HRESULT WINAPI CreateAssemblyCache(IAssemblyCache **ppAsmCache, DWORD dwReserved)
455 IAssemblyCacheImpl *cache;
457 TRACE("(%p, %d)\n", ppAsmCache, dwReserved);
464 cache = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyCacheImpl));
466 return E_OUTOFMEMORY;
468 cache->IAssemblyCache_iface.lpVtbl = &AssemblyCacheVtbl;
470 cache->lock = CreateMutexW( NULL, FALSE, cache_mutex_nameW );
473 HeapFree( GetProcessHeap(), 0, cache );
474 return HRESULT_FROM_WIN32( GetLastError() );
476 *ppAsmCache = &cache->IAssemblyCache_iface;
480 /* IAssemblyCacheItem */
483 IAssemblyCacheItem IAssemblyCacheItem_iface;
486 } IAssemblyCacheItemImpl;
488 static inline IAssemblyCacheItemImpl *impl_from_IAssemblyCacheItem(IAssemblyCacheItem *iface)
490 return CONTAINING_RECORD(iface, IAssemblyCacheItemImpl, IAssemblyCacheItem_iface);
493 static HRESULT WINAPI IAssemblyCacheItemImpl_QueryInterface(IAssemblyCacheItem *iface,
494 REFIID riid, LPVOID *ppobj)
496 IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
498 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
502 if (IsEqualIID(riid, &IID_IUnknown) ||
503 IsEqualIID(riid, &IID_IAssemblyCacheItem))
505 IUnknown_AddRef(iface);
510 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
511 return E_NOINTERFACE;
514 static ULONG WINAPI IAssemblyCacheItemImpl_AddRef(IAssemblyCacheItem *iface)
516 IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
517 ULONG refCount = InterlockedIncrement(&This->ref);
519 TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
524 static ULONG WINAPI IAssemblyCacheItemImpl_Release(IAssemblyCacheItem *iface)
526 IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
527 ULONG refCount = InterlockedDecrement(&This->ref);
529 TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
532 HeapFree(GetProcessHeap(), 0, This);
537 static HRESULT WINAPI IAssemblyCacheItemImpl_CreateStream(IAssemblyCacheItem *iface,
539 LPCWSTR pszStreamName,
543 ULARGE_INTEGER *puliMaxSize)
545 FIXME("(%p, %d, %s, %d, %d, %p, %p) stub!\n", iface, dwFlags,
546 debugstr_w(pszStreamName), dwFormat, dwFormatFlags, ppIStream, puliMaxSize);
551 static HRESULT WINAPI IAssemblyCacheItemImpl_Commit(IAssemblyCacheItem *iface,
553 ULONG *pulDisposition)
555 FIXME("(%p, %d, %p) stub!\n", iface, dwFlags, pulDisposition);
559 static HRESULT WINAPI IAssemblyCacheItemImpl_AbortItem(IAssemblyCacheItem *iface)
561 FIXME("(%p) stub!\n", iface);
565 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl = {
566 IAssemblyCacheItemImpl_QueryInterface,
567 IAssemblyCacheItemImpl_AddRef,
568 IAssemblyCacheItemImpl_Release,
569 IAssemblyCacheItemImpl_CreateStream,
570 IAssemblyCacheItemImpl_Commit,
571 IAssemblyCacheItemImpl_AbortItem