Assorted typo, spelling, wording and case fixes.
[wine] / dlls / fusion / asmcache.c
1 /*
2  * IAssemblyCache implementation
3  *
4  * Copyright 2008 James Hawkins
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winver.h"
30 #include "wincrypt.h"
31 #include "winreg.h"
32 #include "shlwapi.h"
33 #include "dbghelp.h"
34 #include "ole2.h"
35 #include "fusion.h"
36 #include "corerror.h"
37
38 #include "fusionpriv.h"
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(fusion);
43
44 static const WCHAR cache_mutex_nameW[] =
45     {'_','_','W','I','N','E','_','F','U','S','I','O','N','_','C','A','C','H','E','_','M','U','T','E','X','_','_',0};
46
47 static BOOL create_full_path(LPCWSTR path)
48 {
49     LPWSTR new_path;
50     BOOL ret = TRUE;
51     int len;
52
53     new_path = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1) * sizeof(WCHAR));
54     if (!new_path)
55         return FALSE;
56
57     strcpyW(new_path, path);
58
59     while ((len = strlenW(new_path)) && new_path[len - 1] == '\\')
60         new_path[len - 1] = 0;
61
62     while (!CreateDirectoryW(new_path, NULL))
63     {
64         LPWSTR slash;
65         DWORD last_error = GetLastError();
66
67         if(last_error == ERROR_ALREADY_EXISTS)
68             break;
69
70         if(last_error != ERROR_PATH_NOT_FOUND)
71         {
72             ret = FALSE;
73             break;
74         }
75
76         if(!(slash = strrchrW(new_path, '\\')))
77         {
78             ret = FALSE;
79             break;
80         }
81
82         len = slash - new_path;
83         new_path[len] = 0;
84         if(!create_full_path(new_path))
85         {
86             ret = FALSE;
87             break;
88         }
89
90         new_path[len] = '\\';
91     }
92
93     HeapFree(GetProcessHeap(), 0, new_path);
94     return ret;
95 }
96
97 static BOOL get_assembly_directory(LPWSTR dir, DWORD size, BYTE architecture)
98 {
99     static const WCHAR gac[] = {'\\','a','s','s','e','m','b','l','y','\\','G','A','C',0};
100
101     static const WCHAR msil[] = {'_','M','S','I','L',0};
102     static const WCHAR x86[] = {'_','3','2',0};
103     static const WCHAR amd64[] = {'_','6','4',0};
104
105     GetWindowsDirectoryW(dir, size);
106     strcatW(dir, gac);
107
108     switch (architecture)
109     {
110         case peMSIL:
111             strcatW(dir, msil);
112             break;
113
114         case peI386:
115             strcatW(dir, x86);
116             break;
117
118         case peAMD64:
119             strcatW(dir, amd64);
120             break;
121     }
122
123     return TRUE;
124 }
125
126 /* IAssemblyCache */
127
128 typedef struct {
129     IAssemblyCache IAssemblyCache_iface;
130
131     LONG ref;
132     HANDLE lock;
133 } IAssemblyCacheImpl;
134
135 static inline IAssemblyCacheImpl *impl_from_IAssemblyCache(IAssemblyCache *iface)
136 {
137     return CONTAINING_RECORD(iface, IAssemblyCacheImpl, IAssemblyCache_iface);
138 }
139
140 static HRESULT WINAPI IAssemblyCacheImpl_QueryInterface(IAssemblyCache *iface,
141                                                         REFIID riid, LPVOID *ppobj)
142 {
143     IAssemblyCacheImpl *This = impl_from_IAssemblyCache(iface);
144
145     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
146
147     *ppobj = NULL;
148
149     if (IsEqualIID(riid, &IID_IUnknown) ||
150         IsEqualIID(riid, &IID_IAssemblyCache))
151     {
152         IUnknown_AddRef(iface);
153         *ppobj = This;
154         return S_OK;
155     }
156
157     WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
158     return E_NOINTERFACE;
159 }
160
161 static ULONG WINAPI IAssemblyCacheImpl_AddRef(IAssemblyCache *iface)
162 {
163     IAssemblyCacheImpl *This = impl_from_IAssemblyCache(iface);
164     ULONG refCount = InterlockedIncrement(&This->ref);
165
166     TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
167
168     return refCount;
169 }
170
171 static ULONG WINAPI IAssemblyCacheImpl_Release(IAssemblyCache *iface)
172 {
173     IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
174     ULONG refCount = InterlockedDecrement( &cache->ref );
175
176     TRACE("(%p)->(ref before = %u)\n", cache, refCount + 1);
177
178     if (!refCount)
179     {
180         CloseHandle( cache->lock );
181         HeapFree( GetProcessHeap(), 0, cache );
182     }
183     return refCount;
184 }
185
186 static void cache_lock( IAssemblyCacheImpl *cache )
187 {
188     WaitForSingleObject( cache->lock, INFINITE );
189 }
190
191 static void cache_unlock( IAssemblyCacheImpl *cache )
192 {
193     ReleaseMutex( cache->lock );
194 }
195
196 static HRESULT WINAPI IAssemblyCacheImpl_UninstallAssembly(IAssemblyCache *iface,
197                                                            DWORD dwFlags,
198                                                            LPCWSTR pszAssemblyName,
199                                                            LPCFUSION_INSTALL_REFERENCE pRefData,
200                                                            ULONG *pulDisposition)
201 {
202     HRESULT hr;
203     IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
204     IAssemblyName *asmname, *next = NULL;
205     IAssemblyEnum *asmenum = NULL;
206     WCHAR *p, *path = NULL;
207     ULONG disp;
208     DWORD len;
209
210     TRACE("(%p, 0%08x, %s, %p, %p)\n", iface, dwFlags,
211           debugstr_w(pszAssemblyName), pRefData, pulDisposition);
212
213     if (pRefData)
214     {
215         FIXME("application reference not supported\n");
216         return E_NOTIMPL;
217     }
218     hr = CreateAssemblyNameObject( &asmname, pszAssemblyName, CANOF_PARSE_DISPLAY_NAME, NULL );
219     if (FAILED( hr ))
220         return hr;
221
222     cache_lock( cache );
223
224     hr = CreateAssemblyEnum( &asmenum, NULL, asmname, ASM_CACHE_GAC, NULL );
225     if (FAILED( hr ))
226         goto done;
227
228     hr = IAssemblyEnum_GetNextAssembly( asmenum, NULL, &next, 0 );
229     if (hr == S_FALSE)
230     {
231         if (pulDisposition)
232             *pulDisposition = IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED;
233         goto done;
234     }
235     hr = IAssemblyName_GetPath( next, NULL, &len );
236     if (hr != HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER ))
237         goto done;
238
239     if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
240     {
241         hr = E_OUTOFMEMORY;
242         goto done;
243     }
244     hr = IAssemblyName_GetPath( next, path, &len );
245     if (FAILED( hr ))
246         goto done;
247
248     if (DeleteFileW( path ))
249     {
250         if ((p = strrchrW( path, '\\' )))
251         {
252             *p = 0;
253             RemoveDirectoryW( path );
254             if ((p = strrchrW( path, '\\' )))
255             {
256                 *p = 0;
257                 RemoveDirectoryW( path );
258             }
259         }
260         disp = IASSEMBLYCACHE_UNINSTALL_DISPOSITION_UNINSTALLED;
261         hr = S_OK;
262     }
263     else
264     {
265         disp = IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED;
266         hr = S_FALSE;
267     }
268     if (pulDisposition) *pulDisposition = disp;
269
270 done:
271     IAssemblyName_Release( asmname );
272     if (next) IAssemblyName_Release( next );
273     if (asmenum) IAssemblyEnum_Release( asmenum );
274     HeapFree( GetProcessHeap(), 0, path );
275     cache_unlock( cache );
276     return hr;
277 }
278
279 static HRESULT WINAPI IAssemblyCacheImpl_QueryAssemblyInfo(IAssemblyCache *iface,
280                                                            DWORD dwFlags,
281                                                            LPCWSTR pszAssemblyName,
282                                                            ASSEMBLY_INFO *pAsmInfo)
283 {
284     IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
285     IAssemblyName *asmname, *next = NULL;
286     IAssemblyEnum *asmenum = NULL;
287     HRESULT hr;
288
289     TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
290           debugstr_w(pszAssemblyName), pAsmInfo);
291
292     if (pAsmInfo)
293     {
294         if (pAsmInfo->cbAssemblyInfo == 0)
295             pAsmInfo->cbAssemblyInfo = sizeof(ASSEMBLY_INFO);
296         else if (pAsmInfo->cbAssemblyInfo != sizeof(ASSEMBLY_INFO))
297             return E_INVALIDARG;
298     }
299
300     hr = CreateAssemblyNameObject(&asmname, pszAssemblyName,
301                                   CANOF_PARSE_DISPLAY_NAME, NULL);
302     if (FAILED(hr))
303         return hr;
304
305     cache_lock( cache );
306
307     hr = CreateAssemblyEnum(&asmenum, NULL, asmname, ASM_CACHE_GAC, NULL);
308     if (FAILED(hr))
309         goto done;
310
311     hr = IAssemblyEnum_GetNextAssembly(asmenum, NULL, &next, 0);
312     if (hr == S_FALSE)
313     {
314         hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
315         goto done;
316     }
317
318     if (!pAsmInfo)
319         goto done;
320
321     hr = IAssemblyName_GetPath(next, pAsmInfo->pszCurrentAssemblyPathBuf, &pAsmInfo->cchBuf);
322
323     pAsmInfo->dwAssemblyFlags = ASSEMBLYINFO_FLAG_INSTALLED;
324
325 done:
326     IAssemblyName_Release(asmname);
327     if (next) IAssemblyName_Release(next);
328     if (asmenum) IAssemblyEnum_Release(asmenum);
329     cache_unlock( cache );
330     return hr;
331 }
332
333 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyCacheItem(IAssemblyCache *iface,
334                                                                  DWORD dwFlags,
335                                                                  PVOID pvReserved,
336                                                                  IAssemblyCacheItem **ppAsmItem,
337                                                                  LPCWSTR pszAssemblyName)
338 {
339     FIXME("(%p, %d, %p, %p, %s) stub!\n", iface, dwFlags, pvReserved,
340           ppAsmItem, debugstr_w(pszAssemblyName));
341
342     return E_NOTIMPL;
343 }
344
345 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyScavenger(IAssemblyCache *iface,
346                                                                  IUnknown **ppUnkReserved)
347 {
348     FIXME("(%p, %p) stub!\n", iface, ppUnkReserved);
349     return E_NOTIMPL;
350 }
351
352 static HRESULT WINAPI IAssemblyCacheImpl_InstallAssembly(IAssemblyCache *iface,
353                                                          DWORD dwFlags,
354                                                          LPCWSTR pszManifestFilePath,
355                                                          LPCFUSION_INSTALL_REFERENCE pRefData)
356 {
357     static const WCHAR format[] = {'%','s','\\','%','s','\\','%','s','_','_','%','s','\\',0};
358     static const WCHAR ext_exe[] = {'.','e','x','e',0};
359     static const WCHAR ext_dll[] = {'.','d','l','l',0};
360     IAssemblyCacheImpl *cache = impl_from_IAssemblyCache(iface);
361     ASSEMBLY *assembly;
362     LPWSTR filename;
363     LPWSTR name = NULL;
364     LPWSTR token = NULL;
365     LPWSTR version = NULL;
366     LPWSTR asmpath = NULL;
367     WCHAR path[MAX_PATH];
368     WCHAR asmdir[MAX_PATH];
369     LPWSTR ext;
370     HRESULT hr;
371
372     TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
373           debugstr_w(pszManifestFilePath), pRefData);
374
375     if (!pszManifestFilePath || !*pszManifestFilePath)
376         return E_INVALIDARG;
377
378     if (!(ext = strrchrW(pszManifestFilePath, '.')))
379         return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
380
381     if (lstrcmpiW(ext, ext_exe) && lstrcmpiW(ext, ext_dll))
382         return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
383
384     if (GetFileAttributesW(pszManifestFilePath) == INVALID_FILE_ATTRIBUTES)
385         return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
386
387     hr = assembly_create(&assembly, pszManifestFilePath);
388     if (FAILED(hr))
389     {
390         hr = COR_E_ASSEMBLYEXPECTED;
391         goto done;
392     }
393
394     hr = assembly_get_name(assembly, &name);
395     if (FAILED(hr))
396         goto done;
397
398     hr = assembly_get_pubkey_token(assembly, &token);
399     if (FAILED(hr))
400         goto done;
401
402     hr = assembly_get_version(assembly, &version);
403     if (FAILED(hr))
404         goto done;
405
406     cache_lock( cache );
407
408     get_assembly_directory(asmdir, MAX_PATH, assembly_get_architecture(assembly));
409
410     sprintfW(path, format, asmdir, name, version, token);
411
412     create_full_path(path);
413
414     hr = assembly_get_path(assembly, &asmpath);
415     if (FAILED(hr))
416         goto done;
417
418     filename = PathFindFileNameW(asmpath);
419
420     strcatW(path, filename);
421     if (!CopyFileW(asmpath, path, FALSE))
422         hr = HRESULT_FROM_WIN32(GetLastError());
423
424 done:
425     HeapFree(GetProcessHeap(), 0, name);
426     HeapFree(GetProcessHeap(), 0, token);
427     HeapFree(GetProcessHeap(), 0, version);
428     HeapFree(GetProcessHeap(), 0, asmpath);
429     assembly_release(assembly);
430     cache_unlock( cache );
431     return hr;
432 }
433
434 static const IAssemblyCacheVtbl AssemblyCacheVtbl = {
435     IAssemblyCacheImpl_QueryInterface,
436     IAssemblyCacheImpl_AddRef,
437     IAssemblyCacheImpl_Release,
438     IAssemblyCacheImpl_UninstallAssembly,
439     IAssemblyCacheImpl_QueryAssemblyInfo,
440     IAssemblyCacheImpl_CreateAssemblyCacheItem,
441     IAssemblyCacheImpl_CreateAssemblyScavenger,
442     IAssemblyCacheImpl_InstallAssembly
443 };
444
445 /******************************************************************
446  *  CreateAssemblyCache   (FUSION.@)
447  */
448 HRESULT WINAPI CreateAssemblyCache(IAssemblyCache **ppAsmCache, DWORD dwReserved)
449 {
450     IAssemblyCacheImpl *cache;
451
452     TRACE("(%p, %d)\n", ppAsmCache, dwReserved);
453
454     if (!ppAsmCache)
455         return E_INVALIDARG;
456
457     *ppAsmCache = NULL;
458
459     cache = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyCacheImpl));
460     if (!cache)
461         return E_OUTOFMEMORY;
462
463     cache->IAssemblyCache_iface.lpVtbl = &AssemblyCacheVtbl;
464     cache->ref = 1;
465     cache->lock = CreateMutexW( NULL, FALSE, cache_mutex_nameW );
466     if (!cache->lock)
467     {
468         HeapFree( GetProcessHeap(), 0, cache );
469         return HRESULT_FROM_WIN32( GetLastError() );
470     }
471     *ppAsmCache = &cache->IAssemblyCache_iface;
472     return S_OK;
473 }
474
475 /* IAssemblyCacheItem */
476
477 typedef struct {
478     IAssemblyCacheItem IAssemblyCacheItem_iface;
479
480     LONG ref;
481 } IAssemblyCacheItemImpl;
482
483 static inline IAssemblyCacheItemImpl *impl_from_IAssemblyCacheItem(IAssemblyCacheItem *iface)
484 {
485     return CONTAINING_RECORD(iface, IAssemblyCacheItemImpl, IAssemblyCacheItem_iface);
486 }
487
488 static HRESULT WINAPI IAssemblyCacheItemImpl_QueryInterface(IAssemblyCacheItem *iface,
489                                                             REFIID riid, LPVOID *ppobj)
490 {
491     IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
492
493     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
494
495     *ppobj = NULL;
496
497     if (IsEqualIID(riid, &IID_IUnknown) ||
498         IsEqualIID(riid, &IID_IAssemblyCacheItem))
499     {
500         IUnknown_AddRef(iface);
501         *ppobj = This;
502         return S_OK;
503     }
504
505     WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
506     return E_NOINTERFACE;
507 }
508
509 static ULONG WINAPI IAssemblyCacheItemImpl_AddRef(IAssemblyCacheItem *iface)
510 {
511     IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
512     ULONG refCount = InterlockedIncrement(&This->ref);
513
514     TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
515
516     return refCount;
517 }
518
519 static ULONG WINAPI IAssemblyCacheItemImpl_Release(IAssemblyCacheItem *iface)
520 {
521     IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
522     ULONG refCount = InterlockedDecrement(&This->ref);
523
524     TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
525
526     if (!refCount)
527         HeapFree(GetProcessHeap(), 0, This);
528
529     return refCount;
530 }
531
532 static HRESULT WINAPI IAssemblyCacheItemImpl_CreateStream(IAssemblyCacheItem *iface,
533                                                         DWORD dwFlags,
534                                                         LPCWSTR pszStreamName,
535                                                         DWORD dwFormat,
536                                                         DWORD dwFormatFlags,
537                                                         IStream **ppIStream,
538                                                         ULARGE_INTEGER *puliMaxSize)
539 {
540     FIXME("(%p, %d, %s, %d, %d, %p, %p) stub!\n", iface, dwFlags,
541           debugstr_w(pszStreamName), dwFormat, dwFormatFlags, ppIStream, puliMaxSize);
542
543     return E_NOTIMPL;
544 }
545
546 static HRESULT WINAPI IAssemblyCacheItemImpl_Commit(IAssemblyCacheItem *iface,
547                                                   DWORD dwFlags,
548                                                   ULONG *pulDisposition)
549 {
550     FIXME("(%p, %d, %p) stub!\n", iface, dwFlags, pulDisposition);
551     return E_NOTIMPL;
552 }
553
554 static HRESULT WINAPI IAssemblyCacheItemImpl_AbortItem(IAssemblyCacheItem *iface)
555 {
556     FIXME("(%p) stub!\n", iface);
557     return E_NOTIMPL;
558 }
559
560 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl = {
561     IAssemblyCacheItemImpl_QueryInterface,
562     IAssemblyCacheItemImpl_AddRef,
563     IAssemblyCacheItemImpl_Release,
564     IAssemblyCacheItemImpl_CreateStream,
565     IAssemblyCacheItemImpl_Commit,
566     IAssemblyCacheItemImpl_AbortItem
567 };