blob: d7aa7115cad7d15462cc5c000d1a64cbf8bba32d [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
Chris Lattnerdbe89cd2009-08-23 22:57:38 +000019#include "llvm/ADT/SmallVector.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;
Chris Lattner0f550142009-07-12 19:01:16 +000095 return path.length() < MAXPATHLEN;
Reid Spencer69a16162004-12-24 06:29:42 +000096}
97
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +000098bool
Chris Lattner88d7e402009-06-15 04:17:07 +000099Path::isAbsolute(const char *NameStart, unsigned NameLen) {
100 assert(NameStart);
101 if (NameLen == 0)
102 return false;
103 return NameStart[0] == '/';
104}
105
106bool
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}
Daniel Dunbara00e85c2009-07-12 20:23:56 +0000112
113void Path::makeAbsolute() {
114 if (isAbsolute())
115 return;
116
117 Path CWD = Path::GetCurrentDirectory();
118 assert(CWD.isAbsolute() && "GetCurrentDirectory returned relative path!");
119
120 CWD.appendComponent(path);
121
Chris Lattner74382b72009-08-23 22:45:37 +0000122 path = CWD.str();
Daniel Dunbara00e85c2009-07-12 20:23:56 +0000123}
124
Reid Spencer8e665952004-08-29 05:24:01 +0000125Path
126Path::GetRootDirectory() {
127 Path result;
Reid Spencerdd04df02005-07-07 23:21:43 +0000128 result.set("/");
Reid Spencer8e665952004-08-29 05:24:01 +0000129 return result;
130}
131
Reid Spencer69a16162004-12-24 06:29:42 +0000132Path
Chris Lattnerd18a2c12009-02-19 05:34:35 +0000133Path::GetTemporaryDirectory(std::string *ErrMsg) {
Reid Spencer69a16162004-12-24 06:29:42 +0000134#if defined(HAVE_MKDTEMP)
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000135 // The best way is with mkdtemp but that's not available on many systems,
Reid Spencer69a16162004-12-24 06:29:42 +0000136 // Linux and FreeBSD have it. Others probably won't.
137 char pathname[MAXPATHLEN];
138 strcpy(pathname,"/tmp/llvm_XXXXXX");
Reid Spencer48744762006-08-22 19:01:30 +0000139 if (0 == mkdtemp(pathname)) {
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000140 MakeErrMsg(ErrMsg,
Reid Spencer48744762006-08-22 19:01:30 +0000141 std::string(pathname) + ": can't create temporary directory");
142 return Path();
143 }
Reid Spencer69a16162004-12-24 06:29:42 +0000144 Path result;
Reid Spencerdd04df02005-07-07 23:21:43 +0000145 result.set(pathname);
Reid Spencer69a16162004-12-24 06:29:42 +0000146 assert(result.isValid() && "mkdtemp didn't create a valid pathname!");
147 return result;
148#elif defined(HAVE_MKSTEMP)
149 // If no mkdtemp is available, mkstemp can be used to create a temporary file
150 // which is then removed and created as a directory. We prefer this over
151 // mktemp because of mktemp's inherent security and threading risks. We still
152 // have a slight race condition from the time the temporary file is created to
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000153 // the time it is re-created as a directoy.
Reid Spencer69a16162004-12-24 06:29:42 +0000154 char pathname[MAXPATHLEN];
155 strcpy(pathname, "/tmp/llvm_XXXXXX");
156 int fd = 0;
Reid Spencer48744762006-08-22 19:01:30 +0000157 if (-1 == (fd = mkstemp(pathname))) {
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000158 MakeErrMsg(ErrMsg,
Reid Spencer48744762006-08-22 19:01:30 +0000159 std::string(pathname) + ": can't create temporary directory");
160 return Path();
161 }
Reid Spencer69a16162004-12-24 06:29:42 +0000162 ::close(fd);
163 ::unlink(pathname); // start race condition, ignore errors
Reid Spencer48744762006-08-22 19:01:30 +0000164 if (-1 == ::mkdir(pathname, S_IRWXU)) { // end race condition
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000165 MakeErrMsg(ErrMsg,
Reid Spencer48744762006-08-22 19:01:30 +0000166 std::string(pathname) + ": can't create temporary directory");
167 return Path();
168 }
Reid Spencer69a16162004-12-24 06:29:42 +0000169 Path result;
Reid Spencerdd04df02005-07-07 23:21:43 +0000170 result.set(pathname);
Reid Spencer69a16162004-12-24 06:29:42 +0000171 assert(result.isValid() && "mkstemp didn't create a valid pathname!");
172 return result;
173#elif defined(HAVE_MKTEMP)
174 // If a system doesn't have mkdtemp(3) or mkstemp(3) but it does have
175 // mktemp(3) then we'll assume that system (e.g. AIX) has a reasonable
176 // implementation of mktemp(3) and doesn't follow BSD 4.3's lead of replacing
177 // the XXXXXX with the pid of the process and a letter. That leads to only
178 // twenty six temporary files that can be generated.
179 char pathname[MAXPATHLEN];
180 strcpy(pathname, "/tmp/llvm_XXXXXX");
181 char *TmpName = ::mktemp(pathname);
Reid Spencer48744762006-08-22 19:01:30 +0000182 if (TmpName == 0) {
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000183 MakeErrMsg(ErrMsg,
Reid Spencer48744762006-08-22 19:01:30 +0000184 std::string(TmpName) + ": can't create unique directory name");
185 return Path();
186 }
187 if (-1 == ::mkdir(TmpName, S_IRWXU)) {
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000188 MakeErrMsg(ErrMsg,
Reid Spencer48744762006-08-22 19:01:30 +0000189 std::string(TmpName) + ": can't create temporary directory");
190 return Path();
191 }
Reid Spencer69a16162004-12-24 06:29:42 +0000192 Path result;
Reid Spencerdd04df02005-07-07 23:21:43 +0000193 result.set(TmpName);
Reid Spencer69a16162004-12-24 06:29:42 +0000194 assert(result.isValid() && "mktemp didn't create a valid pathname!");
195 return result;
196#else
197 // This is the worst case implementation. tempnam(3) leaks memory unless its
198 // on an SVID2 (or later) system. On BSD 4.3 it leaks. tmpnam(3) has thread
199 // issues. The mktemp(3) function doesn't have enough variability in the
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000200 // temporary name generated. So, we provide our own implementation that
Reid Spencer69a16162004-12-24 06:29:42 +0000201 // increments an integer from a random number seeded by the current time. This
202 // should be sufficiently unique that we don't have many collisions between
203 // processes. Generally LLVM processes don't run very long and don't use very
204 // many temporary files so this shouldn't be a big issue for LLVM.
205 static time_t num = ::time(0);
206 char pathname[MAXPATHLEN];
207 do {
208 num++;
209 sprintf(pathname, "/tmp/llvm_%010u", unsigned(num));
210 } while ( 0 == access(pathname, F_OK ) );
Reid Spencer48744762006-08-22 19:01:30 +0000211 if (-1 == ::mkdir(pathname, S_IRWXU)) {
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000212 MakeErrMsg(ErrMsg,
Reid Spencer48744762006-08-22 19:01:30 +0000213 std::string(pathname) + ": can't create temporary directory");
214 return Path();
Reid Spencer51c5a282006-08-23 20:34:57 +0000215 }
Reid Spencer69a16162004-12-24 06:29:42 +0000216 Path result;
Reid Spencerdd04df02005-07-07 23:21:43 +0000217 result.set(pathname);
Reid Spencer69a16162004-12-24 06:29:42 +0000218 assert(result.isValid() && "mkstemp didn't create a valid pathname!");
219 return result;
220#endif
221}
222
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000223void
Reid Spencer1b6b99b2004-12-13 03:00:51 +0000224Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
225#ifdef LTDL_SHLIBPATH_VAR
226 char* env_var = getenv(LTDL_SHLIBPATH_VAR);
227 if (env_var != 0) {
228 getPathList(env_var,Paths);
Reid Spencer74e72612004-09-14 00:16:39 +0000229 }
Reid Spencer1b6b99b2004-12-13 03:00:51 +0000230#endif
231 // FIXME: Should this look at LD_LIBRARY_PATH too?
232 Paths.push_back(sys::Path("/usr/local/lib/"));
233 Paths.push_back(sys::Path("/usr/X11R6/lib/"));
234 Paths.push_back(sys::Path("/usr/lib/"));
235 Paths.push_back(sys::Path("/lib/"));
Reid Spencer74e72612004-09-14 00:16:39 +0000236}
237
Reid Spencer1b6b99b2004-12-13 03:00:51 +0000238void
Gabor Greifa99be512007-07-05 17:07:56 +0000239Path::GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths) {
Reid Spencer1b6b99b2004-12-13 03:00:51 +0000240 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
241 if (env_var != 0) {
242 getPathList(env_var,Paths);
243 }
Reid Spencer1b6b99b2004-12-13 03:00:51 +0000244#ifdef LLVM_LIBDIR
245 {
246 Path tmpPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000247 if (tmpPath.set(LLVM_LIBDIR))
Reid Spencerc7f08322005-07-07 18:21:42 +0000248 if (tmpPath.canRead())
Reid Spencer1b6b99b2004-12-13 03:00:51 +0000249 Paths.push_back(tmpPath);
250 }
251#endif
252 GetSystemLibraryPaths(Paths);
Reid Spencer8e665952004-08-29 05:24:01 +0000253}
254
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000255Path
Reid Spencer8e665952004-08-29 05:24:01 +0000256Path::GetLLVMDefaultConfigDir() {
257 return Path("/etc/llvm/");
258}
259
Reid Spencer8e665952004-08-29 05:24:01 +0000260Path
261Path::GetUserHomeDirectory() {
262 const char* home = getenv("HOME");
263 if (home) {
264 Path result;
Reid Spencerdd04df02005-07-07 23:21:43 +0000265 if (result.set(home))
Reid Spencer8e665952004-08-29 05:24:01 +0000266 return result;
267 }
268 return GetRootDirectory();
269}
270
Ted Kremenek79200782007-12-18 22:07:33 +0000271Path
272Path::GetCurrentDirectory() {
273 char pathname[MAXPATHLEN];
274 if (!getcwd(pathname,MAXPATHLEN)) {
275 assert (false && "Could not query current working directory.");
276 return Path("");
277 }
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000278
Ted Kremenek79200782007-12-18 22:07:33 +0000279 return Path(pathname);
280}
Reid Spencera229c5c2005-07-08 03:08:58 +0000281
Chris Lattner893a0f92009-03-02 22:17:15 +0000282#ifdef __FreeBSD__
283static int
284test_dir(char buf[PATH_MAX], char ret[PATH_MAX],
285 const char *dir, const char *bin)
286{
Bill Wendling51b16f42009-05-30 01:09:53 +0000287 struct stat sb;
Chris Lattner893a0f92009-03-02 22:17:15 +0000288
Bill Wendling51b16f42009-05-30 01:09:53 +0000289 snprintf(buf, PATH_MAX, "%s//%s", dir, bin);
290 if (realpath(buf, ret) == NULL)
291 return (1);
292 if (stat(buf, &sb) != 0)
293 return (1);
294
295 return (0);
Chris Lattner893a0f92009-03-02 22:17:15 +0000296}
297
298static char *
299getprogpath(char ret[PATH_MAX], const char *bin)
300{
Bill Wendling51b16f42009-05-30 01:09:53 +0000301 char *pv, *s, *t, buf[PATH_MAX];
Chris Lattner893a0f92009-03-02 22:17:15 +0000302
Bill Wendling51b16f42009-05-30 01:09:53 +0000303 /* First approach: absolute path. */
304 if (bin[0] == '/') {
305 if (test_dir(buf, ret, "/", bin) == 0)
306 return (ret);
307 return (NULL);
308 }
Chris Lattner893a0f92009-03-02 22:17:15 +0000309
Bill Wendling51b16f42009-05-30 01:09:53 +0000310 /* Second approach: relative path. */
311 if (strchr(bin, '/') != NULL) {
312 if (getcwd(buf, PATH_MAX) == NULL)
313 return (NULL);
314 if (test_dir(buf, ret, buf, bin) == 0)
315 return (ret);
316 return (NULL);
317 }
Chris Lattner893a0f92009-03-02 22:17:15 +0000318
Bill Wendling51b16f42009-05-30 01:09:53 +0000319 /* Third approach: $PATH */
320 if ((pv = getenv("PATH")) == NULL)
321 return (NULL);
322 s = pv = strdup(pv);
323 if (pv == NULL)
324 return (NULL);
325 while ((t = strsep(&s, ":")) != NULL) {
326 if (test_dir(buf, ret, t, bin) == 0) {
327 free(pv);
328 return (ret);
329 }
330 }
331 free(pv);
332 return (NULL);
Chris Lattner893a0f92009-03-02 22:17:15 +0000333}
334#endif
335
Chris Lattner1a091442008-03-03 02:55:43 +0000336/// GetMainExecutable - Return the path to the main executable, given the
337/// value of argv[0] from program startup.
338Path Path::GetMainExecutable(const char *argv0, void *MainAddr) {
Chris Lattner893a0f92009-03-02 22:17:15 +0000339#if defined(__FreeBSD__)
340 char exe_path[PATH_MAX];
341
342 if (getprogpath(exe_path, argv0) != NULL)
343 return Path(std::string(exe_path));
344#elif defined(__linux__) || defined(__CYGWIN__)
Chris Lattner3bdfa042008-03-13 05:22:05 +0000345 char exe_path[MAXPATHLEN];
Seo Sanghyeonfd6437f2008-06-27 22:55:30 +0000346 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path));
Dan Gohman7b3544b2009-08-05 20:16:55 +0000347 if (len >= 0)
348 return Path(std::string(exe_path, len));
Chris Lattner3bdfa042008-03-13 05:22:05 +0000349#elif defined(HAVE_DLFCN_H)
Chris Lattner1a091442008-03-03 02:55:43 +0000350 // Use dladdr to get executable path if available.
Chris Lattner1a091442008-03-03 02:55:43 +0000351 Dl_info DLInfo;
352 int err = dladdr(MainAddr, &DLInfo);
Chris Lattnerd18a2c12009-02-19 05:34:35 +0000353 if (err == 0)
354 return Path();
Bill Wendling51b16f42009-05-30 01:09:53 +0000355
Chris Lattnerd18a2c12009-02-19 05:34:35 +0000356 // If the filename is a symlink, we need to resolve and return the location of
357 // the actual executable.
358 char link_path[MAXPATHLEN];
359 return Path(std::string(realpath(DLInfo.dli_fname, link_path)));
Chris Lattner1a091442008-03-03 02:55:43 +0000360#endif
361 return Path();
362}
363
364
Ted Kremenek9b01cc02008-04-07 22:01:32 +0000365std::string Path::getDirname() const {
366 return getDirnameCharSep(path, '/');
367}
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000368
Reid Spencer1b554b42004-09-11 04:55:08 +0000369std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000370Path::getBasename() const {
Reid Spencer1b554b42004-09-11 04:55:08 +0000371 // Find the last slash
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000372 std::string::size_type slash = path.rfind('/');
Reid Spencer1b554b42004-09-11 04:55:08 +0000373 if (slash == std::string::npos)
374 slash = 0;
375 else
376 slash++;
377
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000378 std::string::size_type dot = path.rfind('.');
Jeff Cohen73f36672005-07-09 18:42:02 +0000379 if (dot == std::string::npos || dot < slash)
380 return path.substr(slash);
381 else
382 return path.substr(slash, dot - slash);
Reid Spencer1b554b42004-09-11 04:55:08 +0000383}
384
Argyrios Kyrtzidisfc199882008-06-15 15:15:19 +0000385std::string
386Path::getSuffix() const {
387 // Find the last slash
388 std::string::size_type slash = path.rfind('/');
389 if (slash == std::string::npos)
390 slash = 0;
391 else
392 slash++;
393
394 std::string::size_type dot = path.rfind('.');
395 if (dot == std::string::npos || dot < slash)
Wojciech Matyjewicz93c53462008-06-15 18:02:47 +0000396 return std::string();
Argyrios Kyrtzidisfc199882008-06-15 15:15:19 +0000397 else
398 return path.substr(dot + 1);
399}
400
Reid Spencereaf18152004-11-14 22:08:36 +0000401bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
Reid Spencerbe31d2a2004-11-16 17:14:08 +0000402 assert(len < 1024 && "Request for magic string too long");
Chris Lattnerdbe89cd2009-08-23 22:57:38 +0000403 SmallVector<char, 128> Buf;
404 Buf.resize(1 + len);
405 char* buf = Buf.data();
Chris Lattnerc7c453a2006-08-01 17:51:09 +0000406 int fd = ::open(path.c_str(), O_RDONLY);
Reid Spencerbe31d2a2004-11-16 17:14:08 +0000407 if (fd < 0)
408 return false;
Reid Spencer86ac2dc2004-12-02 09:09:48 +0000409 ssize_t bytes_read = ::read(fd, buf, len);
410 ::close(fd);
411 if (ssize_t(len) != bytes_read) {
412 Magic.clear();
Reid Spencerbe31d2a2004-11-16 17:14:08 +0000413 return false;
Reid Spencer86ac2dc2004-12-02 09:09:48 +0000414 }
415 Magic.assign(buf,len);
Reid Spencereaf18152004-11-14 22:08:36 +0000416 return true;
417}
418
Reid Spencer1b554b42004-09-11 04:55:08 +0000419bool
Reid Spencer8e665952004-08-29 05:24:01 +0000420Path::exists() const {
421 return 0 == access(path.c_str(), F_OK );
422}
423
424bool
Ted Kremenekfd527112007-12-18 19:46:22 +0000425Path::isDirectory() const {
426 struct stat buf;
427 if (0 != stat(path.c_str(), &buf))
428 return false;
429 return buf.st_mode & S_IFDIR ? true : false;
430}
431
432bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000433Path::canRead() const {
Dan Gohman73c74ea2009-07-28 23:22:01 +0000434 return 0 == access(path.c_str(), R_OK);
Reid Spencer8e665952004-08-29 05:24:01 +0000435}
436
437bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000438Path::canWrite() const {
Dan Gohman73c74ea2009-07-28 23:22:01 +0000439 return 0 == access(path.c_str(), W_OK);
Reid Spencer8e665952004-08-29 05:24:01 +0000440}
441
442bool
Reid Spencerc7f08322005-07-07 18:21:42 +0000443Path::canExecute() const {
Reid Spencer8b2d1aa2005-07-08 17:46:10 +0000444 if (0 != access(path.c_str(), R_OK | X_OK ))
445 return false;
Reid Spencer2ae9d112007-04-07 18:52:17 +0000446 struct stat buf;
447 if (0 != stat(path.c_str(), &buf))
448 return false;
449 if (!S_ISREG(buf.st_mode))
Misha Brukman8177bf82005-04-20 15:33:22 +0000450 return false;
Reid Spencer8b2d1aa2005-07-08 17:46:10 +0000451 return true;
Reid Spencer8e665952004-08-29 05:24:01 +0000452}
453
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000454std::string
Reid Spencer8e665952004-08-29 05:24:01 +0000455Path::getLast() const {
456 // Find the last slash
457 size_t pos = path.rfind('/');
458
459 // Handle the corner cases
460 if (pos == std::string::npos)
461 return path;
462
463 // If the last character is a slash
464 if (pos == path.length()-1) {
465 // Find the second to last slash
466 size_t pos2 = path.rfind('/', pos-1);
467 if (pos2 == std::string::npos)
468 return path.substr(0,pos);
469 else
470 return path.substr(pos2+1,pos-pos2-1);
471 }
472 // Return everything after the last slash
473 return path.substr(pos+1);
474}
475
Reid Spencer2ae9d112007-04-07 18:52:17 +0000476const FileStatus *
477PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const {
478 if (!fsIsValid || update) {
Reid Spencer69cce812007-03-29 16:43:20 +0000479 struct stat buf;
Reid Spencer8475ec02007-03-29 19:05:44 +0000480 if (0 != stat(path.c_str(), &buf)) {
481 MakeErrMsg(ErrStr, path + ": can't get status of file");
482 return 0;
483 }
Reid Spencer2ae9d112007-04-07 18:52:17 +0000484 status.fileSize = buf.st_size;
485 status.modTime.fromEpochTime(buf.st_mtime);
486 status.mode = buf.st_mode;
487 status.user = buf.st_uid;
488 status.group = buf.st_gid;
489 status.uniqueID = uint64_t(buf.st_ino);
490 status.isDir = S_ISDIR(buf.st_mode);
491 status.isFile = S_ISREG(buf.st_mode);
492 fsIsValid = true;
Reid Spencer69cce812007-03-29 16:43:20 +0000493 }
Reid Spencer2ae9d112007-04-07 18:52:17 +0000494 return &status;
Reid Spencereaf18152004-11-14 22:08:36 +0000495}
496
Chris Lattner252ad032006-07-28 22:03:44 +0000497static bool AddPermissionBits(const Path &File, int bits) {
Reid Spencer77cc91d2004-12-13 19:59:50 +0000498 // Get the umask value from the operating system. We want to use it
499 // when changing the file's permissions. Since calling umask() sets
500 // the umask and returns its old value, we must call it a second
501 // time to reset it to the user's preference.
502 int mask = umask(0777); // The arg. to umask is arbitrary.
503 umask(mask); // Restore the umask.
504
505 // Get the file's current mode.
Reid Spencer2ae9d112007-04-07 18:52:17 +0000506 struct stat buf;
Chris Lattner74382b72009-08-23 22:45:37 +0000507 if (0 != stat(File.c_str(), &buf))
Reid Spencer77cc91d2004-12-13 19:59:50 +0000508 return false;
Reid Spencer2ae9d112007-04-07 18:52:17 +0000509 // Change the file to have whichever permissions bits from 'bits'
510 // that the umask would not disable.
511 if ((chmod(File.c_str(), (buf.st_mode | (bits & ~mask)))) == -1)
512 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000513 return true;
514}
515
Reid Spencere1647f42006-08-22 23:27:23 +0000516bool Path::makeReadableOnDisk(std::string* ErrMsg) {
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000517 if (!AddPermissionBits(*this, 0444))
Reid Spencer5a060772006-08-23 07:30:48 +0000518 return MakeErrMsg(ErrMsg, path + ": can't make file readable");
Reid Spencere1647f42006-08-22 23:27:23 +0000519 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000520}
521
Reid Spencere1647f42006-08-22 23:27:23 +0000522bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
Reid Spencer5a060772006-08-23 07:30:48 +0000523 if (!AddPermissionBits(*this, 0222))
524 return MakeErrMsg(ErrMsg, path + ": can't make file writable");
Reid Spencere1647f42006-08-22 23:27:23 +0000525 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000526}
527
Reid Spencere1647f42006-08-22 23:27:23 +0000528bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
Reid Spencer5a060772006-08-23 07:30:48 +0000529 if (!AddPermissionBits(*this, 0111))
530 return MakeErrMsg(ErrMsg, path + ": can't make file executable");
Reid Spencere1647f42006-08-22 23:27:23 +0000531 return false;
Reid Spencer77cc91d2004-12-13 19:59:50 +0000532}
533
Reid Spencereaf18152004-11-14 22:08:36 +0000534bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000535Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
Reid Spencereaf18152004-11-14 22:08:36 +0000536 DIR* direntries = ::opendir(path.c_str());
Reid Spencer5a060772006-08-23 07:30:48 +0000537 if (direntries == 0)
538 return MakeErrMsg(ErrMsg, path + ": can't open directory");
Reid Spencereaf18152004-11-14 22:08:36 +0000539
Reid Spencer3be872e2005-07-28 16:25:57 +0000540 std::string dirPath = path;
541 if (!lastIsSlash(dirPath))
542 dirPath += '/';
543
Reid Spencereaf18152004-11-14 22:08:36 +0000544 result.clear();
545 struct dirent* de = ::readdir(direntries);
Misha Brukman4619c752005-04-20 04:04:07 +0000546 for ( ; de != 0; de = ::readdir(direntries)) {
Reid Spencereaf18152004-11-14 22:08:36 +0000547 if (de->d_name[0] != '.') {
Reid Spencer3be872e2005-07-28 16:25:57 +0000548 Path aPath(dirPath + (const char*)de->d_name);
Chris Lattnerb14c3422006-07-07 21:21:06 +0000549 struct stat st;
550 if (0 != lstat(aPath.path.c_str(), &st)) {
551 if (S_ISLNK(st.st_mode))
Misha Brukman4619c752005-04-20 04:04:07 +0000552 continue; // dangling symlink -- ignore
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000553 return MakeErrMsg(ErrMsg,
Reid Spencer5a060772006-08-23 07:30:48 +0000554 aPath.path + ": can't determine file object type");
Misha Brukman4619c752005-04-20 04:04:07 +0000555 }
Reid Spencerb608a812004-11-16 06:15:19 +0000556 result.insert(aPath);
Reid Spencereaf18152004-11-14 22:08:36 +0000557 }
Reid Spencereaf18152004-11-14 22:08:36 +0000558 }
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000559
Reid Spencereaf18152004-11-14 22:08:36 +0000560 closedir(direntries);
Reid Spencer142ca8e2006-08-23 06:56:27 +0000561 return false;
Reid Spencer9195f372004-11-09 20:26:31 +0000562}
563
Reid Spencer8e665952004-08-29 05:24:01 +0000564bool
Reid Spencerdd04df02005-07-07 23:21:43 +0000565Path::set(const std::string& a_path) {
566 if (a_path.empty())
Reid Spencer8e665952004-08-29 05:24:01 +0000567 return false;
Reid Spencerdd04df02005-07-07 23:21:43 +0000568 std::string save(path);
Reid Spencer8e665952004-08-29 05:24:01 +0000569 path = a_path;
Reid Spencer07adb282004-11-05 22:15:36 +0000570 if (!isValid()) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000571 path = save;
Reid Spencer8e665952004-08-29 05:24:01 +0000572 return false;
573 }
574 return true;
575}
576
577bool
Reid Spencerdd04df02005-07-07 23:21:43 +0000578Path::appendComponent(const std::string& name) {
579 if (name.empty())
Reid Spencer8e665952004-08-29 05:24:01 +0000580 return false;
Reid Spencerdd04df02005-07-07 23:21:43 +0000581 std::string save(path);
Reid Spencer3be872e2005-07-28 16:25:57 +0000582 if (!lastIsSlash(path))
583 path += '/';
Reid Spencerdd04df02005-07-07 23:21:43 +0000584 path += name;
Reid Spencer07adb282004-11-05 22:15:36 +0000585 if (!isValid()) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000586 path = save;
Reid Spencer8e665952004-08-29 05:24:01 +0000587 return false;
588 }
589 return true;
590}
591
592bool
Reid Spencerdd04df02005-07-07 23:21:43 +0000593Path::eraseComponent() {
Reid Spencer8e665952004-08-29 05:24:01 +0000594 size_t slashpos = path.rfind('/',path.size());
Reid Spencerdd04df02005-07-07 23:21:43 +0000595 if (slashpos == 0 || slashpos == std::string::npos) {
596 path.erase();
597 return true;
598 }
Reid Spencer8e665952004-08-29 05:24:01 +0000599 if (slashpos == path.size() - 1)
600 slashpos = path.rfind('/',slashpos-1);
Reid Spencerdd04df02005-07-07 23:21:43 +0000601 if (slashpos == std::string::npos) {
602 path.erase();
603 return true;
604 }
Reid Spencer8e665952004-08-29 05:24:01 +0000605 path.erase(slashpos);
606 return true;
607}
608
609bool
Reid Spencer07adb282004-11-05 22:15:36 +0000610Path::appendSuffix(const std::string& suffix) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000611 std::string save(path);
Reid Spencer8e665952004-08-29 05:24:01 +0000612 path.append(".");
613 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000614 if (!isValid()) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000615 path = save;
Reid Spencer8e665952004-08-29 05:24:01 +0000616 return false;
617 }
618 return true;
619}
620
Reid Spencerdd04df02005-07-07 23:21:43 +0000621bool
622Path::eraseSuffix() {
Reid Spencer6371ccb2005-07-08 06:53:26 +0000623 std::string save = path;
Reid Spencer8e665952004-08-29 05:24:01 +0000624 size_t dotpos = path.rfind('.',path.size());
625 size_t slashpos = path.rfind('/',path.size());
Jeff Cohen563a17f2005-07-08 04:49:16 +0000626 if (dotpos != std::string::npos) {
Jeff Cohen73f36672005-07-09 18:42:02 +0000627 if (slashpos == std::string::npos || dotpos > slashpos+1) {
Jeff Cohen563a17f2005-07-08 04:49:16 +0000628 path.erase(dotpos, path.size()-dotpos);
Jeff Cohen85c716f2005-07-08 05:02:13 +0000629 return true;
Jeff Cohen563a17f2005-07-08 04:49:16 +0000630 }
Reid Spencer8e665952004-08-29 05:24:01 +0000631 }
Reid Spencer6371ccb2005-07-08 06:53:26 +0000632 if (!isValid())
633 path = save;
Jeff Cohen563a17f2005-07-08 04:49:16 +0000634 return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000635}
636
Ted Kremenekc5412c52008-04-03 16:11:31 +0000637static bool createDirectoryHelper(char* beg, char* end, bool create_parents) {
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000638
Dan Gohman28daa102009-07-29 00:02:58 +0000639 if (access(beg, R_OK | W_OK) == 0)
Ted Kremenekc5412c52008-04-03 16:11:31 +0000640 return false;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000641
Ted Kremenekc5412c52008-04-03 16:11:31 +0000642 if (create_parents) {
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000643
Ted Kremenekc5412c52008-04-03 16:11:31 +0000644 char* c = end;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000645
Ted Kremenekc5412c52008-04-03 16:11:31 +0000646 for (; c != beg; --c)
647 if (*c == '/') {
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000648
Ted Kremenekc5412c52008-04-03 16:11:31 +0000649 // Recurse to handling the parent directory.
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000650 *c = '\0';
Ted Kremenekc5412c52008-04-03 16:11:31 +0000651 bool x = createDirectoryHelper(beg, c, create_parents);
652 *c = '/';
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000653
Ted Kremenekc5412c52008-04-03 16:11:31 +0000654 // Return if we encountered an error.
655 if (x)
656 return true;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000657
Ted Kremenekc5412c52008-04-03 16:11:31 +0000658 break;
659 }
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000660 }
661
Ted Kremenekc5412c52008-04-03 16:11:31 +0000662 return mkdir(beg, S_IRWXU | S_IRWXG) != 0;
663}
664
Reid Spencer8e665952004-08-29 05:24:01 +0000665bool
Reid Spencere5c9cb52006-08-23 00:39:35 +0000666Path::createDirectoryOnDisk( bool create_parents, std::string* ErrMsg ) {
Reid Spencer8e665952004-08-29 05:24:01 +0000667 // Get a writeable copy of the path name
668 char pathname[MAXPATHLEN];
669 path.copy(pathname,MAXPATHLEN);
670
671 // Null-terminate the last component
Evan Cheng34cd4a42008-05-05 18:30:58 +0000672 size_t lastchar = path.length() - 1 ;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000673
Ted Kremenekc5412c52008-04-03 16:11:31 +0000674 if (pathname[lastchar] != '/')
675 ++lastchar;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000676
Ted Kremenekc5412c52008-04-03 16:11:31 +0000677 pathname[lastchar] = 0;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000678
Ted Kremenekc5412c52008-04-03 16:11:31 +0000679 if (createDirectoryHelper(pathname, pathname+lastchar, create_parents))
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000680 return MakeErrMsg(ErrMsg,
Ted Kremenekc5412c52008-04-03 16:11:31 +0000681 std::string(pathname) + ": can't create directory");
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000682
Reid Spencere5c9cb52006-08-23 00:39:35 +0000683 return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000684}
685
686bool
Reid Spencere5c9cb52006-08-23 00:39:35 +0000687Path::createFileOnDisk(std::string* ErrMsg) {
Reid Spencer8e665952004-08-29 05:24:01 +0000688 // Create the file
Reid Spencer622e2202004-09-18 19:25:11 +0000689 int fd = ::creat(path.c_str(), S_IRUSR | S_IWUSR);
Reid Spencer5a060772006-08-23 07:30:48 +0000690 if (fd < 0)
691 return MakeErrMsg(ErrMsg, path + ": can't create file");
Reid Spencer622e2202004-09-18 19:25:11 +0000692 ::close(fd);
Reid Spencere5c9cb52006-08-23 00:39:35 +0000693 return false;
Reid Spencerb89a2232004-08-25 06:20:07 +0000694}
695
Reid Spencer8e665952004-08-29 05:24:01 +0000696bool
Reid Spencere5c9cb52006-08-23 00:39:35 +0000697Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
Reid Spencerc29befb2004-12-15 01:50:13 +0000698 // Make this into a unique file name
Reid Spencer51c5a282006-08-23 20:34:57 +0000699 if (makeUnique( reuse_current, ErrMsg ))
700 return true;
Reid Spencerc29befb2004-12-15 01:50:13 +0000701
702 // create the file
Reid Spencere5c9cb52006-08-23 00:39:35 +0000703 int fd = ::open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000704 if (fd < 0)
Reid Spencer5a060772006-08-23 07:30:48 +0000705 return MakeErrMsg(ErrMsg, path + ": can't create temporary file");
Reid Spencere5c9cb52006-08-23 00:39:35 +0000706 ::close(fd);
Reid Spencerc29befb2004-12-15 01:50:13 +0000707 return false;
Reid Spencer9195f372004-11-09 20:26:31 +0000708}
709
710bool
Chris Lattner0c332312006-07-28 22:29:50 +0000711Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
Reid Spencer2ae9d112007-04-07 18:52:17 +0000712 // Get the status so we can determin if its a file or directory
713 struct stat buf;
714 if (0 != stat(path.c_str(), &buf)) {
715 MakeErrMsg(ErrStr, path + ": can't get status of file");
Chris Lattner0c332312006-07-28 22:29:50 +0000716 return true;
Reid Spencer2ae9d112007-04-07 18:52:17 +0000717 }
718
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000719 // Note: this check catches strange situations. In all cases, LLVM should
720 // only be involved in the creation and deletion of regular files. This
721 // check ensures that what we're trying to erase is a regular file. It
722 // effectively prevents LLVM from erasing things like /dev/null, any block
723 // special file, or other things that aren't "regular" files.
Reid Spencer2ae9d112007-04-07 18:52:17 +0000724 if (S_ISREG(buf.st_mode)) {
725 if (unlink(path.c_str()) != 0)
726 return MakeErrMsg(ErrStr, path + ": can't destroy file");
727 return false;
728 }
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000729
Reid Spencer2ae9d112007-04-07 18:52:17 +0000730 if (!S_ISDIR(buf.st_mode)) {
731 if (ErrStr) *ErrStr = "not a file or directory";
732 return true;
733 }
Reid Spencer8475ec02007-03-29 19:05:44 +0000734
Chris Lattner0c332312006-07-28 22:29:50 +0000735 if (remove_contents) {
736 // Recursively descend the directory to remove its contents.
737 std::string cmd = "/bin/rm -rf " + path;
Mikhail Glushenkov8eada622009-02-15 03:20:32 +0000738 if (system(cmd.c_str()) != 0) {
739 MakeErrMsg(ErrStr, path + ": failed to recursively remove directory.");
740 return true;
741 }
Chris Lattner0c332312006-07-28 22:29:50 +0000742 return false;
743 }
744
745 // Otherwise, try to just remove the one directory.
746 char pathname[MAXPATHLEN];
747 path.copy(pathname, MAXPATHLEN);
Evan Cheng34cd4a42008-05-05 18:30:58 +0000748 size_t lastchar = path.length() - 1;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000749 if (pathname[lastchar] == '/')
Chris Lattner0c332312006-07-28 22:29:50 +0000750 pathname[lastchar] = 0;
751 else
752 pathname[lastchar+1] = 0;
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000753
Chris Lattner0c332312006-07-28 22:29:50 +0000754 if (rmdir(pathname) != 0)
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000755 return MakeErrMsg(ErrStr,
Reid Spencer8475ec02007-03-29 19:05:44 +0000756 std::string(pathname) + ": can't erase directory");
Chris Lattner0c332312006-07-28 22:29:50 +0000757 return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000758}
759
760bool
Reid Spencer5a060772006-08-23 07:30:48 +0000761Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000762 if (0 != ::rename(path.c_str(), newName.c_str()))
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000763 return MakeErrMsg(ErrMsg, std::string("can't rename '") + path + "' as '" +
Chris Lattner74382b72009-08-23 22:45:37 +0000764 newName.str() + "'");
Reid Spencer5a060772006-08-23 07:30:48 +0000765 return false;
Reid Spencereaf18152004-11-14 22:08:36 +0000766}
767
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000768bool
Chris Lattner1bebfb52006-07-28 22:36:17 +0000769Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrStr) const {
Reid Spencereaf18152004-11-14 22:08:36 +0000770 struct utimbuf utb;
771 utb.actime = si.modTime.toPosixTime();
772 utb.modtime = utb.actime;
773 if (0 != ::utime(path.c_str(),&utb))
Reid Spencer51c5a282006-08-23 20:34:57 +0000774 return MakeErrMsg(ErrStr, path + ": can't set file modification time");
Reid Spencereaf18152004-11-14 22:08:36 +0000775 if (0 != ::chmod(path.c_str(),si.mode))
Reid Spencer51c5a282006-08-23 20:34:57 +0000776 return MakeErrMsg(ErrStr, path + ": can't set mode");
Chris Lattner1bebfb52006-07-28 22:36:17 +0000777 return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000778}
779
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000780bool
Reid Spencer51c5a282006-08-23 20:34:57 +0000781sys::CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg){
Reid Spencerc29befb2004-12-15 01:50:13 +0000782 int inFile = -1;
783 int outFile = -1;
Reid Spencer51c5a282006-08-23 20:34:57 +0000784 inFile = ::open(Src.c_str(), O_RDONLY);
785 if (inFile == -1)
Chris Lattner74382b72009-08-23 22:45:37 +0000786 return MakeErrMsg(ErrMsg, Src.str() +
Reid Spencer51c5a282006-08-23 20:34:57 +0000787 ": can't open source file to copy");
Reid Spencerc29befb2004-12-15 01:50:13 +0000788
Reid Spencer51c5a282006-08-23 20:34:57 +0000789 outFile = ::open(Dest.c_str(), O_WRONLY|O_CREAT, 0666);
790 if (outFile == -1) {
791 ::close(inFile);
Chris Lattner74382b72009-08-23 22:45:37 +0000792 return MakeErrMsg(ErrMsg, Dest.str() +
Reid Spencer51c5a282006-08-23 20:34:57 +0000793 ": can't create destination file for copy");
794 }
Reid Spencerc29befb2004-12-15 01:50:13 +0000795
Reid Spencer51c5a282006-08-23 20:34:57 +0000796 char Buffer[16*1024];
797 while (ssize_t Amt = ::read(inFile, Buffer, 16*1024)) {
798 if (Amt == -1) {
799 if (errno != EINTR && errno != EAGAIN) {
800 ::close(inFile);
801 ::close(outFile);
Chris Lattner74382b72009-08-23 22:45:37 +0000802 return MakeErrMsg(ErrMsg, Src.str()+": can't read source file");
Reid Spencer51c5a282006-08-23 20:34:57 +0000803 }
804 } else {
805 char *BufPtr = Buffer;
806 while (Amt) {
807 ssize_t AmtWritten = ::write(outFile, BufPtr, Amt);
808 if (AmtWritten == -1) {
809 if (errno != EINTR && errno != EAGAIN) {
810 ::close(inFile);
811 ::close(outFile);
Chris Lattner74382b72009-08-23 22:45:37 +0000812 return MakeErrMsg(ErrMsg, Dest.str() +
Daniel Dunbara7f2a9e2009-04-20 20:50:13 +0000813 ": can't write destination file");
Reid Spencerc29befb2004-12-15 01:50:13 +0000814 }
Reid Spencer51c5a282006-08-23 20:34:57 +0000815 } else {
816 Amt -= AmtWritten;
817 BufPtr += AmtWritten;
Reid Spencerc29befb2004-12-15 01:50:13 +0000818 }
819 }
820 }
Reid Spencerc29befb2004-12-15 01:50:13 +0000821 }
Reid Spencer51c5a282006-08-23 20:34:57 +0000822 ::close(inFile);
823 ::close(outFile);
824 return false;
Reid Spencerc29befb2004-12-15 01:50:13 +0000825}
826
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000827bool
Reid Spencer51c5a282006-08-23 20:34:57 +0000828Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000829 if (reuse_current && !exists())
Reid Spencer51c5a282006-08-23 20:34:57 +0000830 return false; // File doesn't exist already, just use it!
Reid Spencerc29befb2004-12-15 01:50:13 +0000831
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000832 // Append an XXXXXX pattern to the end of the file for use with mkstemp,
Reid Spencerc29befb2004-12-15 01:50:13 +0000833 // mktemp or our own implementation.
Chris Lattnerdbe89cd2009-08-23 22:57:38 +0000834 SmallVector<char, 128> Buf;
835 Buf.resize(path.size()+8);
836 char *FNBuffer = Buf.data();
Devang Patelce8fdf12008-07-22 20:02:39 +0000837 path.copy(FNBuffer,path.size());
Devang Patelbdfa9ac2008-07-24 00:35:38 +0000838 if (isDirectory())
839 strcpy(FNBuffer+path.size(), "/XXXXXX");
840 else
Devang Patelce8fdf12008-07-22 20:02:39 +0000841 strcpy(FNBuffer+path.size(), "-XXXXXX");
Reid Spencerc29befb2004-12-15 01:50:13 +0000842
843#if defined(HAVE_MKSTEMP)
844 int TempFD;
Reid Spencer51c5a282006-08-23 20:34:57 +0000845 if ((TempFD = mkstemp(FNBuffer)) == -1)
846 return MakeErrMsg(ErrMsg, path + ": can't make unique filename");
Reid Spencerc29befb2004-12-15 01:50:13 +0000847
848 // We don't need to hold the temp file descriptor... we will trust that no one
849 // will overwrite/delete the file before we can open it again.
850 close(TempFD);
851
852 // Save the name
853 path = FNBuffer;
854#elif defined(HAVE_MKTEMP)
855 // If we don't have mkstemp, use the old and obsolete mktemp function.
Reid Spencer51c5a282006-08-23 20:34:57 +0000856 if (mktemp(FNBuffer) == 0)
857 return MakeErrMsg(ErrMsg, path + ": can't make unique filename");
Reid Spencerc29befb2004-12-15 01:50:13 +0000858
859 // Save the name
860 path = FNBuffer;
861#else
862 // Okay, looks like we have to do it all by our lonesome.
863 static unsigned FCounter = 0;
864 unsigned offset = path.size() + 1;
865 while ( FCounter < 999999 && exists()) {
866 sprintf(FNBuffer+offset,"%06u",++FCounter);
867 path = FNBuffer;
868 }
869 if (FCounter > 999999)
Mikhail Glushenkovb8863a12009-02-15 03:20:03 +0000870 return MakeErrMsg(ErrMsg,
Reid Spencer51c5a282006-08-23 20:34:57 +0000871 path + ": can't make unique filename: too many files");
Reid Spencerc29befb2004-12-15 01:50:13 +0000872#endif
Reid Spencer51c5a282006-08-23 20:34:57 +0000873 return false;
874}
Reid Spencerc29befb2004-12-15 01:50:13 +0000875
Chris Lattner799ed102008-04-01 06:00:12 +0000876const char *Path::MapInFilePages(int FD, uint64_t FileSize) {
Chris Lattner9ffd19a2008-04-01 06:16:24 +0000877 int Flags = MAP_PRIVATE;
878#ifdef MAP_FILE
879 Flags |= MAP_FILE;
880#endif
881 void *BasePtr = ::mmap(0, FileSize, PROT_READ, Flags, FD, 0);
882 if (BasePtr == MAP_FAILED)
883 return 0;
Chris Lattner14c762d2008-04-01 06:25:23 +0000884 return (const char*)BasePtr;
Chris Lattner799ed102008-04-01 06:00:12 +0000885}
886
Chris Lattner9ffd19a2008-04-01 06:16:24 +0000887void Path::UnMapFilePages(const char *BasePtr, uint64_t FileSize) {
Chris Lattner14c762d2008-04-01 06:25:23 +0000888 ::munmap((void*)BasePtr, FileSize);
Chris Lattner799ed102008-04-01 06:00:12 +0000889}
890
Reid Spencer51c5a282006-08-23 20:34:57 +0000891} // end llvm namespace