Support/PathV2: Add identify_magic.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123548 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Michael J. Spencer
2011-01-15 20:39:36 +00:00
parent b6516aeef1
commit 28f0ed5c9d
5 changed files with 49 additions and 40 deletions

View File

@ -30,6 +30,7 @@
#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallString.h"
#include "llvm/ADT/Twine.h" #include "llvm/ADT/Twine.h"
#include "llvm/Support/DataTypes.h" #include "llvm/Support/DataTypes.h"
#include "llvm/Support/PathV1.h"
#include "llvm/Support/system_error.h" #include "llvm/Support/system_error.h"
#include <ctime> #include <ctime>
#include <iterator> #include <iterator>
@ -463,6 +464,14 @@ error_code has_magic(const Twine &path, const Twine &magic, bool &result);
error_code get_magic(const Twine &path, uint32_t len, error_code get_magic(const Twine &path, uint32_t len,
SmallVectorImpl<char> &result); SmallVectorImpl<char> &result);
/// @brief Get and identify \a path's type based on its content.
///
/// @param path Input path.
/// @param result Set to the type of file, or LLVMFileType::Unknown_FileType.
/// @results errc::success if result has been successfully set, otherwise a
/// platform specific error_code.
error_code identify_magic(const Twine &path, LLVMFileType &result);
/// @brief Is file bitcode? /// @brief Is file bitcode?
/// ///
/// @param path Input path. /// @param path Input path.

View File

@ -116,11 +116,10 @@ bool ArchiveMember::replaceWith(const sys::Path& newFile, std::string* ErrMsg) {
// Get the signature and status info // Get the signature and status info
const char* signature = (const char*) data; const char* signature = (const char*) data;
std::string magic; SmallString<4> magic;
if (!signature) { if (!signature) {
path.getMagicNumber(magic,4); sys::fs::get_magic(path.str(), magic.capacity(), magic);
signature = magic.c_str(); signature = magic.c_str();
std::string err;
const sys::FileStatus *FSinfo = path.getFileStatus(false, ErrMsg); const sys::FileStatus *FSinfo = path.getFileStatus(false, ErrMsg);
if (FSinfo) if (FSinfo)
info = *FSinfo; info = *FSinfo;

View File

@ -181,9 +181,11 @@ Archive::addFileBefore(const sys::Path& filePath, iterator where,
flags |= ArchiveMember::HasPathFlag; flags |= ArchiveMember::HasPathFlag;
if (hasSlash || filePath.str().length() > 15) if (hasSlash || filePath.str().length() > 15)
flags |= ArchiveMember::HasLongFilenameFlag; flags |= ArchiveMember::HasLongFilenameFlag;
std::string magic;
mbr->path.getMagicNumber(magic,4); sys::LLVMFileType type;
switch (sys::IdentifyFileType(magic.c_str(),4)) { if (sys::fs::identify_magic(mbr->path.str(), type))
type = sys::Unknown_FileType;
switch (type) {
case sys::Bitcode_FileType: case sys::Bitcode_FileType:
flags |= ArchiveMember::BitcodeFlag; flags |= ArchiveMember::BitcodeFlag;
break; break;

View File

@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/Support/Path.h" #include "llvm/Support/Path.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Config/config.h" #include "llvm/Config/config.h"
#include "llvm/Support/FileSystem.h" #include "llvm/Support/FileSystem.h"
#include <cassert> #include <cassert>
@ -141,42 +142,33 @@ sys::IdentifyFileType(const char *magic, unsigned length) {
bool bool
Path::isArchive() const { Path::isArchive() const {
std::string Magic; LLVMFileType type;
if (getMagicNumber(Magic, 8)) if (fs::identify_magic(str(), type))
if (IdentifyFileType(Magic.c_str(), Magic.length()) == Archive_FileType) return false;
return true; return type == Archive_FileType;
return false;
} }
bool bool
Path::isDynamicLibrary() const { Path::isDynamicLibrary() const {
std::string Magic; LLVMFileType type;
if (getMagicNumber(Magic, 64)) if (fs::identify_magic(str(), type))
switch (IdentifyFileType(Magic.c_str(), return false;
static_cast<unsigned>(Magic.length()))) { switch (type) {
default: return false; default: return false;
case Mach_O_FixedVirtualMemorySharedLib_FileType: case Mach_O_FixedVirtualMemorySharedLib_FileType:
case Mach_O_DynamicallyLinkedSharedLib_FileType: case Mach_O_DynamicallyLinkedSharedLib_FileType:
case Mach_O_DynamicallyLinkedSharedLibStub_FileType: case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
case ELF_SharedObject_FileType: case ELF_SharedObject_FileType:
case COFF_FileType: return true; case COFF_FileType: return true;
} }
return false;
} }
bool bool
Path::isObjectFile() const { Path::isObjectFile() const {
std::string Magic; LLVMFileType type;
if (getMagicNumber(Magic, 64)) if (fs::identify_magic(str(), type) || type == Unknown_FileType)
if (IdentifyFileType(Magic.c_str(), return false;
static_cast<unsigned>(Magic.length())) return true;
!= Unknown_FileType) {
// Everything in LLVMFileType is currently an object file.
return true;
}
return false;
} }
Path Path
@ -210,13 +202,10 @@ Path::appendSuffix(StringRef suffix) {
bool bool
Path::isBitcodeFile() const { Path::isBitcodeFile() const {
std::string actualMagic; LLVMFileType type;
if (!getMagicNumber(actualMagic, 4)) if (fs::identify_magic(str(), type))
return false; return false;
LLVMFileType FT = return type == Bitcode_FileType;
IdentifyFileType(actualMagic.c_str(),
static_cast<unsigned>(actualMagic.length()));
return FT == Bitcode_FileType;
} }
bool Path::hasMagicNumber(StringRef Magic) const { bool Path::hasMagicNumber(StringRef Magic) const {

View File

@ -703,6 +703,16 @@ error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
return success; return success;
} }
error_code identify_magic(const Twine &path, LLVMFileType &result) {
SmallString<32> Magic;
error_code ec = get_magic(path, Magic.capacity(), Magic);
if (ec && ec != errc::value_too_large)
return ec;
result = IdentifyFileType(Magic.data(), Magic.size());
return success;
}
namespace { namespace {
error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) { error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) {
if (ft == file_type::directory_file) { if (ft == file_type::directory_file) {