1 //===- llvm/System/Linux/Path.cpp - Linux 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 // Modified by Henrik Bach to comply with at least MinGW.
9 // Ported to Win32 by Jeff Cohen.
11 //===----------------------------------------------------------------------===//
13 // This file provides the Win32 specific implementation of the Path class.
15 //===----------------------------------------------------------------------===//
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only generic Win32 code that
19 //=== is guaranteed to work on *all* Win32 variants.
20 //===----------------------------------------------------------------------===//
26 static void FlipBackSlashes(std::string& s) {
27 for (size_t i = 0; i < s.size(); i++)
36 Path::isValid() const {
40 // If there is a colon, it must be the second character, preceded by a letter
41 // and followed by something.
42 size_t len = path.size();
43 size_t pos = path.rfind(':',len);
44 if (pos != std::string::npos) {
45 if (pos != 1 || !isalpha(path[0]) || len < 3)
49 // Check for illegal characters.
50 if (path.find_first_of("\\<>\"|\001\002\003\004\005\006\007\010\011\012"
51 "\013\014\015\016\017\020\021\022\023\024\025\026"
52 "\027\030\031\032\033\034\035\036\037")
56 // A file or directory name may not end in a period.
57 if (path[len-1] == '.')
59 if (len >= 2 && path[len-2] == '.' && path[len-1] == '/')
62 // A file or directory name may not end in a space.
63 if (path[len-1] == ' ')
65 if (len >= 2 && path[len-2] == ' ' && path[len-1] == '/')
71 static Path *TempDirectory = NULL;
74 Path::GetTemporaryDirectory() {
76 return *TempDirectory;
78 char pathname[MAX_PATH];
79 if (!GetTempPath(MAX_PATH, pathname))
80 throw std::string("Can't determine temporary directory");
83 result.setDirectory(pathname);
85 // Append a subdirectory passed on our process id so multiple LLVMs don't
86 // step on each other's toes.
87 sprintf(pathname, "LLVM_%u", GetCurrentProcessId());
88 result.appendDirectory(pathname);
90 // If there's a directory left over from a previous LLVM execution that
91 // happened to have the same process id, get rid of it.
92 result.destroyDirectory(true);
94 // And finally (re-)create the empty directory.
95 result.createDirectory(false);
96 TempDirectory = new Path(result);
97 return *TempDirectory;
100 Path::Path(const std::string& unverified_path)
101 : path(unverified_path)
103 FlipBackSlashes(path);
104 if (unverified_path.empty())
110 throw std::string(unverified_path + ": path is not valid");
113 // FIXME: the following set of functions don't map to Windows very well.
115 Path::GetRootDirectory() {
117 result.setDirectory("/");
122 Path::GetDLLSuffix() {
126 static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
127 const char* at = path;
128 const char* delim = strchr(at, ';');
130 while( delim != 0 ) {
131 std::string tmp(at, size_t(delim-at));
132 if (tmpPath.setDirectory(tmp))
133 if (tmpPath.readable())
134 Paths.push_back(tmpPath);
136 delim = strchr(at, ';');
139 if (tmpPath.setDirectory(std::string(at)))
140 if (tmpPath.readable())
141 Paths.push_back(tmpPath);
146 Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
147 #ifdef LTDL_SHLIBPATH_VAR
148 char* env_var = getenv(LTDL_SHLIBPATH_VAR);
150 getPathList(env_var,Paths);
153 // FIXME: Should this look at LD_LIBRARY_PATH too?
154 Paths.push_back(sys::Path("C:\\WINDOWS\\SYSTEM32\\"));
155 Paths.push_back(sys::Path("C:\\WINDOWS\\"));
159 Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
160 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
162 getPathList(env_var,Paths);
166 Path tmpPath(std::string(LLVMGCCDIR) + "bytecode-libs/");
167 if (tmpPath.readable())
168 Paths.push_back(tmpPath);
174 if (tmpPath.setDirectory(LLVM_LIBDIR))
175 if (tmpPath.readable())
176 Paths.push_back(tmpPath);
179 GetSystemLibraryPaths(Paths);
183 Path::GetLLVMDefaultConfigDir() {
184 return Path("/etc/llvm/");
188 Path::GetLLVMConfigDir() {
189 return GetLLVMDefaultConfigDir();
193 Path::GetUserHomeDirectory() {
194 const char* home = getenv("HOME");
197 if (result.setDirectory(home))
200 return GetRootDirectory();
202 // FIXME: the above set of functions don't map to Windows very well.
205 Path::isFile() const {
206 return (isValid() && path[path.length()-1] != '/');
210 Path::isDirectory() const {
211 return (isValid() && path[path.length()-1] == '/');
215 Path::getBasename() const {
216 // Find the last slash
217 size_t slash = path.rfind('/');
218 if (slash == std::string::npos)
223 return path.substr(slash, path.rfind('.'));
226 bool Path::hasMagicNumber(const std::string &Magic) const {
227 size_t len = Magic.size();
228 char *buf = reinterpret_cast<char *>(_alloca(len+1));
229 std::ifstream f(path.c_str());
236 Path::isBytecodeFile() const {
239 std::ifstream f(path.c_str());
242 ThrowErrno("can't read file signature");
243 return 0 == memcmp(buffer,"llvc",4) || 0 == memcmp(buffer,"llvm",4);
247 Path::exists() const {
248 DWORD attr = GetFileAttributes(path.c_str());
249 return attr != INVALID_FILE_ATTRIBUTES;
253 Path::readable() const {
254 // FIXME: take security attributes into account.
255 DWORD attr = GetFileAttributes(path.c_str());
256 return attr != INVALID_FILE_ATTRIBUTES;
260 Path::writable() const {
261 // FIXME: take security attributes into account.
262 DWORD attr = GetFileAttributes(path.c_str());
263 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
267 Path::executable() const {
268 // FIXME: take security attributes into account.
269 DWORD attr = GetFileAttributes(path.c_str());
270 return attr != INVALID_FILE_ATTRIBUTES;
274 Path::getLast() const {
275 // Find the last slash
276 size_t pos = path.rfind('/');
278 // Handle the corner cases
279 if (pos == std::string::npos)
282 // If the last character is a slash
283 if (pos == path.length()-1) {
284 // Find the second to last slash
285 size_t pos2 = path.rfind('/', pos-1);
286 if (pos2 == std::string::npos)
287 return path.substr(0,pos);
289 return path.substr(pos2+1,pos-pos2-1);
291 // Return everything after the last slash
292 return path.substr(pos+1);
296 Path::setDirectory(const std::string& a_path) {
297 if (a_path.size() == 0)
301 FlipBackSlashes(path);
302 size_t last = a_path.size() -1;
303 if (last != 0 && a_path[last] != '/')
313 Path::setFile(const std::string& a_path) {
314 if (a_path.size() == 0)
318 FlipBackSlashes(path);
319 size_t last = a_path.size() - 1;
320 while (last > 0 && a_path[last] == '/')
331 Path::appendDirectory(const std::string& dir) {
345 Path::elideDirectory() {
348 size_t slashpos = path.rfind('/',path.size());
349 if (slashpos == 0 || slashpos == std::string::npos)
351 if (slashpos == path.size() - 1)
352 slashpos = path.rfind('/',slashpos-1);
353 if (slashpos == std::string::npos)
355 path.erase(slashpos);
360 Path::appendFile(const std::string& file) {
376 size_t slashpos = path.rfind('/',path.size());
377 if (slashpos == std::string::npos)
379 path.erase(slashpos+1);
384 Path::appendSuffix(const std::string& suffix) {
398 Path::elideSuffix() {
399 if (isDirectory()) return false;
400 size_t dotpos = path.rfind('.',path.size());
401 size_t slashpos = path.rfind('/',path.size());
402 if (slashpos != std::string::npos && dotpos != std::string::npos &&
404 path.erase(dotpos, path.size()-dotpos);
412 Path::createDirectory( bool create_parents) {
413 // Make sure we're dealing with a directory
414 if (!isDirectory()) return false;
416 // Get a writeable copy of the path name
417 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+1));
418 path.copy(pathname,path.length());
419 pathname[path.length()] = 0;
421 // Determine starting point for initial / search.
422 char *next = pathname;
423 if (pathname[0] == '/' && pathname[1] == '/') {
425 next = strchr(pathname+2, '/');
427 throw std::string(pathname) + ": badly formed remote directory";
429 next = strchr(next+1, '/');
431 throw std::string(pathname) + ": badly formed remote directory";
434 throw std::string(pathname) + ": badly formed remote directory";
436 if (pathname[1] == ':')
437 next += 2; // skip drive letter
439 next++; // skip root directory
442 // If we're supposed to create intermediate directories
443 if (create_parents) {
444 // Loop through the directory components until we're done
446 next = strchr(next, '/');
448 if (!CreateDirectory(pathname, NULL))
449 ThrowError(std::string(pathname) + ": Can't create directory: ");
453 // Drop trailing slash.
454 pathname[path.size()-1] = 0;
455 if (!CreateDirectory(pathname, NULL)) {
456 ThrowError(std::string(pathname) + ": Can't create directory: ");
464 // Make sure we're dealing with a file
465 if (!isFile()) return false;
468 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
469 FILE_ATTRIBUTE_NORMAL, NULL);
470 if (h == INVALID_HANDLE_VALUE)
471 ThrowError(std::string(path.c_str()) + ": Can't create file: ");
478 Path::destroyDirectory(bool remove_contents) {
479 // Make sure we're dealing with a directory
480 if (!isDirectory()) return false;
482 // If it doesn't exist, we're done.
483 if (!exists()) return true;
485 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+1));
486 path.copy(pathname,path.length()+1);
487 int lastchar = path.length() - 1 ;
488 if (pathname[lastchar] == '/')
489 pathname[lastchar] = 0;
491 if (remove_contents) {
492 // Recursively descend the directory to remove its content
493 // FIXME: The correct way of doing this on Windows isn't pretty...
494 // but this may work if unix-like utils are present.
495 std::string cmd("rm -rf ");
499 // Otherwise, try to just remove the one directory
500 if (!RemoveDirectory(pathname))
501 ThrowError(std::string(pathname) + ": Can't destroy directory: ");
507 Path::destroyFile() {
508 if (!isFile()) return false;
510 DWORD attr = GetFileAttributes(path.c_str());
512 // If it doesn't exist, we're done.
513 if (attr == INVALID_FILE_ATTRIBUTES)
516 // Read-only files cannot be deleted on Windows. Must remove the read-only
518 if (attr & FILE_ATTRIBUTE_READONLY) {
519 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
520 ThrowError(std::string(path.c_str()) + ": Can't destroy file: ");
523 if (!DeleteFile(path.c_str()))
524 ThrowError(std::string(path.c_str()) + ": Can't destroy file: ");
531 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab