1 //===- llvm/System/Unix/Path.cpp - Unix Path Implementation -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Unix specific portion of the Path class.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic UNIX code that
16 //=== is guaranteed to work on *all* UNIX variants.
17 //===----------------------------------------------------------------------===//
19 #include <llvm/Config/config.h>
28 Path::Path(std::string unverified_path)
29 : path(unverified_path)
31 if (unverified_path.empty())
37 ThrowErrno(unverified_path + ": path is not valid");
41 Path::GetRootDirectory() {
43 result.setDirectory("/");
47 static inline bool IsLibrary(Path& path, const std::string& basename) {
48 if (path.appendFile(std::string("lib") + basename)) {
49 if (path.appendSuffix(Path::GetDLLSuffix()) && path.readable())
51 else if (path.elideSuffix() && path.appendSuffix("a") && path.readable())
53 else if (path.elideSuffix() && path.appendSuffix("o") && path.readable())
55 else if (path.elideSuffix() && path.appendSuffix("bc") && path.readable())
57 } else if (path.elideFile() && path.appendFile(basename)) {
58 if (path.appendSuffix(Path::GetDLLSuffix()) && path.readable())
60 else if (path.elideSuffix() && path.appendSuffix("a") && path.readable())
62 else if (path.elideSuffix() && path.appendSuffix("o") && path.readable())
64 else if (path.elideSuffix() && path.appendSuffix("bc") && path.readable())
72 Path::GetLibraryPath(const std::string& basename,
73 const std::vector<std::string>& LibPaths) {
76 // Try the paths provided
77 for (std::vector<std::string>::const_iterator I = LibPaths.begin(),
78 E = LibPaths.end(); I != E; ++I ) {
79 if (result.setDirectory(*I) && IsLibrary(result,basename))
83 // Try the LLVM lib directory in the LLVM install area
84 if (result.setDirectory(LLVM_LIBDIR) && IsLibrary(result,basename))
88 if (result.setDirectory("/usr/lib/") && IsLibrary(result,basename))
92 if (result.setDirectory("/lib/") && IsLibrary(result,basename))
95 // Can't find it, give up and return invalid path.
101 Path::GetSystemLibraryPath1() {
102 return Path("/lib/");
106 Path::GetSystemLibraryPath2() {
107 return Path("/usr/lib/");
111 Path::GetLLVMDefaultConfigDir() {
112 return Path("/etc/llvm/");
116 Path::GetLLVMConfigDir() {
118 if (result.setDirectory(LLVM_ETCDIR))
120 return GetLLVMDefaultConfigDir();
124 Path::GetUserHomeDirectory() {
125 const char* home = getenv("HOME");
128 if (result.setDirectory(home))
131 return GetRootDirectory();
135 Path::isFile() const {
136 return (isValid() && path[path.length()-1] != '/');
140 Path::isDirectory() const {
141 return (isValid() && path[path.length()-1] == '/');
145 Path::getBasename() const {
146 // Find the last slash
147 size_t slash = path.rfind('/');
148 if (slash == std::string::npos)
153 return path.substr(slash, path.rfind('.'));
156 bool Path::hasMagicNumber(const std::string &Magic) const {
157 size_t len = Magic.size();
159 std::ifstream f(path.c_str());
166 Path::isBytecodeFile() const {
169 std::ifstream f(path.c_str());
172 ThrowErrno("can't read file signature");
173 return 0 == memcmp(buffer,"llvc",4) || 0 == memcmp(buffer,"llvm",4);
177 Path::isArchive() const {
179 return hasMagicNumber("!<arch>\012");
185 Path::exists() const {
186 return 0 == access(path.c_str(), F_OK );
190 Path::readable() const {
191 return 0 == access(path.c_str(), F_OK | R_OK );
195 Path::writable() const {
196 return 0 == access(path.c_str(), F_OK | W_OK );
200 Path::executable() const {
201 return 0 == access(path.c_str(), R_OK | X_OK );
205 Path::getLast() const {
206 // Find the last slash
207 size_t pos = path.rfind('/');
209 // Handle the corner cases
210 if (pos == std::string::npos)
213 // If the last character is a slash
214 if (pos == path.length()-1) {
215 // Find the second to last slash
216 size_t pos2 = path.rfind('/', pos-1);
217 if (pos2 == std::string::npos)
218 return path.substr(0,pos);
220 return path.substr(pos2+1,pos-pos2-1);
222 // Return everything after the last slash
223 return path.substr(pos+1);
227 Path::getStatusInfo(StatusInfo& info) const {
229 if (0 != stat(path.c_str(), &buf)) {
230 ThrowErrno(std::string("Can't get status: ")+path);
232 info.fileSize = buf.st_size;
233 info.modTime.fromPosixTime(buf.st_mtime);
234 info.mode = buf.st_mode;
235 info.user = buf.st_uid;
236 info.group = buf.st_gid;
240 Path::setDirectory(const std::string& a_path) {
241 if (a_path.size() == 0)
245 size_t last = a_path.size() -1;
246 if (last != 0 && a_path[last] != '/')
256 Path::setFile(const std::string& a_path) {
257 if (a_path.size() == 0)
261 size_t last = a_path.size() - 1;
262 while (last > 0 && a_path[last] == '/')
273 Path::appendDirectory(const std::string& dir) {
287 Path::elideDirectory() {
290 size_t slashpos = path.rfind('/',path.size());
291 if (slashpos == 0 || slashpos == std::string::npos)
293 if (slashpos == path.size() - 1)
294 slashpos = path.rfind('/',slashpos-1);
295 if (slashpos == std::string::npos)
297 path.erase(slashpos);
302 Path::appendFile(const std::string& file) {
318 size_t slashpos = path.rfind('/',path.size());
319 if (slashpos == std::string::npos)
321 path.erase(slashpos+1);
326 Path::appendSuffix(const std::string& suffix) {
340 Path::elideSuffix() {
341 if (isDirectory()) return false;
342 size_t dotpos = path.rfind('.',path.size());
343 size_t slashpos = path.rfind('/',path.size());
344 if (slashpos != std::string::npos && dotpos != std::string::npos &&
346 path.erase(dotpos, path.size()-dotpos);
354 Path::createDirectory( bool create_parents) {
355 // Make sure we're dealing with a directory
356 if (!isDirectory()) return false;
358 // Get a writeable copy of the path name
359 char pathname[MAXPATHLEN];
360 path.copy(pathname,MAXPATHLEN);
362 // Null-terminate the last component
363 int lastchar = path.length() - 1 ;
364 if (pathname[lastchar] == '/')
365 pathname[lastchar] = 0;
367 // If we're supposed to create intermediate directories
368 if ( create_parents ) {
369 // Find the end of the initial name component
370 char * next = strchr(pathname,'/');
371 if ( pathname[0] == '/')
372 next = strchr(&pathname[1],'/');
374 // Loop through the directory components until we're done
375 while ( next != 0 ) {
377 if (0 != access(pathname, F_OK | R_OK | W_OK))
378 if (0 != mkdir(pathname, S_IRWXU | S_IRWXG))
379 ThrowErrno(std::string(pathname) + ": Can't create directory");
381 next = strchr(pathname,'/');
384 } else if (0 != mkdir(pathname, S_IRWXU | S_IRWXG)) {
385 ThrowErrno(std::string(pathname) + ": Can't create directory");
392 // Make sure we're dealing with a file
393 if (!isFile()) return false;
396 int fd = ::creat(path.c_str(), S_IRUSR | S_IWUSR);
398 ThrowErrno(path + ": Can't create file");
405 Path::createTemporaryFile() {
406 // Make sure we're dealing with a file
407 if (!isFile()) return false;
409 // Append the filename filler
410 char pathname[MAXPATHLEN];
411 path.copy(pathname,MAXPATHLEN);
412 strcat(pathname,"XXXXXX");
413 int fd = ::mkstemp(pathname);
415 ThrowErrno(path + ": Can't create temporary file");
423 Path::destroyDirectory(bool remove_contents) {
424 // Make sure we're dealing with a directory
425 if (!isDirectory()) return false;
427 // If it doesn't exist, we're done.
428 if (!exists()) return true;
430 if (remove_contents) {
431 // Recursively descend the directory to remove its content
432 std::string cmd("/bin/rm -rf ");
436 // Otherwise, try to just remove the one directory
437 char pathname[MAXPATHLEN];
438 path.copy(pathname,MAXPATHLEN);
439 int lastchar = path.length() - 1 ;
440 if (pathname[lastchar] == '/')
441 pathname[lastchar] = 0;
442 if ( 0 != rmdir(pathname))
443 ThrowErrno(std::string(pathname) + ": Can't destroy directory");
449 Path::destroyFile() {
450 if (!isFile()) return false;
451 if (0 != unlink(path.c_str()))
452 ThrowErrno(std::string(path.c_str()) + ": Can't destroy file");