diff options
author | Michael J. Spencer <bigcheesegs@gmail.com> | 2011-01-15 18:52:41 +0000 |
---|---|---|
committer | Michael J. Spencer <bigcheesegs@gmail.com> | 2011-01-15 18:52:41 +0000 |
commit | b33594be3de0e73e0b79f5d9827228d58eef62fa (patch) | |
tree | 49ddae11562b29859311a2d10d705842cdd64461 /lib/Support/PathV2.cpp | |
parent | d6cdf1d3cb0263c413684aa20cb8aa91f9e128de (diff) |
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
Diffstat (limited to 'lib/Support/PathV2.cpp')
-rw-r--r-- | lib/Support/PathV2.cpp | 34 |
1 files changed, 8 insertions, 26 deletions
diff --git a/lib/Support/PathV2.cpp b/lib/Support/PathV2.cpp index e2565d55f0..2e818a5a89 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; } |