Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- FileSpec.cpp --------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 11 | #ifndef _WIN32 |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 12 | #include <dirent.h> |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 13 | #else |
| 14 | #include "lldb/Host/windows/windows.h" |
| 15 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include <fcntl.h> |
Virgile Bello | 6957195 | 2013-09-20 22:35:22 +0000 | [diff] [blame] | 17 | #ifndef _MSC_VER |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include <libgen.h> |
Virgile Bello | 6957195 | 2013-09-20 22:35:22 +0000 | [diff] [blame] | 19 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include <sys/stat.h> |
Rafael Espindola | 0907916 | 2013-06-13 20:10:23 +0000 | [diff] [blame] | 21 | #include <set> |
Greg Clayton | e0f3c02 | 2011-02-07 17:41:11 +0000 | [diff] [blame] | 22 | #include <string.h> |
Jim Ingham | 9035e7c | 2011-02-07 19:42:39 +0000 | [diff] [blame] | 23 | #include <fstream> |
Greg Clayton | fd18426 | 2011-02-05 02:27:52 +0000 | [diff] [blame] | 24 | |
Jim Ingham | 9035e7c | 2011-02-07 19:42:39 +0000 | [diff] [blame] | 25 | #include "lldb/Host/Config.h" // Have to include this before we test the define... |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 26 | #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 27 | #include <pwd.h> |
Greg Clayton | fd18426 | 2011-02-05 02:27:52 +0000 | [diff] [blame] | 28 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringRef.h" |
Greg Clayton | 38a6140 | 2010-12-02 23:20:03 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Path.h" |
| 32 | #include "llvm/Support/Program.h" |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 33 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 34 | #include "lldb/Core/StreamString.h" |
Greg Clayton | 96c0968 | 2012-01-04 22:56:43 +0000 | [diff] [blame] | 35 | #include "lldb/Host/File.h" |
Greg Clayton | 53239f0 | 2011-02-08 05:05:52 +0000 | [diff] [blame] | 36 | #include "lldb/Host/FileSpec.h" |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 37 | #include "lldb/Host/Host.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | #include "lldb/Core/DataBufferHeap.h" |
| 39 | #include "lldb/Core/DataBufferMemoryMap.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 40 | #include "lldb/Core/RegularExpression.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | #include "lldb/Core/Stream.h" |
Caroline Tice | 428a9a5 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 42 | #include "lldb/Host/Host.h" |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 43 | #include "lldb/Utility/CleanUp.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | |
| 45 | using namespace lldb; |
| 46 | using namespace lldb_private; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | |
| 48 | static bool |
| 49 | GetFileStats (const FileSpec *file_spec, struct stat *stats_ptr) |
| 50 | { |
| 51 | char resolved_path[PATH_MAX]; |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 52 | if (file_spec->GetPath (resolved_path, sizeof(resolved_path))) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | return ::stat (resolved_path, stats_ptr) == 0; |
| 54 | return false; |
| 55 | } |
| 56 | |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 57 | #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
Greg Clayton | fd18426 | 2011-02-05 02:27:52 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 59 | static const char* |
| 60 | GetCachedGlobTildeSlash() |
| 61 | { |
| 62 | static std::string g_tilde; |
| 63 | if (g_tilde.empty()) |
| 64 | { |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 65 | struct passwd *user_entry; |
| 66 | user_entry = getpwuid(geteuid()); |
| 67 | if (user_entry != NULL) |
| 68 | g_tilde = user_entry->pw_dir; |
| 69 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | if (g_tilde.empty()) |
| 71 | return NULL; |
| 72 | } |
| 73 | return g_tilde.c_str(); |
| 74 | } |
| 75 | |
Greg Clayton | 87e5ff0 | 2011-02-08 05:19:06 +0000 | [diff] [blame] | 76 | #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
| 77 | |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 78 | // Resolves the username part of a path of the form ~user/other/directories, and |
| 79 | // writes the result into dst_path. |
| 80 | // Returns 0 if there WAS a ~ in the path but the username couldn't be resolved. |
| 81 | // Otherwise returns the number of characters copied into dst_path. If the return |
| 82 | // is >= dst_len, then the resolved path is too long... |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 83 | size_t |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 84 | FileSpec::ResolveUsername (const char *src_path, char *dst_path, size_t dst_len) |
| 85 | { |
Greg Clayton | 87e5ff0 | 2011-02-08 05:19:06 +0000 | [diff] [blame] | 86 | if (src_path == NULL || src_path[0] == '\0') |
| 87 | return 0; |
| 88 | |
| 89 | #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
| 90 | |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 91 | char user_home[PATH_MAX]; |
| 92 | const char *user_name; |
| 93 | |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 94 | |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 95 | // If there's no ~, then just copy src_path straight to dst_path (they may be the same string...) |
| 96 | if (src_path[0] != '~') |
| 97 | { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 98 | size_t len = strlen (src_path); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 99 | if (len >= dst_len) |
| 100 | { |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 101 | ::bcopy (src_path, dst_path, dst_len - 1); |
| 102 | dst_path[dst_len] = '\0'; |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 103 | } |
| 104 | else |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 105 | ::bcopy (src_path, dst_path, len + 1); |
| 106 | |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 107 | return len; |
| 108 | } |
| 109 | |
Eli Friedman | feaeebf | 2010-07-02 19:15:50 +0000 | [diff] [blame] | 110 | const char *first_slash = ::strchr (src_path, '/'); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 111 | char remainder[PATH_MAX]; |
| 112 | |
| 113 | if (first_slash == NULL) |
| 114 | { |
| 115 | // The whole name is the username (minus the ~): |
| 116 | user_name = src_path + 1; |
| 117 | remainder[0] = '\0'; |
| 118 | } |
| 119 | else |
| 120 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 121 | size_t user_name_len = first_slash - src_path - 1; |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 122 | ::memcpy (user_home, src_path + 1, user_name_len); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 123 | user_home[user_name_len] = '\0'; |
| 124 | user_name = user_home; |
| 125 | |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 126 | ::strcpy (remainder, first_slash); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 127 | } |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 128 | |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 129 | if (user_name == NULL) |
| 130 | return 0; |
| 131 | // User name of "" means the current user... |
| 132 | |
| 133 | struct passwd *user_entry; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 134 | const char *home_dir = NULL; |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 135 | |
| 136 | if (user_name[0] == '\0') |
| 137 | { |
| 138 | home_dir = GetCachedGlobTildeSlash(); |
| 139 | } |
| 140 | else |
| 141 | { |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 142 | user_entry = ::getpwnam (user_name); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 143 | if (user_entry != NULL) |
| 144 | home_dir = user_entry->pw_dir; |
| 145 | } |
| 146 | |
| 147 | if (home_dir == NULL) |
| 148 | return 0; |
| 149 | else |
| 150 | return ::snprintf (dst_path, dst_len, "%s%s", home_dir, remainder); |
Greg Clayton | 87e5ff0 | 2011-02-08 05:19:06 +0000 | [diff] [blame] | 151 | #else |
| 152 | // Resolving home directories is not supported, just copy the path... |
| 153 | return ::snprintf (dst_path, dst_len, "%s", src_path); |
| 154 | #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 157 | size_t |
Jim Ingham | 8436307 | 2011-02-08 23:24:09 +0000 | [diff] [blame] | 158 | FileSpec::ResolvePartialUsername (const char *partial_name, StringList &matches) |
| 159 | { |
| 160 | #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
| 161 | size_t extant_entries = matches.GetSize(); |
| 162 | |
| 163 | setpwent(); |
| 164 | struct passwd *user_entry; |
| 165 | const char *name_start = partial_name + 1; |
| 166 | std::set<std::string> name_list; |
| 167 | |
| 168 | while ((user_entry = getpwent()) != NULL) |
| 169 | { |
| 170 | if (strstr(user_entry->pw_name, name_start) == user_entry->pw_name) |
| 171 | { |
| 172 | std::string tmp_buf("~"); |
| 173 | tmp_buf.append(user_entry->pw_name); |
| 174 | tmp_buf.push_back('/'); |
| 175 | name_list.insert(tmp_buf); |
| 176 | } |
| 177 | } |
| 178 | std::set<std::string>::iterator pos, end = name_list.end(); |
| 179 | for (pos = name_list.begin(); pos != end; pos++) |
| 180 | { |
| 181 | matches.AppendString((*pos).c_str()); |
| 182 | } |
| 183 | return matches.GetSize() - extant_entries; |
| 184 | #else |
| 185 | // Resolving home directories is not supported, just copy the path... |
| 186 | return 0; |
| 187 | #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
| 188 | } |
| 189 | |
| 190 | |
| 191 | |
| 192 | size_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 193 | FileSpec::Resolve (const char *src_path, char *dst_path, size_t dst_len) |
| 194 | { |
| 195 | if (src_path == NULL || src_path[0] == '\0') |
| 196 | return 0; |
| 197 | |
| 198 | // Glob if needed for ~/, otherwise copy in case src_path is same as dst_path... |
| 199 | char unglobbed_path[PATH_MAX]; |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 200 | #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 201 | if (src_path[0] == '~') |
| 202 | { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 203 | size_t return_count = ResolveUsername(src_path, unglobbed_path, sizeof(unglobbed_path)); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 204 | |
| 205 | // If we couldn't find the user referred to, or the resultant path was too long, |
| 206 | // then just copy over the src_path. |
| 207 | if (return_count == 0 || return_count >= sizeof(unglobbed_path)) |
| 208 | ::snprintf (unglobbed_path, sizeof(unglobbed_path), "%s", src_path); |
| 209 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 210 | else |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 211 | #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
Greg Clayton | fd18426 | 2011-02-05 02:27:52 +0000 | [diff] [blame] | 212 | { |
| 213 | ::snprintf(unglobbed_path, sizeof(unglobbed_path), "%s", src_path); |
| 214 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 215 | |
| 216 | // Now resolve the path if needed |
| 217 | char resolved_path[PATH_MAX]; |
| 218 | if (::realpath (unglobbed_path, resolved_path)) |
| 219 | { |
| 220 | // Success, copy the resolved path |
| 221 | return ::snprintf(dst_path, dst_len, "%s", resolved_path); |
| 222 | } |
| 223 | else |
| 224 | { |
| 225 | // Failed, just copy the unglobbed path |
| 226 | return ::snprintf(dst_path, dst_len, "%s", unglobbed_path); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | FileSpec::FileSpec() : |
| 231 | m_directory(), |
| 232 | m_filename() |
| 233 | { |
| 234 | } |
| 235 | |
| 236 | //------------------------------------------------------------------ |
| 237 | // Default constructor that can take an optional full path to a |
| 238 | // file on disk. |
| 239 | //------------------------------------------------------------------ |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 240 | FileSpec::FileSpec(const char *pathname, bool resolve_path) : |
| 241 | m_directory(), |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 242 | m_filename(), |
| 243 | m_is_resolved(false) |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 244 | { |
| 245 | if (pathname && pathname[0]) |
| 246 | SetFile(pathname, resolve_path); |
| 247 | } |
| 248 | |
| 249 | //------------------------------------------------------------------ |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 250 | // Copy constructor |
| 251 | //------------------------------------------------------------------ |
| 252 | FileSpec::FileSpec(const FileSpec& rhs) : |
| 253 | m_directory (rhs.m_directory), |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 254 | m_filename (rhs.m_filename), |
| 255 | m_is_resolved (rhs.m_is_resolved) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 256 | { |
| 257 | } |
| 258 | |
| 259 | //------------------------------------------------------------------ |
| 260 | // Copy constructor |
| 261 | //------------------------------------------------------------------ |
| 262 | FileSpec::FileSpec(const FileSpec* rhs) : |
| 263 | m_directory(), |
| 264 | m_filename() |
| 265 | { |
| 266 | if (rhs) |
| 267 | *this = *rhs; |
| 268 | } |
| 269 | |
| 270 | //------------------------------------------------------------------ |
| 271 | // Virtual destrcuctor in case anyone inherits from this class. |
| 272 | //------------------------------------------------------------------ |
| 273 | FileSpec::~FileSpec() |
| 274 | { |
| 275 | } |
| 276 | |
| 277 | //------------------------------------------------------------------ |
| 278 | // Assignment operator. |
| 279 | //------------------------------------------------------------------ |
| 280 | const FileSpec& |
| 281 | FileSpec::operator= (const FileSpec& rhs) |
| 282 | { |
| 283 | if (this != &rhs) |
| 284 | { |
| 285 | m_directory = rhs.m_directory; |
| 286 | m_filename = rhs.m_filename; |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 287 | m_is_resolved = rhs.m_is_resolved; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 288 | } |
| 289 | return *this; |
| 290 | } |
| 291 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 292 | //------------------------------------------------------------------ |
| 293 | // Update the contents of this object with a new path. The path will |
| 294 | // be split up into a directory and filename and stored as uniqued |
| 295 | // string values for quick comparison and efficient memory usage. |
| 296 | //------------------------------------------------------------------ |
| 297 | void |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 298 | FileSpec::SetFile (const char *pathname, bool resolve) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 299 | { |
| 300 | m_filename.Clear(); |
| 301 | m_directory.Clear(); |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 302 | m_is_resolved = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 303 | if (pathname == NULL || pathname[0] == '\0') |
| 304 | return; |
| 305 | |
| 306 | char resolved_path[PATH_MAX]; |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 307 | bool path_fit = true; |
| 308 | |
| 309 | if (resolve) |
| 310 | { |
| 311 | path_fit = (FileSpec::Resolve (pathname, resolved_path, sizeof(resolved_path)) < sizeof(resolved_path) - 1); |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 312 | m_is_resolved = path_fit; |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 313 | } |
| 314 | else |
| 315 | { |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 316 | // Copy the path because "basename" and "dirname" want to muck with the |
| 317 | // path buffer |
| 318 | if (::strlen (pathname) > sizeof(resolved_path) - 1) |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 319 | path_fit = false; |
| 320 | else |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 321 | ::strcpy (resolved_path, pathname); |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 322 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 323 | |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 324 | |
| 325 | if (path_fit) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 326 | { |
| 327 | char *filename = ::basename (resolved_path); |
| 328 | if (filename) |
| 329 | { |
| 330 | m_filename.SetCString (filename); |
| 331 | // Truncate the basename off the end of the resolved path |
| 332 | |
| 333 | // Only attempt to get the dirname if it looks like we have a path |
Virgile Bello | 1fd7ec7 | 2013-09-20 22:31:10 +0000 | [diff] [blame] | 334 | if (strchr(resolved_path, '/') |
| 335 | #ifdef _WIN32 |
| 336 | || strchr(resolved_path, '\\') |
| 337 | #endif |
| 338 | ) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 339 | { |
| 340 | char *directory = ::dirname (resolved_path); |
| 341 | |
| 342 | // Make sure we didn't get our directory resolved to "." without having |
| 343 | // specified |
| 344 | if (directory) |
| 345 | m_directory.SetCString(directory); |
| 346 | else |
| 347 | { |
| 348 | char *last_resolved_path_slash = strrchr(resolved_path, '/'); |
Virgile Bello | 1fd7ec7 | 2013-09-20 22:31:10 +0000 | [diff] [blame] | 349 | #ifdef _WIN32 |
| 350 | char* last_resolved_path_slash_windows = strrchr(resolved_path, '\\'); |
| 351 | if (last_resolved_path_slash_windows > last_resolved_path_slash) |
| 352 | last_resolved_path_slash = last_resolved_path_slash_windows; |
| 353 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 354 | if (last_resolved_path_slash) |
| 355 | { |
| 356 | *last_resolved_path_slash = '\0'; |
| 357 | m_directory.SetCString(resolved_path); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | else |
| 363 | m_directory.SetCString(resolved_path); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | //---------------------------------------------------------------------- |
| 368 | // Convert to pointer operator. This allows code to check any FileSpec |
| 369 | // objects to see if they contain anything valid using code such as: |
| 370 | // |
| 371 | // if (file_spec) |
| 372 | // {} |
| 373 | //---------------------------------------------------------------------- |
Greg Clayton | 6372d1c | 2011-09-12 04:00:42 +0000 | [diff] [blame] | 374 | FileSpec::operator bool() const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 375 | { |
Greg Clayton | 6372d1c | 2011-09-12 04:00:42 +0000 | [diff] [blame] | 376 | return m_filename || m_directory; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | //---------------------------------------------------------------------- |
| 380 | // Logical NOT operator. This allows code to check any FileSpec |
| 381 | // objects to see if they are invalid using code such as: |
| 382 | // |
| 383 | // if (!file_spec) |
| 384 | // {} |
| 385 | //---------------------------------------------------------------------- |
| 386 | bool |
| 387 | FileSpec::operator!() const |
| 388 | { |
| 389 | return !m_directory && !m_filename; |
| 390 | } |
| 391 | |
| 392 | //------------------------------------------------------------------ |
| 393 | // Equal to operator |
| 394 | //------------------------------------------------------------------ |
| 395 | bool |
| 396 | FileSpec::operator== (const FileSpec& rhs) const |
| 397 | { |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 398 | if (m_filename == rhs.m_filename) |
| 399 | { |
| 400 | if (m_directory == rhs.m_directory) |
| 401 | return true; |
| 402 | |
| 403 | // TODO: determine if we want to keep this code in here. |
| 404 | // The code below was added to handle a case where we were |
| 405 | // trying to set a file and line breakpoint and one path |
| 406 | // was resolved, and the other not and the directory was |
| 407 | // in a mount point that resolved to a more complete path: |
| 408 | // "/tmp/a.c" == "/private/tmp/a.c". I might end up pulling |
| 409 | // this out... |
| 410 | if (IsResolved() && rhs.IsResolved()) |
| 411 | { |
| 412 | // Both paths are resolved, no need to look further... |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | FileSpec resolved_lhs(*this); |
| 417 | |
| 418 | // If "this" isn't resolved, resolve it |
| 419 | if (!IsResolved()) |
| 420 | { |
| 421 | if (resolved_lhs.ResolvePath()) |
| 422 | { |
| 423 | // This path wasn't resolved but now it is. Check if the resolved |
| 424 | // directory is the same as our unresolved directory, and if so, |
| 425 | // we can mark this object as resolved to avoid more future resolves |
| 426 | m_is_resolved = (m_directory == resolved_lhs.m_directory); |
| 427 | } |
| 428 | else |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | FileSpec resolved_rhs(rhs); |
| 433 | if (!rhs.IsResolved()) |
| 434 | { |
| 435 | if (resolved_rhs.ResolvePath()) |
| 436 | { |
| 437 | // rhs's path wasn't resolved but now it is. Check if the resolved |
| 438 | // directory is the same as rhs's unresolved directory, and if so, |
| 439 | // we can mark this object as resolved to avoid more future resolves |
Jim Ingham | a537f6c | 2012-11-03 02:12:46 +0000 | [diff] [blame] | 440 | rhs.m_is_resolved = (rhs.m_directory == resolved_rhs.m_directory); |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 441 | } |
| 442 | else |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | // If we reach this point in the code we were able to resolve both paths |
| 447 | // and since we only resolve the paths if the basenames are equal, then |
| 448 | // we can just check if both directories are equal... |
| 449 | return resolved_lhs.GetDirectory() == resolved_rhs.GetDirectory(); |
| 450 | } |
| 451 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | //------------------------------------------------------------------ |
| 455 | // Not equal to operator |
| 456 | //------------------------------------------------------------------ |
| 457 | bool |
| 458 | FileSpec::operator!= (const FileSpec& rhs) const |
| 459 | { |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 460 | return !(*this == rhs); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | //------------------------------------------------------------------ |
| 464 | // Less than operator |
| 465 | //------------------------------------------------------------------ |
| 466 | bool |
| 467 | FileSpec::operator< (const FileSpec& rhs) const |
| 468 | { |
| 469 | return FileSpec::Compare(*this, rhs, true) < 0; |
| 470 | } |
| 471 | |
| 472 | //------------------------------------------------------------------ |
| 473 | // Dump a FileSpec object to a stream |
| 474 | //------------------------------------------------------------------ |
| 475 | Stream& |
| 476 | lldb_private::operator << (Stream &s, const FileSpec& f) |
| 477 | { |
| 478 | f.Dump(&s); |
| 479 | return s; |
| 480 | } |
| 481 | |
| 482 | //------------------------------------------------------------------ |
| 483 | // Clear this object by releasing both the directory and filename |
| 484 | // string values and making them both the empty string. |
| 485 | //------------------------------------------------------------------ |
| 486 | void |
| 487 | FileSpec::Clear() |
| 488 | { |
| 489 | m_directory.Clear(); |
| 490 | m_filename.Clear(); |
| 491 | } |
| 492 | |
| 493 | //------------------------------------------------------------------ |
| 494 | // Compare two FileSpec objects. If "full" is true, then both |
| 495 | // the directory and the filename must match. If "full" is false, |
| 496 | // then the directory names for "a" and "b" are only compared if |
| 497 | // they are both non-empty. This allows a FileSpec object to only |
| 498 | // contain a filename and it can match FileSpec objects that have |
| 499 | // matching filenames with different paths. |
| 500 | // |
| 501 | // Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" |
| 502 | // and "1" if "a" is greater than "b". |
| 503 | //------------------------------------------------------------------ |
| 504 | int |
| 505 | FileSpec::Compare(const FileSpec& a, const FileSpec& b, bool full) |
| 506 | { |
| 507 | int result = 0; |
| 508 | |
| 509 | // If full is true, then we must compare both the directory and filename. |
| 510 | |
| 511 | // If full is false, then if either directory is empty, then we match on |
| 512 | // the basename only, and if both directories have valid values, we still |
| 513 | // do a full compare. This allows for matching when we just have a filename |
| 514 | // in one of the FileSpec objects. |
| 515 | |
| 516 | if (full || (a.m_directory && b.m_directory)) |
| 517 | { |
| 518 | result = ConstString::Compare(a.m_directory, b.m_directory); |
| 519 | if (result) |
| 520 | return result; |
| 521 | } |
| 522 | return ConstString::Compare (a.m_filename, b.m_filename); |
| 523 | } |
| 524 | |
| 525 | bool |
| 526 | FileSpec::Equal (const FileSpec& a, const FileSpec& b, bool full) |
| 527 | { |
Jim Ingham | 87df91b | 2011-09-23 00:54:11 +0000 | [diff] [blame] | 528 | if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty())) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 529 | return a.m_filename == b.m_filename; |
Jim Ingham | 87df91b | 2011-09-23 00:54:11 +0000 | [diff] [blame] | 530 | else |
| 531 | return a == b; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | |
| 535 | |
| 536 | //------------------------------------------------------------------ |
| 537 | // Dump the object to the supplied stream. If the object contains |
| 538 | // a valid directory name, it will be displayed followed by a |
| 539 | // directory delimiter, and the filename. |
| 540 | //------------------------------------------------------------------ |
| 541 | void |
| 542 | FileSpec::Dump(Stream *s) const |
| 543 | { |
Jason Molenda | db7d11c | 2013-05-06 10:21:11 +0000 | [diff] [blame] | 544 | static ConstString g_slash_only ("/"); |
Enrico Granata | 80fcdd4 | 2012-11-03 00:09:46 +0000 | [diff] [blame] | 545 | if (s) |
| 546 | { |
| 547 | m_directory.Dump(s); |
Jason Molenda | db7d11c | 2013-05-06 10:21:11 +0000 | [diff] [blame] | 548 | if (m_directory && m_directory != g_slash_only) |
Enrico Granata | 80fcdd4 | 2012-11-03 00:09:46 +0000 | [diff] [blame] | 549 | s->PutChar('/'); |
| 550 | m_filename.Dump(s); |
| 551 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | //------------------------------------------------------------------ |
| 555 | // Returns true if the file exists. |
| 556 | //------------------------------------------------------------------ |
| 557 | bool |
| 558 | FileSpec::Exists () const |
| 559 | { |
| 560 | struct stat file_stats; |
| 561 | return GetFileStats (this, &file_stats); |
| 562 | } |
| 563 | |
Caroline Tice | 428a9a5 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 564 | bool |
| 565 | FileSpec::ResolveExecutableLocation () |
| 566 | { |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 567 | if (!m_directory) |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 568 | { |
Greg Clayton | 58f4171 | 2011-01-25 21:32:01 +0000 | [diff] [blame] | 569 | const char *file_cstr = m_filename.GetCString(); |
| 570 | if (file_cstr) |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 571 | { |
Greg Clayton | 58f4171 | 2011-01-25 21:32:01 +0000 | [diff] [blame] | 572 | const std::string file_str (file_cstr); |
Rafael Espindola | ff89ff2 | 2013-06-13 19:25:41 +0000 | [diff] [blame] | 573 | std::string path = llvm::sys::FindProgramByName (file_str); |
| 574 | llvm::StringRef dir_ref = llvm::sys::path::parent_path(path); |
Greg Clayton | 58f4171 | 2011-01-25 21:32:01 +0000 | [diff] [blame] | 575 | //llvm::StringRef dir_ref = path.getDirname(); |
| 576 | if (! dir_ref.empty()) |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 577 | { |
Greg Clayton | 58f4171 | 2011-01-25 21:32:01 +0000 | [diff] [blame] | 578 | // FindProgramByName returns "." if it can't find the file. |
| 579 | if (strcmp (".", dir_ref.data()) == 0) |
| 580 | return false; |
| 581 | |
| 582 | m_directory.SetCString (dir_ref.data()); |
| 583 | if (Exists()) |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 584 | return true; |
Greg Clayton | 58f4171 | 2011-01-25 21:32:01 +0000 | [diff] [blame] | 585 | else |
| 586 | { |
| 587 | // If FindProgramByName found the file, it returns the directory + filename in its return results. |
| 588 | // We need to separate them. |
| 589 | FileSpec tmp_file (dir_ref.data(), false); |
| 590 | if (tmp_file.Exists()) |
| 591 | { |
| 592 | m_directory = tmp_file.m_directory; |
| 593 | return true; |
| 594 | } |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | return false; |
Caroline Tice | 428a9a5 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 603 | bool |
| 604 | FileSpec::ResolvePath () |
| 605 | { |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 606 | if (m_is_resolved) |
| 607 | return true; // We have already resolved this path |
| 608 | |
| 609 | char path_buf[PATH_MAX]; |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 610 | if (!GetPath (path_buf, PATH_MAX)) |
| 611 | return false; |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 612 | // SetFile(...) will set m_is_resolved correctly if it can resolve the path |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 613 | SetFile (path_buf, true); |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 614 | return m_is_resolved; |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 617 | uint64_t |
| 618 | FileSpec::GetByteSize() const |
| 619 | { |
| 620 | struct stat file_stats; |
| 621 | if (GetFileStats (this, &file_stats)) |
| 622 | return file_stats.st_size; |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | FileSpec::FileType |
| 627 | FileSpec::GetFileType () const |
| 628 | { |
| 629 | struct stat file_stats; |
| 630 | if (GetFileStats (this, &file_stats)) |
| 631 | { |
| 632 | mode_t file_type = file_stats.st_mode & S_IFMT; |
| 633 | switch (file_type) |
| 634 | { |
| 635 | case S_IFDIR: return eFileTypeDirectory; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 636 | case S_IFREG: return eFileTypeRegular; |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 637 | #ifndef _WIN32 |
| 638 | case S_IFIFO: return eFileTypePipe; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 639 | case S_IFSOCK: return eFileTypeSocket; |
| 640 | case S_IFLNK: return eFileTypeSymbolicLink; |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 641 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 642 | default: |
| 643 | break; |
| 644 | } |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 645 | return eFileTypeUnknown; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 646 | } |
| 647 | return eFileTypeInvalid; |
| 648 | } |
| 649 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 650 | uint32_t |
| 651 | FileSpec::GetPermissions () const |
| 652 | { |
| 653 | uint32_t file_permissions = 0; |
| 654 | if (*this) |
| 655 | Host::GetFilePermissions(GetPath().c_str(), file_permissions); |
| 656 | return file_permissions; |
| 657 | } |
| 658 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 659 | TimeValue |
| 660 | FileSpec::GetModificationTime () const |
| 661 | { |
| 662 | TimeValue mod_time; |
| 663 | struct stat file_stats; |
| 664 | if (GetFileStats (this, &file_stats)) |
Eli Friedman | 6abb634 | 2010-06-11 04:52:22 +0000 | [diff] [blame] | 665 | mod_time.OffsetWithSeconds(file_stats.st_mtime); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 666 | return mod_time; |
| 667 | } |
| 668 | |
| 669 | //------------------------------------------------------------------ |
| 670 | // Directory string get accessor. |
| 671 | //------------------------------------------------------------------ |
| 672 | ConstString & |
| 673 | FileSpec::GetDirectory() |
| 674 | { |
| 675 | return m_directory; |
| 676 | } |
| 677 | |
| 678 | //------------------------------------------------------------------ |
| 679 | // Directory string const get accessor. |
| 680 | //------------------------------------------------------------------ |
| 681 | const ConstString & |
| 682 | FileSpec::GetDirectory() const |
| 683 | { |
| 684 | return m_directory; |
| 685 | } |
| 686 | |
| 687 | //------------------------------------------------------------------ |
| 688 | // Filename string get accessor. |
| 689 | //------------------------------------------------------------------ |
| 690 | ConstString & |
| 691 | FileSpec::GetFilename() |
| 692 | { |
| 693 | return m_filename; |
| 694 | } |
| 695 | |
| 696 | //------------------------------------------------------------------ |
| 697 | // Filename string const get accessor. |
| 698 | //------------------------------------------------------------------ |
| 699 | const ConstString & |
| 700 | FileSpec::GetFilename() const |
| 701 | { |
| 702 | return m_filename; |
| 703 | } |
| 704 | |
| 705 | //------------------------------------------------------------------ |
| 706 | // Extract the directory and path into a fixed buffer. This is |
| 707 | // needed as the directory and path are stored in separate string |
| 708 | // values. |
| 709 | //------------------------------------------------------------------ |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 710 | size_t |
| 711 | FileSpec::GetPath(char *path, size_t path_max_len) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 712 | { |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 713 | if (path_max_len) |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 714 | { |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 715 | const char *dirname = m_directory.GetCString(); |
| 716 | const char *filename = m_filename.GetCString(); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 717 | if (dirname) |
| 718 | { |
| 719 | if (filename) |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 720 | return ::snprintf (path, path_max_len, "%s/%s", dirname, filename); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 721 | else |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 722 | return ::snprintf (path, path_max_len, "%s", dirname); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 723 | } |
| 724 | else if (filename) |
| 725 | { |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 726 | return ::snprintf (path, path_max_len, "%s", filename); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 727 | } |
| 728 | } |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 729 | if (path) |
| 730 | path[0] = '\0'; |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 731 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Greg Clayton | a44c1e6 | 2013-04-29 16:36:27 +0000 | [diff] [blame] | 734 | std::string |
| 735 | FileSpec::GetPath (void) const |
Jason Molenda | a7ae467 | 2013-04-29 09:46:43 +0000 | [diff] [blame] | 736 | { |
Jason Molenda | db7d11c | 2013-05-06 10:21:11 +0000 | [diff] [blame] | 737 | static ConstString g_slash_only ("/"); |
Greg Clayton | a44c1e6 | 2013-04-29 16:36:27 +0000 | [diff] [blame] | 738 | std::string path; |
Jason Molenda | a7ae467 | 2013-04-29 09:46:43 +0000 | [diff] [blame] | 739 | const char *dirname = m_directory.GetCString(); |
| 740 | const char *filename = m_filename.GetCString(); |
Jason Molenda | a7ae467 | 2013-04-29 09:46:43 +0000 | [diff] [blame] | 741 | if (dirname) |
| 742 | { |
| 743 | path.append (dirname); |
Jason Molenda | db7d11c | 2013-05-06 10:21:11 +0000 | [diff] [blame] | 744 | if (filename && m_directory != g_slash_only) |
Jason Molenda | a7ae467 | 2013-04-29 09:46:43 +0000 | [diff] [blame] | 745 | path.append ("/"); |
| 746 | } |
| 747 | if (filename) |
Jason Molenda | a7ae467 | 2013-04-29 09:46:43 +0000 | [diff] [blame] | 748 | path.append (filename); |
Jason Molenda | a7ae467 | 2013-04-29 09:46:43 +0000 | [diff] [blame] | 749 | return path; |
| 750 | } |
| 751 | |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 752 | ConstString |
| 753 | FileSpec::GetFileNameExtension () const |
| 754 | { |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 755 | if (m_filename) |
| 756 | { |
| 757 | const char *filename = m_filename.GetCString(); |
| 758 | const char* dot_pos = strrchr(filename, '.'); |
| 759 | if (dot_pos && dot_pos[1] != '\0') |
| 760 | return ConstString(dot_pos+1); |
| 761 | } |
| 762 | return ConstString(); |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | ConstString |
| 766 | FileSpec::GetFileNameStrippingExtension () const |
| 767 | { |
| 768 | const char *filename = m_filename.GetCString(); |
| 769 | if (filename == NULL) |
| 770 | return ConstString(); |
| 771 | |
Johnny Chen | f5df537 | 2011-10-18 19:28:30 +0000 | [diff] [blame] | 772 | const char* dot_pos = strrchr(filename, '.'); |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 773 | if (dot_pos == NULL) |
| 774 | return m_filename; |
| 775 | |
| 776 | return ConstString(filename, dot_pos-filename); |
| 777 | } |
| 778 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 779 | //------------------------------------------------------------------ |
| 780 | // Returns a shared pointer to a data buffer that contains all or |
| 781 | // part of the contents of a file. The data is memory mapped and |
| 782 | // will lazily page in data from the file as memory is accessed. |
| 783 | // The data that is mappped will start "file_offset" bytes into the |
| 784 | // file, and "file_size" bytes will be mapped. If "file_size" is |
| 785 | // greater than the number of bytes available in the file starting |
| 786 | // at "file_offset", the number of bytes will be appropriately |
| 787 | // truncated. The final number of bytes that get mapped can be |
| 788 | // verified using the DataBuffer::GetByteSize() function. |
| 789 | //------------------------------------------------------------------ |
| 790 | DataBufferSP |
| 791 | FileSpec::MemoryMapFileContents(off_t file_offset, size_t file_size) const |
| 792 | { |
| 793 | DataBufferSP data_sp; |
Greg Clayton | 7b0992d | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 794 | std::unique_ptr<DataBufferMemoryMap> mmap_data(new DataBufferMemoryMap()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 795 | if (mmap_data.get()) |
| 796 | { |
Greg Clayton | d398a1c | 2013-04-20 00:23:26 +0000 | [diff] [blame] | 797 | const size_t mapped_length = mmap_data->MemoryMapFromFileSpec (this, file_offset, file_size); |
| 798 | if (((file_size == SIZE_MAX) && (mapped_length > 0)) || (mapped_length >= file_size)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 799 | data_sp.reset(mmap_data.release()); |
| 800 | } |
| 801 | return data_sp; |
| 802 | } |
| 803 | |
| 804 | |
| 805 | //------------------------------------------------------------------ |
| 806 | // Return the size in bytes that this object takes in memory. This |
| 807 | // returns the size in bytes of this object, not any shared string |
| 808 | // values it may refer to. |
| 809 | //------------------------------------------------------------------ |
| 810 | size_t |
| 811 | FileSpec::MemorySize() const |
| 812 | { |
| 813 | return m_filename.MemorySize() + m_directory.MemorySize(); |
| 814 | } |
| 815 | |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 816 | |
| 817 | size_t |
Greg Clayton | 4017fa3 | 2012-01-06 02:01:06 +0000 | [diff] [blame] | 818 | FileSpec::ReadFileContents (off_t file_offset, void *dst, size_t dst_len, Error *error_ptr) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 819 | { |
Greg Clayton | 4017fa3 | 2012-01-06 02:01:06 +0000 | [diff] [blame] | 820 | Error error; |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 821 | size_t bytes_read = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 822 | char resolved_path[PATH_MAX]; |
| 823 | if (GetPath(resolved_path, sizeof(resolved_path))) |
| 824 | { |
Greg Clayton | 96c0968 | 2012-01-04 22:56:43 +0000 | [diff] [blame] | 825 | File file; |
| 826 | error = file.Open(resolved_path, File::eOpenOptionRead); |
| 827 | if (error.Success()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 828 | { |
Greg Clayton | 96c0968 | 2012-01-04 22:56:43 +0000 | [diff] [blame] | 829 | off_t file_offset_after_seek = file_offset; |
| 830 | bytes_read = dst_len; |
| 831 | error = file.Read(dst, bytes_read, file_offset_after_seek); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 832 | } |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 833 | } |
Greg Clayton | 4017fa3 | 2012-01-06 02:01:06 +0000 | [diff] [blame] | 834 | else |
| 835 | { |
| 836 | error.SetErrorString("invalid file specification"); |
| 837 | } |
| 838 | if (error_ptr) |
| 839 | *error_ptr = error; |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 840 | return bytes_read; |
| 841 | } |
| 842 | |
| 843 | //------------------------------------------------------------------ |
| 844 | // Returns a shared pointer to a data buffer that contains all or |
| 845 | // part of the contents of a file. The data copies into a heap based |
| 846 | // buffer that lives in the DataBuffer shared pointer object returned. |
| 847 | // The data that is cached will start "file_offset" bytes into the |
| 848 | // file, and "file_size" bytes will be mapped. If "file_size" is |
| 849 | // greater than the number of bytes available in the file starting |
| 850 | // at "file_offset", the number of bytes will be appropriately |
| 851 | // truncated. The final number of bytes that get mapped can be |
| 852 | // verified using the DataBuffer::GetByteSize() function. |
| 853 | //------------------------------------------------------------------ |
| 854 | DataBufferSP |
Greg Clayton | 4017fa3 | 2012-01-06 02:01:06 +0000 | [diff] [blame] | 855 | FileSpec::ReadFileContents (off_t file_offset, size_t file_size, Error *error_ptr) const |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 856 | { |
Greg Clayton | 4017fa3 | 2012-01-06 02:01:06 +0000 | [diff] [blame] | 857 | Error error; |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 858 | DataBufferSP data_sp; |
| 859 | char resolved_path[PATH_MAX]; |
| 860 | if (GetPath(resolved_path, sizeof(resolved_path))) |
| 861 | { |
Greg Clayton | 96c0968 | 2012-01-04 22:56:43 +0000 | [diff] [blame] | 862 | File file; |
| 863 | error = file.Open(resolved_path, File::eOpenOptionRead); |
| 864 | if (error.Success()) |
Greg Clayton | 0b0b512 | 2012-08-30 18:15:10 +0000 | [diff] [blame] | 865 | { |
| 866 | const bool null_terminate = false; |
| 867 | error = file.Read (file_size, file_offset, null_terminate, data_sp); |
| 868 | } |
| 869 | } |
| 870 | else |
| 871 | { |
| 872 | error.SetErrorString("invalid file specification"); |
| 873 | } |
| 874 | if (error_ptr) |
| 875 | *error_ptr = error; |
| 876 | return data_sp; |
| 877 | } |
| 878 | |
| 879 | DataBufferSP |
| 880 | FileSpec::ReadFileContentsAsCString(Error *error_ptr) |
| 881 | { |
| 882 | Error error; |
| 883 | DataBufferSP data_sp; |
| 884 | char resolved_path[PATH_MAX]; |
| 885 | if (GetPath(resolved_path, sizeof(resolved_path))) |
| 886 | { |
| 887 | File file; |
| 888 | error = file.Open(resolved_path, File::eOpenOptionRead); |
| 889 | if (error.Success()) |
| 890 | { |
| 891 | off_t offset = 0; |
| 892 | size_t length = SIZE_MAX; |
| 893 | const bool null_terminate = true; |
| 894 | error = file.Read (length, offset, null_terminate, data_sp); |
| 895 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 896 | } |
Greg Clayton | 4017fa3 | 2012-01-06 02:01:06 +0000 | [diff] [blame] | 897 | else |
| 898 | { |
| 899 | error.SetErrorString("invalid file specification"); |
| 900 | } |
| 901 | if (error_ptr) |
| 902 | *error_ptr = error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 903 | return data_sp; |
| 904 | } |
| 905 | |
Greg Clayton | 58fc50e | 2010-10-20 22:52:05 +0000 | [diff] [blame] | 906 | size_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 907 | FileSpec::ReadFileLines (STLStringArray &lines) |
| 908 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 909 | lines.clear(); |
Greg Clayton | 58fc50e | 2010-10-20 22:52:05 +0000 | [diff] [blame] | 910 | char path[PATH_MAX]; |
| 911 | if (GetPath(path, sizeof(path))) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 912 | { |
Greg Clayton | e01e07b | 2013-04-18 18:10:51 +0000 | [diff] [blame] | 913 | std::ifstream file_stream (path); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 914 | |
Greg Clayton | 58fc50e | 2010-10-20 22:52:05 +0000 | [diff] [blame] | 915 | if (file_stream) |
| 916 | { |
| 917 | std::string line; |
| 918 | while (getline (file_stream, line)) |
| 919 | lines.push_back (line); |
| 920 | } |
| 921 | } |
| 922 | return lines.size(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 923 | } |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 924 | |
| 925 | FileSpec::EnumerateDirectoryResult |
| 926 | FileSpec::EnumerateDirectory |
| 927 | ( |
| 928 | const char *dir_path, |
| 929 | bool find_directories, |
| 930 | bool find_files, |
| 931 | bool find_other, |
| 932 | EnumerateDirectoryCallbackType callback, |
| 933 | void *callback_baton |
| 934 | ) |
| 935 | { |
| 936 | if (dir_path && dir_path[0]) |
| 937 | { |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 938 | #if _WIN32 |
Hafiz Abid Qadeer | 487767c | 2014-03-12 10:53:50 +0000 | [diff] [blame^] | 939 | std::string szDir(dir_path); |
| 940 | szDir += "\\*"; |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 941 | |
| 942 | WIN32_FIND_DATA ffd; |
Hafiz Abid Qadeer | 487767c | 2014-03-12 10:53:50 +0000 | [diff] [blame^] | 943 | HANDLE hFind = FindFirstFile(szDir.c_str(), &ffd); |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 944 | |
| 945 | if (hFind == INVALID_HANDLE_VALUE) |
| 946 | { |
| 947 | return eEnumerateDirectoryResultNext; |
| 948 | } |
| 949 | |
| 950 | do |
| 951 | { |
| 952 | bool call_callback = false; |
| 953 | FileSpec::FileType file_type = eFileTypeUnknown; |
| 954 | if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
| 955 | { |
| 956 | size_t len = strlen(ffd.cFileName); |
| 957 | |
| 958 | if (len == 1 && ffd.cFileName[0] == '.') |
| 959 | continue; |
| 960 | |
| 961 | if (len == 2 && ffd.cFileName[0] == '.' && ffd.cFileName[1] == '.') |
| 962 | continue; |
| 963 | |
| 964 | file_type = eFileTypeDirectory; |
| 965 | call_callback = find_directories; |
| 966 | } |
| 967 | else if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) |
| 968 | { |
| 969 | file_type = eFileTypeOther; |
| 970 | call_callback = find_other; |
| 971 | } |
| 972 | else |
| 973 | { |
| 974 | file_type = eFileTypeRegular; |
| 975 | call_callback = find_files; |
| 976 | } |
| 977 | if (call_callback) |
| 978 | { |
| 979 | char child_path[MAX_PATH]; |
| 980 | const int child_path_len = ::snprintf (child_path, sizeof(child_path), "%s\\%s", dir_path, ffd.cFileName); |
| 981 | if (child_path_len < (int)(sizeof(child_path) - 1)) |
| 982 | { |
| 983 | // Don't resolve the file type or path |
| 984 | FileSpec child_path_spec (child_path, false); |
| 985 | |
| 986 | EnumerateDirectoryResult result = callback (callback_baton, file_type, child_path_spec); |
| 987 | |
| 988 | switch (result) |
| 989 | { |
| 990 | case eEnumerateDirectoryResultNext: |
| 991 | // Enumerate next entry in the current directory. We just |
| 992 | // exit this switch and will continue enumerating the |
| 993 | // current directory as we currently are... |
| 994 | break; |
| 995 | |
| 996 | case eEnumerateDirectoryResultEnter: // Recurse into the current entry if it is a directory or symlink, or next if not |
| 997 | if (FileSpec::EnumerateDirectory(child_path, |
| 998 | find_directories, |
| 999 | find_files, |
| 1000 | find_other, |
| 1001 | callback, |
| 1002 | callback_baton) == eEnumerateDirectoryResultQuit) |
| 1003 | { |
| 1004 | // The subdirectory returned Quit, which means to |
| 1005 | // stop all directory enumerations at all levels. |
| 1006 | return eEnumerateDirectoryResultQuit; |
| 1007 | } |
| 1008 | break; |
| 1009 | |
| 1010 | case eEnumerateDirectoryResultExit: // Exit from the current directory at the current level. |
| 1011 | // Exit from this directory level and tell parent to |
| 1012 | // keep enumerating. |
| 1013 | return eEnumerateDirectoryResultNext; |
| 1014 | |
| 1015 | case eEnumerateDirectoryResultQuit: // Stop directory enumerations at any level |
| 1016 | return eEnumerateDirectoryResultQuit; |
| 1017 | } |
| 1018 | } |
| 1019 | } |
| 1020 | } while (FindNextFile(hFind, &ffd) != 0); |
| 1021 | |
| 1022 | FindClose(hFind); |
| 1023 | #else |
| 1024 | lldb_utility::CleanUp <DIR *, int> dir_path_dir(opendir(dir_path), NULL, closedir); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1025 | if (dir_path_dir.is_valid()) |
| 1026 | { |
Jason Molenda | 14aef12 | 2013-04-04 03:19:27 +0000 | [diff] [blame] | 1027 | long path_max = fpathconf (dirfd (dir_path_dir.get()), _PC_NAME_MAX); |
| 1028 | #if defined (__APPLE_) && defined (__DARWIN_MAXPATHLEN) |
| 1029 | if (path_max < __DARWIN_MAXPATHLEN) |
| 1030 | path_max = __DARWIN_MAXPATHLEN; |
| 1031 | #endif |
| 1032 | struct dirent *buf, *dp; |
| 1033 | buf = (struct dirent *) malloc (offsetof (struct dirent, d_name) + path_max + 1); |
| 1034 | |
| 1035 | while (buf && readdir_r(dir_path_dir.get(), buf, &dp) == 0 && dp) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1036 | { |
| 1037 | // Only search directories |
| 1038 | if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN) |
| 1039 | { |
Greg Clayton | e0f3c02 | 2011-02-07 17:41:11 +0000 | [diff] [blame] | 1040 | size_t len = strlen(dp->d_name); |
| 1041 | |
| 1042 | if (len == 1 && dp->d_name[0] == '.') |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1043 | continue; |
| 1044 | |
Greg Clayton | e0f3c02 | 2011-02-07 17:41:11 +0000 | [diff] [blame] | 1045 | if (len == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.') |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1046 | continue; |
| 1047 | } |
| 1048 | |
| 1049 | bool call_callback = false; |
| 1050 | FileSpec::FileType file_type = eFileTypeUnknown; |
| 1051 | |
| 1052 | switch (dp->d_type) |
| 1053 | { |
| 1054 | default: |
| 1055 | case DT_UNKNOWN: file_type = eFileTypeUnknown; call_callback = true; break; |
| 1056 | case DT_FIFO: file_type = eFileTypePipe; call_callback = find_other; break; |
| 1057 | case DT_CHR: file_type = eFileTypeOther; call_callback = find_other; break; |
| 1058 | case DT_DIR: file_type = eFileTypeDirectory; call_callback = find_directories; break; |
| 1059 | case DT_BLK: file_type = eFileTypeOther; call_callback = find_other; break; |
| 1060 | case DT_REG: file_type = eFileTypeRegular; call_callback = find_files; break; |
| 1061 | case DT_LNK: file_type = eFileTypeSymbolicLink; call_callback = find_other; break; |
| 1062 | case DT_SOCK: file_type = eFileTypeSocket; call_callback = find_other; break; |
Greg Clayton | 2b4d9b7 | 2011-04-01 18:18:34 +0000 | [diff] [blame] | 1063 | #if !defined(__OpenBSD__) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1064 | case DT_WHT: file_type = eFileTypeOther; call_callback = find_other; break; |
Greg Clayton | 2b4d9b7 | 2011-04-01 18:18:34 +0000 | [diff] [blame] | 1065 | #endif |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | if (call_callback) |
| 1069 | { |
| 1070 | char child_path[PATH_MAX]; |
| 1071 | const int child_path_len = ::snprintf (child_path, sizeof(child_path), "%s/%s", dir_path, dp->d_name); |
Johnny Chen | 4480530 | 2011-07-19 19:48:13 +0000 | [diff] [blame] | 1072 | if (child_path_len < (int)(sizeof(child_path) - 1)) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1073 | { |
| 1074 | // Don't resolve the file type or path |
| 1075 | FileSpec child_path_spec (child_path, false); |
| 1076 | |
| 1077 | EnumerateDirectoryResult result = callback (callback_baton, file_type, child_path_spec); |
| 1078 | |
| 1079 | switch (result) |
| 1080 | { |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1081 | case eEnumerateDirectoryResultNext: |
| 1082 | // Enumerate next entry in the current directory. We just |
| 1083 | // exit this switch and will continue enumerating the |
| 1084 | // current directory as we currently are... |
| 1085 | break; |
| 1086 | |
| 1087 | case eEnumerateDirectoryResultEnter: // Recurse into the current entry if it is a directory or symlink, or next if not |
| 1088 | if (FileSpec::EnumerateDirectory (child_path, |
| 1089 | find_directories, |
| 1090 | find_files, |
| 1091 | find_other, |
| 1092 | callback, |
| 1093 | callback_baton) == eEnumerateDirectoryResultQuit) |
| 1094 | { |
| 1095 | // The subdirectory returned Quit, which means to |
| 1096 | // stop all directory enumerations at all levels. |
Jim Ingham | 5c42d8a | 2013-05-15 18:27:08 +0000 | [diff] [blame] | 1097 | if (buf) |
| 1098 | free (buf); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1099 | return eEnumerateDirectoryResultQuit; |
| 1100 | } |
| 1101 | break; |
| 1102 | |
| 1103 | case eEnumerateDirectoryResultExit: // Exit from the current directory at the current level. |
| 1104 | // Exit from this directory level and tell parent to |
| 1105 | // keep enumerating. |
Jason Molenda | fe80690 | 2013-05-04 00:39:52 +0000 | [diff] [blame] | 1106 | if (buf) |
| 1107 | free (buf); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1108 | return eEnumerateDirectoryResultNext; |
| 1109 | |
| 1110 | case eEnumerateDirectoryResultQuit: // Stop directory enumerations at any level |
Jason Molenda | fe80690 | 2013-05-04 00:39:52 +0000 | [diff] [blame] | 1111 | if (buf) |
| 1112 | free (buf); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1113 | return eEnumerateDirectoryResultQuit; |
| 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | } |
Jason Molenda | 14aef12 | 2013-04-04 03:19:27 +0000 | [diff] [blame] | 1118 | if (buf) |
| 1119 | { |
| 1120 | free (buf); |
| 1121 | } |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1122 | } |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 1123 | #endif |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1124 | } |
| 1125 | // By default when exiting a directory, we tell the parent enumeration |
| 1126 | // to continue enumerating. |
| 1127 | return eEnumerateDirectoryResultNext; |
| 1128 | } |
| 1129 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1130 | FileSpec |
| 1131 | FileSpec::CopyByAppendingPathComponent (const char *new_path) const |
| 1132 | { |
| 1133 | const bool resolve = false; |
| 1134 | if (m_filename.IsEmpty() && m_directory.IsEmpty()) |
| 1135 | return FileSpec(new_path,resolve); |
| 1136 | StreamString stream; |
| 1137 | if (m_filename.IsEmpty()) |
| 1138 | stream.Printf("%s/%s",m_directory.GetCString(),new_path); |
| 1139 | else if (m_directory.IsEmpty()) |
| 1140 | stream.Printf("%s/%s",m_filename.GetCString(),new_path); |
| 1141 | else |
| 1142 | stream.Printf("%s/%s/%s",m_directory.GetCString(), m_filename.GetCString(),new_path); |
| 1143 | return FileSpec(stream.GetData(),resolve); |
| 1144 | } |
| 1145 | |
| 1146 | FileSpec |
| 1147 | FileSpec::CopyByRemovingLastPathComponent () const |
| 1148 | { |
| 1149 | const bool resolve = false; |
| 1150 | if (m_filename.IsEmpty() && m_directory.IsEmpty()) |
| 1151 | return FileSpec("",resolve); |
| 1152 | if (m_directory.IsEmpty()) |
| 1153 | return FileSpec("",resolve); |
| 1154 | if (m_filename.IsEmpty()) |
| 1155 | { |
| 1156 | const char* dir_cstr = m_directory.GetCString(); |
| 1157 | const char* last_slash_ptr = ::strrchr(dir_cstr, '/'); |
| 1158 | |
| 1159 | // check for obvious cases before doing the full thing |
| 1160 | if (!last_slash_ptr) |
| 1161 | return FileSpec("",resolve); |
| 1162 | if (last_slash_ptr == dir_cstr) |
| 1163 | return FileSpec("/",resolve); |
| 1164 | |
| 1165 | size_t last_slash_pos = last_slash_ptr - dir_cstr+1; |
| 1166 | ConstString new_path(dir_cstr,last_slash_pos); |
| 1167 | return FileSpec(new_path.GetCString(),resolve); |
| 1168 | } |
| 1169 | else |
| 1170 | return FileSpec(m_directory.GetCString(),resolve); |
| 1171 | } |
| 1172 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1173 | ConstString |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1174 | FileSpec::GetLastPathComponent () const |
| 1175 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1176 | if (m_filename) |
| 1177 | return m_filename; |
| 1178 | if (m_directory) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1179 | { |
| 1180 | const char* dir_cstr = m_directory.GetCString(); |
| 1181 | const char* last_slash_ptr = ::strrchr(dir_cstr, '/'); |
| 1182 | if (last_slash_ptr == NULL) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1183 | return m_directory; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1184 | if (last_slash_ptr == dir_cstr) |
| 1185 | { |
| 1186 | if (last_slash_ptr[1] == 0) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1187 | return ConstString(last_slash_ptr); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1188 | else |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1189 | return ConstString(last_slash_ptr+1); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1190 | } |
| 1191 | if (last_slash_ptr[1] != 0) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1192 | return ConstString(last_slash_ptr+1); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1193 | const char* penultimate_slash_ptr = last_slash_ptr; |
| 1194 | while (*penultimate_slash_ptr) |
| 1195 | { |
| 1196 | --penultimate_slash_ptr; |
| 1197 | if (penultimate_slash_ptr == dir_cstr) |
| 1198 | break; |
| 1199 | if (*penultimate_slash_ptr == '/') |
| 1200 | break; |
| 1201 | } |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1202 | ConstString result(penultimate_slash_ptr+1,last_slash_ptr-penultimate_slash_ptr); |
| 1203 | return result; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1204 | } |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1205 | return ConstString(); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | void |
| 1209 | FileSpec::AppendPathComponent (const char *new_path) |
| 1210 | { |
| 1211 | const bool resolve = false; |
| 1212 | if (m_filename.IsEmpty() && m_directory.IsEmpty()) |
| 1213 | { |
| 1214 | SetFile(new_path,resolve); |
| 1215 | return; |
| 1216 | } |
| 1217 | StreamString stream; |
| 1218 | if (m_filename.IsEmpty()) |
| 1219 | stream.Printf("%s/%s",m_directory.GetCString(),new_path); |
| 1220 | else if (m_directory.IsEmpty()) |
| 1221 | stream.Printf("%s/%s",m_filename.GetCString(),new_path); |
| 1222 | else |
| 1223 | stream.Printf("%s/%s/%s",m_directory.GetCString(), m_filename.GetCString(),new_path); |
| 1224 | SetFile(stream.GetData(), resolve); |
| 1225 | } |
| 1226 | |
| 1227 | void |
| 1228 | FileSpec::RemoveLastPathComponent () |
| 1229 | { |
| 1230 | const bool resolve = false; |
| 1231 | if (m_filename.IsEmpty() && m_directory.IsEmpty()) |
| 1232 | { |
| 1233 | SetFile("",resolve); |
| 1234 | return; |
| 1235 | } |
| 1236 | if (m_directory.IsEmpty()) |
| 1237 | { |
| 1238 | SetFile("",resolve); |
| 1239 | return; |
| 1240 | } |
| 1241 | if (m_filename.IsEmpty()) |
| 1242 | { |
| 1243 | const char* dir_cstr = m_directory.GetCString(); |
| 1244 | const char* last_slash_ptr = ::strrchr(dir_cstr, '/'); |
| 1245 | |
| 1246 | // check for obvious cases before doing the full thing |
| 1247 | if (!last_slash_ptr) |
| 1248 | { |
| 1249 | SetFile("",resolve); |
| 1250 | return; |
| 1251 | } |
| 1252 | if (last_slash_ptr == dir_cstr) |
| 1253 | { |
| 1254 | SetFile("/",resolve); |
| 1255 | return; |
| 1256 | } |
| 1257 | size_t last_slash_pos = last_slash_ptr - dir_cstr+1; |
| 1258 | ConstString new_path(dir_cstr,last_slash_pos); |
| 1259 | SetFile(new_path.GetCString(),resolve); |
| 1260 | } |
| 1261 | else |
| 1262 | SetFile(m_directory.GetCString(),resolve); |
| 1263 | } |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 1264 | //------------------------------------------------------------------ |
| 1265 | /// Returns true if the filespec represents an implementation source |
| 1266 | /// file (files with a ".c", ".cpp", ".m", ".mm" (many more) |
| 1267 | /// extension). |
| 1268 | /// |
| 1269 | /// @return |
| 1270 | /// \b true if the filespec represents an implementation source |
| 1271 | /// file, \b false otherwise. |
| 1272 | //------------------------------------------------------------------ |
| 1273 | bool |
| 1274 | FileSpec::IsSourceImplementationFile () const |
| 1275 | { |
| 1276 | ConstString extension (GetFileNameExtension()); |
| 1277 | if (extension) |
| 1278 | { |
| 1279 | static RegularExpression g_source_file_regex ("^(c|m|mm|cpp|c\\+\\+|cxx|cc|cp|s|asm|f|f77|f90|f95|f03|for|ftn|fpp|ada|adb|ads)$", |
| 1280 | REG_EXTENDED | REG_ICASE); |
| 1281 | return g_source_file_regex.Execute (extension.GetCString()); |
| 1282 | } |
| 1283 | return false; |
| 1284 | } |
| 1285 | |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 1286 | bool |
| 1287 | FileSpec::IsRelativeToCurrentWorkingDirectory () const |
| 1288 | { |
| 1289 | const char *directory = m_directory.GetCString(); |
| 1290 | if (directory && directory[0]) |
| 1291 | { |
| 1292 | // If the path doesn't start with '/' or '~', return true |
| 1293 | switch (directory[0]) |
| 1294 | { |
| 1295 | case '/': |
| 1296 | case '~': |
| 1297 | return false; |
| 1298 | default: |
| 1299 | return true; |
| 1300 | } |
| 1301 | } |
| 1302 | else if (m_filename) |
| 1303 | { |
| 1304 | // No directory, just a basename, return true |
| 1305 | return true; |
| 1306 | } |
| 1307 | return false; |
| 1308 | } |