2 * IAssemblyEnum implementation
4 * Copyright 2008 James Hawkins
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.
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.
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
32 #include "fusionpriv.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36 #include "wine/list.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(fusion);
40 typedef struct _tagASMNAME
48 const IAssemblyEnumVtbl *lpIAssemblyEnumVtbl;
50 struct list assemblies;
55 static HRESULT WINAPI IAssemblyEnumImpl_QueryInterface(IAssemblyEnum *iface,
56 REFIID riid, LPVOID *ppobj)
58 IAssemblyEnumImpl *This = (IAssemblyEnumImpl *)iface;
60 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
64 if (IsEqualIID(riid, &IID_IUnknown) ||
65 IsEqualIID(riid, &IID_IAssemblyEnum))
67 IUnknown_AddRef(iface);
72 WARN("(%p, %s, %p): not found\n", This, debugstr_guid(riid), ppobj);
76 static ULONG WINAPI IAssemblyEnumImpl_AddRef(IAssemblyEnum *iface)
78 IAssemblyEnumImpl *This = (IAssemblyEnumImpl *)iface;
79 ULONG refCount = InterlockedIncrement(&This->ref);
81 TRACE("(%p)->(ref before = %u)\n", This, refCount - 1);
86 static ULONG WINAPI IAssemblyEnumImpl_Release(IAssemblyEnum *iface)
88 IAssemblyEnumImpl *This = (IAssemblyEnumImpl *)iface;
89 ULONG refCount = InterlockedDecrement(&This->ref);
90 struct list *item, *cursor;
92 TRACE("(%p)->(ref before = %u)\n", This, refCount + 1);
96 LIST_FOR_EACH_SAFE(item, cursor, &This->assemblies)
98 ASMNAME *asmname = LIST_ENTRY(item, ASMNAME, entry);
100 list_remove(&asmname->entry);
101 IAssemblyName_Release(asmname->name);
102 HeapFree(GetProcessHeap(), 0, asmname);
105 HeapFree(GetProcessHeap(), 0, This);
111 static HRESULT WINAPI IAssemblyEnumImpl_GetNextAssembly(IAssemblyEnum *iface,
113 IAssemblyName **ppName,
116 IAssemblyEnumImpl *asmenum = (IAssemblyEnumImpl *)iface;
119 TRACE("(%p, %p, %p, %d)\n", iface, pvReserved, ppName, dwFlags);
124 asmname = LIST_ENTRY(asmenum->iter, ASMNAME, entry);
128 *ppName = asmname->name;
129 IAssemblyName_AddRef(*ppName);
131 asmenum->iter = list_next(&asmenum->assemblies, asmenum->iter);
136 static HRESULT WINAPI IAssemblyEnumImpl_Reset(IAssemblyEnum *iface)
138 IAssemblyEnumImpl *asmenum = (IAssemblyEnumImpl *)iface;
140 TRACE("(%p)\n", iface);
142 asmenum->iter = list_head(&asmenum->assemblies);
146 static HRESULT WINAPI IAssemblyEnumImpl_Clone(IAssemblyEnum *iface,
147 IAssemblyEnum **ppEnum)
149 FIXME("(%p, %p) stub!\n", iface, ppEnum);
153 static const IAssemblyEnumVtbl AssemblyEnumVtbl = {
154 IAssemblyEnumImpl_QueryInterface,
155 IAssemblyEnumImpl_AddRef,
156 IAssemblyEnumImpl_Release,
157 IAssemblyEnumImpl_GetNextAssembly,
158 IAssemblyEnumImpl_Reset,
159 IAssemblyEnumImpl_Clone
162 static void parse_name(IAssemblyName *name, int depth, LPWSTR path, LPWSTR buf)
164 WCHAR disp[MAX_PATH];
165 LPCWSTR verptr, pubkeyptr;
167 DWORD size, major_size, minor_size, build_size, revision_size;
168 WORD major, minor, build, revision;
170 static const WCHAR star[] = {'*',0};
171 static const WCHAR ss_fmt[] = {'%','s','\\','%','s',0};
172 static const WCHAR verpubkey[] = {'%','s','\\','%','s','_','_','%','s',0};
173 static const WCHAR ver_fmt[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
175 WCHAR version[24]; /* strlen("65535") * 4 + 3 + 1 */
176 WCHAR token_str[TOKEN_LENGTH + 1];
177 BYTE token[BYTES_PER_TOKEN];
183 hr = IAssemblyName_GetName(name, &size, disp);
185 sprintfW(buf, ss_fmt, path, disp);
187 sprintfW(buf, ss_fmt, path, star);
191 major_size = sizeof(major);
192 IAssemblyName_GetProperty(name, ASM_NAME_MAJOR_VERSION, &major, &major_size);
194 minor_size = sizeof(minor);
195 IAssemblyName_GetProperty(name, ASM_NAME_MINOR_VERSION, &minor, &minor_size);
197 build_size = sizeof(build);
198 IAssemblyName_GetProperty(name, ASM_NAME_BUILD_NUMBER, &build, &build_size);
200 revision_size = sizeof(revision);
201 IAssemblyName_GetProperty(name, ASM_NAME_REVISION_NUMBER, &revision, &revision_size);
203 if (!major_size || !minor_size || !build_size || !revision_size) verptr = star;
206 sprintfW(version, ver_fmt, major, minor, build, revision);
210 size = sizeof(token);
211 IAssemblyName_GetProperty(name, ASM_NAME_PUBLIC_KEY_TOKEN, token, &size);
213 if (!size) pubkeyptr = star;
216 token_to_str(token, token_str);
217 pubkeyptr = token_str;
220 sprintfW(buf, verpubkey, path, verptr, pubkeyptr);
224 static int compare_assembly_names(ASMNAME *asmname1, ASMNAME *asmname2)
227 WORD version1, version2;
228 WCHAR name1[MAX_PATH], name2[MAX_PATH];
229 WCHAR token_str1[TOKEN_LENGTH + 1], token_str2[TOKEN_LENGTH + 1];
230 BYTE token1[BYTES_PER_TOKEN], token2[BYTES_PER_TOKEN];
233 size = sizeof(name1);
234 IAssemblyName_GetProperty(asmname1->name, ASM_NAME_NAME, &name1, &size);
235 size = sizeof(name2);
236 IAssemblyName_GetProperty(asmname2->name, ASM_NAME_NAME, &name2, &size);
238 if ((ret = strcmpiW(name1, name2))) return ret;
240 for (i = ASM_NAME_MAJOR_VERSION; i < ASM_NAME_CULTURE; i++)
242 size = sizeof(version1);
243 IAssemblyName_GetProperty(asmname1->name, i, &version1, &size);
244 size = sizeof(version2);
245 IAssemblyName_GetProperty(asmname2->name, i, &version2, &size);
247 if (version1 < version2) return -1;
248 if (version1 > version2) return 1;
251 /* FIXME: compare cultures */
253 size = sizeof(token1);
254 IAssemblyName_GetProperty(asmname1->name, ASM_NAME_PUBLIC_KEY_TOKEN, token1, &size);
255 size = sizeof(token2);
256 IAssemblyName_GetProperty(asmname2->name, ASM_NAME_PUBLIC_KEY_TOKEN, token2, &size);
258 token_to_str(token1, token_str1);
259 token_to_str(token2, token_str2);
261 if ((ret = strcmpiW(token_str1, token_str2))) return ret;
266 /* insert assembly in list preserving sort order */
267 static void insert_assembly(struct list *assemblies, ASMNAME *to_insert)
271 LIST_FOR_EACH(item, assemblies)
273 ASMNAME *name = LIST_ENTRY(item, ASMNAME, entry);
275 if (compare_assembly_names(name, to_insert) > 0)
277 list_add_before(&name->entry, &to_insert->entry);
281 list_add_tail(assemblies, &to_insert->entry);
284 static HRESULT enum_gac_assemblies(struct list *assemblies, IAssemblyName *name,
285 int depth, LPWSTR path)
287 WIN32_FIND_DATAW ffd;
289 WCHAR disp[MAX_PATH];
295 static WCHAR parent[MAX_PATH];
297 static const WCHAR dot[] = {'.',0};
298 static const WCHAR dotdot[] = {'.','.',0};
299 static const WCHAR search_fmt[] = {'%','s','\\','*',0};
300 static const WCHAR parent_fmt[] = {'%','s',',',' ',0};
301 static const WCHAR dblunder[] = {'_','_',0};
302 static const WCHAR fmt[] = {'V','e','r','s','i','o','n','=','%','s',',',' ',
303 'C','u','l','t','u','r','e','=','n','e','u','t','r','a','l',',',' ',
304 'P','u','b','l','i','c','K','e','y','T','o','k','e','n','=','%','s',0};
305 static const WCHAR ss_fmt[] = {'%','s','\\','%','s',0};
308 parse_name(name, depth, path, buf);
310 sprintfW(buf, search_fmt, path);
312 hfind = FindFirstFileW(buf, &ffd);
313 if (hfind == INVALID_HANDLE_VALUE)
318 if (!lstrcmpW(ffd.cFileName, dot) || !lstrcmpW(ffd.cFileName, dotdot))
324 ptr = strrchrW(buf, '\\') + 1;
328 sprintfW(parent, parent_fmt, ptr);
332 ptr = strstrW(ffd.cFileName, dblunder);
335 sprintfW(buf, fmt, ffd.cFileName, ptr);
337 lstrcpyW(disp, parent);
340 asmname = HeapAlloc(GetProcessHeap(), 0, sizeof(ASMNAME));
347 hr = CreateAssemblyNameObject(&asmname->name, disp,
348 CANOF_PARSE_DISPLAY_NAME, NULL);
351 HeapFree(GetProcessHeap(), 0, asmname);
355 insert_assembly(assemblies, asmname);
359 sprintfW(buf, ss_fmt, path, ffd.cFileName);
360 hr = enum_gac_assemblies(assemblies, name, depth + 1, buf);
363 } while (FindNextFileW(hfind, &ffd) != 0);
369 static HRESULT enumerate_gac(IAssemblyEnumImpl *asmenum, IAssemblyName *pName)
371 WCHAR path[MAX_PATH];
376 static WCHAR under32[] = {'_','3','2',0};
377 static WCHAR msil[] = {'_','M','S','I','L',0};
380 hr = GetCachePath(ASM_CACHE_GAC, buf, &size);
385 lstrcatW(path, under32);
386 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, path);
391 lstrcatW(path, msil);
392 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, path);
396 hr = enum_gac_assemblies(&asmenum->assemblies, pName, 0, buf);
403 /******************************************************************
404 * CreateAssemblyEnum (FUSION.@)
406 HRESULT WINAPI CreateAssemblyEnum(IAssemblyEnum **pEnum, IUnknown *pUnkReserved,
407 IAssemblyName *pName, DWORD dwFlags, LPVOID pvReserved)
409 IAssemblyEnumImpl *asmenum;
412 TRACE("(%p, %p, %p, %08x, %p)\n", pEnum, pUnkReserved,
413 pName, dwFlags, pvReserved);
418 if (dwFlags == 0 || dwFlags == ASM_CACHE_ROOT)
421 asmenum = HeapAlloc(GetProcessHeap(), 0, sizeof(IAssemblyEnumImpl));
423 return E_OUTOFMEMORY;
425 asmenum->lpIAssemblyEnumVtbl = &AssemblyEnumVtbl;
427 list_init(&asmenum->assemblies);
429 if (dwFlags & ASM_CACHE_GAC)
431 hr = enumerate_gac(asmenum, pName);
434 HeapFree(GetProcessHeap(), 0, asmenum);
439 asmenum->iter = list_head(&asmenum->assemblies);
440 *pEnum = (IAssemblyEnum *)asmenum;