d3d10: Create an importlib.
[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     FIXME("(%p, %d, %s, %p) stub!\n", iface, dwFlags,
164           debugstr_w(pszAssemblyName), pAsmInfo);
165
166     return E_NOTIMPL;
167 }
168
169 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyCacheItem(IAssemblyCache *iface,
170                                                                  DWORD dwFlags,
171                                                                  PVOID pvReserved,
172                                                                  IAssemblyCacheItem **ppAsmItem,
173                                                                  LPCWSTR pszAssemblyName)
174 {
175     FIXME("(%p, %d, %p, %p, %s) stub!\n", iface, dwFlags, pvReserved,
176           ppAsmItem, debugstr_w(pszAssemblyName));
177
178     return E_NOTIMPL;
179 }
180
181 static HRESULT WINAPI IAssemblyCacheImpl_CreateAssemblyScavenger(IAssemblyCache *iface,
182                                                                  IUnknown **ppUnkReserved)
183 {
184     FIXME("(%p, %p) stub!\n", iface, ppUnkReserved);
185     return E_NOTIMPL;
186 }
187
188 static HRESULT WINAPI IAssemblyCacheImpl_InstallAssembly(IAssemblyCache *iface,
189                                                          DWORD dwFlags,
190                                                          LPCWSTR pszManifestFilePath,
191                                                          LPCFUSION_INSTALL_REFERENCE pRefData)
192 {
193     ASSEMBLY *assembly;
194     LPSTR filename;
195     LPSTR name = NULL;
196     LPSTR token = NULL;
197     LPSTR version = NULL;
198     LPSTR asmpath = NULL;
199     CHAR path[MAX_PATH];
200     CHAR windir[MAX_PATH];
201     LPWSTR ext;
202     HRESULT hr;
203
204     static const WCHAR ext_exe[] = {'.','e','x','e',0};
205     static const WCHAR ext_dll[] = {'.','d','l','l',0};
206
207     TRACE("(%p, %d, %s, %p)\n", iface, dwFlags,
208           debugstr_w(pszManifestFilePath), pRefData);
209
210     if (!pszManifestFilePath || !*pszManifestFilePath)
211         return E_INVALIDARG;
212
213     if (!(ext = strrchrW(pszManifestFilePath, '.')))
214         return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
215
216     if (lstrcmpiW(ext, ext_exe) && lstrcmpiW(ext, ext_dll))
217         return HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
218
219     if (GetFileAttributesW(pszManifestFilePath) == INVALID_FILE_ATTRIBUTES)
220         return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
221
222     hr = assembly_create(&assembly, pszManifestFilePath);
223     if (FAILED(hr))
224     {
225         hr = COR_E_ASSEMBLYEXPECTED;
226         goto done;
227     }
228
229     hr = assembly_get_name(assembly, &name);
230     if (FAILED(hr))
231         goto done;
232
233     hr = assembly_get_pubkey_token(assembly, &token);
234     if (FAILED(hr))
235         goto done;
236
237     hr = assembly_get_version(assembly, &version);
238     if (FAILED(hr))
239         goto done;
240
241     GetWindowsDirectoryA(windir, MAX_PATH);
242
243     FIXME("Ignoring assembly architecture!\n");
244
245     sprintf(path, "%s\\assembly\\GAC_MSIL\\%s\\%s__%s\\", windir, name,
246             version, token);
247
248     create_full_path(path);
249
250     hr = assembly_get_path(assembly, &asmpath);
251     if (FAILED(hr))
252         goto done;
253
254     filename = PathFindFileNameA(asmpath);
255
256     lstrcatA(path, filename);
257     if (!CopyFileA(asmpath, path, FALSE))
258         hr = HRESULT_FROM_WIN32(GetLastError());
259
260 done:
261     HeapFree(GetProcessHeap(), 0, name);
262     HeapFree(GetProcessHeap(), 0, token);
263     HeapFree(GetProcessHeap(), 0, version);
264     HeapFree(GetProcessHeap(), 0, asmpath);
265     assembly_release(assembly);
266     return hr;
267 }
268
269 static const IAssemblyCacheVtbl AssemblyCacheVtbl = {
270     IAssemblyCacheImpl_QueryInterface,
271     IAssemblyCacheImpl_AddRef,
272     IAssemblyCacheImpl_Release,
273     IAssemblyCacheImpl_UninstallAssembly,
274     IAssemblyCacheImpl_QueryAssemblyInfo,
275     IAssemblyCacheImpl_CreateAssemblyCacheItem,
276     IAssemblyCacheImpl_CreateAssemblyScavenger,
277     IAssemblyCacheImpl_InstallAssembly
278 };
279
280 /******************************************************************
281  *  CreateAssemblyCache   (FUSION.@)
282  */
283 HRESULT WINAPI CreateAssemblyCache(IAssemblyCache **ppAsmCache, DWORD dwReserved)
284 {
285     IAssemblyCacheImpl *cache;
286
287     TRACE("(%p, %d)\n", ppAsmCache, dwReserved);
288
289     if (!ppAsmCache)
290         return E_INVALIDARG;
291
292     *ppAsmCache = NULL;
293
294     cache = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyCacheImpl));
295     if (!cache)
296         return E_OUTOFMEMORY;
297
298     cache->lpIAssemblyCacheVtbl = &AssemblyCacheVtbl;
299     cache->ref = 1;
300
301     *ppAsmCache = (IAssemblyCache *)cache;
302
303     return S_OK;
304 }
305
306 /* IAssemblyCacheItem */
307
308 typedef struct {
309     const IAssemblyCacheItemVtbl *lpIAssemblyCacheItemVtbl;
310
311     LONG ref;
312 } IAssemblyCacheItemImpl;
313
314 static HRESULT WINAPI IAssemblyCacheItemImpl_QueryInterface(IAssemblyCacheItem *iface,
315                                                             REFIID riid, LPVOID *ppobj)
316 {
317     IAssemblyCacheItemImpl *This = (IAssemblyCacheItemImpl *)iface;
318
319     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
320
321     *ppobj = NULL;
322
323     if (IsEqualIID(riid, &IID_IUnknown) ||
324         IsEqualIID(riid, &IID_IAssemblyCacheItem))
325     {
326         IUnknown_AddRef(iface);
327         *ppobj = This;
328         return S_OK;
329     }
330
331     WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
332     return E_NOINTERFACE;
333 }
334
335 static ULONG WINAPI IAssemblyCacheItemImpl_AddRef(IAssemblyCacheItem *iface)
336 {
337     IAssemblyCacheItemImpl *This = (IAssemblyCacheItemImpl *)iface;
338     ULONG refCount = InterlockedIncrement(&This->ref);
339
340     TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
341
342     return refCount;
343 }
344
345 static ULONG WINAPI IAssemblyCacheItemImpl_Release(IAssemblyCacheItem *iface)
346 {
347     IAssemblyCacheItemImpl *This = (IAssemblyCacheItemImpl *)iface;
348     ULONG refCount = InterlockedDecrement(&This->ref);
349
350     TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
351
352     if (!refCount)
353         HeapFree(GetProcessHeap(), 0, This);
354
355     return refCount;
356 }
357
358 static HRESULT WINAPI IAssemblyCacheItemImpl_CreateStream(IAssemblyCacheItem *iface,
359                                                         DWORD dwFlags,
360                                                         LPCWSTR pszStreamName,
361                                                         DWORD dwFormat,
362                                                         DWORD dwFormatFlags,
363                                                         IStream **ppIStream,
364                                                         ULARGE_INTEGER *puliMaxSize)
365 {
366     FIXME("(%p, %d, %s, %d, %d, %p, %p) stub!\n", iface, dwFlags,
367           debugstr_w(pszStreamName), dwFormat, dwFormatFlags, ppIStream, puliMaxSize);
368
369     return E_NOTIMPL;
370 }
371
372 static HRESULT WINAPI IAssemblyCacheItemImpl_Commit(IAssemblyCacheItem *iface,
373                                                   DWORD dwFlags,
374                                                   ULONG *pulDisposition)
375 {
376     FIXME("(%p, %d, %p) stub!\n", iface, dwFlags, pulDisposition);
377     return E_NOTIMPL;
378 }
379
380 static HRESULT WINAPI IAssemblyCacheItemImpl_AbortItem(IAssemblyCacheItem *iface)
381 {
382     FIXME("(%p) stub!\n", iface);
383     return E_NOTIMPL;
384 }
385
386 static const IAssemblyCacheItemVtbl AssemblyCacheItemVtbl = {
387     IAssemblyCacheItemImpl_QueryInterface,
388     IAssemblyCacheItemImpl_AddRef,
389     IAssemblyCacheItemImpl_Release,
390     IAssemblyCacheItemImpl_CreateStream,
391     IAssemblyCacheItemImpl_Commit,
392     IAssemblyCacheItemImpl_AbortItem
393 };
394
395 /* IAssemblyEnum */
396
397 typedef struct {
398     const IAssemblyEnumVtbl *lpIAssemblyEnumVtbl;
399
400     LONG ref;
401 } IAssemblyEnumImpl;
402
403 static HRESULT WINAPI IAssemblyEnumImpl_QueryInterface(IAssemblyEnum *iface,
404                                                        REFIID riid, LPVOID *ppobj)
405 {
406     IAssemblyEnumImpl *This = (IAssemblyEnumImpl *)iface;
407
408     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
409
410     *ppobj = NULL;
411
412     if (IsEqualIID(riid, &IID_IUnknown) ||
413         IsEqualIID(riid, &IID_IAssemblyEnum))
414     {
415         IUnknown_AddRef(iface);
416         *ppobj = This;
417         return S_OK;
418     }
419
420     WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
421     return E_NOINTERFACE;
422 }
423
424 static ULONG WINAPI IAssemblyEnumImpl_AddRef(IAssemblyEnum *iface)
425 {
426     IAssemblyEnumImpl *This = (IAssemblyEnumImpl *)iface;
427     ULONG refCount = InterlockedIncrement(&This->ref);
428
429     TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
430
431     return refCount;
432 }
433
434 static ULONG WINAPI IAssemblyEnumImpl_Release(IAssemblyEnum *iface)
435 {
436     IAssemblyEnumImpl *This = (IAssemblyEnumImpl *)iface;
437     ULONG refCount = InterlockedDecrement(&This->ref);
438
439     TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
440
441     if (!refCount)
442         HeapFree(GetProcessHeap(), 0, This);
443
444     return refCount;
445 }
446
447 static HRESULT WINAPI IAssemblyEnumImpl_GetNextAssembly(IAssemblyEnum *iface,
448                                                         LPVOID pvReserved,
449                                                         IAssemblyName **ppName,
450                                                         DWORD dwFlags)
451 {
452     FIXME("(%p, %p, %p, %d) stub!\n", iface, pvReserved, ppName, dwFlags);
453     return E_NOTIMPL;
454 }
455
456 static HRESULT WINAPI IAssemblyEnumImpl_Reset(IAssemblyEnum *iface)
457 {
458     FIXME("(%p) stub!\n", iface);
459     return E_NOTIMPL;
460 }
461
462 static HRESULT WINAPI IAssemblyEnumImpl_Clone(IAssemblyEnum *iface,
463                                                IAssemblyEnum **ppEnum)
464 {
465     FIXME("(%p, %p) stub!\n", iface, ppEnum);
466     return E_NOTIMPL;
467 }
468
469 static const IAssemblyEnumVtbl AssemblyEnumVtbl = {
470     IAssemblyEnumImpl_QueryInterface,
471     IAssemblyEnumImpl_AddRef,
472     IAssemblyEnumImpl_Release,
473     IAssemblyEnumImpl_GetNextAssembly,
474     IAssemblyEnumImpl_Reset,
475     IAssemblyEnumImpl_Clone
476 };