X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=blobdiff_plain;f=lib%2FSupport%2FPath.cpp;h=4952f59fc24dd62db383ce328d341f8ec1ee611f;hp=cfd9ed6db3dd6a7a5ccf6d51697b177aff93b1ec;hb=8cda80ad2486782d40ac51cb07dfc630fee47639;hpb=3ed45fe2be4356351942a2cfe9bd92e996d4fcad diff --git a/lib/Support/Path.cpp b/lib/Support/Path.cpp index cfd9ed6db3d..4952f59fc24 100644 --- a/lib/Support/Path.cpp +++ b/lib/Support/Path.cpp @@ -11,14 +11,15 @@ // //===----------------------------------------------------------------------===// -#include "llvm/Support/Path.h" +#include "llvm/Support/COFF.h" #include "llvm/Support/Endian.h" +#include "llvm/Support/Errc.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/Process.h" #include -#include #include -#include #if !defined(_MSC_VER) && !defined(__MINGW32__) #include @@ -26,16 +27,19 @@ #include #endif +using namespace llvm; +using namespace llvm::support::endian; + namespace { using llvm::StringRef; using llvm::sys::path::is_separator; #ifdef LLVM_ON_WIN32 const char *separators = "\\/"; - const char prefered_separator = '\\'; + const char preferred_separator = '\\'; #else const char separators = '/'; - const char prefered_separator = '/'; + const char preferred_separator = '/'; #endif StringRef find_first_component(StringRef path) { @@ -43,7 +47,6 @@ namespace { // * empty (in this case we return an empty string) // * either C: or {//,\\}net. // * {/,\} - // * {.,..} // * {file,directory}name if (path.empty()) @@ -70,14 +73,8 @@ namespace { if (is_separator(path[0])) return path.substr(0, 1); - if (path.startswith("..")) - return path.substr(0, 2); - - if (path[0] == '.') - return path.substr(0, 1); - // * {file,directory}name - size_t end = path.find_first_of(separators, 2); + size_t end = path.find_first_of(separators); return path.substr(0, end); } @@ -160,11 +157,73 @@ enum FSEntity { FS_Name }; -// Implemented in Unix/Path.inc and Windows/Path.inc. -static llvm::error_code -createUniqueEntity(const llvm::Twine &Model, int &ResultFD, - llvm::SmallVectorImpl &ResultPath, - bool MakeAbsolute, unsigned Mode, FSEntity Type); +static std::error_code createUniqueEntity(const Twine &Model, int &ResultFD, + SmallVectorImpl &ResultPath, + bool MakeAbsolute, unsigned Mode, + FSEntity Type) { + SmallString<128> ModelStorage; + Model.toVector(ModelStorage); + + if (MakeAbsolute) { + // Make model absolute by prepending a temp directory if it's not already. + if (!sys::path::is_absolute(Twine(ModelStorage))) { + SmallString<128> TDir; + sys::path::system_temp_directory(true, TDir); + sys::path::append(TDir, Twine(ModelStorage)); + ModelStorage.swap(TDir); + } + } + + // From here on, DO NOT modify model. It may be needed if the randomly chosen + // path already exists. + ResultPath = ModelStorage; + // Null terminate. + ResultPath.push_back(0); + ResultPath.pop_back(); + +retry_random_path: + // Replace '%' with random chars. + for (unsigned i = 0, e = ModelStorage.size(); i != e; ++i) { + if (ModelStorage[i] == '%') + ResultPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15]; + } + + // Try to open + create the file. + switch (Type) { + case FS_File: { + if (std::error_code EC = + sys::fs::openFileForWrite(Twine(ResultPath.begin()), ResultFD, + sys::fs::F_RW | sys::fs::F_Excl, Mode)) { + if (EC == errc::file_exists) + goto retry_random_path; + return EC; + } + + return std::error_code(); + } + + case FS_Name: { + std::error_code EC = + sys::fs::access(ResultPath.begin(), sys::fs::AccessMode::Exist); + if (EC == errc::no_such_file_or_directory) + return std::error_code(); + if (EC) + return EC; + goto retry_random_path; + } + + case FS_Dir: { + if (std::error_code EC = + sys::fs::create_directory(ResultPath.begin(), false)) { + if (EC == errc::file_exists) + goto retry_random_path; + return EC; + } + return std::error_code(); + } + } + llvm_unreachable("Invalid Type"); +} namespace llvm { namespace sys { @@ -238,22 +297,42 @@ const_iterator &const_iterator::operator++() { return *this; } -const_iterator &const_iterator::operator--() { - // If we're at the end and the previous char was a '/', return '.'. +bool const_iterator::operator==(const const_iterator &RHS) const { + return Path.begin() == RHS.Path.begin() && Position == RHS.Position; +} + +ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const { + return Position - RHS.Position; +} + +reverse_iterator rbegin(StringRef Path) { + reverse_iterator I; + I.Path = Path; + I.Position = Path.size(); + return ++I; +} + +reverse_iterator rend(StringRef Path) { + reverse_iterator I; + I.Path = Path; + I.Component = Path.substr(0, 0); + I.Position = 0; + return I; +} + +reverse_iterator &reverse_iterator::operator++() { + // If we're at the end and the previous char was a '/', return '.' unless + // we are the root path. + size_t root_dir_pos = root_dir_start(Path); if (Position == Path.size() && - Path.size() > 1 && - is_separator(Path[Position - 1]) -#ifdef LLVM_ON_WIN32 - && Path[Position - 2] != ':' -#endif - ) { + Path.size() > root_dir_pos + 1 && + is_separator(Path[Position - 1])) { --Position; Component = "."; return *this; } // Skip separators unless it's the root directory. - size_t root_dir_pos = root_dir_start(Path); size_t end_pos = Position; while(end_pos > 0 && @@ -268,20 +347,12 @@ const_iterator &const_iterator::operator--() { return *this; } -bool const_iterator::operator==(const const_iterator &RHS) const { - return Path.begin() == RHS.Path.begin() && +bool reverse_iterator::operator==(const reverse_iterator &RHS) const { + return Path.begin() == RHS.Path.begin() && Component == RHS.Component && Position == RHS.Position; } -bool const_iterator::operator!=(const const_iterator &RHS) const { - return !(*this == RHS); -} - -ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const { - return Position - RHS.Position; -} - -const StringRef root_path(StringRef path) { +StringRef root_path(StringRef path) { const_iterator b = begin(path), pos = b, e = end(path); @@ -313,7 +384,7 @@ const StringRef root_path(StringRef path) { return StringRef(); } -const StringRef root_name(StringRef path) { +StringRef root_name(StringRef path) { const_iterator b = begin(path), e = end(path); if (b != e) { @@ -335,7 +406,7 @@ const StringRef root_name(StringRef path) { return StringRef(); } -const StringRef root_directory(StringRef path) { +StringRef root_directory(StringRef path) { const_iterator b = begin(path), pos = b, e = end(path); @@ -364,7 +435,7 @@ const StringRef root_directory(StringRef path) { return StringRef(); } -const StringRef relative_path(StringRef path) { +StringRef relative_path(StringRef path) { StringRef root = root_path(path); return path.substr(root.size()); } @@ -384,17 +455,15 @@ void append(SmallVectorImpl &path, const Twine &a, if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage)); if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage)); - for (SmallVectorImpl::const_iterator i = components.begin(), - e = components.end(); - i != e; ++i) { + for (auto &component : components) { bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]); - bool component_has_sep = !i->empty() && is_separator((*i)[0]); - bool is_root_name = has_root_name(*i); + bool component_has_sep = !component.empty() && is_separator(component[0]); + bool is_root_name = has_root_name(component); if (path_has_sep) { // Strip separators from beginning of component. - size_t loc = i->find_first_not_of(separators); - StringRef c = i->substr(loc); + size_t loc = component.find_first_not_of(separators); + StringRef c = component.substr(loc); // Append it. path.append(c.begin(), c.end()); @@ -403,10 +472,10 @@ void append(SmallVectorImpl &path, const Twine &a, if (!component_has_sep && !(path.empty() || is_root_name)) { // Add a separator. - path.push_back(prefered_separator); + path.push_back(preferred_separator); } - path.append(i->begin(), i->end()); + path.append(component.begin(), component.end()); } } @@ -416,7 +485,7 @@ void append(SmallVectorImpl &path, path::append(path, *begin); } -const StringRef parent_path(StringRef path) { +StringRef parent_path(StringRef path) { size_t end_pos = parent_path_end(path); if (end_pos == StringRef::npos) return StringRef(); @@ -449,31 +518,36 @@ void replace_extension(SmallVectorImpl &path, const Twine &extension) { } void native(const Twine &path, SmallVectorImpl &result) { + assert((!path.isSingleStringRef() || + path.getSingleStringRef().data() != result.data()) && + "path and result are not allowed to overlap!"); // Clear result. result.clear(); + path.toVector(result); + native(result); +} + +void native(SmallVectorImpl &Path) { #ifdef LLVM_ON_WIN32 - SmallString<128> path_storage; - StringRef p = path.toStringRef(path_storage); - result.reserve(p.size()); - for (StringRef::const_iterator i = p.begin(), - e = p.end(); - i != e; - ++i) { - if (*i == '/') - result.push_back('\\'); - else - result.push_back(*i); - } + std::replace(Path.begin(), Path.end(), '/', '\\'); #else - path.toVector(result); + for (auto PI = Path.begin(), PE = Path.end(); PI < PE; ++PI) { + if (*PI == '\\') { + auto PN = PI + 1; + if (PN < PE && *PN == '\\') + ++PI; // increment once, the for loop will move over the escaped slash + else + *PI = '/'; + } + } #endif } -const StringRef filename(StringRef path) { - return *(--end(path)); +StringRef filename(StringRef path) { + return *rbegin(path); } -const StringRef stem(StringRef path) { +StringRef stem(StringRef path) { StringRef fname = filename(path); size_t pos = fname.find_last_of('.'); if (pos == StringRef::npos) @@ -486,7 +560,7 @@ const StringRef stem(StringRef path) { return fname.substr(0, pos); } -const StringRef extension(StringRef path) { +StringRef extension(StringRef path) { StringRef fname = filename(path); size_t pos = fname.find_last_of('.'); if (pos == StringRef::npos) @@ -509,55 +583,10 @@ bool is_separator(char value) { } } -void system_temp_directory(bool erasedOnReboot, SmallVectorImpl &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; -#ifdef LLVM_ON_WIN32 - EnvironmentVariable = "TEMP"; -#else - EnvironmentVariable = "TMPDIR"; -#endif - if (char *RequestedDir = getenv(EnvironmentVariable)) { - result.append(RequestedDir, RequestedDir + strlen(RequestedDir)); - return; - } +static const char preferred_separator_string[] = { preferred_separator, '\0' }; - // Fall back to a system default. - const char *DefaultResult; -#ifdef LLVM_ON_WIN32 - (void)erasedOnReboot; - DefaultResult = "C:\\TEMP"; -#else - if (erasedOnReboot) - DefaultResult = "/tmp"; - else - DefaultResult = "/var/tmp"; -#endif - result.append(DefaultResult, DefaultResult + strlen(DefaultResult)); +StringRef get_separator() { + return preferred_separator_string; } bool has_root_name(const Twine &path) { @@ -630,37 +659,81 @@ bool is_absolute(const Twine &path) { return rootDir && rootName; } -bool is_relative(const Twine &path) { - return !is_absolute(path); +bool is_relative(const Twine &path) { return !is_absolute(path); } + +StringRef remove_leading_dotslash(StringRef Path) { + // Remove leading "./" (or ".//" or "././" etc.) + while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1])) { + Path = Path.substr(2); + while (Path.size() > 0 && is_separator(Path[0])) + Path = Path.substr(1); + } + return Path; +} + +static SmallString<256> remove_dots(StringRef path, bool remove_dot_dot) { + SmallVector components; + + // Skip the root path, then look for traversal in the components. + StringRef rel = path::relative_path(path); + for (StringRef C : llvm::make_range(path::begin(rel), path::end(rel))) { + if (C == ".") + continue; + if (remove_dot_dot) { + if (C == "..") { + if (!components.empty()) + components.pop_back(); + continue; + } + } + components.push_back(C); + } + + SmallString<256> buffer = path::root_path(path); + for (StringRef C : components) + path::append(buffer, C); + return buffer; +} + +bool remove_dots(SmallVectorImpl &path, bool remove_dot_dot) { + StringRef p(path.data(), path.size()); + + SmallString<256> result = remove_dots(p, remove_dot_dot); + if (result == path) + return false; + + path.swap(result); + return true; } } // end namespace path namespace fs { -error_code getUniqueID(const Twine Path, UniqueID &Result) { +std::error_code getUniqueID(const Twine Path, UniqueID &Result) { file_status Status; - error_code EC = status(Path, Status); + std::error_code EC = status(Path, Status); if (EC) return EC; Result = Status.getUniqueID(); - return error_code::success(); + return std::error_code(); } -error_code createUniqueFile(const Twine &Model, int &ResultFd, - SmallVectorImpl &ResultPath, unsigned Mode) { +std::error_code createUniqueFile(const Twine &Model, int &ResultFd, + SmallVectorImpl &ResultPath, + unsigned Mode) { return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File); } -error_code createUniqueFile(const Twine &Model, - SmallVectorImpl &ResultPath) { +std::error_code createUniqueFile(const Twine &Model, + SmallVectorImpl &ResultPath) { int Dummy; return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name); } -static error_code createTemporaryFile(const Twine &Model, int &ResultFD, - llvm::SmallVectorImpl &ResultPath, - FSEntity Type) { +static std::error_code +createTemporaryFile(const Twine &Model, int &ResultFD, + llvm::SmallVectorImpl &ResultPath, FSEntity Type) { SmallString<128> Storage; StringRef P = Model.toNullTerminatedStringRef(Storage); assert(P.find_first_of(separators) == StringRef::npos && @@ -670,24 +743,22 @@ static error_code createTemporaryFile(const Twine &Model, int &ResultFD, true, owner_read | owner_write, Type); } -static error_code +static std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, - llvm::SmallVectorImpl &ResultPath, - FSEntity Type) { + llvm::SmallVectorImpl &ResultPath, FSEntity Type) { const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%."; return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath, Type); } - -error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, - int &ResultFD, - SmallVectorImpl &ResultPath) { +std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, + int &ResultFD, + SmallVectorImpl &ResultPath) { return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File); } -error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, - SmallVectorImpl &ResultPath) { +std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, + SmallVectorImpl &ResultPath) { int Dummy; return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name); } @@ -695,14 +766,16 @@ error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, // This is a mkdtemp with a different pattern. We use createUniqueEntity mostly // for consistency. We should try using mkdtemp. -error_code createUniqueDirectory(const Twine &Prefix, - SmallVectorImpl &ResultPath) { +std::error_code createUniqueDirectory(const Twine &Prefix, + SmallVectorImpl &ResultPath) { int Dummy; return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath, true, 0, FS_Dir); } -error_code make_absolute(SmallVectorImpl &path) { +static std::error_code make_absolute(const Twine ¤t_directory, + SmallVectorImpl &path, + bool use_current_directory) { StringRef p(path.data(), path.size()); bool rootDirectory = path::has_root_directory(p), @@ -714,11 +787,14 @@ error_code make_absolute(SmallVectorImpl &path) { // Already absolute. if (rootName && rootDirectory) - return error_code::success(); + return std::error_code(); // All of the following conditions will need the current directory. SmallString<128> current_dir; - if (error_code ec = current_path(current_dir)) return ec; + if (use_current_directory) + current_directory.toVector(current_dir); + else if (std::error_code ec = current_path(current_dir)) + return ec; // Relative path. Prepend the current directory. if (!rootName && !rootDirectory) { @@ -726,7 +802,7 @@ error_code make_absolute(SmallVectorImpl &path) { path::append(current_dir, p); // Set path to the result. path.swap(current_dir); - return error_code::success(); + return std::error_code(); } if (!rootName && rootDirectory) { @@ -735,7 +811,7 @@ error_code make_absolute(SmallVectorImpl &path) { path::append(curDirRootName, p); // Set path to the result. path.swap(curDirRootName); - return error_code::success(); + return std::error_code(); } if (rootName && !rootDirectory) { @@ -747,27 +823,78 @@ error_code make_absolute(SmallVectorImpl &path) { SmallString<128> res; path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath); path.swap(res); - return error_code::success(); + return std::error_code(); } llvm_unreachable("All rootName and rootDirectory combinations should have " "occurred above!"); } -error_code create_directories(const Twine &path, bool &existed) { - SmallString<128> path_storage; - StringRef p = path.toStringRef(path_storage); +std::error_code make_absolute(const Twine ¤t_directory, + SmallVectorImpl &path) { + return make_absolute(current_directory, path, true); +} + +std::error_code make_absolute(SmallVectorImpl &path) { + return make_absolute(Twine(), path, false); +} + +std::error_code create_directories(const Twine &Path, bool IgnoreExisting, + perms Perms) { + SmallString<128> PathStorage; + StringRef P = Path.toStringRef(PathStorage); + + // Be optimistic and try to create the directory + std::error_code EC = create_directory(P, IgnoreExisting, Perms); + // If we succeeded, or had any error other than the parent not existing, just + // return it. + if (EC != errc::no_such_file_or_directory) + return EC; + + // We failed because of a no_such_file_or_directory, try to create the + // parent. + StringRef Parent = path::parent_path(P); + if (Parent.empty()) + return EC; - StringRef parent = path::parent_path(p); - if (!parent.empty()) { - bool parent_exists; - if (error_code ec = fs::exists(parent, parent_exists)) return ec; + if ((EC = create_directories(Parent, IgnoreExisting, Perms))) + return EC; + + return create_directory(P, IgnoreExisting, Perms); +} - if (!parent_exists) - if (error_code ec = create_directories(parent, existed)) return ec; +std::error_code copy_file(const Twine &From, const Twine &To) { + int ReadFD, WriteFD; + if (std::error_code EC = openFileForRead(From, ReadFD)) + return EC; + if (std::error_code EC = openFileForWrite(To, WriteFD, F_None)) { + close(ReadFD); + return EC; } - return create_directory(p, existed); + const size_t BufSize = 4096; + char *Buf = new char[BufSize]; + int BytesRead = 0, BytesWritten = 0; + for (;;) { + BytesRead = read(ReadFD, Buf, BufSize); + if (BytesRead <= 0) + break; + while (BytesRead) { + BytesWritten = write(WriteFD, Buf, BytesRead); + if (BytesWritten < 0) + break; + BytesRead -= BytesWritten; + } + if (BytesWritten < 0) + break; + } + close(ReadFD); + close(WriteFD); + delete[] Buf; + + if (BytesRead < 0 || BytesWritten < 0) + return std::error_code(errno, std::generic_category()); + return std::error_code(); } bool exists(file_status status) { @@ -782,76 +909,80 @@ bool is_directory(file_status status) { return status.type() == file_type::directory_file; } -error_code is_directory(const Twine &path, bool &result) { +std::error_code is_directory(const Twine &path, bool &result) { file_status st; - if (error_code ec = status(path, st)) + if (std::error_code ec = status(path, st)) return ec; result = is_directory(st); - return error_code::success(); + return std::error_code(); } bool is_regular_file(file_status status) { return status.type() == file_type::regular_file; } -error_code is_regular_file(const Twine &path, bool &result) { +std::error_code is_regular_file(const Twine &path, bool &result) { file_status st; - if (error_code ec = status(path, st)) + if (std::error_code ec = status(path, st)) return ec; result = is_regular_file(st); - return error_code::success(); -} - -bool is_symlink(file_status status) { - return status.type() == file_type::symlink_file; -} - -error_code is_symlink(const Twine &path, bool &result) { - file_status st; - if (error_code ec = status(path, st)) - return ec; - result = is_symlink(st); - return error_code::success(); + return std::error_code(); } bool is_other(file_status status) { return exists(status) && !is_regular_file(status) && - !is_directory(status) && - !is_symlink(status); + !is_directory(status); +} + +std::error_code is_other(const Twine &Path, bool &Result) { + file_status FileStatus; + if (std::error_code EC = status(Path, FileStatus)) + return EC; + Result = is_other(FileStatus); + return std::error_code(); } void directory_entry::replace_filename(const Twine &filename, file_status st) { - SmallString<128> path(Path.begin(), Path.end()); - path::remove_filename(path); + SmallString<128> path = path::parent_path(Path); path::append(path, filename); Path = path.str(); Status = st; } -error_code has_magic(const Twine &path, const Twine &magic, bool &result) { - SmallString<32> MagicStorage; - StringRef Magic = magic.toStringRef(MagicStorage); - SmallString<32> Buffer; - - if (error_code ec = get_magic(path, Magic.size(), Buffer)) { - if (ec == errc::value_too_large) { - // Magic.size() > file_size(Path). - result = false; - return error_code::success(); - } - return ec; - } - - result = Magic == Buffer; - return error_code::success(); -} - /// @brief Identify the magic in magic. - file_magic identify_magic(StringRef Magic) { +file_magic identify_magic(StringRef Magic) { if (Magic.size() < 4) return file_magic::unknown; switch ((unsigned char)Magic[0]) { + case 0x00: { + // COFF bigobj or short import library file + if (Magic[1] == (char)0x00 && Magic[2] == (char)0xff && + Magic[3] == (char)0xff) { + size_t MinSize = offsetof(COFF::BigObjHeader, UUID) + sizeof(COFF::BigObjMagic); + if (Magic.size() < MinSize) + return file_magic::coff_import_library; + + int BigObjVersion = read16le( + Magic.data() + offsetof(COFF::BigObjHeader, Version)); + if (BigObjVersion < COFF::BigObjHeader::MinBigObjectVersion) + return file_magic::coff_import_library; + + const char *Start = Magic.data() + offsetof(COFF::BigObjHeader, UUID); + if (memcmp(Start, COFF::BigObjMagic, sizeof(COFF::BigObjMagic)) != 0) + return file_magic::coff_import_library; + return file_magic::coff_object; + } + // Windows resource file + const char Expected[] = { 0, 0, 0, 0, '\x20', 0, 0, 0, '\xff' }; + if (Magic.size() >= sizeof(Expected) && + memcmp(Magic.data(), Expected, sizeof(Expected)) == 0) + return file_magic::windows_resource; + // 0x0000 = COFF unknown machine type + if (Magic[1] == 0) + return file_magic::coff_object; + break; + } case 0xDE: // 0x0B17C0DE = BC wraper if (Magic[1] == (char)0xC0 && Magic[2] == (char)0x17 && Magic[3] == (char)0x0B) @@ -863,7 +994,8 @@ error_code has_magic(const Twine &path, const Twine &magic, bool &result) { break; case '!': if (Magic.size() >= 8) - if (memcmp(Magic.data(),"!\n",8) == 0) + if (memcmp(Magic.data(), "!\n", 8) == 0 || + memcmp(Magic.data(), "!\n", 8) == 0) return file_magic::archive; break; @@ -875,12 +1007,15 @@ error_code has_magic(const Twine &path, const Twine &magic, bool &result) { unsigned low = Data2MSB ? 17 : 16; if (Magic[high] == 0) switch (Magic[low]) { - default: break; + default: return file_magic::elf; case 1: return file_magic::elf_relocatable; case 2: return file_magic::elf_executable; case 3: return file_magic::elf_shared_object; case 4: return file_magic::elf_core; } + else + // It's still some type of ELF file. + return file_magic::elf; } break; @@ -922,8 +1057,9 @@ error_code has_magic(const Twine &path, const Twine &magic, bool &result) { case 6: return file_magic::macho_dynamically_linked_shared_lib; case 7: return file_magic::macho_dynamic_linker; case 8: return file_magic::macho_bundle; - case 9: return file_magic::macho_dynamic_linker; + case 9: return file_magic::macho_dynamically_linked_shared_lib_stub; case 10: return file_magic::macho_dsym_companion; + case 11: return file_magic::macho_kext_bundle; } break; } @@ -933,6 +1069,7 @@ error_code has_magic(const Twine &path, const Twine &magic, bool &result) { case 0x66: // MPS R4000 Windows case 0x50: // mc68K case 0x4c: // 80386 Windows + case 0xc4: // ARMNT Windows if (Magic[1] == 0x01) return file_magic::coff_object; @@ -942,12 +1079,12 @@ error_code has_magic(const Twine &path, const Twine &magic, bool &result) { return file_magic::coff_object; break; - case 0x4d: // Possible MS-DOS stub on Windows PE file - if (Magic[1] == 0x5a) { - uint32_t off = - *reinterpret_cast(Magic.data() + 0x3c); + case 'M': // Possible MS-DOS stub on Windows PE file + if (Magic[1] == 'Z') { + uint32_t off = read32le(Magic.data() + 0x3c); // PE/COFF file, either EXE or DLL. - if (off < Magic.size() && memcmp(Magic.data() + off, "PE\0\0",4) == 0) + if (off < Magic.size() && + memcmp(Magic.data()+off, COFF::PEMagic, sizeof(COFF::PEMagic)) == 0) return file_magic::pecoff_executable; } break; @@ -963,56 +1100,21 @@ error_code has_magic(const Twine &path, const Twine &magic, bool &result) { return file_magic::unknown; } -error_code identify_magic(const Twine &path, file_magic &result) { - SmallString<32> Magic; - error_code ec = get_magic(path, Magic.capacity(), Magic); - if (ec && ec != errc::value_too_large) - return ec; - - result = identify_magic(Magic); - return error_code::success(); -} - -namespace { -error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) { - if (ft == file_type::directory_file) { - // This code would be a lot better with exceptions ;/. - error_code ec; - directory_iterator i(path, ec); - if (ec) return ec; - for (directory_iterator e; i != e; i.increment(ec)) { - if (ec) return ec; - file_status st; - if (error_code ec = i->status(st)) return ec; - if (error_code ec = remove_all_r(i->path(), st.type(), count)) return ec; - } - bool obviously_this_exists; - if (error_code ec = remove(path, obviously_this_exists)) return ec; - assert(obviously_this_exists); - ++count; // Include the directory itself in the items removed. - } else { - bool obviously_this_exists; - if (error_code ec = remove(path, obviously_this_exists)) return ec; - assert(obviously_this_exists); - ++count; - } - - return error_code::success(); -} -} // end unnamed namespace +std::error_code identify_magic(const Twine &Path, file_magic &Result) { + int FD; + if (std::error_code EC = openFileForRead(Path, FD)) + return EC; -error_code remove_all(const Twine &path, uint32_t &num_removed) { - SmallString<128> path_storage; - StringRef p = path.toStringRef(path_storage); + char Buffer[32]; + int Length = read(FD, Buffer, sizeof(Buffer)); + if (close(FD) != 0 || Length < 0) + return std::error_code(errno, std::generic_category()); - file_status fs; - if (error_code ec = status(path, fs)) - return ec; - num_removed = 0; - return remove_all_r(p, fs.type(), num_removed); + Result = identify_magic(StringRef(Buffer, Length)); + return std::error_code(); } -error_code directory_entry::status(file_status &result) const { +std::error_code directory_entry::status(file_status &result) const { return fs::status(Path, result); } @@ -1027,3 +1129,20 @@ error_code directory_entry::status(file_status &result) const { #if defined(LLVM_ON_WIN32) #include "Windows/Path.inc" #endif + +namespace llvm { +namespace sys { +namespace path { + +bool user_cache_directory(SmallVectorImpl &Result, const Twine &Path1, + const Twine &Path2, const Twine &Path3) { + if (getUserCacheDir(Result)) { + append(Result, Path1, Path2, Path3); + return true; + } + return false; +} + +} // end namespace path +} // end namsspace sys +} // end namespace llvm