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"
41 WINE_DEFAULT_DEBUG_CHANNEL(fusion
);
44 IAssemblyCache IAssemblyCache_iface
;
51 IAssemblyCacheItem IAssemblyCacheItem_iface
;
54 } IAssemblyCacheItemImpl
;
56 static const WCHAR cache_mutex_nameW
[] =
57 {'_','_','W','I','N','E','_','F','U','S','I','O','N','_','C','A','C','H','E','_','M','U','T','E','X','_','_',0};
59 static BOOL
create_full_path(LPCWSTR path
)
65 if (!(new_path
= heap_alloc((lstrlenW(path
) + 1) * sizeof(WCHAR
)))) return FALSE
;
67 lstrcpyW(new_path
, path
);
69 while ((len
= lstrlenW(new_path
)) && new_path
[len
- 1] == '\\')
70 new_path
[len
- 1] = 0;
72 while (!CreateDirectoryW(new_path
, NULL
))
75 DWORD last_error
= GetLastError();
77 if(last_error
== ERROR_ALREADY_EXISTS
)
80 if(last_error
!= ERROR_PATH_NOT_FOUND
)
86 if(!(slash
= wcsrchr(new_path
, '\\')))
92 len
= slash
- new_path
;
94 if(!create_full_path(new_path
))
100 new_path
[len
] = '\\';
107 static BOOL
get_assembly_directory(LPWSTR dir
, DWORD size
, const char *version
, PEKIND architecture
)
109 static const WCHAR dotnet
[] = {'\\','M','i','c','r','o','s','o','f','t','.','N','E','T','\\',0};
110 static const WCHAR gac
[] = {'\\','a','s','s','e','m','b','l','y','\\','G','A','C',0};
111 static const WCHAR msil
[] = {'_','M','S','I','L',0};
112 static const WCHAR x86
[] = {'_','3','2',0};
113 static const WCHAR amd64
[] = {'_','6','4',0};
114 DWORD len
= GetWindowsDirectoryW(dir
, size
);
116 if (!strcmp(version
, "v4.0.30319"))
118 lstrcpyW(dir
+ len
, dotnet
);
119 len
+= ARRAY_SIZE(dotnet
) - 1;
120 lstrcpyW(dir
+ len
, gac
+ 1);
121 len
+= ARRAY_SIZE(gac
) - 2;
125 lstrcpyW(dir
+ len
, gac
);
126 len
+= ARRAY_SIZE(gac
) - 1;
128 switch (architecture
)
134 lstrcpyW(dir
+ len
, msil
);
138 lstrcpyW(dir
+ len
, x86
);
142 lstrcpyW(dir
+ len
, amd64
);
146 WARN("unhandled architecture %u\n", architecture
);
154 static inline IAssemblyCacheImpl
*impl_from_IAssemblyCache(IAssemblyCache
*iface
)
156 return CONTAINING_RECORD(iface
, IAssemblyCacheImpl
, IAssemblyCache_iface
);
159 static HRESULT WINAPI
IAssemblyCacheImpl_QueryInterface(IAssemblyCache
*iface
,
160 REFIID riid
, LPVOID
*ppobj
)
162 IAssemblyCacheImpl
*This
= impl_from_IAssemblyCache(iface
);
164 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
168 if (IsEqualIID(riid
, &IID_IUnknown
) ||
169 IsEqualIID(riid
, &IID_IAssemblyCache
))
171 IAssemblyCache_AddRef(iface
);
172 *ppobj
= &This
->IAssemblyCache_iface
;
176 WARN("(%p, %s, %p): not found\n", This
, debugstr_guid(riid
), ppobj
);
177 return E_NOINTERFACE
;
180 static ULONG WINAPI
IAssemblyCacheImpl_AddRef(IAssemblyCache
*iface
)
182 IAssemblyCacheImpl
*This
= impl_from_IAssemblyCache(iface
);
183 ULONG refCount
= InterlockedIncrement(&This
->ref
);
185 TRACE("(%p)->(ref before = %u)\n", This
, refCount
- 1);
190 static ULONG WINAPI
IAssemblyCacheImpl_Release(IAssemblyCache
*iface
)
192 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
193 ULONG refCount
= InterlockedDecrement( &cache
->ref
);
195 TRACE("(%p)->(ref before = %u)\n", cache
, refCount
+ 1);
199 CloseHandle( cache
->lock
);
205 static void cache_lock( IAssemblyCacheImpl
*cache
)
207 WaitForSingleObject( cache
->lock
, INFINITE
);
210 static void cache_unlock( IAssemblyCacheImpl
*cache
)
212 ReleaseMutex( cache
->lock
);
215 static HRESULT WINAPI
IAssemblyCacheImpl_UninstallAssembly(IAssemblyCache
*iface
,
217 LPCWSTR pszAssemblyName
,
218 LPCFUSION_INSTALL_REFERENCE pRefData
,
219 ULONG
*pulDisposition
)
222 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
223 IAssemblyName
*asmname
, *next
= NULL
;
224 IAssemblyEnum
*asmenum
= NULL
;
225 WCHAR
*p
, *path
= NULL
;
229 TRACE("(%p, 0%08x, %s, %p, %p)\n", iface
, dwFlags
,
230 debugstr_w(pszAssemblyName
), pRefData
, pulDisposition
);
234 FIXME("application reference not supported\n");
237 hr
= CreateAssemblyNameObject( &asmname
, pszAssemblyName
, CANOF_PARSE_DISPLAY_NAME
, NULL
);
243 hr
= CreateAssemblyEnum( &asmenum
, NULL
, asmname
, ASM_CACHE_GAC
, NULL
);
247 hr
= IAssemblyEnum_GetNextAssembly( asmenum
, NULL
, &next
, 0 );
251 *pulDisposition
= IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED
;
254 hr
= IAssemblyName_GetPath( next
, NULL
, &len
);
255 if (hr
!= HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER
))
258 if (!(path
= heap_alloc( len
* sizeof(WCHAR
) )))
263 hr
= IAssemblyName_GetPath( next
, path
, &len
);
267 if (DeleteFileW( path
))
269 if ((p
= wcsrchr( path
, '\\' )))
272 RemoveDirectoryW( path
);
273 if ((p
= wcsrchr( path
, '\\' )))
276 RemoveDirectoryW( path
);
279 disp
= IASSEMBLYCACHE_UNINSTALL_DISPOSITION_UNINSTALLED
;
284 disp
= IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED
;
287 if (pulDisposition
) *pulDisposition
= disp
;
290 IAssemblyName_Release( asmname
);
291 if (next
) IAssemblyName_Release( next
);
292 if (asmenum
) IAssemblyEnum_Release( asmenum
);
294 cache_unlock( cache
);
298 static HRESULT WINAPI
IAssemblyCacheImpl_QueryAssemblyInfo(IAssemblyCache
*iface
,
300 LPCWSTR pszAssemblyName
,
301 ASSEMBLY_INFO
*pAsmInfo
)
303 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
304 IAssemblyName
*asmname
, *next
= NULL
;
305 IAssemblyEnum
*asmenum
= NULL
;
308 TRACE("(%p, %d, %s, %p)\n", iface
, dwFlags
,
309 debugstr_w(pszAssemblyName
), pAsmInfo
);
313 if (pAsmInfo
->cbAssemblyInfo
== 0)
314 pAsmInfo
->cbAssemblyInfo
= sizeof(ASSEMBLY_INFO
);
315 else if (pAsmInfo
->cbAssemblyInfo
!= sizeof(ASSEMBLY_INFO
))
319 hr
= CreateAssemblyNameObject(&asmname
, pszAssemblyName
,
320 CANOF_PARSE_DISPLAY_NAME
, NULL
);
326 hr
= CreateAssemblyEnum(&asmenum
, NULL
, asmname
, ASM_CACHE_GAC
, NULL
);
332 hr
= IAssemblyEnum_GetNextAssembly(asmenum
, NULL
, &next
, 0);
335 hr
= HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
338 hr
= IAssemblyName_IsEqual(asmname
, next
, ASM_CMPF_IL_ALL
);
339 if (hr
== S_OK
) break;
345 hr
= IAssemblyName_GetPath(next
, pAsmInfo
->pszCurrentAssemblyPathBuf
, &pAsmInfo
->cchBuf
);
347 pAsmInfo
->dwAssemblyFlags
= ASSEMBLYINFO_FLAG_INSTALLED
;
350 IAssemblyName_Release(asmname
);
351 if (next
) IAssemblyName_Release(next
);
352 if (asmenum
) IAssemblyEnum_Release(asmenum
);
353 cache_unlock( cache
);
357 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl
;
359 static HRESULT WINAPI
IAssemblyCacheImpl_CreateAssemblyCacheItem(IAssemblyCache
*iface
,
362 IAssemblyCacheItem
**ppAsmItem
,
363 LPCWSTR pszAssemblyName
)
365 IAssemblyCacheItemImpl
*item
;
367 FIXME("(%p, %d, %p, %p, %s) semi-stub!\n", iface
, dwFlags
, pvReserved
,
368 ppAsmItem
, debugstr_w(pszAssemblyName
));
375 if (!(item
= heap_alloc(sizeof(*item
)))) return E_OUTOFMEMORY
;
377 item
->IAssemblyCacheItem_iface
.lpVtbl
= &AssemblyCacheItemVtbl
;
380 *ppAsmItem
= &item
->IAssemblyCacheItem_iface
;
384 static HRESULT WINAPI
IAssemblyCacheImpl_CreateAssemblyScavenger(IAssemblyCache
*iface
,
385 IUnknown
**ppUnkReserved
)
387 FIXME("(%p, %p) stub!\n", iface
, ppUnkReserved
);
391 static HRESULT
copy_file( const WCHAR
*src_dir
, DWORD src_len
, const WCHAR
*dst_dir
, DWORD dst_len
,
392 const WCHAR
*filename
)
394 WCHAR
*src_file
, *dst_file
;
395 DWORD len
= lstrlenW( filename
);
398 if (!(src_file
= heap_alloc( (src_len
+ len
+ 1) * sizeof(WCHAR
) )))
399 return E_OUTOFMEMORY
;
400 memcpy( src_file
, src_dir
, src_len
* sizeof(WCHAR
) );
401 lstrcpyW( src_file
+ src_len
, filename
);
403 if (!(dst_file
= heap_alloc( (dst_len
+ len
+ 1) * sizeof(WCHAR
) )))
405 heap_free( src_file
);
406 return E_OUTOFMEMORY
;
408 memcpy( dst_file
, dst_dir
, dst_len
* sizeof(WCHAR
) );
409 lstrcpyW( dst_file
+ dst_len
, filename
);
411 if (!CopyFileW( src_file
, dst_file
, FALSE
)) hr
= HRESULT_FROM_WIN32( GetLastError() );
412 heap_free( src_file
);
413 heap_free( dst_file
);
417 static HRESULT WINAPI
IAssemblyCacheImpl_InstallAssembly(IAssemblyCache
*iface
,
419 LPCWSTR pszManifestFilePath
,
420 LPCFUSION_INSTALL_REFERENCE pRefData
)
422 static const WCHAR format
[] =
423 {'%','s','\\','%','s','\\','%','s','_','_','%','s','\\',0};
424 static const WCHAR format_v40
[] =
425 {'%','s','\\','%','s','\\','v','4','.','0','_','%','s','_','_','%','s','\\',0};
426 static const WCHAR ext_exe
[] = {'.','e','x','e',0};
427 static const WCHAR ext_dll
[] = {'.','d','l','l',0};
428 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
430 const WCHAR
*extension
, *filename
, *src_dir
;
431 WCHAR
*name
= NULL
, *token
= NULL
, *version
= NULL
, *asmpath
= NULL
;
432 WCHAR asmdir
[MAX_PATH
], *p
, **external_files
= NULL
, *dst_dir
= NULL
;
435 DWORD i
, count
= 0, src_len
, dst_len
= ARRAY_SIZE(format_v40
);
438 TRACE("(%p, %d, %s, %p)\n", iface
, dwFlags
,
439 debugstr_w(pszManifestFilePath
), pRefData
);
441 if (!pszManifestFilePath
|| !*pszManifestFilePath
)
444 if (!(extension
= wcsrchr(pszManifestFilePath
, '.')))
445 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME
);
447 if (lstrcmpiW(extension
, ext_exe
) && lstrcmpiW(extension
, ext_dll
))
448 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME
);
450 if (GetFileAttributesW(pszManifestFilePath
) == INVALID_FILE_ATTRIBUTES
)
451 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
453 hr
= assembly_create(&assembly
, pszManifestFilePath
);
456 hr
= COR_E_ASSEMBLYEXPECTED
;
460 hr
= assembly_get_name(assembly
, &name
);
464 hr
= assembly_get_pubkey_token(assembly
, &token
);
468 hr
= assembly_get_version(assembly
, &version
);
472 hr
= assembly_get_runtime_version(assembly
, &clr_version
);
476 hr
= assembly_get_external_files(assembly
, &external_files
, &count
);
482 architecture
= assembly_get_architecture(assembly
);
483 get_assembly_directory(asmdir
, MAX_PATH
, clr_version
, architecture
);
485 dst_len
+= lstrlenW(asmdir
) + lstrlenW(name
) + lstrlenW(version
) + lstrlenW(token
);
486 if (!(dst_dir
= heap_alloc(dst_len
* sizeof(WCHAR
))))
491 if (!strcmp(clr_version
, "v4.0.30319"))
492 dst_len
= swprintf(dst_dir
, dst_len
, format_v40
, asmdir
, name
, version
, token
);
494 dst_len
= swprintf(dst_dir
, dst_len
, format
, asmdir
, name
, version
, token
);
496 create_full_path(dst_dir
);
498 hr
= assembly_get_path(assembly
, &asmpath
);
502 if ((p
= wcsrchr(asmpath
, '\\')))
506 src_len
= filename
- asmpath
;
514 hr
= copy_file(src_dir
, src_len
, dst_dir
, dst_len
, filename
);
518 for (i
= 0; i
< count
; i
++)
520 hr
= copy_file(src_dir
, src_len
, dst_dir
, dst_len
, external_files
[i
]);
531 for (i
= 0; i
< count
; i
++) heap_free(external_files
[i
]);
532 heap_free(external_files
);
533 assembly_release(assembly
);
534 cache_unlock( cache
);
538 static const IAssemblyCacheVtbl AssemblyCacheVtbl
= {
539 IAssemblyCacheImpl_QueryInterface
,
540 IAssemblyCacheImpl_AddRef
,
541 IAssemblyCacheImpl_Release
,
542 IAssemblyCacheImpl_UninstallAssembly
,
543 IAssemblyCacheImpl_QueryAssemblyInfo
,
544 IAssemblyCacheImpl_CreateAssemblyCacheItem
,
545 IAssemblyCacheImpl_CreateAssemblyScavenger
,
546 IAssemblyCacheImpl_InstallAssembly
549 /******************************************************************
550 * CreateAssemblyCache (FUSION.@)
552 HRESULT WINAPI
CreateAssemblyCache(IAssemblyCache
**ppAsmCache
, DWORD dwReserved
)
554 IAssemblyCacheImpl
*cache
;
556 TRACE("(%p, %d)\n", ppAsmCache
, dwReserved
);
563 if (!(cache
= heap_alloc(sizeof(*cache
)))) return E_OUTOFMEMORY
;
565 cache
->IAssemblyCache_iface
.lpVtbl
= &AssemblyCacheVtbl
;
567 cache
->lock
= CreateMutexW( NULL
, FALSE
, cache_mutex_nameW
);
571 return HRESULT_FROM_WIN32( GetLastError() );
573 *ppAsmCache
= &cache
->IAssemblyCache_iface
;
577 /* IAssemblyCacheItem */
579 static inline IAssemblyCacheItemImpl
*impl_from_IAssemblyCacheItem(IAssemblyCacheItem
*iface
)
581 return CONTAINING_RECORD(iface
, IAssemblyCacheItemImpl
, IAssemblyCacheItem_iface
);
584 static HRESULT WINAPI
IAssemblyCacheItemImpl_QueryInterface(IAssemblyCacheItem
*iface
,
585 REFIID riid
, LPVOID
*ppobj
)
587 IAssemblyCacheItemImpl
*This
= impl_from_IAssemblyCacheItem(iface
);
589 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
593 if (IsEqualIID(riid
, &IID_IUnknown
) ||
594 IsEqualIID(riid
, &IID_IAssemblyCacheItem
))
596 IAssemblyCacheItem_AddRef(iface
);
597 *ppobj
= &This
->IAssemblyCacheItem_iface
;
601 WARN("(%p, %s, %p): not found\n", This
, debugstr_guid(riid
), ppobj
);
602 return E_NOINTERFACE
;
605 static ULONG WINAPI
IAssemblyCacheItemImpl_AddRef(IAssemblyCacheItem
*iface
)
607 IAssemblyCacheItemImpl
*This
= impl_from_IAssemblyCacheItem(iface
);
608 ULONG refCount
= InterlockedIncrement(&This
->ref
);
610 TRACE("(%p)->(ref before = %u)\n", This
, refCount
- 1);
615 static ULONG WINAPI
IAssemblyCacheItemImpl_Release(IAssemblyCacheItem
*iface
)
617 IAssemblyCacheItemImpl
*This
= impl_from_IAssemblyCacheItem(iface
);
618 ULONG refCount
= InterlockedDecrement(&This
->ref
);
620 TRACE("(%p)->(ref before = %u)\n", This
, refCount
+ 1);
628 static HRESULT WINAPI
IAssemblyCacheItemImpl_CreateStream(IAssemblyCacheItem
*iface
,
630 LPCWSTR pszStreamName
,
634 ULARGE_INTEGER
*puliMaxSize
)
636 FIXME("(%p, %d, %s, %d, %d, %p, %p) stub!\n", iface
, dwFlags
,
637 debugstr_w(pszStreamName
), dwFormat
, dwFormatFlags
, ppIStream
, puliMaxSize
);
642 static HRESULT WINAPI
IAssemblyCacheItemImpl_Commit(IAssemblyCacheItem
*iface
,
644 ULONG
*pulDisposition
)
646 FIXME("(%p, %d, %p) stub!\n", iface
, dwFlags
, pulDisposition
);
650 static HRESULT WINAPI
IAssemblyCacheItemImpl_AbortItem(IAssemblyCacheItem
*iface
)
652 FIXME("(%p) stub!\n", iface
);
656 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl
= {
657 IAssemblyCacheItemImpl_QueryInterface
,
658 IAssemblyCacheItemImpl_AddRef
,
659 IAssemblyCacheItemImpl_Release
,
660 IAssemblyCacheItemImpl_CreateStream
,
661 IAssemblyCacheItemImpl_Commit
,
662 IAssemblyCacheItemImpl_AbortItem