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 "fusionpriv.h"
23 static const WCHAR cache_mutex_nameW
[] =
24 {'_','_','W','I','N','E','_','F','U','S','I','O','N','_','C','A','C','H','E','_','M','U','T','E','X','_','_',0};
26 static BOOL
create_full_path(LPCWSTR path
)
32 new_path
= HeapAlloc(GetProcessHeap(), 0, (strlenW(path
) + 1) * sizeof(WCHAR
));
36 strcpyW(new_path
, path
);
38 while ((len
= strlenW(new_path
)) && new_path
[len
- 1] == '\\')
39 new_path
[len
- 1] = 0;
41 while (!CreateDirectoryW(new_path
, NULL
))
44 DWORD last_error
= GetLastError();
46 if(last_error
== ERROR_ALREADY_EXISTS
)
49 if(last_error
!= ERROR_PATH_NOT_FOUND
)
55 if(!(slash
= strrchrW(new_path
, '\\')))
61 len
= slash
- new_path
;
63 if(!create_full_path(new_path
))
72 HeapFree(GetProcessHeap(), 0, new_path
);
76 static BOOL
get_assembly_directory(LPWSTR dir
, DWORD size
, const char *version
, PEKIND architecture
)
78 static const WCHAR dotnet
[] = {'\\','M','i','c','r','o','s','o','f','t','.','N','E','T','\\',0};
79 static const WCHAR gac
[] = {'\\','a','s','s','e','m','b','l','y','\\','G','A','C',0};
80 static const WCHAR msil
[] = {'_','M','S','I','L',0};
81 static const WCHAR x86
[] = {'_','3','2',0};
82 static const WCHAR amd64
[] = {'_','6','4',0};
83 DWORD len
= GetWindowsDirectoryW(dir
, size
);
85 if (!strcmp(version
, "v4.0.30319"))
87 strcpyW(dir
+ len
, dotnet
);
88 len
+= sizeof(dotnet
)/sizeof(WCHAR
) -1;
89 strcpyW(dir
+ len
, gac
+ 1);
90 len
+= sizeof(gac
)/sizeof(WCHAR
) - 2;
94 strcpyW(dir
+ len
, gac
);
95 len
+= sizeof(gac
)/sizeof(WCHAR
) - 1;
103 strcpyW(dir
+ len
, msil
);
107 strcpyW(dir
+ len
, x86
);
111 strcpyW(dir
+ len
, amd64
);
115 WARN("unhandled architecture %u\n", architecture
);
124 IAssemblyCache IAssemblyCache_iface
;
128 } IAssemblyCacheImpl
;
130 static inline IAssemblyCacheImpl
*impl_from_IAssemblyCache(IAssemblyCache
*iface
)
132 return CONTAINING_RECORD(iface
, IAssemblyCacheImpl
, IAssemblyCache_iface
);
135 static HRESULT WINAPI
IAssemblyCacheImpl_QueryInterface(IAssemblyCache
*iface
,
136 REFIID riid
, LPVOID
*ppobj
)
138 IAssemblyCacheImpl
*This
= impl_from_IAssemblyCache(iface
);
140 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
144 if (IsEqualIID(riid
, &IID_IUnknown
) ||
145 IsEqualIID(riid
, &IID_IAssemblyCache
))
147 IAssemblyCache_AddRef(iface
);
152 WARN("(%p, %s, %p): not found\n", This
, debugstr_guid(riid
), ppobj
);
153 return E_NOINTERFACE
;
156 static ULONG WINAPI
IAssemblyCacheImpl_AddRef(IAssemblyCache
*iface
)
158 IAssemblyCacheImpl
*This
= impl_from_IAssemblyCache(iface
);
159 ULONG refCount
= InterlockedIncrement(&This
->ref
);
161 TRACE("(%p)->(ref before = %u)\n", This
, refCount
- 1);
166 static ULONG WINAPI
IAssemblyCacheImpl_Release(IAssemblyCache
*iface
)
168 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
169 ULONG refCount
= InterlockedDecrement( &cache
->ref
);
171 TRACE("(%p)->(ref before = %u)\n", cache
, refCount
+ 1);
175 CloseHandle( cache
->lock
);
176 HeapFree( GetProcessHeap(), 0, cache
);
181 static void cache_lock( IAssemblyCacheImpl
*cache
)
183 WaitForSingleObject( cache
->lock
, INFINITE
);
186 static void cache_unlock( IAssemblyCacheImpl
*cache
)
188 ReleaseMutex( cache
->lock
);
191 static HRESULT WINAPI
IAssemblyCacheImpl_UninstallAssembly(IAssemblyCache
*iface
,
193 LPCWSTR pszAssemblyName
,
194 LPCFUSION_INSTALL_REFERENCE pRefData
,
195 ULONG
*pulDisposition
)
198 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
199 IAssemblyName
*asmname
, *next
= NULL
;
200 IAssemblyEnum
*asmenum
= NULL
;
201 WCHAR
*p
, *path
= NULL
;
205 TRACE("(%p, 0%08x, %s, %p, %p)\n", iface
, dwFlags
,
206 debugstr_w(pszAssemblyName
), pRefData
, pulDisposition
);
210 FIXME("application reference not supported\n");
213 hr
= CreateAssemblyNameObject( &asmname
, pszAssemblyName
, CANOF_PARSE_DISPLAY_NAME
, NULL
);
219 hr
= CreateAssemblyEnum( &asmenum
, NULL
, asmname
, ASM_CACHE_GAC
, NULL
);
223 hr
= IAssemblyEnum_GetNextAssembly( asmenum
, NULL
, &next
, 0 );
227 *pulDisposition
= IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED
;
230 hr
= IAssemblyName_GetPath( next
, NULL
, &len
);
231 if (hr
!= HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER
))
234 if (!(path
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
239 hr
= IAssemblyName_GetPath( next
, path
, &len
);
243 if (DeleteFileW( path
))
245 if ((p
= strrchrW( path
, '\\' )))
248 RemoveDirectoryW( path
);
249 if ((p
= strrchrW( path
, '\\' )))
252 RemoveDirectoryW( path
);
255 disp
= IASSEMBLYCACHE_UNINSTALL_DISPOSITION_UNINSTALLED
;
260 disp
= IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED
;
263 if (pulDisposition
) *pulDisposition
= disp
;
266 IAssemblyName_Release( asmname
);
267 if (next
) IAssemblyName_Release( next
);
268 if (asmenum
) IAssemblyEnum_Release( asmenum
);
269 HeapFree( GetProcessHeap(), 0, path
);
270 cache_unlock( cache
);
274 static HRESULT WINAPI
IAssemblyCacheImpl_QueryAssemblyInfo(IAssemblyCache
*iface
,
276 LPCWSTR pszAssemblyName
,
277 ASSEMBLY_INFO
*pAsmInfo
)
279 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
280 IAssemblyName
*asmname
, *next
= NULL
;
281 IAssemblyEnum
*asmenum
= NULL
;
284 TRACE("(%p, %d, %s, %p)\n", iface
, dwFlags
,
285 debugstr_w(pszAssemblyName
), pAsmInfo
);
289 if (pAsmInfo
->cbAssemblyInfo
== 0)
290 pAsmInfo
->cbAssemblyInfo
= sizeof(ASSEMBLY_INFO
);
291 else if (pAsmInfo
->cbAssemblyInfo
!= sizeof(ASSEMBLY_INFO
))
295 hr
= CreateAssemblyNameObject(&asmname
, pszAssemblyName
,
296 CANOF_PARSE_DISPLAY_NAME
, NULL
);
302 hr
= CreateAssemblyEnum(&asmenum
, NULL
, asmname
, ASM_CACHE_GAC
, NULL
);
308 hr
= IAssemblyEnum_GetNextAssembly(asmenum
, NULL
, &next
, 0);
311 hr
= HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
314 hr
= IAssemblyName_IsEqual(asmname
, next
, ASM_CMPF_IL_ALL
);
315 if (hr
== S_OK
) break;
321 hr
= IAssemblyName_GetPath(next
, pAsmInfo
->pszCurrentAssemblyPathBuf
, &pAsmInfo
->cchBuf
);
323 pAsmInfo
->dwAssemblyFlags
= ASSEMBLYINFO_FLAG_INSTALLED
;
326 IAssemblyName_Release(asmname
);
327 if (next
) IAssemblyName_Release(next
);
328 if (asmenum
) IAssemblyEnum_Release(asmenum
);
329 cache_unlock( cache
);
333 static HRESULT WINAPI
IAssemblyCacheImpl_CreateAssemblyCacheItem(IAssemblyCache
*iface
,
336 IAssemblyCacheItem
**ppAsmItem
,
337 LPCWSTR pszAssemblyName
)
339 FIXME("(%p, %d, %p, %p, %s) stub!\n", iface
, dwFlags
, pvReserved
,
340 ppAsmItem
, debugstr_w(pszAssemblyName
));
345 static HRESULT WINAPI
IAssemblyCacheImpl_CreateAssemblyScavenger(IAssemblyCache
*iface
,
346 IUnknown
**ppUnkReserved
)
348 FIXME("(%p, %p) stub!\n", iface
, ppUnkReserved
);
352 static HRESULT
copy_file( const WCHAR
*src_dir
, DWORD src_len
, const WCHAR
*dst_dir
, DWORD dst_len
,
353 const WCHAR
*filename
)
355 WCHAR
*src_file
, *dst_file
;
356 DWORD len
= strlenW( filename
);
359 if (!(src_file
= HeapAlloc( GetProcessHeap(), 0, (src_len
+ len
+ 1) * sizeof(WCHAR
) )))
360 return E_OUTOFMEMORY
;
361 memcpy( src_file
, src_dir
, src_len
* sizeof(WCHAR
) );
362 strcpyW( src_file
+ src_len
, filename
);
364 if (!(dst_file
= HeapAlloc( GetProcessHeap(), 0, (dst_len
+ len
+ 1) * sizeof(WCHAR
) )))
366 HeapFree( GetProcessHeap(), 0, src_file
);
367 return E_OUTOFMEMORY
;
369 memcpy( dst_file
, dst_dir
, dst_len
* sizeof(WCHAR
) );
370 strcpyW( dst_file
+ dst_len
, filename
);
372 if (!CopyFileW( src_file
, dst_file
, FALSE
)) hr
= HRESULT_FROM_WIN32( GetLastError() );
373 HeapFree( GetProcessHeap(), 0, src_file
);
374 HeapFree( GetProcessHeap(), 0, dst_file
);
378 static HRESULT WINAPI
IAssemblyCacheImpl_InstallAssembly(IAssemblyCache
*iface
,
380 LPCWSTR pszManifestFilePath
,
381 LPCFUSION_INSTALL_REFERENCE pRefData
)
383 static const WCHAR format
[] =
384 {'%','s','\\','%','s','\\','%','s','_','_','%','s','\\',0};
385 static const WCHAR format_v40
[] =
386 {'%','s','\\','%','s','\\','v','4','.','0','_','%','s','_','_','%','s','\\',0};
387 static const WCHAR ext_exe
[] = {'.','e','x','e',0};
388 static const WCHAR ext_dll
[] = {'.','d','l','l',0};
389 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
391 const WCHAR
*extension
, *filename
, *src_dir
;
392 WCHAR
*name
= NULL
, *token
= NULL
, *version
= NULL
, *asmpath
= NULL
;
393 WCHAR asmdir
[MAX_PATH
], *p
, **external_files
= NULL
, *dst_dir
= NULL
;
396 DWORD i
, count
= 0, src_len
, dst_len
= sizeof(format_v40
)/sizeof(format_v40
[0]);
399 TRACE("(%p, %d, %s, %p)\n", iface
, dwFlags
,
400 debugstr_w(pszManifestFilePath
), pRefData
);
402 if (!pszManifestFilePath
|| !*pszManifestFilePath
)
405 if (!(extension
= strrchrW(pszManifestFilePath
, '.')))
406 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME
);
408 if (lstrcmpiW(extension
, ext_exe
) && lstrcmpiW(extension
, ext_dll
))
409 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME
);
411 if (GetFileAttributesW(pszManifestFilePath
) == INVALID_FILE_ATTRIBUTES
)
412 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
414 hr
= assembly_create(&assembly
, pszManifestFilePath
);
417 hr
= COR_E_ASSEMBLYEXPECTED
;
421 hr
= assembly_get_name(assembly
, &name
);
425 hr
= assembly_get_pubkey_token(assembly
, &token
);
429 hr
= assembly_get_version(assembly
, &version
);
433 hr
= assembly_get_runtime_version(assembly
, &clr_version
);
437 hr
= assembly_get_external_files(assembly
, &external_files
, &count
);
443 architecture
= assembly_get_architecture(assembly
);
444 get_assembly_directory(asmdir
, MAX_PATH
, clr_version
, architecture
);
446 dst_len
+= strlenW(asmdir
) + strlenW(name
) + strlenW(version
) + strlenW(token
);
447 if (!(dst_dir
= HeapAlloc(GetProcessHeap(), 0, dst_len
* sizeof(WCHAR
))))
452 if (!strcmp(clr_version
, "v4.0.30319"))
453 dst_len
= sprintfW(dst_dir
, format_v40
, asmdir
, name
, version
, token
);
455 dst_len
= sprintfW(dst_dir
, format
, asmdir
, name
, version
, token
);
457 create_full_path(dst_dir
);
459 hr
= assembly_get_path(assembly
, &asmpath
);
463 if ((p
= strrchrW(asmpath
, '\\')))
467 src_len
= filename
- asmpath
;
475 hr
= copy_file(src_dir
, src_len
, dst_dir
, dst_len
, filename
);
479 for (i
= 0; i
< count
; i
++)
481 hr
= copy_file(src_dir
, src_len
, dst_dir
, dst_len
, external_files
[i
]);
487 HeapFree(GetProcessHeap(), 0, name
);
488 HeapFree(GetProcessHeap(), 0, token
);
489 HeapFree(GetProcessHeap(), 0, version
);
490 HeapFree(GetProcessHeap(), 0, asmpath
);
491 HeapFree(GetProcessHeap(), 0, dst_dir
);
492 for (i
= 0; i
< count
; i
++) HeapFree(GetProcessHeap(), 0, external_files
[i
]);
493 HeapFree(GetProcessHeap(), 0, external_files
);
494 assembly_release(assembly
);
495 cache_unlock( cache
);
499 static const IAssemblyCacheVtbl AssemblyCacheVtbl
= {
500 IAssemblyCacheImpl_QueryInterface
,
501 IAssemblyCacheImpl_AddRef
,
502 IAssemblyCacheImpl_Release
,
503 IAssemblyCacheImpl_UninstallAssembly
,
504 IAssemblyCacheImpl_QueryAssemblyInfo
,
505 IAssemblyCacheImpl_CreateAssemblyCacheItem
,
506 IAssemblyCacheImpl_CreateAssemblyScavenger
,
507 IAssemblyCacheImpl_InstallAssembly
510 /******************************************************************
511 * CreateAssemblyCache (FUSION.@)
513 HRESULT WINAPI
CreateAssemblyCache(IAssemblyCache
**ppAsmCache
, DWORD dwReserved
)
515 IAssemblyCacheImpl
*cache
;
517 TRACE("(%p, %d)\n", ppAsmCache
, dwReserved
);
524 cache
= HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyCacheImpl
));
526 return E_OUTOFMEMORY
;
528 cache
->IAssemblyCache_iface
.lpVtbl
= &AssemblyCacheVtbl
;
530 cache
->lock
= CreateMutexW( NULL
, FALSE
, cache_mutex_nameW
);
533 HeapFree( GetProcessHeap(), 0, cache
);
534 return HRESULT_FROM_WIN32( GetLastError() );
536 *ppAsmCache
= &cache
->IAssemblyCache_iface
;
540 /* IAssemblyCacheItem */
543 IAssemblyCacheItem IAssemblyCacheItem_iface
;
546 } IAssemblyCacheItemImpl
;
548 static inline IAssemblyCacheItemImpl
*impl_from_IAssemblyCacheItem(IAssemblyCacheItem
*iface
)
550 return CONTAINING_RECORD(iface
, IAssemblyCacheItemImpl
, IAssemblyCacheItem_iface
);
553 static HRESULT WINAPI
IAssemblyCacheItemImpl_QueryInterface(IAssemblyCacheItem
*iface
,
554 REFIID riid
, LPVOID
*ppobj
)
556 IAssemblyCacheItemImpl
*This
= impl_from_IAssemblyCacheItem(iface
);
558 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
562 if (IsEqualIID(riid
, &IID_IUnknown
) ||
563 IsEqualIID(riid
, &IID_IAssemblyCacheItem
))
565 IAssemblyCacheItem_AddRef(iface
);
570 WARN("(%p, %s, %p): not found\n", This
, debugstr_guid(riid
), ppobj
);
571 return E_NOINTERFACE
;
574 static ULONG WINAPI
IAssemblyCacheItemImpl_AddRef(IAssemblyCacheItem
*iface
)
576 IAssemblyCacheItemImpl
*This
= impl_from_IAssemblyCacheItem(iface
);
577 ULONG refCount
= InterlockedIncrement(&This
->ref
);
579 TRACE("(%p)->(ref before = %u)\n", This
, refCount
- 1);
584 static ULONG WINAPI
IAssemblyCacheItemImpl_Release(IAssemblyCacheItem
*iface
)
586 IAssemblyCacheItemImpl
*This
= impl_from_IAssemblyCacheItem(iface
);
587 ULONG refCount
= InterlockedDecrement(&This
->ref
);
589 TRACE("(%p)->(ref before = %u)\n", This
, refCount
+ 1);
592 HeapFree(GetProcessHeap(), 0, This
);
597 static HRESULT WINAPI
IAssemblyCacheItemImpl_CreateStream(IAssemblyCacheItem
*iface
,
599 LPCWSTR pszStreamName
,
603 ULARGE_INTEGER
*puliMaxSize
)
605 FIXME("(%p, %d, %s, %d, %d, %p, %p) stub!\n", iface
, dwFlags
,
606 debugstr_w(pszStreamName
), dwFormat
, dwFormatFlags
, ppIStream
, puliMaxSize
);
611 static HRESULT WINAPI
IAssemblyCacheItemImpl_Commit(IAssemblyCacheItem
*iface
,
613 ULONG
*pulDisposition
)
615 FIXME("(%p, %d, %p) stub!\n", iface
, dwFlags
, pulDisposition
);
619 static HRESULT WINAPI
IAssemblyCacheItemImpl_AbortItem(IAssemblyCacheItem
*iface
)
621 FIXME("(%p) stub!\n", iface
);
625 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl
= {
626 IAssemblyCacheItemImpl_QueryInterface
,
627 IAssemblyCacheItemImpl_AddRef
,
628 IAssemblyCacheItemImpl_Release
,
629 IAssemblyCacheItemImpl_CreateStream
,
630 IAssemblyCacheItemImpl_Commit
,
631 IAssemblyCacheItemImpl_AbortItem