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 #define WIN32_NO_STATUS
23 #define COM_NO_WINDOWS_H
32 //#include "winuser.h"
34 //#include "wincrypt.h"
37 //#include "dbghelp.h"
42 #include "fusionpriv.h"
43 #include <wine/debug.h>
44 #include <wine/unicode.h>
46 WINE_DEFAULT_DEBUG_CHANNEL(fusion
);
48 static const WCHAR cache_mutex_nameW
[] =
49 {'_','_','W','I','N','E','_','F','U','S','I','O','N','_','C','A','C','H','E','_','M','U','T','E','X','_','_',0};
51 static BOOL
create_full_path(LPCWSTR path
)
57 new_path
= HeapAlloc(GetProcessHeap(), 0, (strlenW(path
) + 1) * sizeof(WCHAR
));
61 strcpyW(new_path
, path
);
63 while ((len
= strlenW(new_path
)) && new_path
[len
- 1] == '\\')
64 new_path
[len
- 1] = 0;
66 while (!CreateDirectoryW(new_path
, NULL
))
69 DWORD last_error
= GetLastError();
71 if(last_error
== ERROR_ALREADY_EXISTS
)
74 if(last_error
!= ERROR_PATH_NOT_FOUND
)
80 if(!(slash
= strrchrW(new_path
, '\\')))
86 len
= slash
- new_path
;
88 if(!create_full_path(new_path
))
97 HeapFree(GetProcessHeap(), 0, new_path
);
101 static BOOL
get_assembly_directory(LPWSTR dir
, DWORD size
, BYTE architecture
)
103 static const WCHAR gac
[] = {'\\','a','s','s','e','m','b','l','y','\\','G','A','C',0};
105 static const WCHAR msil
[] = {'_','M','S','I','L',0};
106 static const WCHAR x86
[] = {'_','3','2',0};
107 static const WCHAR amd64
[] = {'_','6','4',0};
109 GetWindowsDirectoryW(dir
, size
);
112 switch (architecture
)
133 IAssemblyCache IAssemblyCache_iface
;
137 } IAssemblyCacheImpl
;
139 static inline IAssemblyCacheImpl
*impl_from_IAssemblyCache(IAssemblyCache
*iface
)
141 return CONTAINING_RECORD(iface
, IAssemblyCacheImpl
, IAssemblyCache_iface
);
144 static HRESULT WINAPI
IAssemblyCacheImpl_QueryInterface(IAssemblyCache
*iface
,
145 REFIID riid
, LPVOID
*ppobj
)
147 IAssemblyCacheImpl
*This
= impl_from_IAssemblyCache(iface
);
149 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
153 if (IsEqualIID(riid
, &IID_IUnknown
) ||
154 IsEqualIID(riid
, &IID_IAssemblyCache
))
156 IUnknown_AddRef(iface
);
161 WARN("(%p, %s, %p): not found\n", This
, debugstr_guid(riid
), ppobj
);
162 return E_NOINTERFACE
;
165 static ULONG WINAPI
IAssemblyCacheImpl_AddRef(IAssemblyCache
*iface
)
167 IAssemblyCacheImpl
*This
= impl_from_IAssemblyCache(iface
);
168 ULONG refCount
= InterlockedIncrement(&This
->ref
);
170 TRACE("(%p)->(ref before = %u)\n", This
, refCount
- 1);
175 static ULONG WINAPI
IAssemblyCacheImpl_Release(IAssemblyCache
*iface
)
177 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
178 ULONG refCount
= InterlockedDecrement( &cache
->ref
);
180 TRACE("(%p)->(ref before = %u)\n", cache
, refCount
+ 1);
184 CloseHandle( cache
->lock
);
185 HeapFree( GetProcessHeap(), 0, cache
);
190 static void cache_lock( IAssemblyCacheImpl
*cache
)
192 WaitForSingleObject( cache
->lock
, INFINITE
);
195 static void cache_unlock( IAssemblyCacheImpl
*cache
)
197 ReleaseMutex( cache
->lock
);
200 static HRESULT WINAPI
IAssemblyCacheImpl_UninstallAssembly(IAssemblyCache
*iface
,
202 LPCWSTR pszAssemblyName
,
203 LPCFUSION_INSTALL_REFERENCE pRefData
,
204 ULONG
*pulDisposition
)
207 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
208 IAssemblyName
*asmname
, *next
= NULL
;
209 IAssemblyEnum
*asmenum
= NULL
;
210 WCHAR
*p
, *path
= NULL
;
214 TRACE("(%p, 0%08x, %s, %p, %p)\n", iface
, dwFlags
,
215 debugstr_w(pszAssemblyName
), pRefData
, pulDisposition
);
219 FIXME("application reference not supported\n");
222 hr
= CreateAssemblyNameObject( &asmname
, pszAssemblyName
, CANOF_PARSE_DISPLAY_NAME
, NULL
);
228 hr
= CreateAssemblyEnum( &asmenum
, NULL
, asmname
, ASM_CACHE_GAC
, NULL
);
232 hr
= IAssemblyEnum_GetNextAssembly( asmenum
, NULL
, &next
, 0 );
236 *pulDisposition
= IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED
;
239 hr
= IAssemblyName_GetPath( next
, NULL
, &len
);
240 if (hr
!= HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER
))
243 if (!(path
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
248 hr
= IAssemblyName_GetPath( next
, path
, &len
);
252 if (DeleteFileW( path
))
254 if ((p
= strrchrW( path
, '\\' )))
257 RemoveDirectoryW( path
);
258 if ((p
= strrchrW( path
, '\\' )))
261 RemoveDirectoryW( path
);
264 disp
= IASSEMBLYCACHE_UNINSTALL_DISPOSITION_UNINSTALLED
;
269 disp
= IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED
;
272 if (pulDisposition
) *pulDisposition
= disp
;
275 IAssemblyName_Release( asmname
);
276 if (next
) IAssemblyName_Release( next
);
277 if (asmenum
) IAssemblyEnum_Release( asmenum
);
278 HeapFree( GetProcessHeap(), 0, path
);
279 cache_unlock( cache
);
283 static HRESULT WINAPI
IAssemblyCacheImpl_QueryAssemblyInfo(IAssemblyCache
*iface
,
285 LPCWSTR pszAssemblyName
,
286 ASSEMBLY_INFO
*pAsmInfo
)
288 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
289 IAssemblyName
*asmname
, *next
= NULL
;
290 IAssemblyEnum
*asmenum
= NULL
;
293 TRACE("(%p, %d, %s, %p)\n", iface
, dwFlags
,
294 debugstr_w(pszAssemblyName
), pAsmInfo
);
298 if (pAsmInfo
->cbAssemblyInfo
== 0)
299 pAsmInfo
->cbAssemblyInfo
= sizeof(ASSEMBLY_INFO
);
300 else if (pAsmInfo
->cbAssemblyInfo
!= sizeof(ASSEMBLY_INFO
))
304 hr
= CreateAssemblyNameObject(&asmname
, pszAssemblyName
,
305 CANOF_PARSE_DISPLAY_NAME
, NULL
);
311 hr
= CreateAssemblyEnum(&asmenum
, NULL
, asmname
, ASM_CACHE_GAC
, NULL
);
315 hr
= IAssemblyEnum_GetNextAssembly(asmenum
, NULL
, &next
, 0);
318 hr
= HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
325 hr
= IAssemblyName_GetPath(next
, pAsmInfo
->pszCurrentAssemblyPathBuf
, &pAsmInfo
->cchBuf
);
327 pAsmInfo
->dwAssemblyFlags
= ASSEMBLYINFO_FLAG_INSTALLED
;
330 IAssemblyName_Release(asmname
);
331 if (next
) IAssemblyName_Release(next
);
332 if (asmenum
) IAssemblyEnum_Release(asmenum
);
333 cache_unlock( cache
);
337 static HRESULT WINAPI
IAssemblyCacheImpl_CreateAssemblyCacheItem(IAssemblyCache
*iface
,
340 IAssemblyCacheItem
**ppAsmItem
,
341 LPCWSTR pszAssemblyName
)
343 FIXME("(%p, %d, %p, %p, %s) stub!\n", iface
, dwFlags
, pvReserved
,
344 ppAsmItem
, debugstr_w(pszAssemblyName
));
349 static HRESULT WINAPI
IAssemblyCacheImpl_CreateAssemblyScavenger(IAssemblyCache
*iface
,
350 IUnknown
**ppUnkReserved
)
352 FIXME("(%p, %p) stub!\n", iface
, ppUnkReserved
);
356 static HRESULT WINAPI
IAssemblyCacheImpl_InstallAssembly(IAssemblyCache
*iface
,
358 LPCWSTR pszManifestFilePath
,
359 LPCFUSION_INSTALL_REFERENCE pRefData
)
361 static const WCHAR format
[] = {'%','s','\\','%','s','\\','%','s','_','_','%','s','\\',0};
362 static const WCHAR ext_exe
[] = {'.','e','x','e',0};
363 static const WCHAR ext_dll
[] = {'.','d','l','l',0};
364 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
369 LPWSTR version
= NULL
;
370 LPWSTR asmpath
= NULL
;
371 WCHAR path
[MAX_PATH
];
372 WCHAR asmdir
[MAX_PATH
];
376 TRACE("(%p, %d, %s, %p)\n", iface
, dwFlags
,
377 debugstr_w(pszManifestFilePath
), pRefData
);
379 if (!pszManifestFilePath
|| !*pszManifestFilePath
)
382 if (!(ext
= strrchrW(pszManifestFilePath
, '.')))
383 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME
);
385 if (lstrcmpiW(ext
, ext_exe
) && lstrcmpiW(ext
, ext_dll
))
386 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME
);
388 if (GetFileAttributesW(pszManifestFilePath
) == INVALID_FILE_ATTRIBUTES
)
389 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
391 hr
= assembly_create(&assembly
, pszManifestFilePath
);
394 hr
= COR_E_ASSEMBLYEXPECTED
;
398 hr
= assembly_get_name(assembly
, &name
);
402 hr
= assembly_get_pubkey_token(assembly
, &token
);
406 hr
= assembly_get_version(assembly
, &version
);
412 get_assembly_directory(asmdir
, MAX_PATH
, assembly_get_architecture(assembly
));
414 sprintfW(path
, format
, asmdir
, name
, version
, token
);
416 create_full_path(path
);
418 hr
= assembly_get_path(assembly
, &asmpath
);
422 filename
= PathFindFileNameW(asmpath
);
424 strcatW(path
, filename
);
425 if (!CopyFileW(asmpath
, path
, FALSE
))
426 hr
= HRESULT_FROM_WIN32(GetLastError());
429 HeapFree(GetProcessHeap(), 0, name
);
430 HeapFree(GetProcessHeap(), 0, token
);
431 HeapFree(GetProcessHeap(), 0, version
);
432 HeapFree(GetProcessHeap(), 0, asmpath
);
433 assembly_release(assembly
);
434 cache_unlock( cache
);
438 static const IAssemblyCacheVtbl AssemblyCacheVtbl
= {
439 IAssemblyCacheImpl_QueryInterface
,
440 IAssemblyCacheImpl_AddRef
,
441 IAssemblyCacheImpl_Release
,
442 IAssemblyCacheImpl_UninstallAssembly
,
443 IAssemblyCacheImpl_QueryAssemblyInfo
,
444 IAssemblyCacheImpl_CreateAssemblyCacheItem
,
445 IAssemblyCacheImpl_CreateAssemblyScavenger
,
446 IAssemblyCacheImpl_InstallAssembly
449 /******************************************************************
450 * CreateAssemblyCache (FUSION.@)
452 HRESULT WINAPI
CreateAssemblyCache(IAssemblyCache
**ppAsmCache
, DWORD dwReserved
)
454 IAssemblyCacheImpl
*cache
;
456 TRACE("(%p, %d)\n", ppAsmCache
, dwReserved
);
463 cache
= HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyCacheImpl
));
465 return E_OUTOFMEMORY
;
467 cache
->IAssemblyCache_iface
.lpVtbl
= &AssemblyCacheVtbl
;
469 cache
->lock
= CreateMutexW( NULL
, FALSE
, cache_mutex_nameW
);
472 HeapFree( GetProcessHeap(), 0, cache
);
473 return HRESULT_FROM_WIN32( GetLastError() );
475 *ppAsmCache
= &cache
->IAssemblyCache_iface
;
479 /* IAssemblyCacheItem */
482 IAssemblyCacheItem IAssemblyCacheItem_iface
;
485 } IAssemblyCacheItemImpl
;
487 static inline IAssemblyCacheItemImpl
*impl_from_IAssemblyCacheItem(IAssemblyCacheItem
*iface
)
489 return CONTAINING_RECORD(iface
, IAssemblyCacheItemImpl
, IAssemblyCacheItem_iface
);
492 static HRESULT WINAPI
IAssemblyCacheItemImpl_QueryInterface(IAssemblyCacheItem
*iface
,
493 REFIID riid
, LPVOID
*ppobj
)
495 IAssemblyCacheItemImpl
*This
= impl_from_IAssemblyCacheItem(iface
);
497 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
501 if (IsEqualIID(riid
, &IID_IUnknown
) ||
502 IsEqualIID(riid
, &IID_IAssemblyCacheItem
))
504 IUnknown_AddRef(iface
);
509 WARN("(%p, %s, %p): not found\n", This
, debugstr_guid(riid
), ppobj
);
510 return E_NOINTERFACE
;
513 static ULONG WINAPI
IAssemblyCacheItemImpl_AddRef(IAssemblyCacheItem
*iface
)
515 IAssemblyCacheItemImpl
*This
= impl_from_IAssemblyCacheItem(iface
);
516 ULONG refCount
= InterlockedIncrement(&This
->ref
);
518 TRACE("(%p)->(ref before = %u)\n", This
, refCount
- 1);
523 static ULONG WINAPI
IAssemblyCacheItemImpl_Release(IAssemblyCacheItem
*iface
)
525 IAssemblyCacheItemImpl
*This
= impl_from_IAssemblyCacheItem(iface
);
526 ULONG refCount
= InterlockedDecrement(&This
->ref
);
528 TRACE("(%p)->(ref before = %u)\n", This
, refCount
+ 1);
531 HeapFree(GetProcessHeap(), 0, This
);
536 static HRESULT WINAPI
IAssemblyCacheItemImpl_CreateStream(IAssemblyCacheItem
*iface
,
538 LPCWSTR pszStreamName
,
542 ULARGE_INTEGER
*puliMaxSize
)
544 FIXME("(%p, %d, %s, %d, %d, %p, %p) stub!\n", iface
, dwFlags
,
545 debugstr_w(pszStreamName
), dwFormat
, dwFormatFlags
, ppIStream
, puliMaxSize
);
550 static HRESULT WINAPI
IAssemblyCacheItemImpl_Commit(IAssemblyCacheItem
*iface
,
552 ULONG
*pulDisposition
)
554 FIXME("(%p, %d, %p) stub!\n", iface
, dwFlags
, pulDisposition
);
558 static HRESULT WINAPI
IAssemblyCacheItemImpl_AbortItem(IAssemblyCacheItem
*iface
)
560 FIXME("(%p) stub!\n", iface
);
564 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl
= {
565 IAssemblyCacheItemImpl_QueryInterface
,
566 IAssemblyCacheItemImpl_AddRef
,
567 IAssemblyCacheItemImpl_Release
,
568 IAssemblyCacheItemImpl_CreateStream
,
569 IAssemblyCacheItemImpl_Commit
,
570 IAssemblyCacheItemImpl_AbortItem