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);
311 hr = IAssemblyEnum_GetNextAssembly(asmenum, NULL, &next, 0);
314 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
321 hr = IAssemblyName_GetPath(next, pAsmInfo->pszCurrentAssemblyPathBuf, &pAsmInfo->cchBuf);
323 pAsmInfo->dwAssemblyFlags = ASSEMBLYINFO_FLAG_INSTALLED;
326 IAssemblyName_Release(asmname);
327 if (next) IAssemblyName_Release(next);
328 if (asmenum) IAssemblyEnum_Release(asmenum);
329 cache_unlock( cache );
333 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyCacheItem(IAssemblyCache *iface,
336 IAssemblyCacheItem **ppAsmItem,
337 LPCWSTR pszAssemblyName)
339 FIXME("(%p, %d, %p, %p, %s) stub!\n", iface, dwFlags, pvReserved,
340 ppAsmItem, debugstr_w(pszAssemblyName));
345 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyScavenger(IAssemblyCache *iface,
346 IUnknown **ppUnkReserved)
348 FIXME("(%p, %p) stub!\n", iface, ppUnkReserved);
352 static HRESULT WINAPI IAssemblyCacheImpl_InstallAssembly(IAssemblyCache *iface,
354 LPCWSTR pszManifestFilePath,
355 LPCFUSION_INSTALL_REFERENCE pRefData)
357 static const WCHAR format[] = {'%','s','\\','%','s','\\','%','s','_','_','%','s','\\',0};
358 static const WCHAR ext_exe[] = {'.','e','x','e',0};
359 static const WCHAR ext_dll[] = {'.','d','l','l',0};
360 IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
365 LPWSTR version = NULL;
366 LPWSTR asmpath = NULL;
367 WCHAR path[MAX_PATH];
368 WCHAR asmdir[MAX_PATH];
372 TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
373 debugstr_w(pszManifestFilePath), pRefData);
375 if (!pszManifestFilePath || !*pszManifestFilePath)
378 if (!(ext = strrchrW(pszManifestFilePath, '.')))
379 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
381 if (lstrcmpiW(ext, ext_exe) && lstrcmpiW(ext, ext_dll))
382 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
384 if (GetFileAttributesW(pszManifestFilePath) == INVALID_FILE_ATTRIBUTES)
385 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
387 hr = assembly_create(&assembly, pszManifestFilePath);
390 hr = COR_E_ASSEMBLYEXPECTED;
394 hr = assembly_get_name(assembly, &name);
398 hr = assembly_get_pubkey_token(assembly, &token);
402 hr = assembly_get_version(assembly, &version);
408 get_assembly_directory(asmdir, MAX_PATH, assembly_get_architecture(assembly));
410 sprintfW(path, format, asmdir, name, version, token);
412 create_full_path(path);
414 hr = assembly_get_path(assembly, &asmpath);
418 filename = PathFindFileNameW(asmpath);
420 strcatW(path, filename);
421 if (!CopyFileW(asmpath, path, FALSE))
422 hr = HRESULT_FROM_WIN32(GetLastError());
425 HeapFree(GetProcessHeap(), 0, name);
426 HeapFree(GetProcessHeap(), 0, token);
427 HeapFree(GetProcessHeap(), 0, version);
428 HeapFree(GetProcessHeap(), 0, asmpath);
429 assembly_release(assembly);
430 cache_unlock( cache );
434 static const IAssemblyCacheVtbl AssemblyCacheVtbl = {
435 IAssemblyCacheImpl_QueryInterface,
436 IAssemblyCacheImpl_AddRef,
437 IAssemblyCacheImpl_Release,
438 IAssemblyCacheImpl_UninstallAssembly,
439 IAssemblyCacheImpl_QueryAssemblyInfo,
440 IAssemblyCacheImpl_CreateAssemblyCacheItem,
441 IAssemblyCacheImpl_CreateAssemblyScavenger,
442 IAssemblyCacheImpl_InstallAssembly
445 /******************************************************************
446 * CreateAssemblyCache (FUSION.@)
448 HRESULT WINAPI CreateAssemblyCache(IAssemblyCache **ppAsmCache, DWORD dwReserved)
450 IAssemblyCacheImpl *cache;
452 TRACE("(%p, %d)\n", ppAsmCache, dwReserved);
459 cache = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyCacheImpl));
461 return E_OUTOFMEMORY;
463 cache->IAssemblyCache_iface.lpVtbl = &AssemblyCacheVtbl;
465 cache->lock = CreateMutexW( NULL, FALSE, cache_mutex_nameW );
468 HeapFree( GetProcessHeap(), 0, cache );
469 return HRESULT_FROM_WIN32( GetLastError() );
471 *ppAsmCache = &cache->IAssemblyCache_iface;
475 /* IAssemblyCacheItem */
478 IAssemblyCacheItem IAssemblyCacheItem_iface;
481 } IAssemblyCacheItemImpl;
483 static inline IAssemblyCacheItemImpl *impl_from_IAssemblyCacheItem(IAssemblyCacheItem *iface)
485 return CONTAINING_RECORD(iface, IAssemblyCacheItemImpl, IAssemblyCacheItem_iface);
488 static HRESULT WINAPI IAssemblyCacheItemImpl_QueryInterface(IAssemblyCacheItem *iface,
489 REFIID riid, LPVOID *ppobj)
491 IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
493 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
497 if (IsEqualIID(riid, &IID_IUnknown) ||
498 IsEqualIID(riid, &IID_IAssemblyCacheItem))
500 IUnknown_AddRef(iface);
505 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
506 return E_NOINTERFACE;
509 static ULONG WINAPI IAssemblyCacheItemImpl_AddRef(IAssemblyCacheItem *iface)
511 IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
512 ULONG refCount = InterlockedIncrement(&This->ref);
514 TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
519 static ULONG WINAPI IAssemblyCacheItemImpl_Release(IAssemblyCacheItem *iface)
521 IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
522 ULONG refCount = InterlockedDecrement(&This->ref);
524 TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
527 HeapFree(GetProcessHeap(), 0, This);
532 static HRESULT WINAPI IAssemblyCacheItemImpl_CreateStream(IAssemblyCacheItem *iface,
534 LPCWSTR pszStreamName,
538 ULARGE_INTEGER *puliMaxSize)
540 FIXME("(%p, %d, %s, %d, %d, %p, %p) stub!\n", iface, dwFlags,
541 debugstr_w(pszStreamName), dwFormat, dwFormatFlags, ppIStream, puliMaxSize);
546 static HRESULT WINAPI IAssemblyCacheItemImpl_Commit(IAssemblyCacheItem *iface,
548 ULONG *pulDisposition)
550 FIXME("(%p, %d, %p) stub!\n", iface, dwFlags, pulDisposition);
554 static HRESULT WINAPI IAssemblyCacheItemImpl_AbortItem(IAssemblyCacheItem *iface)
556 FIXME("(%p) stub!\n", iface);
560 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl = {
561 IAssemblyCacheItemImpl_QueryInterface,
562 IAssemblyCacheItemImpl_AddRef,
563 IAssemblyCacheItemImpl_Release,
564 IAssemblyCacheItemImpl_CreateStream,
565 IAssemblyCacheItemImpl_Commit,
566 IAssemblyCacheItemImpl_AbortItem