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("/");
121 static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
122 const char* at = path;
123 const char* delim = strchr(at, ';');
125 while( delim != 0 ) {
126 std::string tmp(at, size_t(delim-at));
127 if (tmpPath.setDirectory(tmp))
128 if (tmpPath.readable())
129 Paths.push_back(tmpPath);
131 delim = strchr(at, ';');
134 if (tmpPath.setDirectory(std::string(at)))
135 if (tmpPath.readable())
136 Paths.push_back(tmpPath);
141 Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
142 #ifdef LTDL_SHLIBPATH_VAR
143 char* env_var = getenv(LTDL_SHLIBPATH_VAR);
145 getPathList(env_var,Paths);
148 // FIXME: Should this look at LD_LIBRARY_PATH too?
149 Paths.push_back(sys::Path("C:\\WINDOWS\\SYSTEM32\\"));
150 Paths.push_back(sys::Path("C:\\WINDOWS\\"));
154 Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
155 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
157 getPathList(env_var,Paths);
161 Path tmpPath(std::string(LLVMGCCDIR) + "bytecode-libs/");
162 if (tmpPath.readable())
163 Paths.push_back(tmpPath);
169 if (tmpPath.setDirectory(LLVM_LIBDIR))
170 if (tmpPath.readable())
171 Paths.push_back(tmpPath);
174 GetSystemLibraryPaths(Paths);
178 Path::GetLLVMDefaultConfigDir() {
179 return Path("/etc/llvm/");
183 Path::GetLLVMConfigDir() {
184 return GetLLVMDefaultConfigDir();
188 Path::GetUserHomeDirectory() {
189 const char* home = getenv("HOME");
192 if (result.setDirectory(home))
195 return GetRootDirectory();
197 // FIXME: the above set of functions don't map to Windows very well.
200 Path::isFile() const {
201 return (isValid() && path[path.length()-1] != '/');
205 Path::isDirectory() const {
206 return (isValid() && path[path.length()-1] == '/');
210 Path::getBasename() const {
211 // Find the last slash
212 size_t slash = path.rfind('/');
213 if (slash == std::string::npos)
218 return path.substr(slash, path.rfind('.'));
221 bool Path::hasMagicNumber(const std::string &Magic) const {
222 size_t len = Magic.size();
223 char *buf = reinterpret_cast<char *>(_alloca(len+1));
224 std::ifstream f(path.c_str());
231 Path::isBytecodeFile() const {
234 std::ifstream f(path.c_str());
237 ThrowErrno("can't read file signature");
238 return 0 == memcmp(buffer,"llvc",4) || 0 == memcmp(buffer,"llvm",4);
242 Path::exists() const {
243 DWORD attr = GetFileAttributes(path.c_str());
244 return attr != INVALID_FILE_ATTRIBUTES;
248 Path::readable() const {
249 // FIXME: take security attributes into account.
250 DWORD attr = GetFileAttributes(path.c_str());
251 return attr != INVALID_FILE_ATTRIBUTES;
255 Path::writable() const {
256 // FIXME: take security attributes into account.
257 DWORD attr = GetFileAttributes(path.c_str());
258 return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
262 Path::executable() const {
263 // FIXME: take security attributes into account.
264 DWORD attr = GetFileAttributes(path.c_str());
265 return attr != INVALID_FILE_ATTRIBUTES;
269 Path::getLast() const {
270 // Find the last slash
271 size_t pos = path.rfind('/');
273 // Handle the corner cases
274 if (pos == std::string::npos)
277 // If the last character is a slash
278 if (pos == path.length()-1) {
279 // Find the second to last slash
280 size_t pos2 = path.rfind('/', pos-1);
281 if (pos2 == std::string::npos)
282 return path.substr(0,pos);
284 return path.substr(pos2+1,pos-pos2-1);
286 // Return everything after the last slash
287 return path.substr(pos+1);
291 Path::getStatusInfo(StatusInfo& info) const {
292 WIN32_FILE_ATTRIBUTE_DATA fi;
293 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
294 ThrowError(std::string(path) + ": Can't get status: ");
296 info.fileSize = fi.nFileSizeHigh;
297 info.fileSize <<= 32;
298 info.fileSize += fi.nFileSizeLow;
300 info.mode = 0777; // Not applicable to Windows, so...
301 info.user = 9999; // Not applicable to Windows, so...
302 info.group = 9999; // Not applicable to Windows, so...
304 __int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
305 info.modTime.fromWin32Time(ft);
307 info.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
308 if (info.isDir && path[path.length() - 1] != '/')
310 else if (!info.isDir && path[path.length() - 1] == '/')
311 path.erase(path.length() - 1);
314 void Path::makeReadable() {
315 // All files are readable on Windows (ignoring security attributes).
318 void Path::makeWriteable() {
319 DWORD attr = GetFileAttributes(path.c_str());
321 // If it doesn't exist, we're done.
322 if (attr == INVALID_FILE_ATTRIBUTES)
325 if (attr & FILE_ATTRIBUTE_READONLY) {
326 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
327 ThrowError(std::string(path) + ": Can't make file writable: ");
331 void Path::makeExecutable() {
332 // All files are executable on Windows (ignoring security attributes).
336 Path::setDirectory(const std::string& a_path) {
337 if (a_path.size() == 0)
341 FlipBackSlashes(path);
342 size_t last = a_path.size() -1;
343 if (a_path[last] != '/')
353 Path::setFile(const std::string& a_path) {
354 if (a_path.size() == 0)
358 FlipBackSlashes(path);
359 size_t last = a_path.size() - 1;
360 while (last > 0 && a_path[last] == '/')
371 Path::appendDirectory(const std::string& dir) {
385 Path::elideDirectory() {
388 size_t slashpos = path.rfind('/',path.size());
389 if (slashpos == 0 || slashpos == std::string::npos)
391 if (slashpos == path.size() - 1)
392 slashpos = path.rfind('/',slashpos-1);
393 if (slashpos == std::string::npos)
395 path.erase(slashpos);
400 Path::appendFile(const std::string& file) {
416 size_t slashpos = path.rfind('/',path.size());
417 if (slashpos == std::string::npos)
419 path.erase(slashpos+1);
424 Path::appendSuffix(const std::string& suffix) {
438 Path::elideSuffix() {
439 if (isDirectory()) return false;
440 size_t dotpos = path.rfind('.',path.size());
441 size_t slashpos = path.rfind('/',path.size());
442 if (slashpos != std::string::npos && dotpos != std::string::npos &&
444 path.erase(dotpos, path.size()-dotpos);
452 Path::createDirectory( bool create_parents) {
453 // Make sure we're dealing with a directory
454 if (!isDirectory()) return false;
456 // Get a writeable copy of the path name
457 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+1));
458 path.copy(pathname,path.length());
459 pathname[path.length()] = 0;
461 // Determine starting point for initial / search.
462 char *next = pathname;
463 if (pathname[0] == '/' && pathname[1] == '/') {
465 next = strchr(pathname+2, '/');
467 throw std::string(pathname) + ": badly formed remote directory";
469 next = strchr(next+1, '/');
471 throw std::string(pathname) + ": badly formed remote directory";
474 throw std::string(pathname) + ": badly formed remote directory";
476 if (pathname[1] == ':')
477 next += 2; // skip drive letter
479 next++; // skip root directory
482 // If we're supposed to create intermediate directories
483 if (create_parents) {
484 // Loop through the directory components until we're done
486 next = strchr(next, '/');
488 if (!CreateDirectory(pathname, NULL))
489 ThrowError(std::string(pathname) + ": Can't create directory: ");
493 // Drop trailing slash.
494 pathname[path.size()-1] = 0;
495 if (!CreateDirectory(pathname, NULL)) {
496 ThrowError(std::string(pathname) + ": Can't create directory: ");
504 // Make sure we're dealing with a file
505 if (!isFile()) return false;
508 HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
509 FILE_ATTRIBUTE_NORMAL, NULL);
510 if (h == INVALID_HANDLE_VALUE)
511 ThrowError(std::string(path.c_str()) + ": Can't create file: ");
518 Path::destroyDirectory(bool remove_contents) {
519 // Make sure we're dealing with a directory
520 if (!isDirectory()) return false;
522 // If it doesn't exist, we're done.
523 if (!exists()) return true;
525 char *pathname = reinterpret_cast<char *>(_alloca(path.length()+1));
526 path.copy(pathname,path.length()+1);
527 int lastchar = path.length() - 1 ;
528 if (pathname[lastchar] == '/')
529 pathname[lastchar] = 0;
531 if (remove_contents) {
532 // Recursively descend the directory to remove its content
533 // FIXME: The correct way of doing this on Windows isn't pretty...
534 // but this may work if unix-like utils are present.
535 std::string cmd("rm -rf ");
539 // Otherwise, try to just remove the one directory
540 if (!RemoveDirectory(pathname))
541 ThrowError(std::string(pathname) + ": Can't destroy directory: ");
547 Path::destroyFile() {
548 if (!isFile()) return false;
550 DWORD attr = GetFileAttributes(path.c_str());
552 // If it doesn't exist, we're done.
553 if (attr == INVALID_FILE_ATTRIBUTES)
556 // Read-only files cannot be deleted on Windows. Must remove the read-only
558 if (attr & FILE_ATTRIBUTE_READONLY) {
559 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
560 ThrowError(std::string(path.c_str()) + ": Can't destroy file: ");
563 if (!DeleteFile(path.c_str()))
564 ThrowError(std::string(path.c_str()) + ": Can't destroy file: ");
568 bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
571 assert(len < 1024 && "Request for magic string too long");
572 char* buf = (char*) alloca(1 + len);
573 std::ofstream ofs(path.c_str(),std::ofstream::in);
576 std::ifstream ifs(path.c_str());
590 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab