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