netapi32: Improve stubbed NetGetJoinInformation function.
[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 BOOL create_full_path(LPCWSTR path)
45 {
46     LPWSTR new_path;
47     BOOL ret = TRUE;
48     int len;
49
50     new_path = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1) * sizeof(WCHAR));
51     if (!new_path)
52         return FALSE;
53
54     strcpyW(new_path, path);
55
56     while ((len = strlenW(new_path)) && new_path[len - 1] == '\\')
57         new_path[len - 1] = 0;
58
59     while (!CreateDirectoryW(new_path, NULL))
60     {
61         LPWSTR slash;
62         DWORD last_error = GetLastError();
63
64         if(last_error == ERROR_ALREADY_EXISTS)
65             break;
66
67         if(last_error != ERROR_PATH_NOT_FOUND)
68         {
69             ret = FALSE;
70             break;
71         }
72
73         if(!(slash = strrchrW(new_path, '\\')))
74         {
75             ret = FALSE;
76             break;
77         }
78
79         len = slash - new_path;
80         new_path[len] = 0;
81         if(!create_full_path(new_path))
82         {
83             ret = FALSE;
84             break;
85         }
86
87         new_path[len] = '\\';
88     }
89
90     HeapFree(GetProcessHeap(), 0, new_path);
91     return ret;
92 }
93
94 static BOOL get_assembly_directory(LPWSTR dir, DWORD size, BYTE architecture)
95 {
96     static const WCHAR gac[] = {'\\','a','s','s','e','m','b','l','y','\\','G','A','C',0};
97
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};
101
102     GetWindowsDirectoryW(dir, size);
103     strcatW(dir, gac);
104
105     switch (architecture)
106     {
107         case peMSIL:
108             strcatW(dir, msil);
109             break;
110
111         case peI386:
112             strcatW(dir, x86);
113             break;
114
115         case peAMD64:
116             strcatW(dir, amd64);
117             break;
118     }
119
120     return TRUE;
121 }
122
123 /* IAssemblyCache */
124
125 typedef struct {
126     IAssemblyCache IAssemblyCache_iface;
127
128     LONG ref;
129 } IAssemblyCacheImpl;
130
131 static inline IAssemblyCacheImpl *impl_from_IAssemblyCache(IAssemblyCache *iface)
132 {
133     return CONTAINING_RECORD(iface, IAssemblyCacheImpl, IAssemblyCache_iface);
134 }
135
136 static HRESULT WINAPI IAssemblyCacheImpl_QueryInterface(IAssemblyCache *iface,
137                                                         REFIID riid, LPVOID *ppobj)
138 {
139     IAssemblyCacheImpl *This = impl_from_IAssemblyCache(iface);
140
141     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
142
143     *ppobj = NULL;
144
145     if (IsEqualIID(riid, &IID_IUnknown) ||
146         IsEqualIID(riid, &IID_IAssemblyCache))
147     {
148         IUnknown_AddRef(iface);
149         *ppobj = This;
150         return S_OK;
151     }
152
153     WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
154     return E_NOINTERFACE;
155 }
156
157 static ULONG WINAPI IAssemblyCacheImpl_AddRef(IAssemblyCache *iface)
158 {
159     IAssemblyCacheImpl *This = impl_from_IAssemblyCache(iface);
160     ULONG refCount = InterlockedIncrement(&This->ref);
161
162     TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
163
164     return refCount;
165 }
166
167 static ULONG WINAPI IAssemblyCacheImpl_Release(IAssemblyCache *iface)
168 {
169     IAssemblyCacheImpl *This = impl_from_IAssemblyCache(iface);
170     ULONG refCount = InterlockedDecrement(&This->ref);
171
172     TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
173
174     if (!refCount)
175         HeapFree(GetProcessHeap(), 0, This);
176
177     return refCount;
178 }
179
180 static HRESULT WINAPI IAssemblyCacheImpl_UninstallAssembly(IAssemblyCache *iface,
181                                                            DWORD dwFlags,
182                                                            LPCWSTR pszAssemblyName,
183                                                            LPCFUSION_INSTALL_REFERENCE pRefData,
184                                                            ULONG *pulDisposition)
185 {
186     FIXME("(%p, %d, %s, %p, %p) stub!\n", iface, dwFlags,
187           debugstr_w(pszAssemblyName), pRefData, pulDisposition);
188
189     return E_NOTIMPL;
190 }
191
192 static HRESULT WINAPI IAssemblyCacheImpl_QueryAssemblyInfo(IAssemblyCache *iface,
193                                                            DWORD dwFlags,
194                                                            LPCWSTR pszAssemblyName,
195                                                            ASSEMBLY_INFO *pAsmInfo)
196 {
197     IAssemblyName *asmname, *next = NULL;
198     IAssemblyEnum *asmenum = NULL;
199     HRESULT hr;
200
201     TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
202           debugstr_w(pszAssemblyName), pAsmInfo);
203
204     if (pAsmInfo)
205     {
206         if (pAsmInfo->cbAssemblyInfo == 0)
207             pAsmInfo->cbAssemblyInfo = sizeof(ASSEMBLY_INFO);
208         else if (pAsmInfo->cbAssemblyInfo != sizeof(ASSEMBLY_INFO))
209             return E_INVALIDARG;
210     }
211
212     hr = CreateAssemblyNameObject(&asmname, pszAssemblyName,
213                                   CANOF_PARSE_DISPLAY_NAME, NULL);
214     if (FAILED(hr))
215         return hr;
216
217     hr = CreateAssemblyEnum(&asmenum, NULL, asmname, ASM_CACHE_GAC, NULL);
218     if (FAILED(hr))
219         goto done;
220
221     hr = IAssemblyEnum_GetNextAssembly(asmenum, NULL, &next, 0);
222     if (hr == S_FALSE)
223     {
224         hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
225         goto done;
226     }
227
228     if (!pAsmInfo)
229         goto done;
230
231     hr = IAssemblyName_GetPath(next, pAsmInfo->pszCurrentAssemblyPathBuf, &pAsmInfo->cchBuf);
232
233     pAsmInfo->dwAssemblyFlags = ASSEMBLYINFO_FLAG_INSTALLED;
234
235 done:
236     IAssemblyName_Release(asmname);
237     if (next) IAssemblyName_Release(next);
238     if (asmenum) IAssemblyEnum_Release(asmenum);
239
240     return hr;
241 }
242
243 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyCacheItem(IAssemblyCache *iface,
244                                                                  DWORD dwFlags,
245                                                                  PVOID pvReserved,
246                                                                  IAssemblyCacheItem **ppAsmItem,
247                                                                  LPCWSTR pszAssemblyName)
248 {
249     FIXME("(%p, %d, %p, %p, %s) stub!\n", iface, dwFlags, pvReserved,
250           ppAsmItem, debugstr_w(pszAssemblyName));
251
252     return E_NOTIMPL;
253 }
254
255 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyScavenger(IAssemblyCache *iface,
256                                                                  IUnknown **ppUnkReserved)
257 {
258     FIXME("(%p, %p) stub!\n", iface, ppUnkReserved);
259     return E_NOTIMPL;
260 }
261
262 static HRESULT WINAPI IAssemblyCacheImpl_InstallAssembly(IAssemblyCache *iface,
263                                                          DWORD dwFlags,
264                                                          LPCWSTR pszManifestFilePath,
265                                                          LPCFUSION_INSTALL_REFERENCE pRefData)
266 {
267     static const WCHAR format[] =
268         {'%','s','\\','%','s','\\','%','s','_','_','%','s','\\',0};
269
270     ASSEMBLY *assembly;
271     LPWSTR filename;
272     LPWSTR name = NULL;
273     LPWSTR token = NULL;
274     LPWSTR version = NULL;
275     LPWSTR asmpath = NULL;
276     WCHAR path[MAX_PATH];
277     WCHAR asmdir[MAX_PATH];
278     LPWSTR ext;
279     HRESULT hr;
280
281     static const WCHAR ext_exe[] = {'.','e','x','e',0};
282     static const WCHAR ext_dll[] = {'.','d','l','l',0};
283
284     TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
285           debugstr_w(pszManifestFilePath), pRefData);
286
287     if (!pszManifestFilePath || !*pszManifestFilePath)
288         return E_INVALIDARG;
289
290     if (!(ext = strrchrW(pszManifestFilePath, '.')))
291         return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
292
293     if (lstrcmpiW(ext, ext_exe) && lstrcmpiW(ext, ext_dll))
294         return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
295
296     if (GetFileAttributesW(pszManifestFilePath) == INVALID_FILE_ATTRIBUTES)
297         return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
298
299     hr = assembly_create(&assembly, pszManifestFilePath);
300     if (FAILED(hr))
301     {
302         hr = COR_E_ASSEMBLYEXPECTED;
303         goto done;
304     }
305
306     hr = assembly_get_name(assembly, &name);
307     if (FAILED(hr))
308         goto done;
309
310     hr = assembly_get_pubkey_token(assembly, &token);
311     if (FAILED(hr))
312         goto done;
313
314     hr = assembly_get_version(assembly, &version);
315     if (FAILED(hr))
316         goto done;
317
318     get_assembly_directory(asmdir, MAX_PATH, assembly_get_architecture(assembly));
319
320     sprintfW(path, format, asmdir, name, version, token);
321
322     create_full_path(path);
323
324     hr = assembly_get_path(assembly, &asmpath);
325     if (FAILED(hr))
326         goto done;
327
328     filename = PathFindFileNameW(asmpath);
329
330     strcatW(path, filename);
331     if (!CopyFileW(asmpath, path, FALSE))
332         hr = HRESULT_FROM_WIN32(GetLastError());
333
334 done:
335     HeapFree(GetProcessHeap(), 0, name);
336     HeapFree(GetProcessHeap(), 0, token);
337     HeapFree(GetProcessHeap(), 0, version);
338     HeapFree(GetProcessHeap(), 0, asmpath);
339     assembly_release(assembly);
340     return hr;
341 }
342
343 static const IAssemblyCacheVtbl AssemblyCacheVtbl = {
344     IAssemblyCacheImpl_QueryInterface,
345     IAssemblyCacheImpl_AddRef,
346     IAssemblyCacheImpl_Release,
347     IAssemblyCacheImpl_UninstallAssembly,
348     IAssemblyCacheImpl_QueryAssemblyInfo,
349     IAssemblyCacheImpl_CreateAssemblyCacheItem,
350     IAssemblyCacheImpl_CreateAssemblyScavenger,
351     IAssemblyCacheImpl_InstallAssembly
352 };
353
354 /******************************************************************
355  *  CreateAssemblyCache   (FUSION.@)
356  */
357 HRESULT WINAPI CreateAssemblyCache(IAssemblyCache **ppAsmCache, DWORD dwReserved)
358 {
359     IAssemblyCacheImpl *cache;
360
361     TRACE("(%p, %d)\n", ppAsmCache, dwReserved);
362
363     if (!ppAsmCache)
364         return E_INVALIDARG;
365
366     *ppAsmCache = NULL;
367
368     cache = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyCacheImpl));
369     if (!cache)
370         return E_OUTOFMEMORY;
371
372     cache->IAssemblyCache_iface.lpVtbl = &AssemblyCacheVtbl;
373     cache->ref = 1;
374
375     *ppAsmCache = &cache->IAssemblyCache_iface;
376
377     return S_OK;
378 }
379
380 /* IAssemblyCacheItem */
381
382 typedef struct {
383     IAssemblyCacheItem IAssemblyCacheItem_iface;
384
385     LONG ref;
386 } IAssemblyCacheItemImpl;
387
388 static inline IAssemblyCacheItemImpl *impl_from_IAssemblyCacheItem(IAssemblyCacheItem *iface)
389 {
390     return CONTAINING_RECORD(iface, IAssemblyCacheItemImpl, IAssemblyCacheItem_iface);
391 }
392
393 static HRESULT WINAPI IAssemblyCacheItemImpl_QueryInterface(IAssemblyCacheItem *iface,
394                                                             REFIID riid, LPVOID *ppobj)
395 {
396     IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
397
398     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
399
400     *ppobj = NULL;
401
402     if (IsEqualIID(riid, &IID_IUnknown) ||
403         IsEqualIID(riid, &IID_IAssemblyCacheItem))
404     {
405         IUnknown_AddRef(iface);
406         *ppobj = This;
407         return S_OK;
408     }
409
410     WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
411     return E_NOINTERFACE;
412 }
413
414 static ULONG WINAPI IAssemblyCacheItemImpl_AddRef(IAssemblyCacheItem *iface)
415 {
416     IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
417     ULONG refCount = InterlockedIncrement(&This->ref);
418
419     TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
420
421     return refCount;
422 }
423
424 static ULONG WINAPI IAssemblyCacheItemImpl_Release(IAssemblyCacheItem *iface)
425 {
426     IAssemblyCacheItemImpl *This = impl_from_IAssemblyCacheItem(iface);
427     ULONG refCount = InterlockedDecrement(&This->ref);
428
429     TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
430
431     if (!refCount)
432         HeapFree(GetProcessHeap(), 0, This);
433
434     return refCount;
435 }
436
437 static HRESULT WINAPI IAssemblyCacheItemImpl_CreateStream(IAssemblyCacheItem *iface,
438                                                         DWORD dwFlags,
439                                                         LPCWSTR pszStreamName,
440                                                         DWORD dwFormat,
441                                                         DWORD dwFormatFlags,
442                                                         IStream **ppIStream,
443                                                         ULARGE_INTEGER *puliMaxSize)
444 {
445     FIXME("(%p, %d, %s, %d, %d, %p, %p) stub!\n", iface, dwFlags,
446           debugstr_w(pszStreamName), dwFormat, dwFormatFlags, ppIStream, puliMaxSize);
447
448     return E_NOTIMPL;
449 }
450
451 static HRESULT WINAPI IAssemblyCacheItemImpl_Commit(IAssemblyCacheItem *iface,
452                                                   DWORD dwFlags,
453                                                   ULONG *pulDisposition)
454 {
455     FIXME("(%p, %d, %p) stub!\n", iface, dwFlags, pulDisposition);
456     return E_NOTIMPL;
457 }
458
459 static HRESULT WINAPI IAssemblyCacheItemImpl_AbortItem(IAssemblyCacheItem *iface)
460 {
461     FIXME("(%p) stub!\n", iface);
462     return E_NOTIMPL;
463 }
464
465 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl = {
466     IAssemblyCacheItemImpl_QueryInterface,
467     IAssemblyCacheItemImpl_AddRef,
468     IAssemblyCacheItemImpl_Release,
469     IAssemblyCacheItemImpl_CreateStream,
470     IAssemblyCacheItemImpl_Commit,
471     IAssemblyCacheItemImpl_AbortItem
472 };