fusion: Implement IAssemblyCache::UninstallAssembly.
[wine/multimedia.git] / dlls / fusion / asmcache.c
blobd0b45c7626087cda1267904e931206c71c4b04b7
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"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(fusion);
44 static BOOL create_full_path(LPCWSTR path)
46 LPWSTR new_path;
47 BOOL ret = TRUE;
48 int len;
50 new_path = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1) * sizeof(WCHAR));
51 if (!new_path)
52 return FALSE;
54 strcpyW(new_path, path);
56 while ((len = strlenW(new_path)) && new_path[len - 1] == '\\')
57 new_path[len - 1] = 0;
59 while (!CreateDirectoryW(new_path, NULL))
61 LPWSTR slash;
62 DWORD last_error = GetLastError();
64 if(last_error == ERROR_ALREADY_EXISTS)
65 break;
67 if(last_error != ERROR_PATH_NOT_FOUND)
69 ret = FALSE;
70 break;
73 if(!(slash = strrchrW(new_path, '\\')))
75 ret = FALSE;
76 break;
79 len = slash - new_path;
80 new_path[len] = 0;
81 if(!create_full_path(new_path))
83 ret = FALSE;
84 break;
87 new_path[len] = '\\';
90 HeapFree(GetProcessHeap(), 0, new_path);
91 return ret;
94 static BOOL get_assembly_directory(LPWSTR dir, DWORD size, BYTE architecture)
96 static const WCHAR gac[] = {'\\','a','s','s','e','m','b','l','y','\\','G','A','C',0};
98 static const WCHAR msil[] = {'_','M','S','I','L',0};
99 static const WCHAR x86[] = {'_','3','2',0};
100 static const WCHAR amd64[] = {'_','6','4',0};
102 GetWindowsDirectoryW(dir, size);
103 strcatW(dir, gac);
105 switch (architecture)
107 case peMSIL:
108 strcatW(dir, msil);
109 break;
111 case peI386:
112 strcatW(dir, x86);
113 break;
115 case peAMD64:
116 strcatW(dir, amd64);
117 break;
120 return TRUE;
123 /* IAssemblyCache */
125 typedef struct {
126 IAssemblyCache IAssemblyCache_iface;
128 LONG ref;
129 } IAssemblyCacheImpl;
131 static inline IAssemblyCacheImpl *impl_from_IAssemblyCache(IAssemblyCache *iface)
133 return CONTAINING_RECORD(iface, IAssemblyCacheImpl, IAssemblyCache_iface);
136 static HRESULT WINAPI IAssemblyCacheImpl_QueryInterface(IAssemblyCache *iface,
137 REFIID riid, LPVOID *ppobj)
139 IAssemblyCacheImpl *This = impl_from_IAssemblyCache(iface);
141 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
143 *ppobj = NULL;
145 if (IsEqualIID(riid, &IID_IUnknown) ||
146 IsEqualIID(riid, &IID_IAssemblyCache))
148 IUnknown_AddRef(iface);
149 *ppobj = This;
150 return S_OK;
153 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
154 return E_NOINTERFACE;
157 static ULONG WINAPI IAssemblyCacheImpl_AddRef(IAssemblyCache *iface)
159 IAssemblyCacheImpl *This = impl_from_IAssemblyCache(iface);
160 ULONG refCount = InterlockedIncrement(&This->ref);
162 TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
164 return refCount;
167 static ULONG WINAPI IAssemblyCacheImpl_Release(IAssemblyCache *iface)
169 IAssemblyCacheImpl *This = impl_from_IAssemblyCache(iface);
170 ULONG refCount = InterlockedDecrement(&This->ref);
172 TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
174 if (!refCount)
175 HeapFree(GetProcessHeap(), 0, This);
177 return refCount;
180 static HRESULT WINAPI IAssemblyCacheImpl_UninstallAssembly(IAssemblyCache *iface,
181 DWORD dwFlags,
182 LPCWSTR pszAssemblyName,
183 LPCFUSION_INSTALL_REFERENCE pRefData,
184 ULONG *pulDisposition)
186 HRESULT hr;
187 IAssemblyName *asmname, *next = NULL;
188 IAssemblyEnum *asmenum = NULL;
189 WCHAR *p, *path = NULL;
190 ULONG disp;
191 DWORD len;
193 TRACE("(%p, 0%08x, %s, %p, %p)\n", iface, dwFlags,
194 debugstr_w(pszAssemblyName), pRefData, pulDisposition);
196 if (pRefData)
198 FIXME("application reference not supported\n");
199 return E_NOTIMPL;
201 hr = CreateAssemblyNameObject( &asmname, pszAssemblyName, CANOF_PARSE_DISPLAY_NAME, NULL );
202 if (FAILED( hr ))
203 return hr;
205 hr = CreateAssemblyEnum( &asmenum, NULL, asmname, ASM_CACHE_GAC, NULL );
206 if (FAILED( hr ))
207 goto done;
209 hr = IAssemblyEnum_GetNextAssembly( asmenum, NULL, &next, 0 );
210 if (hr == S_FALSE)
212 if (pulDisposition)
213 *pulDisposition = IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED;
214 goto done;
216 hr = IAssemblyName_GetPath( next, NULL, &len );
217 if (hr != HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER ))
218 goto done;
220 if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
222 hr = E_OUTOFMEMORY;
223 goto done;
225 hr = IAssemblyName_GetPath( next, path, &len );
226 if (FAILED( hr ))
227 goto done;
229 if (DeleteFileW( path ))
231 if ((p = strrchrW( path, '\\' )))
233 *p = 0;
234 RemoveDirectoryW( path );
235 if ((p = strrchrW( path, '\\' )))
237 *p = 0;
238 RemoveDirectoryW( path );
241 disp = IASSEMBLYCACHE_UNINSTALL_DISPOSITION_UNINSTALLED;
242 hr = S_OK;
244 else
246 disp = IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED;
247 hr = S_FALSE;
249 if (pulDisposition) *pulDisposition = disp;
251 done:
252 IAssemblyName_Release( asmname );
253 if (next) IAssemblyName_Release( next );
254 if (asmenum) IAssemblyEnum_Release( asmenum );
255 HeapFree( GetProcessHeap(), 0, path );
256 return hr;
259 static HRESULT WINAPI IAssemblyCacheImpl_QueryAssemblyInfo(IAssemblyCache *iface,
260 DWORD dwFlags,
261 LPCWSTR pszAssemblyName,
262 ASSEMBLY_INFO *pAsmInfo)
264 IAssemblyName *asmname, *next = NULL;
265 IAssemblyEnum *asmenum = NULL;
266 HRESULT hr;
268 TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
269 debugstr_w(pszAssemblyName), pAsmInfo);
271 if (pAsmInfo)
273 if (pAsmInfo->cbAssemblyInfo == 0)
274 pAsmInfo->cbAssemblyInfo = sizeof(ASSEMBLY_INFO);
275 else if (pAsmInfo->cbAssemblyInfo != sizeof(ASSEMBLY_INFO))
276 return E_INVALIDARG;
279 hr = CreateAssemblyNameObject(&asmname, pszAssemblyName,
280 CANOF_PARSE_DISPLAY_NAME, NULL);
281 if (FAILED(hr))
282 return hr;
284 hr = CreateAssemblyEnum(&asmenum, NULL, asmname, ASM_CACHE_GAC, NULL);
285 if (FAILED(hr))
286 goto done;
288 hr = IAssemblyEnum_GetNextAssembly(asmenum, NULL, &next, 0);
289 if (hr == S_FALSE)
291 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
292 goto done;
295 if (!pAsmInfo)
296 goto done;
298 hr = IAssemblyName_GetPath(next, pAsmInfo->pszCurrentAssemblyPathBuf, &pAsmInfo->cchBuf);
300 pAsmInfo->dwAssemblyFlags = ASSEMBLYINFO_FLAG_INSTALLED;
302 done:
303 IAssemblyName_Release(asmname);
304 if (next) IAssemblyName_Release(next);
305 if (asmenum) IAssemblyEnum_Release(asmenum);
307 return hr;
310 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyCacheItem(IAssemblyCache *iface,
311 DWORD dwFlags,
312 PVOID pvReserved,
313 IAssemblyCacheItem **ppAsmItem,
314 LPCWSTR pszAssemblyName)
316 FIXME("(%p, %d, %p, %p, %s) stub!\n", iface, dwFlags, pvReserved,
317 ppAsmItem, debugstr_w(pszAssemblyName));
319 return E_NOTIMPL;
322 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyScavenger(IAssemblyCache *iface,
323 IUnknown **ppUnkReserved)
325 FIXME("(%p, %p) stub!\n", iface, ppUnkReserved);
326 return E_NOTIMPL;
329 static HRESULT WINAPI IAssemblyCacheImpl_InstallAssembly(IAssemblyCache *iface,
330 DWORD dwFlags,
331 LPCWSTR pszManifestFilePath,
332 LPCFUSION_INSTALL_REFERENCE pRefData)
334 static const WCHAR format[] =
335 {'%','s','\\','%','s','\\','%','s','_','_','%','s','\\',0};
337 ASSEMBLY *assembly;
338 LPWSTR filename;
339 LPWSTR name = NULL;
340 LPWSTR token = NULL;
341 LPWSTR version = NULL;
342 LPWSTR asmpath = NULL;
343 WCHAR path[MAX_PATH];
344 WCHAR asmdir[MAX_PATH];
345 LPWSTR ext;
346 HRESULT hr;
348 static const WCHAR ext_exe[] = {'.','e','x','e',0};
349 static const WCHAR ext_dll[] = {'.','d','l','l',0};
351 TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
352 debugstr_w(pszManifestFilePath), pRefData);
354 if (!pszManifestFilePath || !*pszManifestFilePath)
355 return E_INVALIDARG;
357 if (!(ext = strrchrW(pszManifestFilePath, '.')))
358 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
360 if (lstrcmpiW(ext, ext_exe) && lstrcmpiW(ext, ext_dll))
361 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
363 if (GetFileAttributesW(pszManifestFilePath) == INVALID_FILE_ATTRIBUTES)
364 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
366 hr = assembly_create(&assembly, pszManifestFilePath);
367 if (FAILED(hr))
369 hr = COR_E_ASSEMBLYEXPECTED;
370 goto done;
373 hr = assembly_get_name(assembly, &name);
374 if (FAILED(hr))
375 goto done;
377 hr = assembly_get_pubkey_token(assembly, &token);
378 if (FAILED(hr))
379 goto done;
381 hr = assembly_get_version(assembly, &version);
382 if (FAILED(hr))
383 goto done;
385 get_assembly_directory(asmdir, MAX_PATH, assembly_get_architecture(assembly));
387 sprintfW(path, format, asmdir, name, version, token);
389 create_full_path(path);
391 hr = assembly_get_path(assembly, &asmpath);
392 if (FAILED(hr))
393 goto done;
395 filename = PathFindFileNameW(asmpath);
397 strcatW(path, filename);
398 if (!CopyFileW(asmpath, path, FALSE))
399 hr = HRESULT_FROM_WIN32(GetLastError());
401 done:
402 HeapFree(GetProcessHeap(), 0, name);
403 HeapFree(GetProcessHeap(), 0, token);
404 HeapFree(GetProcessHeap(), 0, version);
405 HeapFree(GetProcessHeap(), 0, asmpath);
406 assembly_release(assembly);
407 return hr;
410 static const IAssemblyCacheVtbl AssemblyCacheVtbl = {
411 IAssemblyCacheImpl_QueryInterface,
412 IAssemblyCacheImpl_AddRef,
413 IAssemblyCacheImpl_Release,
414 IAssemblyCacheImpl_UninstallAssembly,
415 IAssemblyCacheImpl_QueryAssemblyInfo,
416 IAssemblyCacheImpl_CreateAssemblyCacheItem,
417 IAssemblyCacheImpl_CreateAssemblyScavenger,
418 IAssemblyCacheImpl_InstallAssembly
421 /******************************************************************
422 * CreateAssemblyCache (FUSION.@)
424 HRESULT WINAPI CreateAssemblyCache(IAssemblyCache **ppAsmCache, DWORD dwReserved)
426 IAssemblyCacheImpl *cache;
428 TRACE("(%p, %d)\n", ppAsmCache, dwReserved);
430 if (!ppAsmCache)
431 return E_INVALIDARG;
433 *ppAsmCache = NULL;
435 cache = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyCacheImpl));
436 if (!cache)
437 return E_OUTOFMEMORY;
439 cache->IAssemblyCache_iface.lpVtbl = &AssemblyCacheVtbl;
440 cache->ref = 1;
442 *ppAsmCache = &cache->IAssemblyCache_iface;
444 return S_OK;
447 /* IAssemblyCacheItem */
449 typedef struct {
450 IAssemblyCacheItem IAssemblyCacheItem_iface;
452 LONG ref;
453 } IAssemblyCacheItemImpl;
455 static inline IAssemblyCacheItemImpl *impl_from_IAssemblyCacheItem(IAssemblyCacheItem *iface)
457 return CONTAINING_RECORD(iface, IAssemblyCacheItemImpl, IAssemblyCacheItem_iface);
460 static HRESULT WINAPI IAssemblyCacheItemImpl_QueryInterface(IAssemblyCacheItem *iface,
461 REFIID riid, LPVOID *ppobj)
463 IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
465 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
467 *ppobj = NULL;
469 if (IsEqualIID(riid, &IID_IUnknown) ||
470 IsEqualIID(riid, &IID_IAssemblyCacheItem))
472 IUnknown_AddRef(iface);
473 *ppobj = This;
474 return S_OK;
477 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
478 return E_NOINTERFACE;
481 static ULONG WINAPI IAssemblyCacheItemImpl_AddRef(IAssemblyCacheItem *iface)
483 IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
484 ULONG refCount = InterlockedIncrement(&This->ref);
486 TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
488 return refCount;
491 static ULONG WINAPI IAssemblyCacheItemImpl_Release(IAssemblyCacheItem *iface)
493 IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
494 ULONG refCount = InterlockedDecrement(&This->ref);
496 TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
498 if (!refCount)
499 HeapFree(GetProcessHeap(), 0, This);
501 return refCount;
504 static HRESULT WINAPI IAssemblyCacheItemImpl_CreateStream(IAssemblyCacheItem *iface,
505 DWORD dwFlags,
506 LPCWSTR pszStreamName,
507 DWORD dwFormat,
508 DWORD dwFormatFlags,
509 IStream **ppIStream,
510 ULARGE_INTEGER *puliMaxSize)
512 FIXME("(%p, %d, %s, %d, %d, %p, %p) stub!\n", iface, dwFlags,
513 debugstr_w(pszStreamName), dwFormat, dwFormatFlags, ppIStream, puliMaxSize);
515 return E_NOTIMPL;
518 static HRESULT WINAPI IAssemblyCacheItemImpl_Commit(IAssemblyCacheItem *iface,
519 DWORD dwFlags,
520 ULONG *pulDisposition)
522 FIXME("(%p, %d, %p) stub!\n", iface, dwFlags, pulDisposition);
523 return E_NOTIMPL;
526 static HRESULT WINAPI IAssemblyCacheItemImpl_AbortItem(IAssemblyCacheItem *iface)
528 FIXME("(%p) stub!\n", iface);
529 return E_NOTIMPL;
532 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl = {
533 IAssemblyCacheItemImpl_QueryInterface,
534 IAssemblyCacheItemImpl_AddRef,
535 IAssemblyCacheItemImpl_Release,
536 IAssemblyCacheItemImpl_CreateStream,
537 IAssemblyCacheItemImpl_Commit,
538 IAssemblyCacheItemImpl_AbortItem