blob: e655ef40cdf9d8d99af5ad0622389a207c93e526 [file] [log] [blame]
Reid Spencerb89a2232004-08-25 06:20:07 +00001//===- llvm/System/Unix/Path.cpp - Unix Path Implementation -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
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
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include <llvm/Config/config.h>
Reid Spencerbe31d2a2004-11-16 17:14:08 +000020#include <llvm/Config/alloca.h>
Reid Spencerb89a2232004-08-25 06:20:07 +000021#include "Unix.h"
22#include <sys/stat.h>
23#include <fcntl.h>
Reid Spencereaf18152004-11-14 22:08:36 +000024#include <utime.h>
25#include <dirent.h>
Reid Spencerb89a2232004-08-25 06:20:07 +000026
Reid Spencer8e665952004-08-29 05:24:01 +000027namespace llvm {
28using namespace sys;
Reid Spencerb89a2232004-08-25 06:20:07 +000029
Reid Spencer732f92d2004-12-13 06:57:15 +000030Path::Path(const std::string& unverified_path)
Reid Spencer8e665952004-08-29 05:24:01 +000031 : path(unverified_path)
Reid Spencerb89a2232004-08-25 06:20:07 +000032{
Reid Spencer8e665952004-08-29 05:24:01 +000033 if (unverified_path.empty())
34 return;
Reid Spencer07adb282004-11-05 22:15:36 +000035 if (this->isValid())
Reid Spencer8e665952004-08-29 05:24:01 +000036 return;
37 // oops, not valid.
38 path.clear();
39 ThrowErrno(unverified_path + ": path is not valid");
Reid Spencerb89a2232004-08-25 06:20:07 +000040}
41
Reid Spencer8e665952004-08-29 05:24:01 +000042Path
43Path::GetRootDirectory() {
44 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +000045 result.setDirectory("/");
Reid Spencer8e665952004-08-29 05:24:01 +000046 return result;
47}
48
Reid Spencer1b6b99b2004-12-13 03:00:51 +000049static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
50 const char* at = path;
51 const char* delim = strchr(at, ':');
52 Path tmpPath;
53 while( delim != 0 ) {
54 std::string tmp(at, size_t(delim-at));
55 if (tmpPath.setDirectory(tmp))
56 if (tmpPath.readable())
57 Paths.push_back(tmpPath);
58 at = delim + 1;
59 delim = strchr(at, ':');
Reid Spencer74e72612004-09-14 00:16:39 +000060 }
Reid Spencer1b6b99b2004-12-13 03:00:51 +000061 if (*at != 0)
62 if (tmpPath.setDirectory(std::string(at)))
63 if (tmpPath.readable())
64 Paths.push_back(tmpPath);
65
Reid Spencer74e72612004-09-14 00:16:39 +000066}
Reid Spencer1b6b99b2004-12-13 03:00:51 +000067void
68Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
69#ifdef LTDL_SHLIBPATH_VAR
70 char* env_var = getenv(LTDL_SHLIBPATH_VAR);
71 if (env_var != 0) {
72 getPathList(env_var,Paths);
Reid Spencer74e72612004-09-14 00:16:39 +000073 }
Reid Spencer1b6b99b2004-12-13 03:00:51 +000074#endif
75 // FIXME: Should this look at LD_LIBRARY_PATH too?
76 Paths.push_back(sys::Path("/usr/local/lib/"));
77 Paths.push_back(sys::Path("/usr/X11R6/lib/"));
78 Paths.push_back(sys::Path("/usr/lib/"));
79 Paths.push_back(sys::Path("/lib/"));
Reid Spencer74e72612004-09-14 00:16:39 +000080}
81
Reid Spencer1b6b99b2004-12-13 03:00:51 +000082void
83Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
84 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
85 if (env_var != 0) {
86 getPathList(env_var,Paths);
87 }
88#ifdef LLVMGCCDIR
89 {
90 Path tmpPath(std::string(LLVMGCCDIR) + "bytecode-libs/");
91 if (tmpPath.readable())
92 Paths.push_back(tmpPath);
93 }
94#endif
95#ifdef LLVM_LIBDIR
96 {
97 Path tmpPath;
98 if (tmpPath.setDirectory(LLVM_LIBDIR))
99 if (tmpPath.readable())
100 Paths.push_back(tmpPath);
101 }
102#endif
103 GetSystemLibraryPaths(Paths);
Reid Spencer8e665952004-08-29 05:24:01 +0000104}
105
106Path
107Path::GetLLVMDefaultConfigDir() {
108 return Path("/etc/llvm/");
109}
110
111Path
112Path::GetLLVMConfigDir() {
113 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +0000114 if (result.setDirectory(LLVM_ETCDIR))
Reid Spencer8e665952004-08-29 05:24:01 +0000115 return result;
116 return GetLLVMDefaultConfigDir();
117}
118
119Path
120Path::GetUserHomeDirectory() {
121 const char* home = getenv("HOME");
122 if (home) {
123 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +0000124 if (result.setDirectory(home))
Reid Spencer8e665952004-08-29 05:24:01 +0000125 return result;
126 }
127 return GetRootDirectory();
128}
129
130bool
Reid Spencer07adb282004-11-05 22:15:36 +0000131Path::isFile() const {
132 return (isValid() && path[path.length()-1] != '/');
Reid Spencer1b554b42004-09-11 04:55:08 +0000133}
134
135bool
Reid Spencer07adb282004-11-05 22:15:36 +0000136Path::isDirectory() const {
137 return (isValid() && path[path.length()-1] == '/');
Reid Spencer1b554b42004-09-11 04:55:08 +0000138}
139
140std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000141Path::getBasename() const {
Reid Spencer1b554b42004-09-11 04:55:08 +0000142 // Find the last slash
143 size_t slash = path.rfind('/');
144 if (slash == std::string::npos)
145 slash = 0;
146 else
147 slash++;
148
149 return path.substr(slash, path.rfind('.'));
150}
151
Reid Spencer07adb282004-11-05 22:15:36 +0000152bool Path::hasMagicNumber(const std::string &Magic) const {
Reid Spencer1b554b42004-09-11 04:55:08 +0000153 size_t len = Magic.size();
Reid Spencerbe31d2a2004-11-16 17:14:08 +0000154 assert(len < 1024 && "Request for magic string too long");
155 char* buf = (char*) alloca(1 + len);
156 int fd = ::open(path.c_str(),O_RDONLY);
157 if (fd < 0)
158 return false;
Reid Spencer1b6b99b2004-12-13 03:00:51 +0000159 size_t read_len = ::read(fd, buf, len);
Reid Spencerbe31d2a2004-11-16 17:14:08 +0000160 close(fd);
Reid Spencer1b6b99b2004-12-13 03:00:51 +0000161 if (len != read_len)
162 return false;
Reid Spencer1b554b42004-09-11 04:55:08 +0000163 buf[len] = '\0';
164 return Magic == buf;
165}
166
Reid Spencereaf18152004-11-14 22:08:36 +0000167bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
168 if (!isFile())
169 return false;
Reid Spencerbe31d2a2004-11-16 17:14:08 +0000170 assert(len < 1024 && "Request for magic string too long");
171 char* buf = (char*) alloca(1 + len);
172 int fd = ::open(path.c_str(),O_RDONLY);
173 if (fd < 0)
174 return false;
Reid Spencer86ac2dc2004-12-02 09:09:48 +0000175 ssize_t bytes_read = ::read(fd, buf, len);
176 ::close(fd);
177 if (ssize_t(len) != bytes_read) {
178 Magic.clear();
Reid Spencerbe31d2a2004-11-16 17:14:08 +0000179 return false;
Reid Spencer86ac2dc2004-12-02 09:09:48 +0000180 }
181 Magic.assign(buf,len);
Reid Spencereaf18152004-11-14 22:08:36 +0000182 return true;
183}
184
Reid Spencer1b554b42004-09-11 04:55:08 +0000185bool
Reid Spencer07adb282004-11-05 22:15:36 +0000186Path::isBytecodeFile() const {
Reid Spencer9195f372004-11-09 20:26:31 +0000187 char buffer[ 4];
188 buffer[0] = 0;
Reid Spencer1fce0912004-12-11 00:14:15 +0000189 int fd = ::open(path.c_str(),O_RDONLY);
190 if (fd < 0)
191 return false;
192 ssize_t bytes_read = ::read(fd, buffer, 4);
193 ::close(fd);
194 if (4 != bytes_read)
195 return false;
Reid Spencereaf18152004-11-14 22:08:36 +0000196
197 return (buffer[0] == 'l' && buffer[1] == 'l' && buffer[2] == 'v' &&
198 (buffer[3] == 'c' || buffer[3] == 'm'));
Reid Spencer1b554b42004-09-11 04:55:08 +0000199}
200
201bool
Reid Spencer8e665952004-08-29 05:24:01 +0000202Path::exists() const {
203 return 0 == access(path.c_str(), F_OK );
204}
205
206bool
207Path::readable() const {
208 return 0 == access(path.c_str(), F_OK | R_OK );
209}
210
211bool
212Path::writable() const {
213 return 0 == access(path.c_str(), F_OK | W_OK );
214}
215
216bool
217Path::executable() const {
218 return 0 == access(path.c_str(), R_OK | X_OK );
219}
220
221std::string
222Path::getLast() const {
223 // Find the last slash
224 size_t pos = path.rfind('/');
225
226 // Handle the corner cases
227 if (pos == std::string::npos)
228 return path;
229
230 // If the last character is a slash
231 if (pos == path.length()-1) {
232 // Find the second to last slash
233 size_t pos2 = path.rfind('/', pos-1);
234 if (pos2 == std::string::npos)
235 return path.substr(0,pos);
236 else
237 return path.substr(pos2+1,pos-pos2-1);
238 }
239 // Return everything after the last slash
240 return path.substr(pos+1);
241}
242
Reid Spencerb608a812004-11-16 06:15:19 +0000243void
244Path::getStatusInfo(StatusInfo& info) const {
Reid Spencer9195f372004-11-09 20:26:31 +0000245 struct stat buf;
246 if (0 != stat(path.c_str(), &buf)) {
247 ThrowErrno(std::string("Can't get status: ")+path);
248 }
249 info.fileSize = buf.st_size;
Reid Spencereaf18152004-11-14 22:08:36 +0000250 info.modTime.fromEpochTime(buf.st_mtime);
Reid Spencer9195f372004-11-09 20:26:31 +0000251 info.mode = buf.st_mode;
252 info.user = buf.st_uid;
253 info.group = buf.st_gid;
Reid Spencereaf18152004-11-14 22:08:36 +0000254 info.isDir = S_ISDIR(buf.st_mode);
255 if (info.isDir && path[path.length()-1] != '/')
256 path += '/';
257}
258
259bool
Reid Spencerb608a812004-11-16 06:15:19 +0000260Path::getDirectoryContents(std::set<Path>& result) const {
Reid Spencereaf18152004-11-14 22:08:36 +0000261 if (!isDirectory())
262 return false;
263 DIR* direntries = ::opendir(path.c_str());
264 if (direntries == 0)
265 ThrowErrno(path + ": can't open directory");
266
267 result.clear();
268 struct dirent* de = ::readdir(direntries);
269 while (de != 0) {
270 if (de->d_name[0] != '.') {
271 Path aPath(path + (const char*)de->d_name);
272 struct stat buf;
273 if (0 != stat(aPath.path.c_str(), &buf))
274 ThrowErrno(aPath.path + ": can't get status");
275 if (S_ISDIR(buf.st_mode))
276 aPath.path += "/";
Reid Spencerb608a812004-11-16 06:15:19 +0000277 result.insert(aPath);
Reid Spencereaf18152004-11-14 22:08:36 +0000278 }
279 de = ::readdir(direntries);
280 }
281
282 closedir(direntries);
283 return true;
Reid Spencer9195f372004-11-09 20:26:31 +0000284}
285
Reid Spencer8e665952004-08-29 05:24:01 +0000286bool
Reid Spencer07adb282004-11-05 22:15:36 +0000287Path::setDirectory(const std::string& a_path) {
Reid Spencer8e665952004-08-29 05:24:01 +0000288 if (a_path.size() == 0)
289 return false;
290 Path save(*this);
291 path = a_path;
292 size_t last = a_path.size() -1;
293 if (last != 0 && a_path[last] != '/')
294 path += '/';
Reid Spencer07adb282004-11-05 22:15:36 +0000295 if (!isValid()) {
Reid Spencer8e665952004-08-29 05:24:01 +0000296 path = save.path;
297 return false;
298 }
299 return true;
300}
301
302bool
Reid Spencer07adb282004-11-05 22:15:36 +0000303Path::setFile(const std::string& a_path) {
Reid Spencer8e665952004-08-29 05:24:01 +0000304 if (a_path.size() == 0)
305 return false;
306 Path save(*this);
307 path = a_path;
308 size_t last = a_path.size() - 1;
309 while (last > 0 && a_path[last] == '/')
310 last--;
311 path.erase(last+1);
Reid Spencer07adb282004-11-05 22:15:36 +0000312 if (!isValid()) {
Reid Spencer8e665952004-08-29 05:24:01 +0000313 path = save.path;
314 return false;
315 }
316 return true;
317}
318
319bool
Reid Spencer07adb282004-11-05 22:15:36 +0000320Path::appendDirectory(const std::string& dir) {
321 if (isFile())
Reid Spencer8e665952004-08-29 05:24:01 +0000322 return false;
323 Path save(*this);
324 path += dir;
325 path += "/";
Reid Spencer07adb282004-11-05 22:15:36 +0000326 if (!isValid()) {
Reid Spencer8e665952004-08-29 05:24:01 +0000327 path = save.path;
328 return false;
329 }
330 return true;
331}
332
333bool
Reid Spencer07adb282004-11-05 22:15:36 +0000334Path::elideDirectory() {
335 if (isFile())
Reid Spencer8e665952004-08-29 05:24:01 +0000336 return false;
337 size_t slashpos = path.rfind('/',path.size());
338 if (slashpos == 0 || slashpos == std::string::npos)
339 return false;
340 if (slashpos == path.size() - 1)
341 slashpos = path.rfind('/',slashpos-1);
342 if (slashpos == std::string::npos)
343 return false;
344 path.erase(slashpos);
345 return true;
346}
347
348bool
Reid Spencer07adb282004-11-05 22:15:36 +0000349Path::appendFile(const std::string& file) {
350 if (!isDirectory())
Reid Spencer8e665952004-08-29 05:24:01 +0000351 return false;
352 Path save(*this);
353 path += file;
Reid Spencer07adb282004-11-05 22:15:36 +0000354 if (!isValid()) {
Reid Spencer8e665952004-08-29 05:24:01 +0000355 path = save.path;
356 return false;
357 }
358 return true;
359}
360
361bool
Reid Spencer07adb282004-11-05 22:15:36 +0000362Path::elideFile() {
363 if (isDirectory())
Reid Spencer8e665952004-08-29 05:24:01 +0000364 return false;
365 size_t slashpos = path.rfind('/',path.size());
366 if (slashpos == std::string::npos)
367 return false;
368 path.erase(slashpos+1);
369 return true;
370}
371
372bool
Reid Spencer07adb282004-11-05 22:15:36 +0000373Path::appendSuffix(const std::string& suffix) {
374 if (isDirectory())
Reid Spencer8e665952004-08-29 05:24:01 +0000375 return false;
376 Path save(*this);
377 path.append(".");
378 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000379 if (!isValid()) {
Reid Spencer8e665952004-08-29 05:24:01 +0000380 path = save.path;
381 return false;
382 }
383 return true;
384}
385
386bool
Reid Spencer07adb282004-11-05 22:15:36 +0000387Path::elideSuffix() {
388 if (isDirectory()) return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000389 size_t dotpos = path.rfind('.',path.size());
390 size_t slashpos = path.rfind('/',path.size());
391 if (slashpos != std::string::npos && dotpos != std::string::npos &&
392 dotpos > slashpos) {
393 path.erase(dotpos, path.size()-dotpos);
394 return true;
395 }
396 return false;
397}
398
399
400bool
Reid Spencer07adb282004-11-05 22:15:36 +0000401Path::createDirectory( bool create_parents) {
Reid Spencer8e665952004-08-29 05:24:01 +0000402 // Make sure we're dealing with a directory
Reid Spencer07adb282004-11-05 22:15:36 +0000403 if (!isDirectory()) return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000404
405 // Get a writeable copy of the path name
406 char pathname[MAXPATHLEN];
407 path.copy(pathname,MAXPATHLEN);
408
409 // Null-terminate the last component
410 int lastchar = path.length() - 1 ;
411 if (pathname[lastchar] == '/')
412 pathname[lastchar] = 0;
Reid Spencereaf18152004-11-14 22:08:36 +0000413 else
414 pathname[lastchar+1] = 0;
Reid Spencer8e665952004-08-29 05:24:01 +0000415
416 // If we're supposed to create intermediate directories
417 if ( create_parents ) {
418 // Find the end of the initial name component
419 char * next = strchr(pathname,'/');
420 if ( pathname[0] == '/')
421 next = strchr(&pathname[1],'/');
422
423 // Loop through the directory components until we're done
424 while ( next != 0 ) {
425 *next = 0;
426 if (0 != access(pathname, F_OK | R_OK | W_OK))
427 if (0 != mkdir(pathname, S_IRWXU | S_IRWXG))
428 ThrowErrno(std::string(pathname) + ": Can't create directory");
429 char* save = next;
Reid Spencereaf18152004-11-14 22:08:36 +0000430 next = strchr(next+1,'/');
Reid Spencer8e665952004-08-29 05:24:01 +0000431 *save = '/';
432 }
Reid Spencer8e665952004-08-29 05:24:01 +0000433 }
Reid Spencereaf18152004-11-14 22:08:36 +0000434
435 if (0 != access(pathname, F_OK | R_OK))
436 if (0 != mkdir(pathname, S_IRWXU | S_IRWXG))
437 ThrowErrno(std::string(pathname) + ": Can't create directory");
Reid Spencer8e665952004-08-29 05:24:01 +0000438 return true;
439}
440
441bool
Reid Spencer07adb282004-11-05 22:15:36 +0000442Path::createFile() {
Reid Spencer8e665952004-08-29 05:24:01 +0000443 // Make sure we're dealing with a file
Reid Spencer07adb282004-11-05 22:15:36 +0000444 if (!isFile()) return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000445
446 // Create the file
Reid Spencer622e2202004-09-18 19:25:11 +0000447 int fd = ::creat(path.c_str(), S_IRUSR | S_IWUSR);
448 if (fd < 0)
Reid Spencer9195f372004-11-09 20:26:31 +0000449 ThrowErrno(path + ": Can't create file");
Reid Spencer622e2202004-09-18 19:25:11 +0000450 ::close(fd);
Reid Spencer8e665952004-08-29 05:24:01 +0000451
452 return true;
Reid Spencerb89a2232004-08-25 06:20:07 +0000453}
454
Reid Spencer8e665952004-08-29 05:24:01 +0000455bool
Reid Spencer9195f372004-11-09 20:26:31 +0000456Path::createTemporaryFile() {
457 // Make sure we're dealing with a file
458 if (!isFile()) return false;
459
460 // Append the filename filler
461 char pathname[MAXPATHLEN];
462 path.copy(pathname,MAXPATHLEN);
Reid Spencereaf18152004-11-14 22:08:36 +0000463 pathname[path.length()] = 0;
Reid Spencer9195f372004-11-09 20:26:31 +0000464 strcat(pathname,"XXXXXX");
465 int fd = ::mkstemp(pathname);
466 if (fd < 0) {
467 ThrowErrno(path + ": Can't create temporary file");
468 }
469 path = pathname;
470 ::close(fd);
471 return true;
472}
473
474bool
Reid Spencer07adb282004-11-05 22:15:36 +0000475Path::destroyDirectory(bool remove_contents) {
Reid Spencer8e665952004-08-29 05:24:01 +0000476 // Make sure we're dealing with a directory
Reid Spencer07adb282004-11-05 22:15:36 +0000477 if (!isDirectory()) return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000478
479 // If it doesn't exist, we're done.
480 if (!exists()) return true;
481
482 if (remove_contents) {
483 // Recursively descend the directory to remove its content
484 std::string cmd("/bin/rm -rf ");
485 cmd += path;
486 system(cmd.c_str());
487 } else {
488 // Otherwise, try to just remove the one directory
489 char pathname[MAXPATHLEN];
490 path.copy(pathname,MAXPATHLEN);
491 int lastchar = path.length() - 1 ;
492 if (pathname[lastchar] == '/')
493 pathname[lastchar] = 0;
Reid Spencereaf18152004-11-14 22:08:36 +0000494 else
495 pathname[lastchar+1] = 0;
Reid Spencer8e665952004-08-29 05:24:01 +0000496 if ( 0 != rmdir(pathname))
497 ThrowErrno(std::string(pathname) + ": Can't destroy directory");
498 }
499 return true;
500}
501
502bool
Reid Spencer07adb282004-11-05 22:15:36 +0000503Path::destroyFile() {
504 if (!isFile()) return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000505 if (0 != unlink(path.c_str()))
Reid Spencereaf18152004-11-14 22:08:36 +0000506 ThrowErrno(path + ": Can't destroy file");
507 return true;
508}
509
510bool
511Path::renameFile(const Path& newName) {
512 if (!isFile()) return false;
513 if (0 != rename(path.c_str(), newName.c_str()))
Reid Spencer1fce0912004-12-11 00:14:15 +0000514 ThrowErrno(std::string("can't rename ") + path + " as " +
515 newName.toString());
Reid Spencereaf18152004-11-14 22:08:36 +0000516 return true;
517}
518
519bool
520Path::setStatusInfo(const StatusInfo& si) const {
521 if (!isFile()) return false;
522 struct utimbuf utb;
523 utb.actime = si.modTime.toPosixTime();
524 utb.modtime = utb.actime;
525 if (0 != ::utime(path.c_str(),&utb))
526 ThrowErrno(path + ": can't set file modification time");
527 if (0 != ::chmod(path.c_str(),si.mode))
528 ThrowErrno(path + ": can't set mode");
Reid Spencer8e665952004-08-29 05:24:01 +0000529 return true;
530}
531
Reid Spencerb89a2232004-08-25 06:20:07 +0000532}
533
534// vim: sw=2