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
);
45 IAssemblyCache IAssemblyCache_iface
;
52 IAssemblyCacheItem IAssemblyCacheItem_iface
;
55 } IAssemblyCacheItemImpl
;
57 static const WCHAR cache_mutex_nameW
[] =
58 {'_','_','W','I','N','E','_','F','U','S','I','O','N','_','C','A','C','H','E','_','M','U','T','E','X','_','_',0};
60 static BOOL
create_full_path(LPCWSTR path
)
66 if (!(new_path
= heap_alloc((strlenW(path
) + 1) * sizeof(WCHAR
)))) return FALSE
;
68 strcpyW(new_path
, path
);
70 while ((len
= strlenW(new_path
)) && new_path
[len
- 1] == '\\')
71 new_path
[len
- 1] = 0;
73 while (!CreateDirectoryW(new_path
, NULL
))
76 DWORD last_error
= GetLastError();
78 if(last_error
== ERROR_ALREADY_EXISTS
)
81 if(last_error
!= ERROR_PATH_NOT_FOUND
)
87 if(!(slash
= strrchrW(new_path
, '\\')))
93 len
= slash
- new_path
;
95 if(!create_full_path(new_path
))
101 new_path
[len
] = '\\';
108 static BOOL
get_assembly_directory(LPWSTR dir
, DWORD size
, const char *version
, PEKIND architecture
)
110 static const WCHAR dotnet
[] = {'\\','M','i','c','r','o','s','o','f','t','.','N','E','T','\\',0};
111 static const WCHAR gac
[] = {'\\','a','s','s','e','m','b','l','y','\\','G','A','C',0};
112 static const WCHAR msil
[] = {'_','M','S','I','L',0};
113 static const WCHAR x86
[] = {'_','3','2',0};
114 static const WCHAR amd64
[] = {'_','6','4',0};
115 DWORD len
= GetWindowsDirectoryW(dir
, size
);
117 if (!strcmp(version
, "v4.0.30319"))
119 strcpyW(dir
+ len
, dotnet
);
120 len
+= sizeof(dotnet
)/sizeof(WCHAR
) -1;
121 strcpyW(dir
+ len
, gac
+ 1);
122 len
+= sizeof(gac
)/sizeof(WCHAR
) - 2;
126 strcpyW(dir
+ len
, gac
);
127 len
+= sizeof(gac
)/sizeof(WCHAR
) - 1;
129 switch (architecture
)
135 strcpyW(dir
+ len
, msil
);
139 strcpyW(dir
+ len
, x86
);
143 strcpyW(dir
+ len
, amd64
);
147 WARN("unhandled architecture %u\n", architecture
);
155 static inline IAssemblyCacheImpl
*impl_from_IAssemblyCache(IAssemblyCache
*iface
)
157 return CONTAINING_RECORD(iface
, IAssemblyCacheImpl
, IAssemblyCache_iface
);
160 static HRESULT WINAPI
IAssemblyCacheImpl_QueryInterface(IAssemblyCache
*iface
,
161 REFIID riid
, LPVOID
*ppobj
)
163 IAssemblyCacheImpl
*This
= impl_from_IAssemblyCache(iface
);
165 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
169 if (IsEqualIID(riid
, &IID_IUnknown
) ||
170 IsEqualIID(riid
, &IID_IAssemblyCache
))
172 IAssemblyCache_AddRef(iface
);
173 *ppobj
= &This
->IAssemblyCache_iface
;
177 WARN("(%p, %s, %p): not found\n", This
, debugstr_guid(riid
), ppobj
);
178 return E_NOINTERFACE
;
181 static ULONG WINAPI
IAssemblyCacheImpl_AddRef(IAssemblyCache
*iface
)
183 IAssemblyCacheImpl
*This
= impl_from_IAssemblyCache(iface
);
184 ULONG refCount
= InterlockedIncrement(&This
->ref
);
186 TRACE("(%p)->(ref before = %u)\n", This
, refCount
- 1);
191 static ULONG WINAPI
IAssemblyCacheImpl_Release(IAssemblyCache
*iface
)
193 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
194 ULONG refCount
= InterlockedDecrement( &cache
->ref
);
196 TRACE("(%p)->(ref before = %u)\n", cache
, refCount
+ 1);
200 CloseHandle( cache
->lock
);
206 static void cache_lock( IAssemblyCacheImpl
*cache
)
208 WaitForSingleObject( cache
->lock
, INFINITE
);
211 static void cache_unlock( IAssemblyCacheImpl
*cache
)
213 ReleaseMutex( cache
->lock
);
216 static HRESULT WINAPI
IAssemblyCacheImpl_UninstallAssembly(IAssemblyCache
*iface
,
218 LPCWSTR pszAssemblyName
,
219 LPCFUSION_INSTALL_REFERENCE pRefData
,
220 ULONG
*pulDisposition
)
223 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
224 IAssemblyName
*asmname
, *next
= NULL
;
225 IAssemblyEnum
*asmenum
= NULL
;
226 WCHAR
*p
, *path
= NULL
;
230 TRACE("(%p, 0%08x, %s, %p, %p)\n", iface
, dwFlags
,
231 debugstr_w(pszAssemblyName
), pRefData
, pulDisposition
);
235 FIXME("application reference not supported\n");
238 hr
= CreateAssemblyNameObject( &asmname
, pszAssemblyName
, CANOF_PARSE_DISPLAY_NAME
, NULL
);
244 hr
= CreateAssemblyEnum( &asmenum
, NULL
, asmname
, ASM_CACHE_GAC
, NULL
);
248 hr
= IAssemblyEnum_GetNextAssembly( asmenum
, NULL
, &next
, 0 );
252 *pulDisposition
= IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED
;
255 hr
= IAssemblyName_GetPath( next
, NULL
, &len
);
256 if (hr
!= HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER
))
259 if (!(path
= heap_alloc( len
* sizeof(WCHAR
) )))
264 hr
= IAssemblyName_GetPath( next
, path
, &len
);
268 if (DeleteFileW( path
))
270 if ((p
= strrchrW( path
, '\\' )))
273 RemoveDirectoryW( path
);
274 if ((p
= strrchrW( path
, '\\' )))
277 RemoveDirectoryW( path
);
280 disp
= IASSEMBLYCACHE_UNINSTALL_DISPOSITION_UNINSTALLED
;
285 disp
= IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED
;
288 if (pulDisposition
) *pulDisposition
= disp
;
291 IAssemblyName_Release( asmname
);
292 if (next
) IAssemblyName_Release( next
);
293 if (asmenum
) IAssemblyEnum_Release( asmenum
);
295 cache_unlock( cache
);
299 static HRESULT WINAPI
IAssemblyCacheImpl_QueryAssemblyInfo(IAssemblyCache
*iface
,
301 LPCWSTR pszAssemblyName
,
302 ASSEMBLY_INFO
*pAsmInfo
)
304 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
305 IAssemblyName
*asmname
, *next
= NULL
;
306 IAssemblyEnum
*asmenum
= NULL
;
309 TRACE("(%p, %d, %s, %p)\n", iface
, dwFlags
,
310 debugstr_w(pszAssemblyName
), pAsmInfo
);
314 if (pAsmInfo
->cbAssemblyInfo
== 0)
315 pAsmInfo
->cbAssemblyInfo
= sizeof(ASSEMBLY_INFO
);
316 else if (pAsmInfo
->cbAssemblyInfo
!= sizeof(ASSEMBLY_INFO
))
320 hr
= CreateAssemblyNameObject(&asmname
, pszAssemblyName
,
321 CANOF_PARSE_DISPLAY_NAME
, NULL
);
327 hr
= CreateAssemblyEnum(&asmenum
, NULL
, asmname
, ASM_CACHE_GAC
, NULL
);
333 hr
= IAssemblyEnum_GetNextAssembly(asmenum
, NULL
, &next
, 0);
336 hr
= HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
339 hr
= IAssemblyName_IsEqual(asmname
, next
, ASM_CMPF_IL_ALL
);
340 if (hr
== S_OK
) break;
346 hr
= IAssemblyName_GetPath(next
, pAsmInfo
->pszCurrentAssemblyPathBuf
, &pAsmInfo
->cchBuf
);
348 pAsmInfo
->dwAssemblyFlags
= ASSEMBLYINFO_FLAG_INSTALLED
;
351 IAssemblyName_Release(asmname
);
352 if (next
) IAssemblyName_Release(next
);
353 if (asmenum
) IAssemblyEnum_Release(asmenum
);
354 cache_unlock( cache
);
358 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl
;
360 static HRESULT WINAPI
IAssemblyCacheImpl_CreateAssemblyCacheItem(IAssemblyCache
*iface
,
363 IAssemblyCacheItem
**ppAsmItem
,
364 LPCWSTR pszAssemblyName
)
366 IAssemblyCacheItemImpl
*item
;
368 FIXME("(%p, %d, %p, %p, %s) semi-stub!\n", iface
, dwFlags
, pvReserved
,
369 ppAsmItem
, debugstr_w(pszAssemblyName
));
376 if (!(item
= heap_alloc(sizeof(*item
)))) return E_OUTOFMEMORY
;
378 item
->IAssemblyCacheItem_iface
.lpVtbl
= &AssemblyCacheItemVtbl
;
381 *ppAsmItem
= &item
->IAssemblyCacheItem_iface
;
385 static HRESULT WINAPI
IAssemblyCacheImpl_CreateAssemblyScavenger(IAssemblyCache
*iface
,
386 IUnknown
**ppUnkReserved
)
388 FIXME("(%p, %p) stub!\n", iface
, ppUnkReserved
);
392 static HRESULT
copy_file( const WCHAR
*src_dir
, DWORD src_len
, const WCHAR
*dst_dir
, DWORD dst_len
,
393 const WCHAR
*filename
)
395 WCHAR
*src_file
, *dst_file
;
396 DWORD len
= strlenW( filename
);
399 if (!(src_file
= heap_alloc( (src_len
+ len
+ 1) * sizeof(WCHAR
) )))
400 return E_OUTOFMEMORY
;
401 memcpy( src_file
, src_dir
, src_len
* sizeof(WCHAR
) );
402 strcpyW( src_file
+ src_len
, filename
);
404 if (!(dst_file
= heap_alloc( (dst_len
+ len
+ 1) * sizeof(WCHAR
) )))
406 heap_free( src_file
);
407 return E_OUTOFMEMORY
;
409 memcpy( dst_file
, dst_dir
, dst_len
* sizeof(WCHAR
) );
410 strcpyW( dst_file
+ dst_len
, filename
);
412 if (!CopyFileW( src_file
, dst_file
, FALSE
)) hr
= HRESULT_FROM_WIN32( GetLastError() );
413 heap_free( src_file
);
414 heap_free( dst_file
);
418 static HRESULT WINAPI
IAssemblyCacheImpl_InstallAssembly(IAssemblyCache
*iface
,
420 LPCWSTR pszManifestFilePath
,
421 LPCFUSION_INSTALL_REFERENCE pRefData
)
423 static const WCHAR format
[] =
424 {'%','s','\\','%','s','\\','%','s','_','_','%','s','\\',0};
425 static const WCHAR format_v40
[] =
426 {'%','s','\\','%','s','\\','v','4','.','0','_','%','s','_','_','%','s','\\',0};
427 static const WCHAR ext_exe
[] = {'.','e','x','e',0};
428 static const WCHAR ext_dll
[] = {'.','d','l','l',0};
429 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
431 const WCHAR
*extension
, *filename
, *src_dir
;
432 WCHAR
*name
= NULL
, *token
= NULL
, *version
= NULL
, *asmpath
= NULL
;
433 WCHAR asmdir
[MAX_PATH
], *p
, **external_files
= NULL
, *dst_dir
= NULL
;
436 DWORD i
, count
= 0, src_len
, dst_len
= sizeof(format_v40
)/sizeof(format_v40
[0]);
439 TRACE("(%p, %d, %s, %p)\n", iface
, dwFlags
,
440 debugstr_w(pszManifestFilePath
), pRefData
);
442 if (!pszManifestFilePath
|| !*pszManifestFilePath
)
445 if (!(extension
= strrchrW(pszManifestFilePath
, '.')))
446 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME
);
448 if (lstrcmpiW(extension
, ext_exe
) && lstrcmpiW(extension
, ext_dll
))
449 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME
);
451 if (GetFileAttributesW(pszManifestFilePath
) == INVALID_FILE_ATTRIBUTES
)
452 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
454 hr
= assembly_create(&assembly
, pszManifestFilePath
);
457 hr
= COR_E_ASSEMBLYEXPECTED
;
461 hr
= assembly_get_name(assembly
, &name
);
465 hr
= assembly_get_pubkey_token(assembly
, &token
);
469 hr
= assembly_get_version(assembly
, &version
);
473 hr
= assembly_get_runtime_version(assembly
, &clr_version
);
477 hr
= assembly_get_external_files(assembly
, &external_files
, &count
);
483 architecture
= assembly_get_architecture(assembly
);
484 get_assembly_directory(asmdir
, MAX_PATH
, clr_version
, architecture
);
486 dst_len
+= strlenW(asmdir
) + strlenW(name
) + strlenW(version
) + strlenW(token
);
487 if (!(dst_dir
= heap_alloc(dst_len
* sizeof(WCHAR
))))
492 if (!strcmp(clr_version
, "v4.0.30319"))
493 dst_len
= sprintfW(dst_dir
, format_v40
, asmdir
, name
, version
, token
);
495 dst_len
= sprintfW(dst_dir
, format
, asmdir
, name
, version
, token
);
497 create_full_path(dst_dir
);
499 hr
= assembly_get_path(assembly
, &asmpath
);
503 if ((p
= strrchrW(asmpath
, '\\')))
507 src_len
= filename
- asmpath
;
515 hr
= copy_file(src_dir
, src_len
, dst_dir
, dst_len
, filename
);
519 for (i
= 0; i
< count
; i
++)
521 hr
= copy_file(src_dir
, src_len
, dst_dir
, dst_len
, external_files
[i
]);
532 for (i
= 0; i
< count
; i
++) heap_free(external_files
[i
]);
533 heap_free(external_files
);
534 assembly_release(assembly
);
535 cache_unlock( cache
);
539 static const IAssemblyCacheVtbl AssemblyCacheVtbl
= {
540 IAssemblyCacheImpl_QueryInterface
,
541 IAssemblyCacheImpl_AddRef
,
542 IAssemblyCacheImpl_Release
,
543 IAssemblyCacheImpl_UninstallAssembly
,
544 IAssemblyCacheImpl_QueryAssemblyInfo
,
545 IAssemblyCacheImpl_CreateAssemblyCacheItem
,
546 IAssemblyCacheImpl_CreateAssemblyScavenger
,
547 IAssemblyCacheImpl_InstallAssembly
550 /******************************************************************
551 * CreateAssemblyCache (FUSION.@)
553 HRESULT WINAPI
CreateAssemblyCache(IAssemblyCache
**ppAsmCache
, DWORD dwReserved
)
555 IAssemblyCacheImpl
*cache
;
557 TRACE("(%p, %d)\n", ppAsmCache
, dwReserved
);
564 if (!(cache
= heap_alloc(sizeof(*cache
)))) return E_OUTOFMEMORY
;
566 cache
->IAssemblyCache_iface
.lpVtbl
= &AssemblyCacheVtbl
;
568 cache
->lock
= CreateMutexW( NULL
, FALSE
, cache_mutex_nameW
);
572 return HRESULT_FROM_WIN32( GetLastError() );
574 *ppAsmCache
= &cache
->IAssemblyCache_iface
;
578 /* IAssemblyCacheItem */
580 static inline IAssemblyCacheItemImpl
*impl_from_IAssemblyCacheItem(IAssemblyCacheItem
*iface
)
582 return CONTAINING_RECORD(iface
, IAssemblyCacheItemImpl
, IAssemblyCacheItem_iface
);
585 static HRESULT WINAPI
IAssemblyCacheItemImpl_QueryInterface(IAssemblyCacheItem
*iface
,
586 REFIID riid
, LPVOID
*ppobj
)
588 IAssemblyCacheItemImpl
*This
= impl_from_IAssemblyCacheItem(iface
);
590 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
594 if (IsEqualIID(riid
, &IID_IUnknown
) ||
595 IsEqualIID(riid
, &IID_IAssemblyCacheItem
))
597 IAssemblyCacheItem_AddRef(iface
);
598 *ppobj
= &This
->IAssemblyCacheItem_iface
;
602 WARN("(%p, %s, %p): not found\n", This
, debugstr_guid(riid
), ppobj
);
603 return E_NOINTERFACE
;
606 static ULONG WINAPI
IAssemblyCacheItemImpl_AddRef(IAssemblyCacheItem
*iface
)
608 IAssemblyCacheItemImpl
*This
= impl_from_IAssemblyCacheItem(iface
);
609 ULONG refCount
= InterlockedIncrement(&This
->ref
);
611 TRACE("(%p)->(ref before = %u)\n", This
, refCount
- 1);
616 static ULONG WINAPI
IAssemblyCacheItemImpl_Release(IAssemblyCacheItem
*iface
)
618 IAssemblyCacheItemImpl
*This
= impl_from_IAssemblyCacheItem(iface
);
619 ULONG refCount
= InterlockedDecrement(&This
->ref
);
621 TRACE("(%p)->(ref before = %u)\n", This
, refCount
+ 1);
629 static HRESULT WINAPI
IAssemblyCacheItemImpl_CreateStream(IAssemblyCacheItem
*iface
,
631 LPCWSTR pszStreamName
,
635 ULARGE_INTEGER
*puliMaxSize
)
637 FIXME("(%p, %d, %s, %d, %d, %p, %p) stub!\n", iface
, dwFlags
,
638 debugstr_w(pszStreamName
), dwFormat
, dwFormatFlags
, ppIStream
, puliMaxSize
);
643 static HRESULT WINAPI
IAssemblyCacheItemImpl_Commit(IAssemblyCacheItem
*iface
,
645 ULONG
*pulDisposition
)
647 FIXME("(%p, %d, %p) stub!\n", iface
, dwFlags
, pulDisposition
);
651 static HRESULT WINAPI
IAssemblyCacheItemImpl_AbortItem(IAssemblyCacheItem
*iface
)
653 FIXME("(%p) stub!\n", iface
);
657 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl
= {
658 IAssemblyCacheItemImpl_QueryInterface
,
659 IAssemblyCacheItemImpl_AddRef
,
660 IAssemblyCacheItemImpl_Release
,
661 IAssemblyCacheItemImpl_CreateStream
,
662 IAssemblyCacheItemImpl_Commit
,
663 IAssemblyCacheItemImpl_AbortItem