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.set_directory("/");
47 static inline bool IsLibrary(Path& path, const std::string& basename) {
48 if (path.append_file(std::string("lib") + basename)) {
49 if (path.append_suffix(Path::GetDLLSuffix()) && path.readable())
51 else if (path.elide_suffix() && path.append_suffix("a") && path.readable())
53 else if (path.elide_suffix() && path.append_suffix("o") && path.readable())
55 else if (path.elide_suffix() && path.append_suffix("bc") && path.readable())
57 } else if (path.elide_file() && path.append_file(basename)) {
58 if (path.append_suffix(Path::GetDLLSuffix()) && path.readable())
60 else if (path.elide_suffix() && path.append_suffix("a") && path.readable())
62 else if (path.elide_suffix() && path.append_suffix("o") && path.readable())
64 else if (path.elide_suffix() && path.append_suffix("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.set_directory(*I) && IsLibrary(result,basename))
83 // Try the LLVM lib directory in the LLVM install area
84 if (result.set_directory(LLVM_LIBDIR) && IsLibrary(result,basename))
88 if (result.set_directory("/usr/lib/") && IsLibrary(result,basename))
92 if (result.set_directory("/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.set_directory(LLVM_ETCDIR))
120 return GetLLVMDefaultConfigDir();
124 Path::GetUserHomeDirectory() {
125 const char* home = getenv("HOME");
128 if (result.set_directory(home))
131 return GetRootDirectory();
135 Path::is_file() const {
136 return (is_valid() && path[path.length()-1] != '/');
140 Path::is_directory() const {
141 return (is_valid() && path[path.length()-1] == '/');
145 Path::get_basename() 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::has_magic_number(const std::string &Magic) const {
157 size_t len = Magic.size();
159 std::ifstream f(path.c_str());
166 Path::is_bytecode_file() const {
168 return has_magic_number("llvm");
174 Path::is_archive() const {
176 return has_magic_number("!<arch>\012");
182 Path::exists() const {
183 return 0 == access(path.c_str(), F_OK );
187 Path::readable() const {
188 return 0 == access(path.c_str(), F_OK | R_OK );
192 Path::writable() const {
193 return 0 == access(path.c_str(), F_OK | W_OK );
197 Path::executable() const {
198 return 0 == access(path.c_str(), R_OK | X_OK );
202 Path::getLast() const {
203 // Find the last slash
204 size_t pos = path.rfind('/');
206 // Handle the corner cases
207 if (pos == std::string::npos)
210 // If the last character is a slash
211 if (pos == path.length()-1) {
212 // Find the second to last slash
213 size_t pos2 = path.rfind('/', pos-1);
214 if (pos2 == std::string::npos)
215 return path.substr(0,pos);
217 return path.substr(pos2+1,pos-pos2-1);
219 // Return everything after the last slash
220 return path.substr(pos+1);
224 Path::set_directory(const std::string& a_path) {
225 if (a_path.size() == 0)
229 size_t last = a_path.size() -1;
230 if (last != 0 && a_path[last] != '/')
240 Path::set_file(const std::string& a_path) {
241 if (a_path.size() == 0)
245 size_t last = a_path.size() - 1;
246 while (last > 0 && a_path[last] == '/')
257 Path::append_directory(const std::string& dir) {
271 Path::elide_directory() {
274 size_t slashpos = path.rfind('/',path.size());
275 if (slashpos == 0 || slashpos == std::string::npos)
277 if (slashpos == path.size() - 1)
278 slashpos = path.rfind('/',slashpos-1);
279 if (slashpos == std::string::npos)
281 path.erase(slashpos);
286 Path::append_file(const std::string& file) {
302 size_t slashpos = path.rfind('/',path.size());
303 if (slashpos == std::string::npos)
305 path.erase(slashpos+1);
310 Path::append_suffix(const std::string& suffix) {
324 Path::elide_suffix() {
325 if (is_directory()) return false;
326 size_t dotpos = path.rfind('.',path.size());
327 size_t slashpos = path.rfind('/',path.size());
328 if (slashpos != std::string::npos && dotpos != std::string::npos &&
330 path.erase(dotpos, path.size()-dotpos);
338 Path::create_directory( bool create_parents) {
339 // Make sure we're dealing with a directory
340 if (!is_directory()) return false;
342 // Get a writeable copy of the path name
343 char pathname[MAXPATHLEN];
344 path.copy(pathname,MAXPATHLEN);
346 // Null-terminate the last component
347 int lastchar = path.length() - 1 ;
348 if (pathname[lastchar] == '/')
349 pathname[lastchar] = 0;
351 // If we're supposed to create intermediate directories
352 if ( create_parents ) {
353 // Find the end of the initial name component
354 char * next = strchr(pathname,'/');
355 if ( pathname[0] == '/')
356 next = strchr(&pathname[1],'/');
358 // Loop through the directory components until we're done
359 while ( next != 0 ) {
361 if (0 != access(pathname, F_OK | R_OK | W_OK))
362 if (0 != mkdir(pathname, S_IRWXU | S_IRWXG))
363 ThrowErrno(std::string(pathname) + ": Can't create directory");
365 next = strchr(pathname,'/');
368 } else if (0 != mkdir(pathname, S_IRWXU | S_IRWXG)) {
369 ThrowErrno(std::string(pathname) + ": Can't create directory");
375 Path::create_file() {
376 // Make sure we're dealing with a file
377 if (!is_file()) return false;
380 int fd = ::creat(path.c_str(), S_IRUSR | S_IWUSR);
382 ThrowErrno(std::string(path.c_str()) + ": Can't create file");
389 Path::destroy_directory(bool remove_contents) {
390 // Make sure we're dealing with a directory
391 if (!is_directory()) return false;
393 // If it doesn't exist, we're done.
394 if (!exists()) return true;
396 if (remove_contents) {
397 // Recursively descend the directory to remove its content
398 std::string cmd("/bin/rm -rf ");
402 // Otherwise, try to just remove the one directory
403 char pathname[MAXPATHLEN];
404 path.copy(pathname,MAXPATHLEN);
405 int lastchar = path.length() - 1 ;
406 if (pathname[lastchar] == '/')
407 pathname[lastchar] = 0;
408 if ( 0 != rmdir(pathname))
409 ThrowErrno(std::string(pathname) + ": Can't destroy directory");
415 Path::destroy_file() {
416 if (!is_file()) return false;
417 if (0 != unlink(path.c_str()))
418 ThrowErrno(std::string(path.c_str()) + ": Can't destroy file");