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"
24 IAssemblyCache IAssemblyCache_iface
;
31 IAssemblyCacheItem IAssemblyCacheItem_iface
;
34 } IAssemblyCacheItemImpl
;
36 static const WCHAR cache_mutex_nameW
[] =
37 {'_','_','W','I','N','E','_','F','U','S','I','O','N','_','C','A','C','H','E','_','M','U','T','E','X','_','_',0};
39 static BOOL
create_full_path(LPCWSTR path
)
45 new_path
= HeapAlloc(GetProcessHeap(), 0, (strlenW(path
) + 1) * sizeof(WCHAR
));
49 strcpyW(new_path
, path
);
51 while ((len
= strlenW(new_path
)) && new_path
[len
- 1] == '\\')
52 new_path
[len
- 1] = 0;
54 while (!CreateDirectoryW(new_path
, NULL
))
57 DWORD last_error
= GetLastError();
59 if(last_error
== ERROR_ALREADY_EXISTS
)
62 if(last_error
!= ERROR_PATH_NOT_FOUND
)
68 if(!(slash
= strrchrW(new_path
, '\\')))
74 len
= slash
- new_path
;
76 if(!create_full_path(new_path
))
85 HeapFree(GetProcessHeap(), 0, new_path
);
89 static BOOL
get_assembly_directory(LPWSTR dir
, DWORD size
, const char *version
, PEKIND architecture
)
91 static const WCHAR dotnet
[] = {'\\','M','i','c','r','o','s','o','f','t','.','N','E','T','\\',0};
92 static const WCHAR gac
[] = {'\\','a','s','s','e','m','b','l','y','\\','G','A','C',0};
93 static const WCHAR msil
[] = {'_','M','S','I','L',0};
94 static const WCHAR x86
[] = {'_','3','2',0};
95 static const WCHAR amd64
[] = {'_','6','4',0};
96 DWORD len
= GetWindowsDirectoryW(dir
, size
);
98 if (!strcmp(version
, "v4.0.30319"))
100 strcpyW(dir
+ len
, dotnet
);
101 len
+= sizeof(dotnet
)/sizeof(WCHAR
) -1;
102 strcpyW(dir
+ len
, gac
+ 1);
103 len
+= sizeof(gac
)/sizeof(WCHAR
) - 2;
107 strcpyW(dir
+ len
, gac
);
108 len
+= sizeof(gac
)/sizeof(WCHAR
) - 1;
110 switch (architecture
)
116 strcpyW(dir
+ len
, msil
);
120 strcpyW(dir
+ len
, x86
);
124 strcpyW(dir
+ len
, amd64
);
128 WARN("unhandled architecture %u\n", architecture
);
136 static inline IAssemblyCacheImpl
*impl_from_IAssemblyCache(IAssemblyCache
*iface
)
138 return CONTAINING_RECORD(iface
, IAssemblyCacheImpl
, IAssemblyCache_iface
);
141 static HRESULT WINAPI
IAssemblyCacheImpl_QueryInterface(IAssemblyCache
*iface
,
142 REFIID riid
, LPVOID
*ppobj
)
144 IAssemblyCacheImpl
*This
= impl_from_IAssemblyCache(iface
);
146 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
150 if (IsEqualIID(riid
, &IID_IUnknown
) ||
151 IsEqualIID(riid
, &IID_IAssemblyCache
))
153 IAssemblyCache_AddRef(iface
);
158 WARN("(%p, %s, %p): not found\n", This
, debugstr_guid(riid
), ppobj
);
159 return E_NOINTERFACE
;
162 static ULONG WINAPI
IAssemblyCacheImpl_AddRef(IAssemblyCache
*iface
)
164 IAssemblyCacheImpl
*This
= impl_from_IAssemblyCache(iface
);
165 ULONG refCount
= InterlockedIncrement(&This
->ref
);
167 TRACE("(%p)->(ref before = %u)\n", This
, refCount
- 1);
172 static ULONG WINAPI
IAssemblyCacheImpl_Release(IAssemblyCache
*iface
)
174 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
175 ULONG refCount
= InterlockedDecrement( &cache
->ref
);
177 TRACE("(%p)->(ref before = %u)\n", cache
, refCount
+ 1);
181 CloseHandle( cache
->lock
);
182 HeapFree( GetProcessHeap(), 0, cache
);
187 static void cache_lock( IAssemblyCacheImpl
*cache
)
189 WaitForSingleObject( cache
->lock
, INFINITE
);
192 static void cache_unlock( IAssemblyCacheImpl
*cache
)
194 ReleaseMutex( cache
->lock
);
197 static HRESULT WINAPI
IAssemblyCacheImpl_UninstallAssembly(IAssemblyCache
*iface
,
199 LPCWSTR pszAssemblyName
,
200 LPCFUSION_INSTALL_REFERENCE pRefData
,
201 ULONG
*pulDisposition
)
204 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
205 IAssemblyName
*asmname
, *next
= NULL
;
206 IAssemblyEnum
*asmenum
= NULL
;
207 WCHAR
*p
, *path
= NULL
;
211 TRACE("(%p, 0%08x, %s, %p, %p)\n", iface
, dwFlags
,
212 debugstr_w(pszAssemblyName
), pRefData
, pulDisposition
);
216 FIXME("application reference not supported\n");
219 hr
= CreateAssemblyNameObject( &asmname
, pszAssemblyName
, CANOF_PARSE_DISPLAY_NAME
, NULL
);
225 hr
= CreateAssemblyEnum( &asmenum
, NULL
, asmname
, ASM_CACHE_GAC
, NULL
);
229 hr
= IAssemblyEnum_GetNextAssembly( asmenum
, NULL
, &next
, 0 );
233 *pulDisposition
= IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED
;
236 hr
= IAssemblyName_GetPath( next
, NULL
, &len
);
237 if (hr
!= HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER
))
240 if (!(path
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
245 hr
= IAssemblyName_GetPath( next
, path
, &len
);
249 if (DeleteFileW( path
))
251 if ((p
= strrchrW( path
, '\\' )))
254 RemoveDirectoryW( path
);
255 if ((p
= strrchrW( path
, '\\' )))
258 RemoveDirectoryW( path
);
261 disp
= IASSEMBLYCACHE_UNINSTALL_DISPOSITION_UNINSTALLED
;
266 disp
= IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED
;
269 if (pulDisposition
) *pulDisposition
= disp
;
272 IAssemblyName_Release( asmname
);
273 if (next
) IAssemblyName_Release( next
);
274 if (asmenum
) IAssemblyEnum_Release( asmenum
);
275 HeapFree( GetProcessHeap(), 0, path
);
276 cache_unlock( cache
);
280 static HRESULT WINAPI
IAssemblyCacheImpl_QueryAssemblyInfo(IAssemblyCache
*iface
,
282 LPCWSTR pszAssemblyName
,
283 ASSEMBLY_INFO
*pAsmInfo
)
285 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
286 IAssemblyName
*asmname
, *next
= NULL
;
287 IAssemblyEnum
*asmenum
= NULL
;
290 TRACE("(%p, %d, %s, %p)\n", iface
, dwFlags
,
291 debugstr_w(pszAssemblyName
), pAsmInfo
);
295 if (pAsmInfo
->cbAssemblyInfo
== 0)
296 pAsmInfo
->cbAssemblyInfo
= sizeof(ASSEMBLY_INFO
);
297 else if (pAsmInfo
->cbAssemblyInfo
!= sizeof(ASSEMBLY_INFO
))
301 hr
= CreateAssemblyNameObject(&asmname
, pszAssemblyName
,
302 CANOF_PARSE_DISPLAY_NAME
, NULL
);
308 hr
= CreateAssemblyEnum(&asmenum
, NULL
, asmname
, ASM_CACHE_GAC
, NULL
);
314 hr
= IAssemblyEnum_GetNextAssembly(asmenum
, NULL
, &next
, 0);
317 hr
= HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
320 hr
= IAssemblyName_IsEqual(asmname
, next
, ASM_CMPF_IL_ALL
);
321 if (hr
== S_OK
) break;
327 hr
= IAssemblyName_GetPath(next
, pAsmInfo
->pszCurrentAssemblyPathBuf
, &pAsmInfo
->cchBuf
);
329 pAsmInfo
->dwAssemblyFlags
= ASSEMBLYINFO_FLAG_INSTALLED
;
332 IAssemblyName_Release(asmname
);
333 if (next
) IAssemblyName_Release(next
);
334 if (asmenum
) IAssemblyEnum_Release(asmenum
);
335 cache_unlock( cache
);
339 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl
;
341 static HRESULT WINAPI
IAssemblyCacheImpl_CreateAssemblyCacheItem(IAssemblyCache
*iface
,
344 IAssemblyCacheItem
**ppAsmItem
,
345 LPCWSTR pszAssemblyName
)
347 IAssemblyCacheItemImpl
*item
;
349 FIXME("(%p, %d, %p, %p, %s) semi-stub!\n", iface
, dwFlags
, pvReserved
,
350 ppAsmItem
, debugstr_w(pszAssemblyName
));
357 item
= HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyCacheItemImpl
));
359 return E_OUTOFMEMORY
;
361 item
->IAssemblyCacheItem_iface
.lpVtbl
= &AssemblyCacheItemVtbl
;
364 *ppAsmItem
= &item
->IAssemblyCacheItem_iface
;
368 static HRESULT WINAPI
IAssemblyCacheImpl_CreateAssemblyScavenger(IAssemblyCache
*iface
,
369 IUnknown
**ppUnkReserved
)
371 FIXME("(%p, %p) stub!\n", iface
, ppUnkReserved
);
375 static HRESULT
copy_file( const WCHAR
*src_dir
, DWORD src_len
, const WCHAR
*dst_dir
, DWORD dst_len
,
376 const WCHAR
*filename
)
378 WCHAR
*src_file
, *dst_file
;
379 DWORD len
= strlenW( filename
);
382 if (!(src_file
= HeapAlloc( GetProcessHeap(), 0, (src_len
+ len
+ 1) * sizeof(WCHAR
) )))
383 return E_OUTOFMEMORY
;
384 memcpy( src_file
, src_dir
, src_len
* sizeof(WCHAR
) );
385 strcpyW( src_file
+ src_len
, filename
);
387 if (!(dst_file
= HeapAlloc( GetProcessHeap(), 0, (dst_len
+ len
+ 1) * sizeof(WCHAR
) )))
389 HeapFree( GetProcessHeap(), 0, src_file
);
390 return E_OUTOFMEMORY
;
392 memcpy( dst_file
, dst_dir
, dst_len
* sizeof(WCHAR
) );
393 strcpyW( dst_file
+ dst_len
, filename
);
395 if (!CopyFileW( src_file
, dst_file
, FALSE
)) hr
= HRESULT_FROM_WIN32( GetLastError() );
396 HeapFree( GetProcessHeap(), 0, src_file
);
397 HeapFree( GetProcessHeap(), 0, dst_file
);
401 static HRESULT WINAPI
IAssemblyCacheImpl_InstallAssembly(IAssemblyCache
*iface
,
403 LPCWSTR pszManifestFilePath
,
404 LPCFUSION_INSTALL_REFERENCE pRefData
)
406 static const WCHAR format
[] =
407 {'%','s','\\','%','s','\\','%','s','_','_','%','s','\\',0};
408 static const WCHAR format_v40
[] =
409 {'%','s','\\','%','s','\\','v','4','.','0','_','%','s','_','_','%','s','\\',0};
410 static const WCHAR ext_exe
[] = {'.','e','x','e',0};
411 static const WCHAR ext_dll
[] = {'.','d','l','l',0};
412 IAssemblyCacheImpl
*cache
= impl_from_IAssemblyCache(iface
);
414 const WCHAR
*extension
, *filename
, *src_dir
;
415 WCHAR
*name
= NULL
, *token
= NULL
, *version
= NULL
, *asmpath
= NULL
;
416 WCHAR asmdir
[MAX_PATH
], *p
, **external_files
= NULL
, *dst_dir
= NULL
;
419 DWORD i
, count
= 0, src_len
, dst_len
= sizeof(format_v40
)/sizeof(format_v40
[0]);
422 TRACE("(%p, %d, %s, %p)\n", iface
, dwFlags
,
423 debugstr_w(pszManifestFilePath
), pRefData
);
425 if (!pszManifestFilePath
|| !*pszManifestFilePath
)
428 if (!(extension
= strrchrW(pszManifestFilePath
, '.')))
429 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME
);
431 if (lstrcmpiW(extension
, ext_exe
) && lstrcmpiW(extension
, ext_dll
))
432 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME
);
434 if (GetFileAttributesW(pszManifestFilePath
) == INVALID_FILE_ATTRIBUTES
)
435 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
437 hr
= assembly_create(&assembly
, pszManifestFilePath
);
440 hr
= COR_E_ASSEMBLYEXPECTED
;
444 hr
= assembly_get_name(assembly
, &name
);
448 hr
= assembly_get_pubkey_token(assembly
, &token
);
452 hr
= assembly_get_version(assembly
, &version
);
456 hr
= assembly_get_runtime_version(assembly
, &clr_version
);
460 hr
= assembly_get_external_files(assembly
, &external_files
, &count
);
466 architecture
= assembly_get_architecture(assembly
);
467 get_assembly_directory(asmdir
, MAX_PATH
, clr_version
, architecture
);
469 dst_len
+= strlenW(asmdir
) + strlenW(name
) + strlenW(version
) + strlenW(token
);
470 if (!(dst_dir
= HeapAlloc(GetProcessHeap(), 0, dst_len
* sizeof(WCHAR
))))
475 if (!strcmp(clr_version
, "v4.0.30319"))
476 dst_len
= sprintfW(dst_dir
, format_v40
, asmdir
, name
, version
, token
);
478 dst_len
= sprintfW(dst_dir
, format
, asmdir
, name
, version
, token
);
480 create_full_path(dst_dir
);
482 hr
= assembly_get_path(assembly
, &asmpath
);
486 if ((p
= strrchrW(asmpath
, '\\')))
490 src_len
= filename
- asmpath
;
498 hr
= copy_file(src_dir
, src_len
, dst_dir
, dst_len
, filename
);
502 for (i
= 0; i
< count
; i
++)
504 hr
= copy_file(src_dir
, src_len
, dst_dir
, dst_len
, external_files
[i
]);
510 HeapFree(GetProcessHeap(), 0, name
);
511 HeapFree(GetProcessHeap(), 0, token
);
512 HeapFree(GetProcessHeap(), 0, version
);
513 HeapFree(GetProcessHeap(), 0, asmpath
);
514 HeapFree(GetProcessHeap(), 0, dst_dir
);
515 for (i
= 0; i
< count
; i
++) HeapFree(GetProcessHeap(), 0, external_files
[i
]);
516 HeapFree(GetProcessHeap(), 0, external_files
);
517 assembly_release(assembly
);
518 cache_unlock( cache
);
522 static const IAssemblyCacheVtbl AssemblyCacheVtbl
= {
523 IAssemblyCacheImpl_QueryInterface
,
524 IAssemblyCacheImpl_AddRef
,
525 IAssemblyCacheImpl_Release
,
526 IAssemblyCacheImpl_UninstallAssembly
,
527 IAssemblyCacheImpl_QueryAssemblyInfo
,
528 IAssemblyCacheImpl_CreateAssemblyCacheItem
,
529 IAssemblyCacheImpl_CreateAssemblyScavenger
,
530 IAssemblyCacheImpl_InstallAssembly
533 /******************************************************************
534 * CreateAssemblyCache (FUSION.@)
536 HRESULT WINAPI
CreateAssemblyCache(IAssemblyCache
**ppAsmCache
, DWORD dwReserved
)
538 IAssemblyCacheImpl
*cache
;
540 TRACE("(%p, %d)\n", ppAsmCache
, dwReserved
);
547 cache
= HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyCacheImpl
));
549 return E_OUTOFMEMORY
;
551 cache
->IAssemblyCache_iface
.lpVtbl
= &AssemblyCacheVtbl
;
553 cache
->lock
= CreateMutexW( NULL
, FALSE
, cache_mutex_nameW
);
556 HeapFree( GetProcessHeap(), 0, cache
);
557 return HRESULT_FROM_WIN32( GetLastError() );
559 *ppAsmCache
= &cache
->IAssemblyCache_iface
;
563 /* IAssemblyCacheItem */
565 static inline IAssemblyCacheItemImpl
*impl_from_IAssemblyCacheItem(IAssemblyCacheItem
*iface
)
567 return CONTAINING_RECORD(iface
, IAssemblyCacheItemImpl
, IAssemblyCacheItem_iface
);
570 static HRESULT WINAPI
IAssemblyCacheItemImpl_QueryInterface(IAssemblyCacheItem
*iface
,
571 REFIID riid
, LPVOID
*ppobj
)
573 IAssemblyCacheItemImpl
*This
= impl_from_IAssemblyCacheItem(iface
);
575 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
579 if (IsEqualIID(riid
, &IID_IUnknown
) ||
580 IsEqualIID(riid
, &IID_IAssemblyCacheItem
))
582 IAssemblyCacheItem_AddRef(iface
);
587 WARN("(%p, %s, %p): not found\n", This
, debugstr_guid(riid
), ppobj
);
588 return E_NOINTERFACE
;
591 static ULONG WINAPI
IAssemblyCacheItemImpl_AddRef(IAssemblyCacheItem
*iface
)
593 IAssemblyCacheItemImpl
*This
= impl_from_IAssemblyCacheItem(iface
);
594 ULONG refCount
= InterlockedIncrement(&This
->ref
);
596 TRACE("(%p)->(ref before = %u)\n", This
, refCount
- 1);
601 static ULONG WINAPI
IAssemblyCacheItemImpl_Release(IAssemblyCacheItem
*iface
)
603 IAssemblyCacheItemImpl
*This
= impl_from_IAssemblyCacheItem(iface
);
604 ULONG refCount
= InterlockedDecrement(&This
->ref
);
606 TRACE("(%p)->(ref before = %u)\n", This
, refCount
+ 1);
609 HeapFree(GetProcessHeap(), 0, This
);
614 static HRESULT WINAPI
IAssemblyCacheItemImpl_CreateStream(IAssemblyCacheItem
*iface
,
616 LPCWSTR pszStreamName
,
620 ULARGE_INTEGER
*puliMaxSize
)
622 FIXME("(%p, %d, %s, %d, %d, %p, %p) stub!\n", iface
, dwFlags
,
623 debugstr_w(pszStreamName
), dwFormat
, dwFormatFlags
, ppIStream
, puliMaxSize
);
628 static HRESULT WINAPI
IAssemblyCacheItemImpl_Commit(IAssemblyCacheItem
*iface
,
630 ULONG
*pulDisposition
)
632 FIXME("(%p, %d, %p) stub!\n", iface
, dwFlags
, pulDisposition
);
636 static HRESULT WINAPI
IAssemblyCacheItemImpl_AbortItem(IAssemblyCacheItem
*iface
)
638 FIXME("(%p) stub!\n", iface
);
642 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl
= {
643 IAssemblyCacheItemImpl_QueryInterface
,
644 IAssemblyCacheItemImpl_AddRef
,
645 IAssemblyCacheItemImpl_Release
,
646 IAssemblyCacheItemImpl_CreateStream
,
647 IAssemblyCacheItemImpl_Commit
,
648 IAssemblyCacheItemImpl_AbortItem