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_file() && path.append_file(basename)) {
56 if (path.append_suffix(Path::GetDLLSuffix()) && path.readable())
58 else if (path.elide_suffix() && path.append_suffix("a") && path.readable())
60 else if (path.elide_suffix() && path.append_suffix("o") && path.readable())
68 Path::GetLibraryPath(const std::string& basename,
69 const std::vector<std::string>& LibPaths) {
72 // Try the paths provided
73 for (std::vector<std::string>::const_iterator I = LibPaths.begin(),
74 E = LibPaths.end(); I != E; ++I ) {
75 if (result.set_directory(*I) && IsLibrary(result,basename))
80 if (result.set_directory("/usr/lib/") && IsLibrary(result,basename))
84 if (result.set_directory("/lib/") && IsLibrary(result,basename))
87 // Can't find it, give up and return invalid path.
93 Path::GetSystemLibraryPath1() {
98 Path::GetSystemLibraryPath2() {
99 return Path("/usr/lib/");
103 Path::GetLLVMDefaultConfigDir() {
104 return Path("/etc/llvm/");
108 Path::GetLLVMConfigDir() {
110 if (result.set_directory(LLVM_ETCDIR))
112 return GetLLVMDefaultConfigDir();
116 Path::GetUserHomeDirectory() {
117 const char* home = getenv("HOME");
120 if (result.set_directory(home))
123 return GetRootDirectory();
127 Path::is_file() const {
128 return (is_valid() && path[path.length()-1] != '/');
132 Path::is_directory() const {
133 return (is_valid() && path[path.length()-1] == '/');
137 Path::get_basename() const {
138 // Find the last slash
139 size_t slash = path.rfind('/');
140 if (slash == std::string::npos)
145 return path.substr(slash, path.rfind('.'));
148 bool Path::has_magic_number(const std::string &Magic) const {
149 size_t len = Magic.size();
151 std::ifstream f(path.c_str());
158 Path::is_bytecode_file() const {
160 return has_magic_number("llvm");
166 Path::is_archive() const {
168 return has_magic_number("!<arch>\012");
174 Path::exists() const {
175 return 0 == access(path.c_str(), F_OK );
179 Path::readable() const {
180 return 0 == access(path.c_str(), F_OK | R_OK );
184 Path::writable() const {
185 return 0 == access(path.c_str(), F_OK | W_OK );
189 Path::executable() const {
190 return 0 == access(path.c_str(), R_OK | X_OK );
194 Path::getLast() const {
195 // Find the last slash
196 size_t pos = path.rfind('/');
198 // Handle the corner cases
199 if (pos == std::string::npos)
202 // If the last character is a slash
203 if (pos == path.length()-1) {
204 // Find the second to last slash
205 size_t pos2 = path.rfind('/', pos-1);
206 if (pos2 == std::string::npos)
207 return path.substr(0,pos);
209 return path.substr(pos2+1,pos-pos2-1);
211 // Return everything after the last slash
212 return path.substr(pos+1);
216 Path::set_directory(const std::string& a_path) {
217 if (a_path.size() == 0)
221 size_t last = a_path.size() -1;
222 if (last != 0 && a_path[last] != '/')
232 Path::set_file(const std::string& a_path) {
233 if (a_path.size() == 0)
237 size_t last = a_path.size() - 1;
238 while (last > 0 && a_path[last] == '/')
249 Path::append_directory(const std::string& dir) {
263 Path::elide_directory() {
266 size_t slashpos = path.rfind('/',path.size());
267 if (slashpos == 0 || slashpos == std::string::npos)
269 if (slashpos == path.size() - 1)
270 slashpos = path.rfind('/',slashpos-1);
271 if (slashpos == std::string::npos)
273 path.erase(slashpos);
278 Path::append_file(const std::string& file) {
294 size_t slashpos = path.rfind('/',path.size());
295 if (slashpos == std::string::npos)
297 path.erase(slashpos+1);
302 Path::append_suffix(const std::string& suffix) {
316 Path::elide_suffix() {
317 if (is_directory()) return false;
318 size_t dotpos = path.rfind('.',path.size());
319 size_t slashpos = path.rfind('/',path.size());
320 if (slashpos != std::string::npos && dotpos != std::string::npos &&
322 path.erase(dotpos, path.size()-dotpos);
330 Path::create_directory( bool create_parents) {
331 // Make sure we're dealing with a directory
332 if (!is_directory()) return false;
334 // Get a writeable copy of the path name
335 char pathname[MAXPATHLEN];
336 path.copy(pathname,MAXPATHLEN);
338 // Null-terminate the last component
339 int lastchar = path.length() - 1 ;
340 if (pathname[lastchar] == '/')
341 pathname[lastchar] = 0;
343 // If we're supposed to create intermediate directories
344 if ( create_parents ) {
345 // Find the end of the initial name component
346 char * next = strchr(pathname,'/');
347 if ( pathname[0] == '/')
348 next = strchr(&pathname[1],'/');
350 // Loop through the directory components until we're done
351 while ( next != 0 ) {
353 if (0 != access(pathname, F_OK | R_OK | W_OK))
354 if (0 != mkdir(pathname, S_IRWXU | S_IRWXG))
355 ThrowErrno(std::string(pathname) + ": Can't create directory");
357 next = strchr(pathname,'/');
360 } else if (0 != mkdir(pathname, S_IRWXU | S_IRWXG)) {
361 ThrowErrno(std::string(pathname) + ": Can't create directory");
367 Path::create_file() {
368 // Make sure we're dealing with a file
369 if (!is_file()) return false;
372 if (0 != creat(path.c_str(), S_IRUSR | S_IWUSR))
373 ThrowErrno(std::string(path.c_str()) + ": Can't create file");
379 Path::destroy_directory(bool remove_contents) {
380 // Make sure we're dealing with a directory
381 if (!is_directory()) return false;
383 // If it doesn't exist, we're done.
384 if (!exists()) return true;
386 if (remove_contents) {
387 // Recursively descend the directory to remove its content
388 std::string cmd("/bin/rm -rf ");
392 // Otherwise, try to just remove the one directory
393 char pathname[MAXPATHLEN];
394 path.copy(pathname,MAXPATHLEN);
395 int lastchar = path.length() - 1 ;
396 if (pathname[lastchar] == '/')
397 pathname[lastchar] = 0;
398 if ( 0 != rmdir(pathname))
399 ThrowErrno(std::string(pathname) + ": Can't destroy directory");
405 Path::destroy_file() {
406 if (!is_file()) return false;
407 if (0 != unlink(path.c_str()))
408 ThrowErrno(std::string(path.c_str()) + ": Can't destroy file");