From b33594be3de0e73e0b79f5d9827228d58eef62fa Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Sat, 15 Jan 2011 18:52:41 +0000 Subject: [PATCH] Support/PathV2: Implement has_magic in terms of get_magic. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123545 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/PathV2.cpp | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/lib/Support/PathV2.cpp b/lib/Support/PathV2.cpp index e2565d55f06..2e818a5a89d 100644 --- a/lib/Support/PathV2.cpp +++ b/lib/Support/PathV2.cpp @@ -686,38 +686,20 @@ void directory_entry::replace_filename(const Twine &filename, file_status st, } error_code has_magic(const Twine &path, const Twine &magic, bool &result) { - SmallString<128> PathStorage; SmallString<32> MagicStorage; - StringRef Path = path.toNullTerminatedStringRef(PathStorage); - StringRef Magic = magic.toNullTerminatedStringRef(MagicStorage); - - assert(Magic.size() > 0 && "magic must be non-empty!"); - - SmallString<32> BufferStorage; - BufferStorage.reserve(Magic.size()); - - // Open file. - std::FILE *file = std::fopen(Path.data(), "rb"); - if (file == 0) - return error_code(errno, posix_category()); - size_t size = ::fread(BufferStorage.data(), 1, Magic.size(), file); - if (size != Magic.size()) { - int error = errno; - bool eof = std::feof(file) != 0; - std::fclose(file); - if (eof) { - // EOF, return false. + 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 success; } - return error_code(error, posix_category()); + return ec; } - std::fclose(file); - if (std::memcmp(BufferStorage.data(), Magic.data(), Magic.size()) != 0) - result = false; - else - result = true; + result = Magic == Buffer; return success; } -- 2.34.1