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 //===----------------------------------------------------------------------===//
22 #include <Config/config.h>
27 Path::Path(std::string unverified_path)
28 : path(unverified_path)
30 if (unverified_path.empty())
36 ThrowErrno(unverified_path + ": path is not valid");
40 Path::GetRootDirectory() {
42 result.set_directory("/");
47 Path::GetSystemLibraryPath1() {
52 Path::GetSystemLibraryPath2() {
53 return Path("/usr/lib/");
57 Path::GetLLVMDefaultConfigDir() {
58 return Path("/etc/llvm/");
62 Path::GetLLVMConfigDir() {
64 if (result.set_directory(LLVM_ETCDIR))
66 return GetLLVMDefaultConfigDir();
70 Path::GetUserHomeDirectory() {
71 const char* home = getenv("HOME");
74 if (result.set_directory(home))
77 return GetRootDirectory();
81 Path::exists() const {
82 return 0 == access(path.c_str(), F_OK );
86 Path::readable() const {
87 return 0 == access(path.c_str(), F_OK | R_OK );
91 Path::writable() const {
92 return 0 == access(path.c_str(), F_OK | W_OK );
96 Path::executable() const {
97 return 0 == access(path.c_str(), R_OK | X_OK );
101 Path::getLast() const {
102 // Find the last slash
103 size_t pos = path.rfind('/');
105 // Handle the corner cases
106 if (pos == std::string::npos)
109 // If the last character is a slash
110 if (pos == path.length()-1) {
111 // Find the second to last slash
112 size_t pos2 = path.rfind('/', pos-1);
113 if (pos2 == std::string::npos)
114 return path.substr(0,pos);
116 return path.substr(pos2+1,pos-pos2-1);
118 // Return everything after the last slash
119 return path.substr(pos+1);
123 Path::set_directory(const std::string& a_path) {
124 if (a_path.size() == 0)
128 size_t last = a_path.size() -1;
129 if (last != 0 && a_path[last] != '/')
139 Path::set_file(const std::string& a_path) {
140 if (a_path.size() == 0)
144 size_t last = a_path.size() - 1;
145 while (last > 0 && a_path[last] == '/')
156 Path::append_directory(const std::string& dir) {
170 Path::elide_directory() {
173 size_t slashpos = path.rfind('/',path.size());
174 if (slashpos == 0 || slashpos == std::string::npos)
176 if (slashpos == path.size() - 1)
177 slashpos = path.rfind('/',slashpos-1);
178 if (slashpos == std::string::npos)
180 path.erase(slashpos);
185 Path::append_file(const std::string& file) {
201 size_t slashpos = path.rfind('/',path.size());
202 if (slashpos == std::string::npos)
204 path.erase(slashpos+1);
209 Path::append_suffix(const std::string& suffix) {
223 Path::elide_suffix() {
224 if (is_directory()) return false;
225 size_t dotpos = path.rfind('.',path.size());
226 size_t slashpos = path.rfind('/',path.size());
227 if (slashpos != std::string::npos && dotpos != std::string::npos &&
229 path.erase(dotpos, path.size()-dotpos);
237 Path::create_directory( bool create_parents) {
238 // Make sure we're dealing with a directory
239 if (!is_directory()) return false;
241 // Get a writeable copy of the path name
242 char pathname[MAXPATHLEN];
243 path.copy(pathname,MAXPATHLEN);
245 // Null-terminate the last component
246 int lastchar = path.length() - 1 ;
247 if (pathname[lastchar] == '/')
248 pathname[lastchar] = 0;
250 // If we're supposed to create intermediate directories
251 if ( create_parents ) {
252 // Find the end of the initial name component
253 char * next = strchr(pathname,'/');
254 if ( pathname[0] == '/')
255 next = strchr(&pathname[1],'/');
257 // Loop through the directory components until we're done
258 while ( next != 0 ) {
260 if (0 != access(pathname, F_OK | R_OK | W_OK))
261 if (0 != mkdir(pathname, S_IRWXU | S_IRWXG))
262 ThrowErrno(std::string(pathname) + ": Can't create directory");
264 next = strchr(pathname,'/');
267 } else if (0 != mkdir(pathname, S_IRWXU | S_IRWXG)) {
268 ThrowErrno(std::string(pathname) + ": Can't create directory");
274 Path::create_file() {
275 // Make sure we're dealing with a file
276 if (!is_file()) return false;
279 if (0 != creat(path.c_str(), S_IRUSR | S_IWUSR))
280 ThrowErrno(std::string(path.c_str()) + ": Can't create file");
286 Path::destroy_directory(bool remove_contents) {
287 // Make sure we're dealing with a directory
288 if (!is_directory()) return false;
290 // If it doesn't exist, we're done.
291 if (!exists()) return true;
293 if (remove_contents) {
294 // Recursively descend the directory to remove its content
295 std::string cmd("/bin/rm -rf ");
299 // Otherwise, try to just remove the one directory
300 char pathname[MAXPATHLEN];
301 path.copy(pathname,MAXPATHLEN);
302 int lastchar = path.length() - 1 ;
303 if (pathname[lastchar] == '/')
304 pathname[lastchar] = 0;
305 if ( 0 != rmdir(pathname))
306 ThrowErrno(std::string(pathname) + ": Can't destroy directory");
312 Path::destroy_file() {
313 if (!is_file()) return false;
314 if (0 != unlink(path.c_str()))
315 ThrowErrno(std::string(path.c_str()) + ": Can't destroy file");