configure: Build the import libraries without recursing when possible.
[wine/multimedia.git] / dlls / fusion / asmcache.c
blob7334e5d793e60650081fda562d602dd98f09b8a1
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 const IAssemblyCacheVtbl *lpIAssemblyCacheVtbl;
128 LONG ref;
129 } IAssemblyCacheImpl;
131 static HRESULT WINAPI IAssemblyCacheImpl_QueryInterface(IAssemblyCache *iface,
132 REFIID riid, LPVOID *ppobj)
134 IAssemblyCacheImpl *This = (IAssemblyCacheImpl *)iface;
136 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
138 *ppobj = NULL;
140 if (IsEqualIID(riid, &IID_IUnknown) ||
141 IsEqualIID(riid, &IID_IAssemblyCache))
143 IUnknown_AddRef(iface);
144 *ppobj = This;
145 return S_OK;
148 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
149 return E_NOINTERFACE;
152 static ULONG WINAPI IAssemblyCacheImpl_AddRef(IAssemblyCache *iface)
154 IAssemblyCacheImpl *This = (IAssemblyCacheImpl *)iface;
155 ULONG refCount = InterlockedIncrement(&This->ref);
157 TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
159 return refCount;
162 static ULONG WINAPI IAssemblyCacheImpl_Release(IAssemblyCache *iface)
164 IAssemblyCacheImpl *This = (IAssemblyCacheImpl *)iface;
165 ULONG refCount = InterlockedDecrement(&This->ref);
167 TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
169 if (!refCount)
170 HeapFree(GetProcessHeap(), 0, This);
172 return refCount;
175 static HRESULT WINAPI IAssemblyCacheImpl_UninstallAssembly(IAssemblyCache *iface,
176 DWORD dwFlags,
177 LPCWSTR pszAssemblyName,
178 LPCFUSION_INSTALL_REFERENCE pRefData,
179 ULONG *pulDisposition)
181 FIXME("(%p, %d, %s, %p, %p) stub!\n", iface, dwFlags,
182 debugstr_w(pszAssemblyName), pRefData, pulDisposition);
184 return E_NOTIMPL;
187 static HRESULT WINAPI IAssemblyCacheImpl_QueryAssemblyInfo(IAssemblyCache *iface,
188 DWORD dwFlags,
189 LPCWSTR pszAssemblyName,
190 ASSEMBLY_INFO *pAsmInfo)
192 IAssemblyName *asmname, *next = NULL;
193 IAssemblyEnum *asmenum = NULL;
194 HRESULT hr;
196 TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
197 debugstr_w(pszAssemblyName), pAsmInfo);
199 if (pAsmInfo)
201 if (pAsmInfo->cbAssemblyInfo == 0)
202 pAsmInfo->cbAssemblyInfo = sizeof(ASSEMBLY_INFO);
203 else if (pAsmInfo->cbAssemblyInfo != sizeof(ASSEMBLY_INFO))
204 return E_INVALIDARG;
207 hr = CreateAssemblyNameObject(&asmname, pszAssemblyName,
208 CANOF_PARSE_DISPLAY_NAME, NULL);
209 if (FAILED(hr))
210 return hr;
212 hr = CreateAssemblyEnum(&asmenum, NULL, asmname, ASM_CACHE_GAC, NULL);
213 if (FAILED(hr))
214 goto done;
216 hr = IAssemblyEnum_GetNextAssembly(asmenum, NULL, &next, 0);
217 if (hr == S_FALSE)
219 hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
220 goto done;
223 if (!pAsmInfo)
224 goto done;
226 pAsmInfo->dwAssemblyFlags = ASSEMBLYINFO_FLAG_INSTALLED;
228 done:
229 IAssemblyName_Release(asmname);
230 if (next) IAssemblyName_Release(next);
231 if (asmenum) IAssemblyEnum_Release(asmenum);
233 return hr;
236 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyCacheItem(IAssemblyCache *iface,
237 DWORD dwFlags,
238 PVOID pvReserved,
239 IAssemblyCacheItem **ppAsmItem,
240 LPCWSTR pszAssemblyName)
242 FIXME("(%p, %d, %p, %p, %s) stub!\n", iface, dwFlags, pvReserved,
243 ppAsmItem, debugstr_w(pszAssemblyName));
245 return E_NOTIMPL;
248 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyScavenger(IAssemblyCache *iface,
249 IUnknown **ppUnkReserved)
251 FIXME("(%p, %p) stub!\n", iface, ppUnkReserved);
252 return E_NOTIMPL;
255 static HRESULT WINAPI IAssemblyCacheImpl_InstallAssembly(IAssemblyCache *iface,
256 DWORD dwFlags,
257 LPCWSTR pszManifestFilePath,
258 LPCFUSION_INSTALL_REFERENCE pRefData)
260 static const WCHAR format[] =
261 {'%','s','\\','%','s','\\','%','s','_','_','%','s','\\',0};
263 ASSEMBLY *assembly;
264 LPWSTR filename;
265 LPWSTR name = NULL;
266 LPWSTR token = NULL;
267 LPWSTR version = NULL;
268 LPWSTR asmpath = NULL;
269 WCHAR path[MAX_PATH];
270 WCHAR asmdir[MAX_PATH];
271 LPWSTR ext;
272 HRESULT hr;
274 static const WCHAR ext_exe[] = {'.','e','x','e',0};
275 static const WCHAR ext_dll[] = {'.','d','l','l',0};
277 TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
278 debugstr_w(pszManifestFilePath), pRefData);
280 if (!pszManifestFilePath || !*pszManifestFilePath)
281 return E_INVALIDARG;
283 if (!(ext = strrchrW(pszManifestFilePath, '.')))
284 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
286 if (lstrcmpiW(ext, ext_exe) && lstrcmpiW(ext, ext_dll))
287 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
289 if (GetFileAttributesW(pszManifestFilePath) == INVALID_FILE_ATTRIBUTES)
290 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
292 hr = assembly_create(&assembly, pszManifestFilePath);
293 if (FAILED(hr))
295 hr = COR_E_ASSEMBLYEXPECTED;
296 goto done;
299 hr = assembly_get_name(assembly, &name);
300 if (FAILED(hr))
301 goto done;
303 hr = assembly_get_pubkey_token(assembly, &token);
304 if (FAILED(hr))
305 goto done;
307 hr = assembly_get_version(assembly, &version);
308 if (FAILED(hr))
309 goto done;
311 get_assembly_directory(asmdir, MAX_PATH, assembly_get_architecture(assembly));
313 sprintfW(path, format, asmdir, name, version, token);
315 create_full_path(path);
317 hr = assembly_get_path(assembly, &asmpath);
318 if (FAILED(hr))
319 goto done;
321 filename = PathFindFileNameW(asmpath);
323 strcatW(path, filename);
324 if (!CopyFileW(asmpath, path, FALSE))
325 hr = HRESULT_FROM_WIN32(GetLastError());
327 done:
328 HeapFree(GetProcessHeap(), 0, name);
329 HeapFree(GetProcessHeap(), 0, token);
330 HeapFree(GetProcessHeap(), 0, version);
331 HeapFree(GetProcessHeap(), 0, asmpath);
332 assembly_release(assembly);
333 return hr;
336 static const IAssemblyCacheVtbl AssemblyCacheVtbl = {
337 IAssemblyCacheImpl_QueryInterface,
338 IAssemblyCacheImpl_AddRef,
339 IAssemblyCacheImpl_Release,
340 IAssemblyCacheImpl_UninstallAssembly,
341 IAssemblyCacheImpl_QueryAssemblyInfo,
342 IAssemblyCacheImpl_CreateAssemblyCacheItem,
343 IAssemblyCacheImpl_CreateAssemblyScavenger,
344 IAssemblyCacheImpl_InstallAssembly
347 /******************************************************************
348 * CreateAssemblyCache (FUSION.@)
350 HRESULT WINAPI CreateAssemblyCache(IAssemblyCache **ppAsmCache, DWORD dwReserved)
352 IAssemblyCacheImpl *cache;
354 TRACE("(%p, %d)\n", ppAsmCache, dwReserved);
356 if (!ppAsmCache)
357 return E_INVALIDARG;
359 *ppAsmCache = NULL;
361 cache = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyCacheImpl));
362 if (!cache)
363 return E_OUTOFMEMORY;
365 cache->lpIAssemblyCacheVtbl = &AssemblyCacheVtbl;
366 cache->ref = 1;
368 *ppAsmCache = (IAssemblyCache *)cache;
370 return S_OK;
373 /* IAssemblyCacheItem */
375 typedef struct {
376 const IAssemblyCacheItemVtbl *lpIAssemblyCacheItemVtbl;
378 LONG ref;
379 } IAssemblyCacheItemImpl;
381 static HRESULT WINAPI IAssemblyCacheItemImpl_QueryInterface(IAssemblyCacheItem *iface,
382 REFIID riid, LPVOID *ppobj)
384 IAssemblyCacheItemImpl *This = (IAssemblyCacheItemImpl *)iface;
386 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
388 *ppobj = NULL;
390 if (IsEqualIID(riid, &IID_IUnknown) ||
391 IsEqualIID(riid, &IID_IAssemblyCacheItem))
393 IUnknown_AddRef(iface);
394 *ppobj = This;
395 return S_OK;
398 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
399 return E_NOINTERFACE;
402 static ULONG WINAPI IAssemblyCacheItemImpl_AddRef(IAssemblyCacheItem *iface)
404 IAssemblyCacheItemImpl *This = (IAssemblyCacheItemImpl *)iface;
405 ULONG refCount = InterlockedIncrement(&This->ref);
407 TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
409 return refCount;
412 static ULONG WINAPI IAssemblyCacheItemImpl_Release(IAssemblyCacheItem *iface)
414 IAssemblyCacheItemImpl *This = (IAssemblyCacheItemImpl *)iface;
415 ULONG refCount = InterlockedDecrement(&This->ref);
417 TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
419 if (!refCount)
420 HeapFree(GetProcessHeap(), 0, This);
422 return refCount;
425 static HRESULT WINAPI IAssemblyCacheItemImpl_CreateStream(IAssemblyCacheItem *iface,
426 DWORD dwFlags,
427 LPCWSTR pszStreamName,
428 DWORD dwFormat,
429 DWORD dwFormatFlags,
430 IStream **ppIStream,
431 ULARGE_INTEGER *puliMaxSize)
433 FIXME("(%p, %d, %s, %d, %d, %p, %p) stub!\n", iface, dwFlags,
434 debugstr_w(pszStreamName), dwFormat, dwFormatFlags, ppIStream, puliMaxSize);
436 return E_NOTIMPL;
439 static HRESULT WINAPI IAssemblyCacheItemImpl_Commit(IAssemblyCacheItem *iface,
440 DWORD dwFlags,
441 ULONG *pulDisposition)
443 FIXME("(%p, %d, %p) stub!\n", iface, dwFlags, pulDisposition);
444 return E_NOTIMPL;
447 static HRESULT WINAPI IAssemblyCacheItemImpl_AbortItem(IAssemblyCacheItem *iface)
449 FIXME("(%p) stub!\n", iface);
450 return E_NOTIMPL;
453 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl = {
454 IAssemblyCacheItemImpl_QueryInterface,
455 IAssemblyCacheItemImpl_AddRef,
456 IAssemblyCacheItemImpl_Release,
457 IAssemblyCacheItemImpl_CreateStream,
458 IAssemblyCacheItemImpl_Commit,
459 IAssemblyCacheItemImpl_AbortItem