//===----------------------------------------------------------------------===//
#include "llvm/Support/PathV2.h"
-#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/FileSystem.h"
#include <cctype>
#include <cstdio>
#include <cstring>
+#ifdef __APPLE__
+#include <unistd.h>
+#endif
namespace {
using llvm::StringRef;
using llvm::sys::path::is_separator;
#ifdef LLVM_ON_WIN32
- const StringRef separators = "\\/";
- const char prefered_separator = '\\';
+ const char *separators = "\\/";
+ const char prefered_separator = '\\';
#else
- const StringRef separators = "/";
- const char prefered_separator = '/';
+ const char separators = '/';
+ const char prefered_separator = '/';
#endif
- const llvm::error_code success;
-
StringRef find_first_component(StringRef path) {
// Look for this first component in the following order.
// * empty (in this case we return an empty string)
#ifdef LLVM_ON_WIN32
// C:
- if (path.size() >= 2 && std::isalpha(path[0]) && path[1] == ':')
+ if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
+ path[1] == ':')
return path.substr(0, 2);
#endif
const StringRef relative_path(StringRef path) {
StringRef root = root_path(path);
- return root.substr(root.size());
+ return path.substr(root.size());
}
void append(SmallVectorImpl<char> &path, const Twine &a,
void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result) {
result.clear();
+#ifdef __APPLE__
+ // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
+ int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR
+ : _CS_DARWIN_USER_CACHE_DIR;
+ size_t ConfLen = confstr(ConfName, 0, 0);
+ if (ConfLen > 0) {
+ do {
+ result.resize(ConfLen);
+ ConfLen = confstr(ConfName, result.data(), result.size());
+ } while (ConfLen > 0 && ConfLen != result.size());
+
+ if (ConfLen > 0) {
+ assert(result.back() == 0);
+ result.pop_back();
+ return;
+ }
+
+ result.clear();
+ }
+#endif
+
// Check whether the temporary directory is specified by an environment
// variable.
const char *EnvironmentVariable;
error_code make_absolute(SmallVectorImpl<char> &path) {
StringRef p(path.data(), path.size());
- bool rootName = path::has_root_name(p),
- rootDirectory = path::has_root_directory(p);
+ bool rootDirectory = path::has_root_directory(p),
+#ifdef LLVM_ON_WIN32
+ rootName = path::has_root_name(p);
+#else
+ rootName = true;
+#endif
// Already absolute.
if (rootName && rootDirectory)
- return success;
+ return error_code::success();
// All of the following conditions will need the current directory.
SmallString<128> current_dir;
path::append(current_dir, p);
// Set path to the result.
path.swap(current_dir);
- return success;
+ return error_code::success();
}
if (!rootName && rootDirectory) {
path::append(curDirRootName, p);
// Set path to the result.
path.swap(curDirRootName);
- return success;
+ return error_code::success();
}
if (rootName && !rootDirectory) {
SmallString<128> res;
path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
path.swap(res);
- return success;
+ return error_code::success();
}
llvm_unreachable("All rootName and rootDirectory combinations should have "
StringRef p = path.toStringRef(path_storage);
StringRef parent = path::parent_path(p);
- bool parent_exists;
-
- if (error_code ec = fs::exists(parent, parent_exists)) return ec;
+ if (!parent.empty()) {
+ bool parent_exists;
+ if (error_code ec = fs::exists(parent, parent_exists)) return ec;
- if (!parent_exists)
- if (error_code ec = create_directories(parent, existed)) return ec;
+ if (!parent_exists)
+ if (error_code ec = create_directories(parent, existed)) return ec;
+ }
return create_directory(p, existed);
}
if (error_code ec = status(path, st))
return ec;
result = is_directory(st);
- return success;
+ return error_code::success();
}
bool is_regular_file(file_status status) {
if (error_code ec = status(path, st))
return ec;
result = is_regular_file(st);
- return success;
+ return error_code::success();
}
bool is_symlink(file_status status) {
if (error_code ec = status(path, st))
return ec;
result = is_symlink(st);
- return success;
+ return error_code::success();
}
bool is_other(file_status status) {
if (ec == errc::value_too_large) {
// Magic.size() > file_size(Path).
result = false;
- return success;
+ return error_code::success();
}
return ec;
}
result = Magic == Buffer;
- return success;
+ return error_code::success();
}
/// @brief Identify the magic in magic.
file_magic identify_magic(StringRef magic) {
+ if (magic.size() < 4)
+ return file_magic::unknown;
switch ((unsigned char)magic[0]) {
case 0xDE: // 0x0B17C0DE = BC wraper
if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 &&
case '\177':
if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
- if (magic.size() >= 18 && magic[17] == 0)
- switch (magic[16]) {
+ bool Data2MSB = magic[5] == 2;
+ unsigned high = Data2MSB ? 16 : 17;
+ unsigned low = Data2MSB ? 17 : 16;
+ if (magic.size() >= 18 && magic[high] == 0)
+ switch (magic[low]) {
default: break;
case 1: return file_magic::elf_relocatable;
case 2: return file_magic::elf_executable;
return ec;
result = identify_magic(Magic);
- return success;
+ return error_code::success();
}
namespace {
++count;
}
- return success;
+ return error_code::success();
}
} // end unnamed namespace