fusion: Add initial implementation of IAssemblyCache::QueryAssemblyInfo.
[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(LPCSTR path)
45 {
46     LPSTR new_path;
47     BOOL ret = TRUE;
48     int len;
49
50     new_path = HeapAlloc(GetProcessHeap(), 0, lstrlenA(path) + 1);
51     if (!new_path)
52         return FALSE;
53
54     lstrcpyA(new_path, path);
55
56     while ((len = lstrlenA(new_path)) && new_path[len - 1] == '\\')
57         new_path[len - 1] = 0;
58
59     while (!CreateDirectoryA(new_path, NULL))
60     {
61         LPSTR 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 = strrchr(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 /* IAssemblyCache */
95
96 typedef struct {
97     const IAssemblyCacheVtbl *lpIAssemblyCacheVtbl;
98
99     LONG ref;
100 } IAssemblyCacheImpl;
101
102 static HRESULT WINAPI IAssemblyCacheImpl_QueryInterface(IAssemblyCache *iface,
103                                                         REFIID riid, LPVOID *ppobj)
104 {
105     IAssemblyCacheImpl *This = (IAssemblyCacheImpl *)iface;
106
107     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
108
109     *ppobj = NULL;
110
111     if (IsEqualIID(riid, &IID_IUnknown) ||
112         IsEqualIID(riid, &IID_IAssemblyCache))
113     {
114         IUnknown_AddRef(iface);
115         *ppobj = This;
116         return S_OK;
117     }
118
119     WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
120     return E_NOINTERFACE;
121 }
122
123 static ULONG WINAPI IAssemblyCacheImpl_AddRef(IAssemblyCache *iface)
124 {
125     IAssemblyCacheImpl *This = (IAssemblyCacheImpl *)iface;
126     ULONG refCount = InterlockedIncrement(&This->ref);
127
128     TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
129
130     return refCount;
131 }
132
133 static ULONG WINAPI IAssemblyCacheImpl_Release(IAssemblyCache *iface)
134 {
135     IAssemblyCacheImpl *This = (IAssemblyCacheImpl *)iface;
136     ULONG refCount = InterlockedDecrement(&This->ref);
137
138     TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
139
140     if (!refCount)
141         HeapFree(GetProcessHeap(), 0, This);
142
143     return refCount;
144 }
145
146 static HRESULT WINAPI IAssemblyCacheImpl_UninstallAssembly(IAssemblyCache *iface,
147                                                            DWORD dwFlags,
148                                                            LPCWSTR pszAssemblyName,
149                                                            LPCFUSION_INSTALL_REFERENCE pRefData,
150                                                            ULONG *pulDisposition)
151 {
152     FIXME("(%p, %d, %s, %p, %p) stub!\n", iface, dwFlags,
153           debugstr_w(pszAssemblyName), pRefData, pulDisposition);
154
155     return E_NOTIMPL;
156 }
157
158 static HRESULT WINAPI IAssemblyCacheImpl_QueryAssemblyInfo(IAssemblyCache *iface,
159                                                            DWORD dwFlags,
160                                                            LPCWSTR pszAssemblyName,
161                                                            ASSEMBLY_INFO *pAsmInfo)
162 {
163     IAssemblyName *asmname, *next = NULL;
164     IAssemblyEnum *asmenum = NULL;
165     HRESULT hr;
166
167     TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
168           debugstr_w(pszAssemblyName), pAsmInfo);
169
170     if (pAsmInfo)
171     {
172         if (pAsmInfo->cbAssemblyInfo == 0)
173             pAsmInfo->cbAssemblyInfo = sizeof(ASSEMBLY_INFO);
174         else if (pAsmInfo->cbAssemblyInfo != sizeof(ASSEMBLY_INFO))
175             return E_INVALIDARG;
176     }
177
178     hr = CreateAssemblyNameObject(&asmname, pszAssemblyName,
179                                   CANOF_PARSE_DISPLAY_NAME, NULL);
180     if (FAILED(hr))
181         return hr;
182
183     hr = CreateAssemblyEnum(&asmenum, NULL, asmname, ASM_CACHE_GAC, NULL);
184     if (FAILED(hr))
185         goto done;
186
187     hr = IAssemblyEnum_GetNextAssembly(asmenum, NULL, &next, 0);
188     if (hr == S_FALSE)
189     {
190         hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
191         goto done;
192     }
193
194     if (!pAsmInfo)
195         goto done;
196
197     pAsmInfo->dwAssemblyFlags = ASSEMBLYINFO_FLAG_INSTALLED;
198
199 done:
200     IAssemblyName_Release(asmname);
201     if (next) IAssemblyName_Release(next);
202     if (asmenum) IAssemblyEnum_Release(asmenum);
203
204     return hr;
205 }
206
207 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyCacheItem(IAssemblyCache *iface,
208                                                                  DWORD dwFlags,
209                                                                  PVOID pvReserved,
210                                                                  IAssemblyCacheItem **ppAsmItem,
211                                                                  LPCWSTR pszAssemblyName)
212 {
213     FIXME("(%p, %d, %p, %p, %s) stub!\n", iface, dwFlags, pvReserved,
214           ppAsmItem, debugstr_w(pszAssemblyName));
215
216     return E_NOTIMPL;
217 }
218
219 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyScavenger(IAssemblyCache *iface,
220                                                                  IUnknown **ppUnkReserved)
221 {
222     FIXME("(%p, %p) stub!\n", iface, ppUnkReserved);
223     return E_NOTIMPL;
224 }
225
226 static HRESULT WINAPI IAssemblyCacheImpl_InstallAssembly(IAssemblyCache *iface,
227                                                          DWORD dwFlags,
228                                                          LPCWSTR pszManifestFilePath,
229                                                          LPCFUSION_INSTALL_REFERENCE pRefData)
230 {
231     ASSEMBLY *assembly;
232     LPSTR filename;
233     LPSTR name = NULL;
234     LPSTR token = NULL;
235     LPSTR version = NULL;
236     LPSTR asmpath = NULL;
237     CHAR path[MAX_PATH];
238     CHAR windir[MAX_PATH];
239     LPWSTR ext;
240     HRESULT hr;
241
242     static const WCHAR ext_exe[] = {'.','e','x','e',0};
243     static const WCHAR ext_dll[] = {'.','d','l','l',0};
244
245     TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
246           debugstr_w(pszManifestFilePath), pRefData);
247
248     if (!pszManifestFilePath || !*pszManifestFilePath)
249         return E_INVALIDARG;
250
251     if (!(ext = strrchrW(pszManifestFilePath, '.')))
252         return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
253
254     if (lstrcmpiW(ext, ext_exe) && lstrcmpiW(ext, ext_dll))
255         return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
256
257     if (GetFileAttributesW(pszManifestFilePath) == INVALID_FILE_ATTRIBUTES)
258         return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
259
260     hr = assembly_create(&assembly, pszManifestFilePath);
261     if (FAILED(hr))
262     {
263         hr = COR_E_ASSEMBLYEXPECTED;
264         goto done;
265     }
266
267     hr = assembly_get_name(assembly, &name);
268     if (FAILED(hr))
269         goto done;
270
271     hr = assembly_get_pubkey_token(assembly, &token);
272     if (FAILED(hr))
273         goto done;
274
275     hr = assembly_get_version(assembly, &version);
276     if (FAILED(hr))
277         goto done;
278
279     GetWindowsDirectoryA(windir, MAX_PATH);
280
281     FIXME("Ignoring assembly architecture!\n");
282
283     sprintf(path, "%s\\assembly\\GAC_MSIL\\%s\\%s__%s\\", windir, name,
284             version, token);
285
286     create_full_path(path);
287
288     hr = assembly_get_path(assembly, &asmpath);
289     if (FAILED(hr))
290         goto done;
291
292     filename = PathFindFileNameA(asmpath);
293
294     lstrcatA(path, filename);
295     if (!CopyFileA(asmpath, path, FALSE))
296         hr = HRESULT_FROM_WIN32(GetLastError());
297
298 done:
299     HeapFree(GetProcessHeap(), 0, name);
300     HeapFree(GetProcessHeap(), 0, token);
301     HeapFree(GetProcessHeap(), 0, version);
302     HeapFree(GetProcessHeap(), 0, asmpath);
303     assembly_release(assembly);
304     return hr;
305 }
306
307 static const IAssemblyCacheVtbl AssemblyCacheVtbl = {
308     IAssemblyCacheImpl_QueryInterface,
309     IAssemblyCacheImpl_AddRef,
310     IAssemblyCacheImpl_Release,
311     IAssemblyCacheImpl_UninstallAssembly,
312     IAssemblyCacheImpl_QueryAssemblyInfo,
313     IAssemblyCacheImpl_CreateAssemblyCacheItem,
314     IAssemblyCacheImpl_CreateAssemblyScavenger,
315     IAssemblyCacheImpl_InstallAssembly
316 };
317
318 /******************************************************************
319  *  CreateAssemblyCache   (FUSION.@)
320  */
321 HRESULT WINAPI CreateAssemblyCache(IAssemblyCache **ppAsmCache, DWORD dwReserved)
322 {
323     IAssemblyCacheImpl *cache;
324
325     TRACE("(%p, %d)\n", ppAsmCache, dwReserved);
326
327     if (!ppAsmCache)
328         return E_INVALIDARG;
329
330     *ppAsmCache = NULL;
331
332     cache = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyCacheImpl));
333     if (!cache)
334         return E_OUTOFMEMORY;
335
336     cache->lpIAssemblyCacheVtbl = &AssemblyCacheVtbl;
337     cache->ref = 1;
338
339     *ppAsmCache = (IAssemblyCache *)cache;
340
341     return S_OK;
342 }
343
344 /* IAssemblyCacheItem */
345
346 typedef struct {
347     const IAssemblyCacheItemVtbl *lpIAssemblyCacheItemVtbl;
348
349     LONG ref;
350 } IAssemblyCacheItemImpl;
351
352 static HRESULT WINAPI IAssemblyCacheItemImpl_QueryInterface(IAssemblyCacheItem *iface,
353                                                             REFIID riid, LPVOID *ppobj)
354 {
355     IAssemblyCacheItemImpl *This = (IAssemblyCacheItemImpl *)iface;
356
357     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
358
359     *ppobj = NULL;
360
361     if (IsEqualIID(riid, &IID_IUnknown) ||
362         IsEqualIID(riid, &IID_IAssemblyCacheItem))
363     {
364         IUnknown_AddRef(iface);
365         *ppobj = This;
366         return S_OK;
367     }
368
369     WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
370     return E_NOINTERFACE;
371 }
372
373 static ULONG WINAPI IAssemblyCacheItemImpl_AddRef(IAssemblyCacheItem *iface)
374 {
375     IAssemblyCacheItemImpl *This = (IAssemblyCacheItemImpl *)iface;
376     ULONG refCount = InterlockedIncrement(&This->ref);
377
378     TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
379
380     return refCount;
381 }
382
383 static ULONG WINAPI IAssemblyCacheItemImpl_Release(IAssemblyCacheItem *iface)
384 {
385     IAssemblyCacheItemImpl *This = (IAssemblyCacheItemImpl *)iface;
386     ULONG refCount = InterlockedDecrement(&This->ref);
387
388     TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
389
390     if (!refCount)
391         HeapFree(GetProcessHeap(), 0, This);
392
393     return refCount;
394 }
395
396 static HRESULT WINAPI IAssemblyCacheItemImpl_CreateStream(IAssemblyCacheItem *iface,
397                                                         DWORD dwFlags,
398                                                         LPCWSTR pszStreamName,
399                                                         DWORD dwFormat,
400                                                         DWORD dwFormatFlags,
401                                                         IStream **ppIStream,
402                                                         ULARGE_INTEGER *puliMaxSize)
403 {
404     FIXME("(%p, %d, %s, %d, %d, %p, %p) stub!\n", iface, dwFlags,
405           debugstr_w(pszStreamName), dwFormat, dwFormatFlags, ppIStream, puliMaxSize);
406
407     return E_NOTIMPL;
408 }
409
410 static HRESULT WINAPI IAssemblyCacheItemImpl_Commit(IAssemblyCacheItem *iface,
411                                                   DWORD dwFlags,
412                                                   ULONG *pulDisposition)
413 {
414     FIXME("(%p, %d, %p) stub!\n", iface, dwFlags, pulDisposition);
415     return E_NOTIMPL;
416 }
417
418 static HRESULT WINAPI IAssemblyCacheItemImpl_AbortItem(IAssemblyCacheItem *iface)
419 {
420     FIXME("(%p) stub!\n", iface);
421     return E_NOTIMPL;
422 }
423
424 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl = {
425     IAssemblyCacheItemImpl_QueryInterface,
426     IAssemblyCacheItemImpl_AddRef,
427     IAssemblyCacheItemImpl_Release,
428     IAssemblyCacheItemImpl_CreateStream,
429     IAssemblyCacheItemImpl_Commit,
430     IAssemblyCacheItemImpl_AbortItem
431 };