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
);
44 static BOOL
create_full_path(LPCWSTR path
)
50 new_path
= HeapAlloc(GetProcessHeap(), 0, (strlenW(path
) + 1) * sizeof(WCHAR
));
54 strcpyW(new_path
, path
);
56 while ((len
= strlenW(new_path
)) && new_path
[len
- 1] == '\\')
57 new_path
[len
- 1] = 0;
59 while (!CreateDirectoryW(new_path
, NULL
))
62 DWORD last_error
= GetLastError();
64 if(last_error
== ERROR_ALREADY_EXISTS
)
67 if(last_error
!= ERROR_PATH_NOT_FOUND
)
73 if(!(slash
= strrchrW(new_path
, '\\')))
79 len
= slash
- new_path
;
81 if(!create_full_path(new_path
))
90 HeapFree(GetProcessHeap(), 0, new_path
);
94 static BOOL
get_assembly_directory(LPWSTR dir
, DWORD size
, BYTE architecture
)
96 static const WCHAR gac
[] = {'\\','a','s','s','e','m','b','l','y','\\','G','A','C',0};
98 static const WCHAR msil
[] = {'_','M','S','I','L',0};
99 static const WCHAR x86
[] = {'_','3','2',0};
100 static const WCHAR amd64
[] = {'_','6','4',0};
102 GetWindowsDirectoryW(dir
, size
);
105 switch (architecture
)
126 IAssemblyCache IAssemblyCache_iface
;
129 } IAssemblyCacheImpl
;
131 static inline IAssemblyCacheImpl
*impl_from_IAssemblyCache(IAssemblyCache
*iface
)
133 return CONTAINING_RECORD(iface
, IAssemblyCacheImpl
, IAssemblyCache_iface
);
136 static HRESULT WINAPI
IAssemblyCacheImpl_QueryInterface(IAssemblyCache
*iface
,
137 REFIID riid
, LPVOID
*ppobj
)
139 IAssemblyCacheImpl
*This
= impl_from_IAssemblyCache(iface
);
141 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
145 if (IsEqualIID(riid
, &IID_IUnknown
) ||
146 IsEqualIID(riid
, &IID_IAssemblyCache
))
148 IUnknown_AddRef(iface
);
153 WARN("(%p, %s, %p): not found\n", This
, debugstr_guid(riid
), ppobj
);
154 return E_NOINTERFACE
;
157 static ULONG WINAPI
IAssemblyCacheImpl_AddRef(IAssemblyCache
*iface
)
159 IAssemblyCacheImpl
*This
= impl_from_IAssemblyCache(iface
);
160 ULONG refCount
= InterlockedIncrement(&This
->ref
);
162 TRACE("(%p)->(ref before = %u)\n", This
, refCount
- 1);
167 static ULONG WINAPI
IAssemblyCacheImpl_Release(IAssemblyCache
*iface
)
169 IAssemblyCacheImpl
*This
= impl_from_IAssemblyCache(iface
);
170 ULONG refCount
= InterlockedDecrement(&This
->ref
);
172 TRACE("(%p)->(ref before = %u)\n", This
, refCount
+ 1);
175 HeapFree(GetProcessHeap(), 0, This
);
180 static HRESULT WINAPI
IAssemblyCacheImpl_UninstallAssembly(IAssemblyCache
*iface
,
182 LPCWSTR pszAssemblyName
,
183 LPCFUSION_INSTALL_REFERENCE pRefData
,
184 ULONG
*pulDisposition
)
187 IAssemblyName
*asmname
, *next
= NULL
;
188 IAssemblyEnum
*asmenum
= NULL
;
189 WCHAR
*p
, *path
= NULL
;
193 TRACE("(%p, 0%08x, %s, %p, %p)\n", iface
, dwFlags
,
194 debugstr_w(pszAssemblyName
), pRefData
, pulDisposition
);
198 FIXME("application reference not supported\n");
201 hr
= CreateAssemblyNameObject( &asmname
, pszAssemblyName
, CANOF_PARSE_DISPLAY_NAME
, NULL
);
205 hr
= CreateAssemblyEnum( &asmenum
, NULL
, asmname
, ASM_CACHE_GAC
, NULL
);
209 hr
= IAssemblyEnum_GetNextAssembly( asmenum
, NULL
, &next
, 0 );
213 *pulDisposition
= IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED
;
216 hr
= IAssemblyName_GetPath( next
, NULL
, &len
);
217 if (hr
!= HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER
))
220 if (!(path
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
225 hr
= IAssemblyName_GetPath( next
, path
, &len
);
229 if (DeleteFileW( path
))
231 if ((p
= strrchrW( path
, '\\' )))
234 RemoveDirectoryW( path
);
235 if ((p
= strrchrW( path
, '\\' )))
238 RemoveDirectoryW( path
);
241 disp
= IASSEMBLYCACHE_UNINSTALL_DISPOSITION_UNINSTALLED
;
246 disp
= IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED
;
249 if (pulDisposition
) *pulDisposition
= disp
;
252 IAssemblyName_Release( asmname
);
253 if (next
) IAssemblyName_Release( next
);
254 if (asmenum
) IAssemblyEnum_Release( asmenum
);
255 HeapFree( GetProcessHeap(), 0, path
);
259 static HRESULT WINAPI
IAssemblyCacheImpl_QueryAssemblyInfo(IAssemblyCache
*iface
,
261 LPCWSTR pszAssemblyName
,
262 ASSEMBLY_INFO
*pAsmInfo
)
264 IAssemblyName
*asmname
, *next
= NULL
;
265 IAssemblyEnum
*asmenum
= NULL
;
268 TRACE("(%p, %d, %s, %p)\n", iface
, dwFlags
,
269 debugstr_w(pszAssemblyName
), pAsmInfo
);
273 if (pAsmInfo
->cbAssemblyInfo
== 0)
274 pAsmInfo
->cbAssemblyInfo
= sizeof(ASSEMBLY_INFO
);
275 else if (pAsmInfo
->cbAssemblyInfo
!= sizeof(ASSEMBLY_INFO
))
279 hr
= CreateAssemblyNameObject(&asmname
, pszAssemblyName
,
280 CANOF_PARSE_DISPLAY_NAME
, NULL
);
284 hr
= CreateAssemblyEnum(&asmenum
, NULL
, asmname
, ASM_CACHE_GAC
, NULL
);
288 hr
= IAssemblyEnum_GetNextAssembly(asmenum
, NULL
, &next
, 0);
291 hr
= HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
298 hr
= IAssemblyName_GetPath(next
, pAsmInfo
->pszCurrentAssemblyPathBuf
, &pAsmInfo
->cchBuf
);
300 pAsmInfo
->dwAssemblyFlags
= ASSEMBLYINFO_FLAG_INSTALLED
;
303 IAssemblyName_Release(asmname
);
304 if (next
) IAssemblyName_Release(next
);
305 if (asmenum
) IAssemblyEnum_Release(asmenum
);
310 static HRESULT WINAPI
IAssemblyCacheImpl_CreateAssemblyCacheItem(IAssemblyCache
*iface
,
313 IAssemblyCacheItem
**ppAsmItem
,
314 LPCWSTR pszAssemblyName
)
316 FIXME("(%p, %d, %p, %p, %s) stub!\n", iface
, dwFlags
, pvReserved
,
317 ppAsmItem
, debugstr_w(pszAssemblyName
));
322 static HRESULT WINAPI
IAssemblyCacheImpl_CreateAssemblyScavenger(IAssemblyCache
*iface
,
323 IUnknown
**ppUnkReserved
)
325 FIXME("(%p, %p) stub!\n", iface
, ppUnkReserved
);
329 static HRESULT WINAPI
IAssemblyCacheImpl_InstallAssembly(IAssemblyCache
*iface
,
331 LPCWSTR pszManifestFilePath
,
332 LPCFUSION_INSTALL_REFERENCE pRefData
)
334 static const WCHAR format
[] =
335 {'%','s','\\','%','s','\\','%','s','_','_','%','s','\\',0};
341 LPWSTR version
= NULL
;
342 LPWSTR asmpath
= NULL
;
343 WCHAR path
[MAX_PATH
];
344 WCHAR asmdir
[MAX_PATH
];
348 static const WCHAR ext_exe
[] = {'.','e','x','e',0};
349 static const WCHAR ext_dll
[] = {'.','d','l','l',0};
351 TRACE("(%p, %d, %s, %p)\n", iface
, dwFlags
,
352 debugstr_w(pszManifestFilePath
), pRefData
);
354 if (!pszManifestFilePath
|| !*pszManifestFilePath
)
357 if (!(ext
= strrchrW(pszManifestFilePath
, '.')))
358 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME
);
360 if (lstrcmpiW(ext
, ext_exe
) && lstrcmpiW(ext
, ext_dll
))
361 return HRESULT_FROM_WIN32(ERROR_INVALID_NAME
);
363 if (GetFileAttributesW(pszManifestFilePath
) == INVALID_FILE_ATTRIBUTES
)
364 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
366 hr
= assembly_create(&assembly
, pszManifestFilePath
);
369 hr
= COR_E_ASSEMBLYEXPECTED
;
373 hr
= assembly_get_name(assembly
, &name
);
377 hr
= assembly_get_pubkey_token(assembly
, &token
);
381 hr
= assembly_get_version(assembly
, &version
);
385 get_assembly_directory(asmdir
, MAX_PATH
, assembly_get_architecture(assembly
));
387 sprintfW(path
, format
, asmdir
, name
, version
, token
);
389 create_full_path(path
);
391 hr
= assembly_get_path(assembly
, &asmpath
);
395 filename
= PathFindFileNameW(asmpath
);
397 strcatW(path
, filename
);
398 if (!CopyFileW(asmpath
, path
, FALSE
))
399 hr
= HRESULT_FROM_WIN32(GetLastError());
402 HeapFree(GetProcessHeap(), 0, name
);
403 HeapFree(GetProcessHeap(), 0, token
);
404 HeapFree(GetProcessHeap(), 0, version
);
405 HeapFree(GetProcessHeap(), 0, asmpath
);
406 assembly_release(assembly
);
410 static const IAssemblyCacheVtbl AssemblyCacheVtbl
= {
411 IAssemblyCacheImpl_QueryInterface
,
412 IAssemblyCacheImpl_AddRef
,
413 IAssemblyCacheImpl_Release
,
414 IAssemblyCacheImpl_UninstallAssembly
,
415 IAssemblyCacheImpl_QueryAssemblyInfo
,
416 IAssemblyCacheImpl_CreateAssemblyCacheItem
,
417 IAssemblyCacheImpl_CreateAssemblyScavenger
,
418 IAssemblyCacheImpl_InstallAssembly
421 /******************************************************************
422 * CreateAssemblyCache (FUSION.@)
424 HRESULT WINAPI
CreateAssemblyCache(IAssemblyCache
**ppAsmCache
, DWORD dwReserved
)
426 IAssemblyCacheImpl
*cache
;
428 TRACE("(%p, %d)\n", ppAsmCache
, dwReserved
);
435 cache
= HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyCacheImpl
));
437 return E_OUTOFMEMORY
;
439 cache
->IAssemblyCache_iface
.lpVtbl
= &AssemblyCacheVtbl
;
442 *ppAsmCache
= &cache
->IAssemblyCache_iface
;
447 /* IAssemblyCacheItem */
450 IAssemblyCacheItem IAssemblyCacheItem_iface
;
453 } IAssemblyCacheItemImpl
;
455 static inline IAssemblyCacheItemImpl
*impl_from_IAssemblyCacheItem(IAssemblyCacheItem
*iface
)
457 return CONTAINING_RECORD(iface
, IAssemblyCacheItemImpl
, IAssemblyCacheItem_iface
);
460 static HRESULT WINAPI
IAssemblyCacheItemImpl_QueryInterface(IAssemblyCacheItem
*iface
,
461 REFIID riid
, LPVOID
*ppobj
)
463 IAssemblyCacheItemImpl
*This
= impl_from_IAssemblyCacheItem(iface
);
465 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
469 if (IsEqualIID(riid
, &IID_IUnknown
) ||
470 IsEqualIID(riid
, &IID_IAssemblyCacheItem
))
472 IUnknown_AddRef(iface
);
477 WARN("(%p, %s, %p): not found\n", This
, debugstr_guid(riid
), ppobj
);
478 return E_NOINTERFACE
;
481 static ULONG WINAPI
IAssemblyCacheItemImpl_AddRef(IAssemblyCacheItem
*iface
)
483 IAssemblyCacheItemImpl
*This
= impl_from_IAssemblyCacheItem(iface
);
484 ULONG refCount
= InterlockedIncrement(&This
->ref
);
486 TRACE("(%p)->(ref before = %u)\n", This
, refCount
- 1);
491 static ULONG WINAPI
IAssemblyCacheItemImpl_Release(IAssemblyCacheItem
*iface
)
493 IAssemblyCacheItemImpl
*This
= impl_from_IAssemblyCacheItem(iface
);
494 ULONG refCount
= InterlockedDecrement(&This
->ref
);
496 TRACE("(%p)->(ref before = %u)\n", This
, refCount
+ 1);
499 HeapFree(GetProcessHeap(), 0, This
);
504 static HRESULT WINAPI
IAssemblyCacheItemImpl_CreateStream(IAssemblyCacheItem
*iface
,
506 LPCWSTR pszStreamName
,
510 ULARGE_INTEGER
*puliMaxSize
)
512 FIXME("(%p, %d, %s, %d, %d, %p, %p) stub!\n", iface
, dwFlags
,
513 debugstr_w(pszStreamName
), dwFormat
, dwFormatFlags
, ppIStream
, puliMaxSize
);
518 static HRESULT WINAPI
IAssemblyCacheItemImpl_Commit(IAssemblyCacheItem
*iface
,
520 ULONG
*pulDisposition
)
522 FIXME("(%p, %d, %p) stub!\n", iface
, dwFlags
, pulDisposition
);
526 static HRESULT WINAPI
IAssemblyCacheItemImpl_AbortItem(IAssemblyCacheItem
*iface
)
528 FIXME("(%p) stub!\n", iface
);
532 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl
= {
533 IAssemblyCacheItemImpl_QueryInterface
,
534 IAssemblyCacheItemImpl_AddRef
,
535 IAssemblyCacheItemImpl_Release
,
536 IAssemblyCacheItemImpl_CreateStream
,
537 IAssemblyCacheItemImpl_Commit
,
538 IAssemblyCacheItemImpl_AbortItem