[llvm-commits] [llvm] r123545 - /llvm/trunk/lib/Support/PathV2.cpp
Michael J. Spencer
bigcheesegs at gmail.com
Sat Jan 15 10:52:41 PST 2011
Author: mspencer
Date: Sat Jan 15 12:52:41 2011
New Revision: 123545
URL: http://llvm.org/viewvc/llvm-project?rev=123545&view=rev
Log:
Support/PathV2: Implement has_magic in terms of get_magic.
Modified:
llvm/trunk/lib/Support/PathV2.cpp
Modified: llvm/trunk/lib/Support/PathV2.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/PathV2.cpp?rev=123545&r1=123544&r2=123545&view=diff
==============================================================================
--- llvm/trunk/lib/Support/PathV2.cpp (original)
+++ llvm/trunk/lib/Support/PathV2.cpp Sat Jan 15 12:52:41 2011
@@ -686,38 +686,20 @@
}
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);
+ StringRef Magic = magic.toStringRef(MagicStorage);
+ SmallString<32> Buffer;
- 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.
+ 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;
}
More information about the llvm-commits
mailing list