blob: 3a651f957cd0aa5f37ae3ac8f33e848a1cc6e4b4 [file] [log] [blame]
Reid Spencerb89a2232004-08-25 06:20:07 +00001//===- llvm/System/Unix/Path.cpp - Unix Path Implementation -----*- C++ -*-===//
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +00002//
Reid Spencerb89a2232004-08-25 06:20:07 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +00007//
Reid Spencerb89a2232004-08-25 06:20:07 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the Unix specific portion of the Path class.
11//
12//===----------------------------------------------------------------------===//
13
14//===----------------------------------------------------------------------===//
15//=== WARNING: Implementation here must contain only generic UNIX code that
Reid Spencer8e665952004-08-29 05:24:01 +000016//=== is guaranteed to work on *all* UNIX variants.
Reid Spencerb89a2232004-08-25 06:20:07 +000017//===----------------------------------------------------------------------===//
18
Misha Brukman210b32b2004-12-20 00:16:38 +000019#include "llvm/Config/alloca.h"
Reid Spencerb89a2232004-08-25 06:20:07 +000020#include "Unix.h"
Reid Spenceraf2f2082004-12-27 06:17:15 +000021#if HAVE_SYS_STAT_H
Reid Spencerb89a2232004-08-25 06:20:07 +000022#include <sys/stat.h>
Reid Spenceraf2f2082004-12-27 06:17:15 +000023#endif
24#if HAVE_FCNTL_H
Reid Spencerb89a2232004-08-25 06:20:07 +000025#include <fcntl.h>
Reid Spenceraf2f2082004-12-27 06:17:15 +000026#endif
Chris Lattner14c762d2008-04-01 06:25:23 +000027#ifdef HAVE_SYS_MMAN_H
28#include <sys/mman.h>
29#endif
30#ifdef HAVE_SYS_STAT_H
31#include <sys/stat.h>
32#endif
Reid Spenceraf2f2082004-12-27 06:17:15 +000033#if HAVE_UTIME_H
Reid Spencereaf18152004-11-14 22:08:36 +000034#include <utime.h>
Reid Spenceraf2f2082004-12-27 06:17:15 +000035#endif
36#if HAVE_TIME_H
Reid Spencer69a16162004-12-24 06:29:42 +000037#include <time.h>
Reid Spenceraf2f2082004-12-27 06:17:15 +000038#endif
39#if HAVE_DIRENT_H
40# include <dirent.h>
41# define NAMLEN(dirent) strlen((dirent)->d_name)
42#else
43# define dirent direct
44# define NAMLEN(dirent) (dirent)->d_namlen
45# if HAVE_SYS_NDIR_H
46# include <sys/ndir.h>
47# endif
48# if HAVE_SYS_DIR_H
49# include <sys/dir.h>
50# endif
51# if HAVE_NDIR_H
52# include <ndir.h>
53# endif
54#endif
55
Chris Lattner1a091442008-03-03 02:55:43 +000056#if HAVE_DLFCN_H
57#include <dlfcn.h>
58#endif
59
Reid Spencer932e2e32005-06-02 05:38:20 +000060// Put in a hack for Cygwin which falsely reports that the mkdtemp function
61// is available when it is not.
62#ifdef __CYGWIN__
63# undef HAVE_MKDTEMP
64#endif
Reid Spencerb89a2232004-08-25 06:20:07 +000065
Reid Spencer3be872e2005-07-28 16:25:57 +000066namespace {
67inline bool lastIsSlash(const std::string& path) {
68 return !path.empty() && path[path.length() - 1] == '/';
69}
70
71}
72
Reid Spencer8e665952004-08-29 05:24:01 +000073namespace llvm {
74using namespace sys;
Reid Spencerb89a2232004-08-25 06:20:07 +000075
Chris Lattnere1b332a2008-02-27 06:17:10 +000076extern const char sys::PathSeparator = ':';
77
Nick Lewyckyfff116f2008-05-11 17:37:40 +000078Path::Path(const std::string& p)
79 : path(p) {}
80
81Path::Path(const char *StrStart, unsigned StrLen)
82 : path(StrStart, StrLen) {}
83
Chris Lattner0eab5e22008-08-11 23:39:47 +000084Path&
85Path::operator=(const std::string &that) {
86 path = that;
87 return *this;
88}
89
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +000090bool
Reid Spencer69a16162004-12-24 06:29:42 +000091Path::isValid() const {
Reid Spencer6371ccb2005-07-08 06:53:26 +000092 // Check some obvious things
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +000093 if (path.empty())
Reid Spencer69a16162004-12-24 06:29:42 +000094 return false;
95 else if (path.length() >= MAXPATHLEN)
96 return false;
Reid Spencer6371ccb2005-07-08 06:53:26 +000097
98 // Check that the characters are ascii chars
99 size_t len = path.length();
100 unsigned i = 0;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000101 while (i < len && isascii(path[i]))
Reid Spencer6371ccb2005-07-08 06:53:26 +0000102 ++i;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000103 return i >= len;
Reid Spencer69a16162004-12-24 06:29:42 +0000104}
105
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000106bool
Reid Spencer69cce812007-03-29 16:43:20 +0000107Path::isAbsolute() const {
108 if (path.empty())
109 return false;
110 return path[0] == '/';
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000111}
Reid Spencer8e665952004-08-29 05:24:01 +0000112Path
113Path::GetRootDirectory() {
114 Path result;
Reid Spencerdd04df02005-07-07 23:21:43 +0000115 result.set("/");
Reid Spencer8e665952004-08-29 05:24:01 +0000116 return result;
117}
118
Reid Spencer69a16162004-12-24 06:29:42 +0000119Path
Chris Lattnerd18a2c12009-02-19 05:34:35 +0000120Path::GetTemporaryDirectory(std::string *ErrMsg) {
Reid Spencer69a16162004-12-24 06:29:42 +0000121#if defined(HAVE_MKDTEMP)
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000122 // The best way is with mkdtemp but that's not available on many systems,
Reid Spencer69a16162004-12-24 06:29:42 +0000123 // Linux and FreeBSD have it. Others probably won't.
124 char pathname[MAXPATHLEN];
125 strcpy(pathname,"/tmp/llvm_XXXXXX");
Reid Spencer48744762006-08-22 19:01:30 +0000126 if (0 == mkdtemp(pathname)) {
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000127 MakeErrMsg(ErrMsg,
Reid Spencer48744762006-08-22 19:01:30 +0000128 std::string(pathname) + ": can't create temporary directory");
129 return Path();
130 }
Reid Spencer69a16162004-12-24 06:29:42 +0000131 Path result;
Reid Spencerdd04df02005-07-07 23:21:43 +0000132 result.set(pathname);
Reid Spencer69a16162004-12-24 06:29:42 +0000133 assert(result.isValid() && "mkdtemp didn't create a valid pathname!");
134 return result;
135#elif defined(HAVE_MKSTEMP)
136 // If no mkdtemp is available, mkstemp can be used to create a temporary file
137 // which is then removed and created as a directory. We prefer this over
138 // mktemp because of mktemp's inherent security and threading risks. We still
139 // have a slight race condition from the time the temporary file is created to
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000140 // the time it is re-created as a directoy.
Reid Spencer69a16162004-12-24 06:29:42 +0000141 char pathname[MAXPATHLEN];
142 strcpy(pathname, "/tmp/llvm_XXXXXX");
143 int fd = 0;
Reid Spencer48744762006-08-22 19:01:30 +0000144 if (-1 == (fd = mkstemp(pathname))) {
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000145 MakeErrMsg(ErrMsg,
Reid Spencer48744762006-08-22 19:01:30 +0000146 std::string(pathname) + ": can't create temporary directory");
147 return Path();
148 }
Reid Spencer69a16162004-12-24 06:29:42 +0000149 ::close(fd);
150 ::unlink(pathname); // start race condition, ignore errors
Reid Spencer48744762006-08-22 19:01:30 +0000151 if (-1 == ::mkdir(pathname, S_IRWXU)) { // end race condition
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000152 MakeErrMsg(ErrMsg,
Reid Spencer48744762006-08-22 19:01:30 +0000153 std::string(pathname) + ": can't create temporary directory");
154 return Path();
155 }
Reid Spencer69a16162004-12-24 06:29:42 +0000156 Path result;
Reid Spencerdd04df02005-07-07 23:21:43 +0000157 result.set(pathname);
Reid Spencer69a16162004-12-24 06:29:42 +0000158 assert(result.isValid() && "mkstemp didn't create a valid pathname!");
159 return result;
160#elif defined(HAVE_MKTEMP)
161 // If a system doesn't have mkdtemp(3) or mkstemp(3) but it does have
162 // mktemp(3) then we'll assume that system (e.g. AIX) has a reasonable
163 // implementation of mktemp(3) and doesn't follow BSD 4.3's lead of replacing
164 // the XXXXXX with the pid of the process and a letter. That leads to only
165 // twenty six temporary files that can be generated.
166 char pathname[MAXPATHLEN];
167 strcpy(pathname, "/tmp/llvm_XXXXXX");
168 char *TmpName = ::mktemp(pathname);
Reid Spencer48744762006-08-22 19:01:30 +0000169 if (TmpName == 0) {
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000170 MakeErrMsg(ErrMsg,
Reid Spencer48744762006-08-22 19:01:30 +0000171 std::string(TmpName) + ": can't create unique directory name");
172 return Path();
173 }
174 if (-1 == ::mkdir(TmpName, S_IRWXU)) {
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000175 MakeErrMsg(ErrMsg,
Reid Spencer48744762006-08-22 19:01:30 +0000176 std::string(TmpName) + ": can't create temporary directory");
177 return Path();
178 }
Reid Spencer69a16162004-12-24 06:29:42 +0000179 Path result;
Reid Spencerdd04df02005-07-07 23:21:43 +0000180 result.set(TmpName);
Reid Spencer69a16162004-12-24 06:29:42 +0000181 assert(result.isValid() && "mktemp didn't create a valid pathname!");
182 return result;
183#else
184 // This is the worst case implementation. tempnam(3) leaks memory unless its
185 // on an SVID2 (or later) system. On BSD 4.3 it leaks. tmpnam(3) has thread
186 // issues. The mktemp(3) function doesn't have enough variability in the
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000187 // temporary name generated. So, we provide our own implementation that
Reid Spencer69a16162004-12-24 06:29:42 +0000188 // increments an integer from a random number seeded by the current time. This
189 // should be sufficiently unique that we don't have many collisions between
190 // processes. Generally LLVM processes don't run very long and don't use very
191 // many temporary files so this shouldn't be a big issue for LLVM.
192 static time_t num = ::time(0);
193 char pathname[MAXPATHLEN];
194 do {
195 num++;
196 sprintf(pathname, "/tmp/llvm_%010u", unsigned(num));
197 } while ( 0 == access(pathname, F_OK ) );
Reid Spencer48744762006-08-22 19:01:30 +0000198 if (-1 == ::mkdir(pathname, S_IRWXU)) {
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000199 MakeErrMsg(ErrMsg,
Reid Spencer48744762006-08-22 19:01:30 +0000200 std::string(pathname) + ": can't create temporary directory");
201 return Path();
Reid Spencer51c5a282006-08-23 20:34:57 +0000202 }
Reid Spencer69a16162004-12-24 06:29:42 +0000203 Path result;
Reid Spencerdd04df02005-07-07 23:21:43 +0000204 result.set(pathname);
Reid Spencer69a16162004-12-24 06:29:42 +0000205 assert(result.isValid() && "mkstemp didn't create a valid pathname!");
206 return result;
207#endif
208}
209
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000210void
Reid Spencer1b6b99b2004-12-13 03:00:51 +0000211Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
212#ifdef LTDL_SHLIBPATH_VAR
213 char* env_var = getenv(LTDL_SHLIBPATH_VAR);
214 if (env_var != 0) {
215 getPathList(env_var,Paths);
Reid Spencer74e72612004-09-14 00:16:39 +0000216 }
Reid Spencer1b6b99b2004-12-13 03:00:51 +0000217#endif
218 // FIXME: Should this look at LD_LIBRARY_PATH too?
219 Paths.push_back(sys::Path("/usr/local/lib/"));
220 Paths.push_back(sys::Path("/usr/X11R6/lib/"));
221 Paths.push_back(sys::Path("/usr/lib/"));
222 Paths.push_back(sys::Path("/lib/"));
Reid Spencer74e72612004-09-14 00:16:39 +0000223}
224
Reid Spencer1b6b99b2004-12-13 03:00:51 +0000225void
Gabor Greifa99be512007-07-05 17:07:56 +0000226Path::GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths) {
Reid Spencer1b6b99b2004-12-13 03:00:51 +0000227 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
228 if (env_var != 0) {
229 getPathList(env_var,Paths);
230 }
Reid Spencer1b6b99b2004-12-13 03:00:51 +0000231#ifdef LLVM_LIBDIR
232 {
233 Path tmpPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000234 if (tmpPath.set(LLVM_LIBDIR))
Reid Spencerc7f08322005-07-07 18:21:42 +0000235 if (tmpPath.canRead())
Reid Spencer1b6b99b2004-12-13 03:00:51 +0000236 Paths.push_back(tmpPath);
237 }
238#endif
239 GetSystemLibraryPaths(Paths);
Reid Spencer8e665952004-08-29 05:24:01 +0000240}
241
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000242Path
Reid Spencer8e665952004-08-29 05:24:01 +0000243Path::GetLLVMDefaultConfigDir() {
244 return Path("/etc/llvm/");
245}
246
Reid Spencer8e665952004-08-29 05:24:01 +0000247Path
248Path::GetUserHomeDirectory() {
249 const char* home = getenv("HOME");
250 if (home) {
251 Path result;
Reid Spencerdd04df02005-07-07 23:21:43 +0000252 if (result.set(home))
Reid Spencer8e665952004-08-29 05:24:01 +0000253 return result;
254 }
255 return GetRootDirectory();
256}
257
Ted Kremenek79200782007-12-18 22:07:33 +0000258Path
259Path::GetCurrentDirectory() {
260 char pathname[MAXPATHLEN];
261 if (!getcwd(pathname,MAXPATHLEN)) {
262 assert (false && "Could not query current working directory.");
263 return Path("");
264 }
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000265
Ted Kremenek79200782007-12-18 22:07:33 +0000266 return Path(pathname);
267}
Reid Spencera229c5c2005-07-08 03:08:58 +0000268
Chris Lattner1a091442008-03-03 02:55:43 +0000269/// GetMainExecutable - Return the path to the main executable, given the
270/// value of argv[0] from program startup.
271Path Path::GetMainExecutable(const char *argv0, void *MainAddr) {
Seo Sanghyeonfd6437f2008-06-27 22:55:30 +0000272#if defined(__linux__) || defined(__CYGWIN__)
Chris Lattner3bdfa042008-03-13 05:22:05 +0000273 char exe_path[MAXPATHLEN];
Seo Sanghyeonfd6437f2008-06-27 22:55:30 +0000274 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path));
Chris Lattner3bdfa042008-03-13 05:22:05 +0000275 if (len > 0 && len < MAXPATHLEN - 1) {
276 exe_path[len] = '\0';
277 return Path(std::string(exe_path));
278 }
279#elif defined(HAVE_DLFCN_H)
Chris Lattner1a091442008-03-03 02:55:43 +0000280 // Use dladdr to get executable path if available.
Chris Lattner1a091442008-03-03 02:55:43 +0000281 Dl_info DLInfo;
282 int err = dladdr(MainAddr, &DLInfo);
Chris Lattnerd18a2c12009-02-19 05:34:35 +0000283 if (err == 0)
284 return Path();
285
286 // If the filename is a symlink, we need to resolve and return the location of
287 // the actual executable.
288 char link_path[MAXPATHLEN];
289 return Path(std::string(realpath(DLInfo.dli_fname, link_path)));
Chris Lattner1a091442008-03-03 02:55:43 +0000290#endif
291 return Path();
292}
293
294
Ted Kremenek9b01cc02008-04-07 22:01:32 +0000295std::string Path::getDirname() const {
296 return getDirnameCharSep(path, '/');
297}
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000298
Reid Spencer1b554b42004-09-11 04:55:08 +0000299std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000300Path::getBasename() const {
Reid Spencer1b554b42004-09-11 04:55:08 +0000301 // Find the last slash
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000302 std::string::size_type slash = path.rfind('/');
Reid Spencer1b554b42004-09-11 04:55:08 +0000303 if (slash == std::string::npos)
304 slash = 0;
305 else
306 slash++;
307
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000308 std::string::size_type dot = path.rfind('.');
Jeff Cohen73f36672005-07-09 18:42:02 +0000309 if (dot == std::string::npos || dot < slash)
310 return path.substr(slash);
311 else
312 return path.substr(slash, dot - slash);
Reid Spencer1b554b42004-09-11 04:55:08 +0000313}
314
Argyrios Kyrtzidisfc199882008-06-15 15:15:19 +0000315std::string
316Path::getSuffix() const {
317 // Find the last slash
318 std::string::size_type slash = path.rfind('/');
319 if (slash == std::string::npos)
320 slash = 0;
321 else
322 slash++;
323
324 std::string::size_type dot = path.rfind('.');
325 if (dot == std::string::npos || dot < slash)
Wojciech Matyjewicz93c53462008-06-15 18:02:47 +0000326 return std::string();
Argyrios Kyrtzidisfc199882008-06-15 15:15:19 +0000327 else
328 return path.substr(dot + 1);
329}
330
Reid Spencereaf18152004-11-14 22:08:36 +0000331bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
Reid Spencerbe31d2a2004-11-16 17:14:08 +0000332 assert(len < 1024 && "Request for magic string too long");
333 char* buf = (char*) alloca(1 + len);
Chris Lattnerc7c453a2006-08-01 17:51:09 +0000334 int fd = ::open(path.c_str(), O_RDONLY);
Reid Spencerbe31d2a2004-11-16 17:14:08 +0000335 if (fd < 0)
336 return false;
Reid Spencer86ac2dc2004-12-02 09:09:48 +0000337 ssize_t bytes_read = ::read(fd, buf, len);
338 ::close(fd);
339 if (ssize_t(len) != bytes_read) {
340 Magic.clear();
Reid Spencerbe31d2a2004-11-16 17:14:08 +0000341 return false;
Reid Spencer86ac2dc2004-12-02 09:09:48 +0000342 }
343 Magic.assign(buf,len);
Reid Spencereaf18152004-11-14 22:08:36 +0000344 return true;
345}
346
Reid Spencer1b554b42004-09-11 04:55:08 +0000347bool
Reid Spencer8e665952004-08-29 05:24:01 +0000348Path::exists() const {
349 return 0 == access(path.c_str(), F_OK );
350}
351
352bool
Ted Kremenekfd527112007-12-18 19:46:22 +0000353Path::isDirectory() const {
354 struct stat buf;
355 if (0 != stat(path.c_str(), &buf))
356 return false;
357 return buf.st_mode & S_IFDIR ? true : false;
358}
359
360bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000361Path::canRead() const {
Reid Spencer8e665952004-08-29 05:24:01 +0000362 return 0 == access(path.c_str(), F_OK | R_OK );
363}
364
365bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000366Path::canWrite() const {
Reid Spencer8e665952004-08-29 05:24:01 +0000367 return 0 == access(path.c_str(), F_OK | W_OK );
368}
369
370bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000371Path::canExecute() const {
Reid Spencer8b2d1aa2005-07-08 17:46:10 +0000372 if (0 != access(path.c_str(), R_OK | X_OK ))
373 return false;
Reid Spencer2ae9d112007-04-07 18:52:17 +0000374 struct stat buf;
375 if (0 != stat(path.c_str(), &buf))
376 return false;
377 if (!S_ISREG(buf.st_mode))
Misha Brukman8177bf82005-04-20 15:33:22 +0000378 return false;
Reid Spencer8b2d1aa2005-07-08 17:46:10 +0000379 return true;
Reid Spencer8e665952004-08-29 05:24:01 +0000380}
381
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000382std::string
Reid Spencer8e665952004-08-29 05:24:01 +0000383Path::getLast() const {
384 // Find the last slash
385 size_t pos = path.rfind('/');
386
387 // Handle the corner cases
388 if (pos == std::string::npos)
389 return path;
390
391 // If the last character is a slash
392 if (pos == path.length()-1) {
393 // Find the second to last slash
394 size_t pos2 = path.rfind('/', pos-1);
395 if (pos2 == std::string::npos)
396 return path.substr(0,pos);
397 else
398 return path.substr(pos2+1,pos-pos2-1);
399 }
400 // Return everything after the last slash
401 return path.substr(pos+1);
402}
403
Reid Spencer2ae9d112007-04-07 18:52:17 +0000404const FileStatus *
405PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const {
406 if (!fsIsValid || update) {
Reid Spencer69cce812007-03-29 16:43:20 +0000407 struct stat buf;
Reid Spencer8475ec02007-03-29 19:05:44 +0000408 if (0 != stat(path.c_str(), &buf)) {
409 MakeErrMsg(ErrStr, path + ": can't get status of file");
410 return 0;
411 }
Reid Spencer2ae9d112007-04-07 18:52:17 +0000412 status.fileSize = buf.st_size;
413 status.modTime.fromEpochTime(buf.st_mtime);
414 status.mode = buf.st_mode;
415 status.user = buf.st_uid;
416 status.group = buf.st_gid;
417 status.uniqueID = uint64_t(buf.st_ino);
418 status.isDir = S_ISDIR(buf.st_mode);
419 status.isFile = S_ISREG(buf.st_mode);
420 fsIsValid = true;
Reid Spencer69cce812007-03-29 16:43:20 +0000421 }
Reid Spencer2ae9d112007-04-07 18:52:17 +0000422 return &status;
Reid Spencereaf18152004-11-14 22:08:36 +0000423}
424
Chris Lattner252ad032006-07-28 22:03:44 +0000425static bool AddPermissionBits(const Path &File, int bits) {
Reid Spencer77cc91d2004-12-13 19:59:50 +0000426 // Get the umask value from the operating system. We want to use it
427 // when changing the file's permissions. Since calling umask() sets
428 // the umask and returns its old value, we must call it a second
429 // time to reset it to the user's preference.
430 int mask = umask(0777); // The arg. to umask is arbitrary.
431 umask(mask); // Restore the umask.
432
433 // Get the file's current mode.
Reid Spencer2ae9d112007-04-07 18:52:17 +0000434 struct stat buf;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000435 if (0 != stat(File.toString().c_str(), &buf))
Reid Spencer77cc91d2004-12-13 19:59:50 +0000436 return false;
Reid Spencer2ae9d112007-04-07 18:52:17 +0000437 // Change the file to have whichever permissions bits from 'bits'
438 // that the umask would not disable.
439 if ((chmod(File.c_str(), (buf.st_mode | (bits & ~mask)))) == -1)
440 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000441 return true;
442}
443
Reid Spencere1647f42006-08-22 23:27:23 +0000444bool Path::makeReadableOnDisk(std::string* ErrMsg) {
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000445 if (!AddPermissionBits(*this, 0444))
Reid Spencer5a060772006-08-23 07:30:48 +0000446 return MakeErrMsg(ErrMsg, path + ": can't make file readable");
Reid Spencere1647f42006-08-22 23:27:23 +0000447 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000448}
449
Reid Spencere1647f42006-08-22 23:27:23 +0000450bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
Reid Spencer5a060772006-08-23 07:30:48 +0000451 if (!AddPermissionBits(*this, 0222))
452 return MakeErrMsg(ErrMsg, path + ": can't make file writable");
Reid Spencere1647f42006-08-22 23:27:23 +0000453 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000454}
455
Reid Spencere1647f42006-08-22 23:27:23 +0000456bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
Reid Spencer5a060772006-08-23 07:30:48 +0000457 if (!AddPermissionBits(*this, 0111))
458 return MakeErrMsg(ErrMsg, path + ": can't make file executable");
Reid Spencere1647f42006-08-22 23:27:23 +0000459 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000460}
461
Reid Spencereaf18152004-11-14 22:08:36 +0000462bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000463Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
Reid Spencereaf18152004-11-14 22:08:36 +0000464 DIR* direntries = ::opendir(path.c_str());
Reid Spencer5a060772006-08-23 07:30:48 +0000465 if (direntries == 0)
466 return MakeErrMsg(ErrMsg, path + ": can't open directory");
Reid Spencereaf18152004-11-14 22:08:36 +0000467
Reid Spencer3be872e2005-07-28 16:25:57 +0000468 std::string dirPath = path;
469 if (!lastIsSlash(dirPath))
470 dirPath += '/';
471
Reid Spencereaf18152004-11-14 22:08:36 +0000472 result.clear();
473 struct dirent* de = ::readdir(direntries);
Misha Brukman4619c752005-04-20 04:04:07 +0000474 for ( ; de != 0; de = ::readdir(direntries)) {
Reid Spencereaf18152004-11-14 22:08:36 +0000475 if (de->d_name[0] != '.') {
Reid Spencer3be872e2005-07-28 16:25:57 +0000476 Path aPath(dirPath + (const char*)de->d_name);
Chris Lattnerb14c3422006-07-07 21:21:06 +0000477 struct stat st;
478 if (0 != lstat(aPath.path.c_str(), &st)) {
479 if (S_ISLNK(st.st_mode))
Misha Brukman4619c752005-04-20 04:04:07 +0000480 continue; // dangling symlink -- ignore
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000481 return MakeErrMsg(ErrMsg,
Reid Spencer5a060772006-08-23 07:30:48 +0000482 aPath.path + ": can't determine file object type");
Misha Brukman4619c752005-04-20 04:04:07 +0000483 }
Reid Spencerb608a812004-11-16 06:15:19 +0000484 result.insert(aPath);
Reid Spencereaf18152004-11-14 22:08:36 +0000485 }
Reid Spencereaf18152004-11-14 22:08:36 +0000486 }
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000487
Reid Spencereaf18152004-11-14 22:08:36 +0000488 closedir(direntries);
Reid Spencer142ca8e2006-08-23 06:56:27 +0000489 return false;
Reid Spencer9195f372004-11-09 20:26:31 +0000490}
491
Reid Spencer8e665952004-08-29 05:24:01 +0000492bool
Reid Spencerdd04df02005-07-07 23:21:43 +0000493Path::set(const std::string& a_path) {
494 if (a_path.empty())
Reid Spencer8e665952004-08-29 05:24:01 +0000495 return false;
Reid Spencerdd04df02005-07-07 23:21:43 +0000496 std::string save(path);
Reid Spencer8e665952004-08-29 05:24:01 +0000497 path = a_path;
Reid Spencer07adb282004-11-05 22:15:36 +0000498 if (!isValid()) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000499 path = save;
Reid Spencer8e665952004-08-29 05:24:01 +0000500 return false;
501 }
502 return true;
503}
504
505bool
Reid Spencerdd04df02005-07-07 23:21:43 +0000506Path::appendComponent(const std::string& name) {
507 if (name.empty())
Reid Spencer8e665952004-08-29 05:24:01 +0000508 return false;
Reid Spencerdd04df02005-07-07 23:21:43 +0000509 std::string save(path);
Reid Spencer3be872e2005-07-28 16:25:57 +0000510 if (!lastIsSlash(path))
511 path += '/';
Reid Spencerdd04df02005-07-07 23:21:43 +0000512 path += name;
Reid Spencer07adb282004-11-05 22:15:36 +0000513 if (!isValid()) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000514 path = save;
Reid Spencer8e665952004-08-29 05:24:01 +0000515 return false;
516 }
517 return true;
518}
519
520bool
Reid Spencerdd04df02005-07-07 23:21:43 +0000521Path::eraseComponent() {
Reid Spencer8e665952004-08-29 05:24:01 +0000522 size_t slashpos = path.rfind('/',path.size());
Reid Spencerdd04df02005-07-07 23:21:43 +0000523 if (slashpos == 0 || slashpos == std::string::npos) {
524 path.erase();
525 return true;
526 }
Reid Spencer8e665952004-08-29 05:24:01 +0000527 if (slashpos == path.size() - 1)
528 slashpos = path.rfind('/',slashpos-1);
Reid Spencerdd04df02005-07-07 23:21:43 +0000529 if (slashpos == std::string::npos) {
530 path.erase();
531 return true;
532 }
Reid Spencer8e665952004-08-29 05:24:01 +0000533 path.erase(slashpos);
534 return true;
535}
536
537bool
Reid Spencer07adb282004-11-05 22:15:36 +0000538Path::appendSuffix(const std::string& suffix) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000539 std::string save(path);
Reid Spencer8e665952004-08-29 05:24:01 +0000540 path.append(".");
541 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000542 if (!isValid()) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000543 path = save;
Reid Spencer8e665952004-08-29 05:24:01 +0000544 return false;
545 }
546 return true;
547}
548
Reid Spencerdd04df02005-07-07 23:21:43 +0000549bool
550Path::eraseSuffix() {
Reid Spencer6371ccb2005-07-08 06:53:26 +0000551 std::string save = path;
Reid Spencer8e665952004-08-29 05:24:01 +0000552 size_t dotpos = path.rfind('.',path.size());
553 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen563a17f2005-07-08 04:49:16 +0000554 if (dotpos != std::string::npos) {
Jeff Cohen73f36672005-07-09 18:42:02 +0000555 if (slashpos == std::string::npos || dotpos > slashpos+1) {
Jeff Cohen563a17f2005-07-08 04:49:16 +0000556 path.erase(dotpos, path.size()-dotpos);
Jeff Cohen85c716f2005-07-08 05:02:13 +0000557 return true;
Jeff Cohen563a17f2005-07-08 04:49:16 +0000558 }
Reid Spencer8e665952004-08-29 05:24:01 +0000559 }
Reid Spencer6371ccb2005-07-08 06:53:26 +0000560 if (!isValid())
561 path = save;
Jeff Cohen563a17f2005-07-08 04:49:16 +0000562 return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000563}
564
Ted Kremenekc5412c52008-04-03 16:11:31 +0000565static bool createDirectoryHelper(char* beg, char* end, bool create_parents) {
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000566
Ted Kremenekc5412c52008-04-03 16:11:31 +0000567 if (access(beg, F_OK | R_OK | W_OK) == 0)
568 return false;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000569
Ted Kremenekc5412c52008-04-03 16:11:31 +0000570 if (create_parents) {
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000571
Ted Kremenekc5412c52008-04-03 16:11:31 +0000572 char* c = end;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000573
Ted Kremenekc5412c52008-04-03 16:11:31 +0000574 for (; c != beg; --c)
575 if (*c == '/') {
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000576
Ted Kremenekc5412c52008-04-03 16:11:31 +0000577 // Recurse to handling the parent directory.
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000578 *c = '\0';
Ted Kremenekc5412c52008-04-03 16:11:31 +0000579 bool x = createDirectoryHelper(beg, c, create_parents);
580 *c = '/';
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000581
Ted Kremenekc5412c52008-04-03 16:11:31 +0000582 // Return if we encountered an error.
583 if (x)
584 return true;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000585
Ted Kremenekc5412c52008-04-03 16:11:31 +0000586 break;
587 }
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000588 }
589
Ted Kremenekc5412c52008-04-03 16:11:31 +0000590 return mkdir(beg, S_IRWXU | S_IRWXG) != 0;
591}
592
Reid Spencer8e665952004-08-29 05:24:01 +0000593bool
Reid Spencere5c9cb52006-08-23 00:39:35 +0000594Path::createDirectoryOnDisk( bool create_parents, std::string* ErrMsg ) {
Reid Spencer8e665952004-08-29 05:24:01 +0000595 // Get a writeable copy of the path name
596 char pathname[MAXPATHLEN];
597 path.copy(pathname,MAXPATHLEN);
598
599 // Null-terminate the last component
Evan Cheng34cd4a42008-05-05 18:30:58 +0000600 size_t lastchar = path.length() - 1 ;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000601
Ted Kremenekc5412c52008-04-03 16:11:31 +0000602 if (pathname[lastchar] != '/')
603 ++lastchar;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000604
Ted Kremenekc5412c52008-04-03 16:11:31 +0000605 pathname[lastchar] = 0;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000606
Ted Kremenekc5412c52008-04-03 16:11:31 +0000607 if (createDirectoryHelper(pathname, pathname+lastchar, create_parents))
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000608 return MakeErrMsg(ErrMsg,
Ted Kremenekc5412c52008-04-03 16:11:31 +0000609 std::string(pathname) + ": can't create directory");
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000610
Reid Spencere5c9cb52006-08-23 00:39:35 +0000611 return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000612}
613
614bool
Reid Spencere5c9cb52006-08-23 00:39:35 +0000615Path::createFileOnDisk(std::string* ErrMsg) {
Reid Spencer8e665952004-08-29 05:24:01 +0000616 // Create the file
Reid Spencer622e2202004-09-18 19:25:11 +0000617 int fd = ::creat(path.c_str(), S_IRUSR | S_IWUSR);
Reid Spencer5a060772006-08-23 07:30:48 +0000618 if (fd < 0)
619 return MakeErrMsg(ErrMsg, path + ": can't create file");
Reid Spencer622e2202004-09-18 19:25:11 +0000620 ::close(fd);
Reid Spencere5c9cb52006-08-23 00:39:35 +0000621 return false;
Reid Spencerb89a2232004-08-25 06:20:07 +0000622}
623
Reid Spencer8e665952004-08-29 05:24:01 +0000624bool
Reid Spencere5c9cb52006-08-23 00:39:35 +0000625Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
Reid Spencerc29befb2004-12-15 01:50:13 +0000626 // Make this into a unique file name
Reid Spencer51c5a282006-08-23 20:34:57 +0000627 if (makeUnique( reuse_current, ErrMsg ))
628 return true;
Reid Spencerc29befb2004-12-15 01:50:13 +0000629
630 // create the file
Reid Spencere5c9cb52006-08-23 00:39:35 +0000631 int fd = ::open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000632 if (fd < 0)
Reid Spencer5a060772006-08-23 07:30:48 +0000633 return MakeErrMsg(ErrMsg, path + ": can't create temporary file");
Reid Spencere5c9cb52006-08-23 00:39:35 +0000634 ::close(fd);
Reid Spencerc29befb2004-12-15 01:50:13 +0000635 return false;
Reid Spencer9195f372004-11-09 20:26:31 +0000636}
637
638bool
Chris Lattner0c332312006-07-28 22:29:50 +0000639Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
Reid Spencer2ae9d112007-04-07 18:52:17 +0000640 // Get the status so we can determin if its a file or directory
641 struct stat buf;
642 if (0 != stat(path.c_str(), &buf)) {
643 MakeErrMsg(ErrStr, path + ": can't get status of file");
Chris Lattner0c332312006-07-28 22:29:50 +0000644 return true;
Reid Spencer2ae9d112007-04-07 18:52:17 +0000645 }
646
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000647 // Note: this check catches strange situations. In all cases, LLVM should
648 // only be involved in the creation and deletion of regular files. This
649 // check ensures that what we're trying to erase is a regular file. It
650 // effectively prevents LLVM from erasing things like /dev/null, any block
651 // special file, or other things that aren't "regular" files.
Reid Spencer2ae9d112007-04-07 18:52:17 +0000652 if (S_ISREG(buf.st_mode)) {
653 if (unlink(path.c_str()) != 0)
654 return MakeErrMsg(ErrStr, path + ": can't destroy file");
655 return false;
656 }
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000657
Reid Spencer2ae9d112007-04-07 18:52:17 +0000658 if (!S_ISDIR(buf.st_mode)) {
659 if (ErrStr) *ErrStr = "not a file or directory";
660 return true;
661 }
Reid Spencer8475ec02007-03-29 19:05:44 +0000662
Chris Lattner0c332312006-07-28 22:29:50 +0000663 if (remove_contents) {
664 // Recursively descend the directory to remove its contents.
665 std::string cmd = "/bin/rm -rf " + path;
Mikhail Glushenkov8eada622009-02-15 03:20:32 +0000666 if (system(cmd.c_str()) != 0) {
667 MakeErrMsg(ErrStr, path + ": failed to recursively remove directory.");
668 return true;
669 }
Chris Lattner0c332312006-07-28 22:29:50 +0000670 return false;
671 }
672
673 // Otherwise, try to just remove the one directory.
674 char pathname[MAXPATHLEN];
675 path.copy(pathname, MAXPATHLEN);
Evan Cheng34cd4a42008-05-05 18:30:58 +0000676 size_t lastchar = path.length() - 1;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000677 if (pathname[lastchar] == '/')
Chris Lattner0c332312006-07-28 22:29:50 +0000678 pathname[lastchar] = 0;
679 else
680 pathname[lastchar+1] = 0;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000681
Chris Lattner0c332312006-07-28 22:29:50 +0000682 if (rmdir(pathname) != 0)
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000683 return MakeErrMsg(ErrStr,
Reid Spencer8475ec02007-03-29 19:05:44 +0000684 std::string(pathname) + ": can't erase directory");
Chris Lattner0c332312006-07-28 22:29:50 +0000685 return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000686}
687
688bool
Reid Spencer5a060772006-08-23 07:30:48 +0000689Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000690 if (0 != ::rename(path.c_str(), newName.c_str()))
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000691 return MakeErrMsg(ErrMsg, std::string("can't rename '") + path + "' as '" +
Reid Spencer2aafadc2005-04-21 02:50:10 +0000692 newName.toString() + "' ");
Reid Spencer5a060772006-08-23 07:30:48 +0000693 return false;
Reid Spencereaf18152004-11-14 22:08:36 +0000694}
695
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000696bool
Chris Lattner1bebfb52006-07-28 22:36:17 +0000697Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrStr) const {
Reid Spencereaf18152004-11-14 22:08:36 +0000698 struct utimbuf utb;
699 utb.actime = si.modTime.toPosixTime();
700 utb.modtime = utb.actime;
701 if (0 != ::utime(path.c_str(),&utb))
Reid Spencer51c5a282006-08-23 20:34:57 +0000702 return MakeErrMsg(ErrStr, path + ": can't set file modification time");
Reid Spencereaf18152004-11-14 22:08:36 +0000703 if (0 != ::chmod(path.c_str(),si.mode))
Reid Spencer51c5a282006-08-23 20:34:57 +0000704 return MakeErrMsg(ErrStr, path + ": can't set mode");
Chris Lattner1bebfb52006-07-28 22:36:17 +0000705 return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000706}
707
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000708bool
Reid Spencer51c5a282006-08-23 20:34:57 +0000709sys::CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg){
Reid Spencerc29befb2004-12-15 01:50:13 +0000710 int inFile = -1;
711 int outFile = -1;
Reid Spencer51c5a282006-08-23 20:34:57 +0000712 inFile = ::open(Src.c_str(), O_RDONLY);
713 if (inFile == -1)
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000714 return MakeErrMsg(ErrMsg, Src.toString() +
Reid Spencer51c5a282006-08-23 20:34:57 +0000715 ": can't open source file to copy");
Reid Spencerc29befb2004-12-15 01:50:13 +0000716
Reid Spencer51c5a282006-08-23 20:34:57 +0000717 outFile = ::open(Dest.c_str(), O_WRONLY|O_CREAT, 0666);
718 if (outFile == -1) {
719 ::close(inFile);
720 return MakeErrMsg(ErrMsg, Dest.toString() +
721 ": can't create destination file for copy");
722 }
Reid Spencerc29befb2004-12-15 01:50:13 +0000723
Reid Spencer51c5a282006-08-23 20:34:57 +0000724 char Buffer[16*1024];
725 while (ssize_t Amt = ::read(inFile, Buffer, 16*1024)) {
726 if (Amt == -1) {
727 if (errno != EINTR && errno != EAGAIN) {
728 ::close(inFile);
729 ::close(outFile);
730 return MakeErrMsg(ErrMsg, Src.toString()+": can't read source file: ");
731 }
732 } else {
733 char *BufPtr = Buffer;
734 while (Amt) {
735 ssize_t AmtWritten = ::write(outFile, BufPtr, Amt);
736 if (AmtWritten == -1) {
737 if (errno != EINTR && errno != EAGAIN) {
738 ::close(inFile);
739 ::close(outFile);
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000740 return MakeErrMsg(ErrMsg, Dest.toString() +
Reid Spencer51c5a282006-08-23 20:34:57 +0000741 ": can't write destination file: ");
Reid Spencerc29befb2004-12-15 01:50:13 +0000742 }
Reid Spencer51c5a282006-08-23 20:34:57 +0000743 } else {
744 Amt -= AmtWritten;
745 BufPtr += AmtWritten;
Reid Spencerc29befb2004-12-15 01:50:13 +0000746 }
747 }
748 }
Reid Spencerc29befb2004-12-15 01:50:13 +0000749 }
Reid Spencer51c5a282006-08-23 20:34:57 +0000750 ::close(inFile);
751 ::close(outFile);
752 return false;
Reid Spencerc29befb2004-12-15 01:50:13 +0000753}
754
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000755bool
Reid Spencer51c5a282006-08-23 20:34:57 +0000756Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000757 if (reuse_current && !exists())
Reid Spencer51c5a282006-08-23 20:34:57 +0000758 return false; // File doesn't exist already, just use it!
Reid Spencerc29befb2004-12-15 01:50:13 +0000759
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000760 // Append an XXXXXX pattern to the end of the file for use with mkstemp,
Reid Spencerc29befb2004-12-15 01:50:13 +0000761 // mktemp or our own implementation.
762 char *FNBuffer = (char*) alloca(path.size()+8);
Devang Patelce8fdf12008-07-22 20:02:39 +0000763 path.copy(FNBuffer,path.size());
Devang Patelbdfa9ac2008-07-24 00:35:38 +0000764 if (isDirectory())
765 strcpy(FNBuffer+path.size(), "/XXXXXX");
766 else
Devang Patelce8fdf12008-07-22 20:02:39 +0000767 strcpy(FNBuffer+path.size(), "-XXXXXX");
Reid Spencerc29befb2004-12-15 01:50:13 +0000768
769#if defined(HAVE_MKSTEMP)
770 int TempFD;
Reid Spencer51c5a282006-08-23 20:34:57 +0000771 if ((TempFD = mkstemp(FNBuffer)) == -1)
772 return MakeErrMsg(ErrMsg, path + ": can't make unique filename");
Reid Spencerc29befb2004-12-15 01:50:13 +0000773
774 // We don't need to hold the temp file descriptor... we will trust that no one
775 // will overwrite/delete the file before we can open it again.
776 close(TempFD);
777
778 // Save the name
779 path = FNBuffer;
780#elif defined(HAVE_MKTEMP)
781 // If we don't have mkstemp, use the old and obsolete mktemp function.
Reid Spencer51c5a282006-08-23 20:34:57 +0000782 if (mktemp(FNBuffer) == 0)
783 return MakeErrMsg(ErrMsg, path + ": can't make unique filename");
Reid Spencerc29befb2004-12-15 01:50:13 +0000784
785 // Save the name
786 path = FNBuffer;
787#else
788 // Okay, looks like we have to do it all by our lonesome.
789 static unsigned FCounter = 0;
790 unsigned offset = path.size() + 1;
791 while ( FCounter < 999999 && exists()) {
792 sprintf(FNBuffer+offset,"%06u",++FCounter);
793 path = FNBuffer;
794 }
795 if (FCounter > 999999)
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000796 return MakeErrMsg(ErrMsg,
Reid Spencer51c5a282006-08-23 20:34:57 +0000797 path + ": can't make unique filename: too many files");
Reid Spencerc29befb2004-12-15 01:50:13 +0000798#endif
Reid Spencer51c5a282006-08-23 20:34:57 +0000799 return false;
800}
Reid Spencerc29befb2004-12-15 01:50:13 +0000801
Chris Lattner799ed102008-04-01 06:00:12 +0000802const char *Path::MapInFilePages(int FD, uint64_t FileSize) {
Chris Lattner9ffd19a2008-04-01 06:16:24 +0000803 int Flags = MAP_PRIVATE;
804#ifdef MAP_FILE
805 Flags |= MAP_FILE;
806#endif
807 void *BasePtr = ::mmap(0, FileSize, PROT_READ, Flags, FD, 0);
808 if (BasePtr == MAP_FAILED)
809 return 0;
Chris Lattner14c762d2008-04-01 06:25:23 +0000810 return (const char*)BasePtr;
Chris Lattner799ed102008-04-01 06:00:12 +0000811}
812
Chris Lattner9ffd19a2008-04-01 06:16:24 +0000813void Path::UnMapFilePages(const char *BasePtr, uint64_t FileSize) {
Chris Lattner14c762d2008-04-01 06:25:23 +0000814 ::munmap((void*)BasePtr, FileSize);
Chris Lattner799ed102008-04-01 06:00:12 +0000815}
816
Reid Spencer51c5a282006-08-23 20:34:57 +0000817} // end llvm namespace
Reid Spencerb89a2232004-08-25 06:20:07 +0000818