cmd: DIR command outputs free space for the path.
[wine.git] / dlls / dbghelp / path.c
blobdce700b34e7fc53e00b6eec0ee146458c24736e4
1 /*
2 * File path.c - managing path in debugging environments
4 * Copyright (C) 2004,2008, Eric Pouech
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
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
25 #include "dbghelp_private.h"
26 #include "image_private.h"
27 #include "winnls.h"
28 #include "winternl.h"
29 #include "wine/debug.h"
30 #include "wine/heap.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
34 static const struct machine_dir
36 DWORD machine;
37 const WCHAR *pe_dir;
38 const WCHAR *so_dir;
40 all_machine_dir[] =
42 {IMAGE_FILE_MACHINE_I386, L"\\i386-windows\\", L"\\i386-unix\\"},
43 {IMAGE_FILE_MACHINE_AMD64, L"\\x86_64-windows\\", L"\\x86_64-unix\\"},
44 {IMAGE_FILE_MACHINE_ARMNT, L"\\arm-windows\\", L"\\arm-unix\\"},
45 {IMAGE_FILE_MACHINE_ARM64, L"\\aarch64-windows\\", L"\\aarch64-unix\\"},
48 static inline BOOL is_sepA(char ch) {return ch == '/' || ch == '\\';}
49 static inline BOOL is_sep(WCHAR ch) {return ch == '/' || ch == '\\';}
51 const char* file_nameA(const char* str)
53 const char* p;
55 for (p = str + strlen(str) - 1; p >= str && !is_sepA(*p); p--);
56 return p + 1;
59 const WCHAR* file_name(const WCHAR* str)
61 const WCHAR* p;
63 for (p = str + lstrlenW(str) - 1; p >= str && !is_sep(*p); p--);
64 return p + 1;
67 static inline void file_pathW(const WCHAR *src, WCHAR *dst)
69 int len;
71 for (len = lstrlenW(src) - 1; (len > 0) && (!is_sep(src[len])); len--);
72 memcpy( dst, src, len * sizeof(WCHAR) );
73 dst[len] = 0;
76 /******************************************************************
77 * FindDebugInfoFile (DBGHELP.@)
80 HANDLE WINAPI FindDebugInfoFile(PCSTR FileName, PCSTR SymbolPath, PSTR DebugFilePath)
82 HANDLE h;
84 h = CreateFileA(FileName, GENERIC_READ, FILE_SHARE_READ, NULL,
85 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
86 if (h == INVALID_HANDLE_VALUE)
88 if (!SearchPathA(SymbolPath, file_nameA(FileName), NULL, MAX_PATH, DebugFilePath, NULL))
89 return NULL;
90 h = CreateFileA(DebugFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
91 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
93 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
96 /******************************************************************
97 * FindDebugInfoFileEx (DBGHELP.@)
100 HANDLE WINAPI FindDebugInfoFileEx(PCSTR FileName, PCSTR SymbolPath,
101 PSTR DebugFilePath,
102 PFIND_DEBUG_FILE_CALLBACK Callback,
103 PVOID CallerData)
105 FIXME("(%s %s %s %p %p): stub\n", debugstr_a(FileName), debugstr_a(SymbolPath),
106 debugstr_a(DebugFilePath), Callback, CallerData);
107 return NULL;
110 /******************************************************************
111 * FindExecutableImageExW (DBGHELP.@)
114 HANDLE WINAPI FindExecutableImageExW(PCWSTR FileName, PCWSTR SymbolPath, PWSTR ImageFilePath,
115 PFIND_EXE_FILE_CALLBACKW Callback, PVOID user)
117 HANDLE h;
119 if (Callback) FIXME("Unsupported callback yet\n");
120 if (!SearchPathW(SymbolPath, FileName, NULL, MAX_PATH, ImageFilePath, NULL))
121 return NULL;
122 h = CreateFileW(ImageFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
123 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
124 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
127 /******************************************************************
128 * FindExecutableImageEx (DBGHELP.@)
131 HANDLE WINAPI FindExecutableImageEx(PCSTR FileName, PCSTR SymbolPath, PSTR ImageFilePath,
132 PFIND_EXE_FILE_CALLBACK Callback, PVOID user)
134 HANDLE h;
136 if (Callback) FIXME("Unsupported callback yet\n");
137 if (!SearchPathA(SymbolPath, FileName, NULL, MAX_PATH, ImageFilePath, NULL))
138 return NULL;
139 h = CreateFileA(ImageFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
140 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
141 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
144 /******************************************************************
145 * FindExecutableImage (DBGHELP.@)
148 HANDLE WINAPI FindExecutableImage(PCSTR FileName, PCSTR SymbolPath, PSTR ImageFilePath)
150 return FindExecutableImageEx(FileName, SymbolPath, ImageFilePath, NULL, NULL);
153 /***********************************************************************
154 * MakeSureDirectoryPathExists (DBGHELP.@)
156 BOOL WINAPI MakeSureDirectoryPathExists(PCSTR DirPath)
158 char path[MAX_PATH];
159 const char *p = DirPath;
160 int n;
162 if (p[0] && p[1] == ':') p += 2;
163 while (*p == '\\') p++; /* skip drive root */
164 while ((p = strchr(p, '\\')) != NULL)
166 n = p - DirPath + 1;
167 memcpy(path, DirPath, n);
168 path[n] = '\0';
169 if( !CreateDirectoryA(path, NULL) &&
170 (GetLastError() != ERROR_ALREADY_EXISTS))
171 return FALSE;
172 p++;
174 if (GetLastError() == ERROR_ALREADY_EXISTS)
175 SetLastError(ERROR_SUCCESS);
177 return TRUE;
180 /******************************************************************
181 * SymMatchFileNameW (DBGHELP.@)
184 BOOL WINAPI SymMatchFileNameW(PCWSTR file, PCWSTR match,
185 PWSTR* filestop, PWSTR* matchstop)
187 PCWSTR fptr;
188 PCWSTR mptr;
190 TRACE("(%s %s %p %p)\n",
191 debugstr_w(file), debugstr_w(match), filestop, matchstop);
193 fptr = file + lstrlenW(file) - 1;
194 mptr = match + lstrlenW(match) - 1;
196 while (fptr >= file && mptr >= match)
198 if (towupper(*fptr) != towupper(*mptr) && !(is_sep(*fptr) && is_sep(*mptr)))
199 break;
200 fptr--; mptr--;
202 if (filestop) *filestop = (PWSTR)fptr;
203 if (matchstop) *matchstop = (PWSTR)mptr;
205 return mptr == match - 1;
208 /******************************************************************
209 * SymMatchFileName (DBGHELP.@)
212 BOOL WINAPI SymMatchFileName(PCSTR file, PCSTR match,
213 PSTR* filestop, PSTR* matchstop)
215 PCSTR fptr;
216 PCSTR mptr;
218 TRACE("(%s %s %p %p)\n", debugstr_a(file), debugstr_a(match), filestop, matchstop);
220 fptr = file + strlen(file) - 1;
221 mptr = match + strlen(match) - 1;
223 while (fptr >= file && mptr >= match)
225 if (toupper(*fptr) != toupper(*mptr) && !(is_sepA(*fptr) && is_sepA(*mptr)))
226 break;
227 fptr--; mptr--;
229 if (filestop) *filestop = (PSTR)fptr;
230 if (matchstop) *matchstop = (PSTR)mptr;
232 return mptr == match - 1;
235 static BOOL do_searchW(PCWSTR file, PWSTR buffer, BOOL recurse,
236 PENUMDIRTREE_CALLBACKW cb, PVOID user)
238 HANDLE h;
239 WIN32_FIND_DATAW fd;
240 unsigned pos;
241 BOOL found = FALSE;
243 pos = lstrlenW(buffer);
244 if (pos == 0) return FALSE;
245 if (buffer[pos - 1] != '\\') buffer[pos++] = '\\';
246 lstrcpyW(buffer + pos, L"*.*");
247 if ((h = FindFirstFileW(buffer, &fd)) == INVALID_HANDLE_VALUE)
248 return FALSE;
249 /* doc doesn't specify how the tree is enumerated...
250 * doing a depth first based on, but may be wrong
254 if (!wcscmp(fd.cFileName, L".") || !wcscmp(fd.cFileName, L"..")) continue;
256 lstrcpyW(buffer + pos, fd.cFileName);
257 if (recurse && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
258 found = do_searchW(file, buffer, TRUE, cb, user);
259 else if (SymMatchFileNameW(buffer, file, NULL, NULL))
261 if (!cb || cb(buffer, user)) found = TRUE;
263 } while (!found && FindNextFileW(h, &fd));
264 if (!found) buffer[--pos] = '\0';
265 FindClose(h);
267 return found;
270 /***********************************************************************
271 * SearchTreeForFileW (DBGHELP.@)
273 BOOL WINAPI SearchTreeForFileW(PCWSTR root, PCWSTR file, PWSTR buffer)
275 TRACE("(%s, %s, %p)\n",
276 debugstr_w(root), debugstr_w(file), buffer);
277 lstrcpyW(buffer, root);
278 return do_searchW(file, buffer, TRUE, NULL, NULL);
281 /***********************************************************************
282 * SearchTreeForFile (DBGHELP.@)
284 BOOL WINAPI SearchTreeForFile(PCSTR root, PCSTR file, PSTR buffer)
286 WCHAR rootW[MAX_PATH];
287 WCHAR fileW[MAX_PATH];
288 WCHAR bufferW[MAX_PATH];
289 BOOL ret;
291 MultiByteToWideChar(CP_ACP, 0, root, -1, rootW, MAX_PATH);
292 MultiByteToWideChar(CP_ACP, 0, file, -1, fileW, MAX_PATH);
293 ret = SearchTreeForFileW(rootW, fileW, bufferW);
294 if (ret)
295 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);
296 return ret;
299 /******************************************************************
300 * EnumDirTreeW (DBGHELP.@)
304 BOOL WINAPI EnumDirTreeW(HANDLE hProcess, PCWSTR root, PCWSTR file,
305 PWSTR buffer, PENUMDIRTREE_CALLBACKW cb, PVOID user)
307 TRACE("(%p %s %s %p %p %p)\n",
308 hProcess, debugstr_w(root), debugstr_w(file), buffer, cb, user);
310 lstrcpyW(buffer, root);
311 return do_searchW(file, buffer, TRUE, cb, user);
314 /******************************************************************
315 * EnumDirTree (DBGHELP.@)
319 struct enum_dir_treeWA
321 PENUMDIRTREE_CALLBACK cb;
322 void* user;
323 char name[MAX_PATH];
326 static BOOL CALLBACK enum_dir_treeWA(PCWSTR name, PVOID user)
328 struct enum_dir_treeWA* edt = user;
330 WideCharToMultiByte(CP_ACP, 0, name, -1, edt->name, MAX_PATH, NULL, NULL);
331 return edt->cb(edt->name, edt->user);
334 BOOL WINAPI EnumDirTree(HANDLE hProcess, PCSTR root, PCSTR file,
335 PSTR buffer, PENUMDIRTREE_CALLBACK cb, PVOID user)
337 WCHAR rootW[MAX_PATH];
338 WCHAR fileW[MAX_PATH];
339 WCHAR bufferW[MAX_PATH];
340 struct enum_dir_treeWA edt;
341 BOOL ret;
343 edt.cb = cb;
344 edt.user = user;
345 MultiByteToWideChar(CP_ACP, 0, root, -1, rootW, MAX_PATH);
346 MultiByteToWideChar(CP_ACP, 0, file, -1, fileW, MAX_PATH);
347 if ((ret = EnumDirTreeW(hProcess, rootW, fileW, bufferW, enum_dir_treeWA, &edt)))
348 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);
349 return ret;
352 struct sffip
354 PFINDFILEINPATHCALLBACKW cb;
355 void* user;
358 /* checks that buffer (as found by matching the name) matches the info
359 * (information is based on file type)
360 * returns TRUE when file is found, FALSE to continue searching
361 * (NB this is the opposite convention of SymFindFileInPathProc)
363 static BOOL CALLBACK sffip_cb(PCWSTR buffer, PVOID user)
365 struct sffip* s = user;
367 if (!s->cb) return TRUE;
368 /* yes, EnumDirTree/do_search and SymFindFileInPath callbacks use the opposite
369 * convention to stop/continue enumeration. sigh.
371 return !(s->cb)(buffer, s->user);
374 /******************************************************************
375 * SymFindFileInPathW (DBGHELP.@)
378 BOOL WINAPI SymFindFileInPathW(HANDLE hProcess, PCWSTR searchPath, PCWSTR full_path,
379 PVOID id, DWORD two, DWORD three, DWORD flags,
380 PWSTR buffer, PFINDFILEINPATHCALLBACKW cb,
381 PVOID user)
383 struct sffip s;
384 struct process* pcs = process_find_by_handle(hProcess);
385 WCHAR tmp[MAX_PATH];
386 WCHAR* ptr;
387 const WCHAR* filename;
389 TRACE("(hProcess = %p, searchPath = %s, full_path = %s, id = %p, two = 0x%08lx, three = 0x%08lx, flags = 0x%08lx, buffer = %p, cb = %p, user = %p)\n",
390 hProcess, debugstr_w(searchPath), debugstr_w(full_path),
391 id, two, three, flags, buffer, cb, user);
393 if (!pcs) return FALSE;
394 if (!searchPath) searchPath = pcs->search_path;
396 s.cb = cb;
397 s.user = user;
399 filename = file_name(full_path);
401 /* first check full path to file */
402 if (sffip_cb(full_path, &s))
404 lstrcpyW(buffer, full_path);
405 return TRUE;
408 while (searchPath)
410 ptr = wcschr(searchPath, ';');
411 if (ptr)
413 memcpy(tmp, searchPath, (ptr - searchPath) * sizeof(WCHAR));
414 tmp[ptr - searchPath] = 0;
415 searchPath = ptr + 1;
417 else
419 lstrcpyW(tmp, searchPath);
420 searchPath = NULL;
422 if (do_searchW(filename, tmp, FALSE, sffip_cb, &s))
424 lstrcpyW(buffer, tmp);
425 return TRUE;
428 return FALSE;
431 /******************************************************************
432 * SymFindFileInPath (DBGHELP.@)
435 BOOL WINAPI SymFindFileInPath(HANDLE hProcess, PCSTR searchPath, PCSTR full_path,
436 PVOID id, DWORD two, DWORD three, DWORD flags,
437 PSTR buffer, PFINDFILEINPATHCALLBACK cb,
438 PVOID user)
440 WCHAR searchPathW[MAX_PATH];
441 WCHAR full_pathW[MAX_PATH];
442 WCHAR bufferW[MAX_PATH];
443 struct enum_dir_treeWA edt;
444 BOOL ret;
446 /* a PFINDFILEINPATHCALLBACK and a PENUMDIRTREE_CALLBACK have actually the
447 * same signature & semantics, hence we can reuse the EnumDirTree W->A
448 * conversion helper
450 edt.cb = cb;
451 edt.user = user;
452 if (searchPath)
453 MultiByteToWideChar(CP_ACP, 0, searchPath, -1, searchPathW, MAX_PATH);
454 MultiByteToWideChar(CP_ACP, 0, full_path, -1, full_pathW, MAX_PATH);
455 if ((ret = SymFindFileInPathW(hProcess, searchPath ? searchPathW : NULL, full_pathW,
456 id, two, three, flags,
457 bufferW, enum_dir_treeWA, &edt)))
458 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);
459 return ret;
462 struct module_find
464 enum module_type kind;
465 /* pe: dw1 DWORD:timestamp
466 * dw2 size of image (from PE header)
467 * pdb: guid PDB guid (if DS PDB file)
468 * or dw1 PDB timestamp (if JG PDB file)
469 * dw2 PDB age
470 * elf: dw1 DWORD:CRC 32 of ELF image (Wine only)
472 const GUID* guid;
473 DWORD dw1;
474 DWORD dw2;
475 WCHAR filename[MAX_PATH];
476 unsigned matched;
479 /* checks that buffer (as found by matching the name) matches the info
480 * (information is based on file type)
481 * returns TRUE when file is found, FALSE to continue searching
482 * (NB this is the opposite convention of SymFindFileInPathProc)
484 static BOOL CALLBACK module_find_cb(PCWSTR buffer, PVOID user)
486 struct module_find* mf = user;
487 DWORD size, timestamp;
488 unsigned matched = 0;
490 /* the matching weights:
491 * +1 if a file with same name is found and is a decent file of expected type
492 * +1 if first parameter and second parameter match
495 /* FIXME: should check that id/two match the file pointed
496 * by buffer
498 switch (mf->kind)
500 case DMT_PE:
502 HANDLE hFile, hMap;
503 void* mapping;
505 timestamp = ~mf->dw1;
506 size = ~mf->dw2;
507 hFile = CreateFileW(buffer, GENERIC_READ, FILE_SHARE_READ, NULL,
508 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
509 if (hFile == INVALID_HANDLE_VALUE) return FALSE;
510 if ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != NULL)
512 if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
514 IMAGE_NT_HEADERS* nth = RtlImageNtHeader(mapping);
515 if (!nth)
517 UnmapViewOfFile(mapping);
518 CloseHandle(hMap);
519 CloseHandle(hFile);
520 return FALSE;
522 matched++;
523 timestamp = nth->FileHeader.TimeDateStamp;
524 size = nth->OptionalHeader.SizeOfImage;
525 UnmapViewOfFile(mapping);
527 CloseHandle(hMap);
529 CloseHandle(hFile);
530 if (timestamp != mf->dw1)
531 WARN("Found %s, but wrong timestamp\n", debugstr_w(buffer));
532 if (size != mf->dw2)
533 WARN("Found %s, but wrong size\n", debugstr_w(buffer));
534 if (timestamp == mf->dw1 && size == mf->dw2) matched++;
536 break;
537 case DMT_PDB:
539 struct pdb_lookup pdb_lookup;
540 char fn[MAX_PATH];
542 WideCharToMultiByte(CP_ACP, 0, buffer, -1, fn, MAX_PATH, NULL, NULL);
543 pdb_lookup.filename = fn;
545 if (mf->guid)
547 pdb_lookup.kind = PDB_DS;
548 pdb_lookup.timestamp = 0;
549 pdb_lookup.guid = *mf->guid;
551 else
553 pdb_lookup.kind = PDB_JG;
554 pdb_lookup.timestamp = mf->dw1;
555 /* pdb_loopkup.guid = */
557 pdb_lookup.age = mf->dw2;
559 if (!pdb_fetch_file_info(&pdb_lookup, &matched)) return FALSE;
561 break;
562 case DMT_DBG:
564 HANDLE hFile, hMap;
565 void* mapping;
567 timestamp = ~mf->dw1;
568 hFile = CreateFileW(buffer, GENERIC_READ, FILE_SHARE_READ, NULL,
569 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
570 if (hFile == INVALID_HANDLE_VALUE) return FALSE;
571 if ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != NULL)
573 if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
575 const IMAGE_SEPARATE_DEBUG_HEADER* hdr;
576 hdr = mapping;
578 if (hdr->Signature == IMAGE_SEPARATE_DEBUG_SIGNATURE)
580 matched++;
581 timestamp = hdr->TimeDateStamp;
583 UnmapViewOfFile(mapping);
585 CloseHandle(hMap);
587 CloseHandle(hFile);
588 if (timestamp == mf->dw1) matched++;
589 else WARN("Found %s, but wrong timestamp\n", debugstr_w(buffer));
591 break;
592 default:
593 FIXME("What the heck??\n");
594 return FALSE;
596 if (matched > mf->matched)
598 lstrcpyW(mf->filename, buffer);
599 mf->matched = matched;
601 /* yes, EnumDirTree/do_search and SymFindFileInPath callbacks use the opposite
602 * convention to stop/continue enumeration. sigh.
604 return mf->matched == 2;
607 BOOL path_find_symbol_file(const struct process* pcs, const struct module* module,
608 PCSTR full_path, enum module_type type, const GUID* guid, DWORD dw1, DWORD dw2,
609 WCHAR *buffer, BOOL* is_unmatched)
611 struct module_find mf;
612 WCHAR full_pathW[MAX_PATH];
613 WCHAR* ptr;
614 const WCHAR* filename;
615 WCHAR* searchPath = pcs->search_path;
617 TRACE("(pcs = %p, full_path = %s, guid = %s, dw1 = 0x%08lx, dw2 = 0x%08lx, buffer = %p)\n",
618 pcs, debugstr_a(full_path), debugstr_guid(guid), dw1, dw2, buffer);
620 mf.guid = guid;
621 mf.dw1 = dw1;
622 mf.dw2 = dw2;
623 mf.matched = 0;
625 MultiByteToWideChar(CP_ACP, 0, full_path, -1, full_pathW, MAX_PATH);
626 filename = file_name(full_pathW);
627 mf.kind = type;
628 *is_unmatched = FALSE;
630 /* first check full path to file */
631 if (module_find_cb(full_pathW, &mf))
633 lstrcpyW( buffer, full_pathW );
634 return TRUE;
637 /* FIXME: Use Environment-Variables (see MS docs)
638 _NT_SYMBOL_PATH and _NT_ALT_SYMBOL_PATH
639 FIXME: Implement "Standard Path Elements" (Path) ... (see MS docs)
640 do a search for (every?) path-element like this ...
641 <path>
642 <path>\dll
643 <path>\symbols\dll
644 (dll may be exe, or sys depending on the file extension) */
646 /* 2. check module-path */
647 file_pathW(module->module.LoadedImageName, buffer);
648 if (do_searchW(filename, buffer, FALSE, module_find_cb, &mf)) return TRUE;
649 if (module->real_path)
651 file_pathW(module->real_path, buffer);
652 if (do_searchW(filename, buffer, FALSE, module_find_cb, &mf)) return TRUE;
655 while (searchPath)
657 ptr = wcschr(searchPath, ';');
658 if (ptr)
660 memcpy(buffer, searchPath, (ptr - searchPath) * sizeof(WCHAR));
661 buffer[ptr - searchPath] = '\0';
662 searchPath = ptr + 1;
664 else
666 lstrcpyW(buffer, searchPath);
667 searchPath = NULL;
669 /* return first fully matched file */
670 if (do_searchW(filename, buffer, FALSE, module_find_cb, &mf)) return TRUE;
672 /* if no fully matching file is found, return the best matching file if any */
673 if ((dbghelp_options & SYMOPT_LOAD_ANYTHING) && mf.matched)
675 lstrcpyW( buffer, mf.filename );
676 *is_unmatched = TRUE;
677 return TRUE;
679 return FALSE;
682 WCHAR *get_dos_file_name(const WCHAR *filename)
684 WCHAR *dos_path;
685 size_t len;
687 if (*filename == '/')
689 char *unix_path;
690 len = WideCharToMultiByte(CP_UNIXCP, 0, filename, -1, NULL, 0, NULL, NULL);
691 unix_path = heap_alloc(len * sizeof(WCHAR));
692 WideCharToMultiByte(CP_UNIXCP, 0, filename, -1, unix_path, len, NULL, NULL);
693 dos_path = wine_get_dos_file_name(unix_path);
694 heap_free(unix_path);
696 else
698 len = lstrlenW(filename);
699 dos_path = heap_alloc((len + 1) * sizeof(WCHAR));
700 memcpy(dos_path, filename, (len + 1) * sizeof(WCHAR));
702 return dos_path;
705 static inline const WCHAR* get_machine_dir(const struct machine_dir *machine_dir, const WCHAR *name)
707 WCHAR *ptr;
708 if ((ptr = wcsrchr(name, L'.')) && !lstrcmpW(ptr, L".so"))
709 return machine_dir->so_dir;
710 return machine_dir->pe_dir;
713 static BOOL try_match_file(const WCHAR *name, BOOL (*match)(void*, HANDLE, const WCHAR*), void *param)
715 HANDLE file = CreateFileW(name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
716 if (file != INVALID_HANDLE_VALUE)
718 BOOL ret = match(param, file, name);
719 CloseHandle(file);
720 return ret;
722 return FALSE;
725 BOOL search_dll_path(const struct process *process, const WCHAR *name, WORD machine, BOOL (*match)(void*, HANDLE, const WCHAR*), void *param)
727 const WCHAR *env;
728 WCHAR *p, *end;
729 size_t len, i, machine_dir_len;
730 WCHAR *buf;
731 const struct cpu* cpu;
732 const struct machine_dir* machine_dir;
734 name = file_name(name);
736 cpu = machine == IMAGE_FILE_MACHINE_UNKNOWN ? process_get_cpu(process) : cpu_find(machine);
738 for (machine_dir = all_machine_dir; machine_dir < all_machine_dir + ARRAY_SIZE(all_machine_dir); machine_dir++)
739 if (machine_dir->machine == cpu->machine) break;
740 if (machine_dir >= all_machine_dir + ARRAY_SIZE(all_machine_dir)) return FALSE;
741 machine_dir_len = max(wcslen(machine_dir->pe_dir), wcslen(machine_dir->so_dir));
743 if ((env = process_getenv(process, L"WINEBUILDDIR")))
745 len = lstrlenW(env);
746 if (!(buf = heap_alloc((len + wcslen(L"\\programs\\") + machine_dir_len +
747 2 * lstrlenW(name) + 1) * sizeof(WCHAR)))) return FALSE;
748 wcscpy(buf, env);
749 end = buf + len;
751 wcscpy(end, L"\\dlls\\");
752 wcscat(end, name);
753 if ((p = wcsrchr(end, '.')) && !lstrcmpW(p, L".so")) *p = 0;
754 if ((p = wcsrchr(end, '.')) && !lstrcmpW(p, L".dll")) *p = 0;
755 p = end + lstrlenW(end);
756 /* try multi-arch first */
757 wcscpy(p, get_machine_dir(machine_dir, name));
758 wcscpy(p + wcslen(p), name);
759 if (try_match_file(buf, match, param)) goto found;
760 /* then old mono-arch */
761 *p++ = '\\';
762 lstrcpyW(p, name);
763 if (try_match_file(buf, match, param)) goto found;
765 wcscpy(end, L"\\programs\\");
766 end += wcslen(end);
767 wcscpy(end, name);
768 if ((p = wcsrchr(end, '.')) && !lstrcmpW(p, L".so")) *p = 0;
769 if ((p = wcsrchr(end, '.')) && !lstrcmpW(p, L".exe")) *p = 0;
770 p = end + lstrlenW(end);
771 /* try multi-arch first */
772 wcscpy(p, get_machine_dir(machine_dir, name));
773 wcscpy(p + wcslen(p), name);
774 if (try_match_file(buf, match, param)) goto found;
775 /* then old mono-arch */
776 *p++ = '\\';
777 lstrcpyW(p, name);
778 if (try_match_file(buf, match, param)) goto found;
780 heap_free(buf);
783 for (i = 0;; i++)
785 WCHAR env_name[64];
786 swprintf(env_name, ARRAY_SIZE(env_name), L"WINEDLLDIR%u", i);
787 if (!(env = process_getenv(process, env_name))) return FALSE;
788 len = wcslen(env) + machine_dir_len + wcslen(name) + 1;
789 if (!(buf = heap_alloc(len * sizeof(WCHAR)))) return FALSE;
790 swprintf(buf, len, L"%s%s%s", env, get_machine_dir(machine_dir, name), name);
791 if (try_match_file(buf, match, param)) goto found;
792 swprintf(buf, len, L"%s\\%s", env, name);
793 if (try_match_file(buf, match, param)) goto found;
794 heap_free(buf);
797 return FALSE;
799 found:
800 TRACE("found %s\n", debugstr_w(buf));
801 heap_free(buf);
802 return TRUE;
805 BOOL search_unix_path(const WCHAR *name, const WCHAR *path, BOOL (*match)(void*, HANDLE, const WCHAR*), void *param)
807 const WCHAR *iter, *next;
808 size_t size, len;
809 WCHAR *dos_path;
810 char *buf;
811 BOOL ret = FALSE;
813 if (!path) return FALSE;
814 name = file_name(name);
816 size = WideCharToMultiByte(CP_UNIXCP, 0, name, -1, NULL, 0, NULL, NULL)
817 + WideCharToMultiByte(CP_UNIXCP, 0, path, -1, NULL, 0, NULL, NULL);
818 if (!(buf = heap_alloc(size))) return FALSE;
820 for (iter = path;; iter = next + 1)
822 if (!(next = wcschr(iter, ':'))) next = iter + lstrlenW(iter);
823 if (*iter == '/')
825 len = WideCharToMultiByte(CP_UNIXCP, 0, iter, next - iter, buf, size, NULL, NULL);
826 if (buf[len - 1] != '/') buf[len++] = '/';
827 WideCharToMultiByte(CP_UNIXCP, 0, name, -1, buf + len, size - len, NULL, NULL);
828 if ((dos_path = wine_get_dos_file_name(buf)))
830 ret = try_match_file(dos_path, match, param);
831 if (ret) TRACE("found %s\n", debugstr_w(dos_path));
832 heap_free(dos_path);
833 if (ret) break;
836 if (*next != ':') break;
839 heap_free(buf);
840 return ret;
843 /******************************************************************
844 * SymSrvGetFileIndexInfo (DBGHELP.@)
847 BOOL WINAPI SymSrvGetFileIndexInfo(const char *file, SYMSRV_INDEX_INFO* info, DWORD flags)
849 SYMSRV_INDEX_INFOW infoW;
850 WCHAR fileW[MAX_PATH];
851 BOOL ret;
853 TRACE("(%s, %p, 0x%08lx)\n", debugstr_a(file), info, flags);
855 if (info->sizeofstruct < sizeof(*info))
857 SetLastError(ERROR_INVALID_PARAMETER);
858 return FALSE;
860 MultiByteToWideChar(CP_ACP, 0, file, -1, fileW, ARRAY_SIZE(fileW));
861 infoW.sizeofstruct = sizeof(infoW);
862 ret = SymSrvGetFileIndexInfoW(fileW, &infoW, flags);
863 if (ret)
865 WideCharToMultiByte(CP_ACP, 0, infoW.file, -1, info->file, ARRAY_SIZE(info->file), NULL, NULL);
866 info->stripped = infoW.stripped;
867 info->timestamp = infoW.timestamp;
868 info->size = infoW.size;
869 WideCharToMultiByte(CP_ACP, 0, infoW.dbgfile, -1, info->dbgfile, ARRAY_SIZE(info->dbgfile), NULL, NULL);
870 WideCharToMultiByte(CP_ACP, 0, infoW.pdbfile, -1, info->pdbfile, ARRAY_SIZE(info->pdbfile), NULL, NULL);
871 info->guid = infoW.guid;
872 info->sig = infoW.sig;
873 info->age = infoW.age;
875 return ret;
878 /******************************************************************
879 * SymSrvGetFileIndexInfoW (DBGHELP.@)
882 BOOL WINAPI SymSrvGetFileIndexInfoW(const WCHAR *file, SYMSRV_INDEX_INFOW* info, DWORD flags)
884 HANDLE hFile, hMap = NULL;
885 void* image = NULL;
886 DWORD fsize, ret;
888 TRACE("(%s, %p, 0x%08lx)\n", debugstr_w(file), info, flags);
890 if (info->sizeofstruct < sizeof(*info))
892 SetLastError(ERROR_INVALID_PARAMETER);
893 return FALSE;
896 if ((hFile = CreateFileW(file, GENERIC_READ, FILE_SHARE_READ, NULL,
897 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
898 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != NULL) &&
899 ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
901 /* must handle PE, or .dbg or .pdb files. So each helper will return:
902 * - ERROR_SUCCESS: if the file format is recognized and index info filled,
903 * - ERROR_BAD_FORMAT: if the file doesn't match the expected format,
904 * - any other error: if the file has expected format, but internal errors
906 fsize = GetFileSize(hFile, NULL);
907 /* try PE module first */
908 ret = pe_get_file_indexinfo(image, fsize, info);
909 if (ret == ERROR_BAD_FORMAT)
910 ret = pdb_get_file_indexinfo(image, fsize, info);
912 else ret = ERROR_FILE_NOT_FOUND;
914 if (image) UnmapViewOfFile(image);
915 if (hMap) CloseHandle(hMap);
916 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
918 if (ret == ERROR_SUCCESS) wcscpy(info->file, file_name(file)); /* overflow? */
919 SetLastError(ret);
920 return ret == ERROR_SUCCESS;
923 /******************************************************************
924 * SymSrvGetFileIndexes (DBGHELP.@)
927 BOOL WINAPI SymSrvGetFileIndexes(PCSTR file, GUID* guid, PDWORD pdw1, PDWORD pdw2, DWORD flags)
929 WCHAR fileW[MAX_PATH];
931 TRACE("(%s, %p, %p, %p, 0x%08lx)\n", debugstr_a(file), guid, pdw1, pdw2, flags);
933 MultiByteToWideChar(CP_ACP, 0, file, -1, fileW, ARRAY_SIZE(fileW));
934 return SymSrvGetFileIndexesW(fileW, guid, pdw1, pdw2, flags);
937 /******************************************************************
938 * SymSrvGetFileIndexesW (DBGHELP.@)
941 BOOL WINAPI SymSrvGetFileIndexesW(PCWSTR file, GUID* guid, PDWORD pdw1, PDWORD pdw2, DWORD flags)
943 FIXME("(%s, %p, %p, %p, 0x%08lx): stub!\n", debugstr_w(file), guid, pdw1, pdw2, flags);
945 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
946 return FALSE;