2 * File path.c - managing path in debugging environments
4 * Copyright (C) 2004, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "dbghelp_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
34 static inline BOOL is_sep(char ch) {return ch == '/' || ch == '\\';}
36 static inline const char* file_name(const char* str)
40 for (p = str + strlen(str) - 1; p >= str && !is_sep(*p); p--);
44 /******************************************************************
45 * FindDebugInfoFile (DBGHELP.@)
48 HANDLE WINAPI FindDebugInfoFile(PCSTR FileName, PCSTR SymbolPath, PSTR DebugFilePath)
52 h = CreateFileA(DebugFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
53 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
54 if (h == INVALID_HANDLE_VALUE)
56 if (!SearchPathA(SymbolPath, file_name(FileName), NULL, MAX_PATH, DebugFilePath, NULL))
58 h = CreateFileA(DebugFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
59 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
61 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
64 /******************************************************************
65 * FindDebugInfoFileEx (DBGHELP.@)
68 HANDLE WINAPI FindDebugInfoFileEx(PCSTR FileName, PCSTR SymbolPath,
70 PFIND_DEBUG_FILE_CALLBACK Callback,
73 FIXME("(%s %s %p %p %p): stub\n",
74 FileName, SymbolPath, DebugFilePath, Callback, CallerData);
78 /******************************************************************
79 * FindExecutableImage (DBGHELP.@)
82 HANDLE WINAPI FindExecutableImage(PCSTR FileName, PCSTR SymbolPath, PSTR ImageFilePath)
85 if (!SearchPathA(SymbolPath, FileName, NULL, MAX_PATH, ImageFilePath, NULL))
87 h = CreateFileA(ImageFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
88 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
89 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
92 /***********************************************************************
93 * MakeSureDirectoryPathExists (DBGHELP.@)
95 BOOL WINAPI MakeSureDirectoryPathExists(LPCSTR DirPath)
98 const char *p = DirPath;
101 if (p[0] && p[1] == ':') p += 2;
102 while (*p == '\\') p++; /* skip drive root */
103 while ((p = strchr(p, '\\')) != NULL)
106 memcpy(path, DirPath, n);
108 if( !CreateDirectoryA(path, NULL) &&
109 (GetLastError() != ERROR_ALREADY_EXISTS))
113 if (GetLastError() == ERROR_ALREADY_EXISTS)
114 SetLastError(ERROR_SUCCESS);
119 /******************************************************************
120 * SymMatchFileName (DBGHELP.@)
123 BOOL WINAPI SymMatchFileName(char* file, char* match,
124 char** filestop, char** matchstop)
129 TRACE("(%s %s %p %p)\n", file, match, filestop, matchstop);
131 fptr = file + strlen(file) - 1;
132 mptr = match + strlen(match) - 1;
134 while (fptr >= file && mptr >= match)
136 if (toupper(*fptr) != toupper(*mptr) && !(is_sep(*fptr) && is_sep(*mptr)))
140 if (filestop) *filestop = fptr;
141 if (matchstop) *matchstop = mptr;
143 return mptr == match - 1;
146 static BOOL do_search(const char* file, char* buffer, BOOL recurse,
147 PENUMDIRTREE_CALLBACK cb, void* user)
154 pos = strlen(buffer);
155 if (buffer[pos - 1] != '\\') buffer[pos++] = '\\';
156 strcpy(buffer + pos, "*.*");
157 if ((h = FindFirstFileA(buffer, &fd)) == INVALID_HANDLE_VALUE)
159 /* doc doesn't specify how the tree is enumerated...
160 * doing a depth first based on, but may be wrong
164 if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, "..")) continue;
166 strcpy(buffer + pos, fd.cFileName);
167 if (recurse && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
168 found = do_search(file, buffer, TRUE, cb, user);
169 else if (SymMatchFileName(buffer, (char*)file, NULL, NULL))
171 if (!cb || cb(buffer, user)) found = TRUE;
173 } while (!found && FindNextFileA(h, &fd));
174 if (!found) buffer[--pos] = '\0';
180 /***********************************************************************
181 * SearchTreeForFile (DBGHELP.@)
183 BOOL WINAPI SearchTreeForFile(PCSTR root, PCSTR file, PSTR buffer)
185 TRACE("(%s, %s, %p)\n",
186 debugstr_a(root), debugstr_a(file), buffer);
187 strcpy(buffer, root);
188 return do_search(file, buffer, TRUE, NULL, NULL);
191 /******************************************************************
192 * EnumDirTree (DBGHELP.@)
196 BOOL WINAPI EnumDirTree(HANDLE hProcess, PCSTR root, PCSTR file,
197 LPSTR buffer, PENUMDIRTREE_CALLBACK cb, PVOID user)
199 TRACE("(%p %s %s %p %p %p)\n", hProcess, root, file, buffer, cb, user);
201 strcpy(buffer, root);
202 return do_search(file, buffer, TRUE, cb, user);
207 enum module_type kind;
208 /* pe: id -> DWORD:timestamp
209 * two -> size of image (from PE header)
210 * pdb: id -> PDB signature
211 * I think either DWORD:timestamp or GUID:guid depending on PDB version
213 * elf: id -> DWORD:CRC 32 of ELF image (Wine only)
219 PFINDFILEINPATHCALLBACK cb;
223 static BOOL CALLBACK sffip_cb(LPCSTR buffer, void* user)
225 struct sffip* s = (struct sffip*)user;
226 DWORD size, checksum;
228 /* FIXME: should check that id/two/three match the file pointed
238 timestamp = ~(DWORD_PTR)s->id;
240 hFile = CreateFileA(buffer, GENERIC_READ, FILE_SHARE_READ, NULL,
241 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
242 if (hFile == INVALID_HANDLE_VALUE) return TRUE;
243 if ((hMap = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != NULL)
245 if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
247 IMAGE_NT_HEADERS* nth = RtlImageNtHeader(mapping);
248 timestamp = nth->FileHeader.TimeDateStamp;
249 size = nth->OptionalHeader.SizeOfImage;
250 UnmapViewOfFile(mapping);
255 if (timestamp != (DWORD_PTR)s->id || size != s->two)
257 WARN("Found %s, but wrong size or timestamp\n", buffer);
263 if (elf_fetch_file_info(buffer, 0, &size, &checksum))
265 if (checksum != (DWORD_PTR)s->id)
267 WARN("Found %s, but wrong checksums: %08lx %08lx\n",
268 buffer, checksum, (DWORD_PTR)s->id);
274 WARN("Couldn't read %s\n", buffer);
280 FIXME("NIY on '%s'\n", buffer);
283 FIXME("What the heck??\n");
286 /* yes, EnumDirTree/do_search and SymFindFileInPath callbacks use the opposite
287 * convention to stop/continue enumeration. sigh.
289 return !(s->cb)((char*)buffer, s->user);
292 /******************************************************************
293 * SymFindFileInPath (DBGHELP.@)
296 BOOL WINAPI SymFindFileInPath(HANDLE hProcess, PCSTR searchPath, PCSTR full_path,
297 PVOID id, DWORD two, DWORD three, DWORD flags,
298 LPSTR buffer, PFINDFILEINPATHCALLBACK cb,
302 struct process* pcs = process_find_by_handle(hProcess);
305 const char* filename;
307 TRACE("(%p %s %s %p %08lx %08lx %08lx %p %p %p)\n",
308 hProcess, searchPath, full_path, id, two, three, flags,
311 if (!pcs) return FALSE;
312 if (!searchPath) searchPath = pcs->search_path;
321 filename = file_name(full_path);
322 s.kind = module_get_type_by_name(filename);
324 /* first check full path to file */
325 if (sffip_cb(full_path, &s))
327 strcpy(buffer, full_path);
333 ptr = strchr(searchPath, ';');
336 memcpy(tmp, searchPath, ptr - searchPath);
337 tmp[ptr - searchPath] = 0;
338 searchPath = ptr + 1;
342 strcpy(tmp, searchPath);
345 if (do_search(filename, tmp, FALSE, sffip_cb, &s)) {