[llvm-commits] [llvm] r122587 - in /llvm/trunk: include/llvm/Support/FileSystem.h lib/Support/PathV2.cpp

Michael J. Spencer bigcheesegs at gmail.com
Mon Dec 27 17:49:01 PST 2010


Author: mspencer
Date: Mon Dec 27 19:49:01 2010
New Revision: 122587

URL: http://llvm.org/viewvc/llvm-project?rev=122587&view=rev
Log:
Support/PathV2: Implement has_magic.

Modified:
    llvm/trunk/include/llvm/Support/FileSystem.h
    llvm/trunk/lib/Support/PathV2.cpp

Modified: llvm/trunk/include/llvm/Support/FileSystem.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/FileSystem.h?rev=122587&r1=122586&r2=122587&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/FileSystem.h (original)
+++ llvm/trunk/include/llvm/Support/FileSystem.h Mon Dec 27 19:49:01 2010
@@ -448,7 +448,7 @@
 /// @param magic Byte sequence to compare \a path's first len(magic) bytes to.
 /// @results errc::success if result has been successfully set, otherwise a
 ///          platform specific error_code.
-error_code has_magic(const Twine &path, const Twine &magic);
+error_code has_magic(const Twine &path, const Twine &magic, bool &result);
 
 /// @brief Get \a path's first \a len bytes.
 ///

Modified: llvm/trunk/lib/Support/PathV2.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/PathV2.cpp?rev=122587&r1=122586&r2=122587&view=diff
==============================================================================
--- llvm/trunk/lib/Support/PathV2.cpp (original)
+++ llvm/trunk/lib/Support/PathV2.cpp Mon Dec 27 19:49:01 2010
@@ -15,6 +15,8 @@
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/ErrorHandling.h"
 #include <cctype>
+#include <cstdio>
+#include <cstring>
 
 namespace {
   using llvm::StringRef;
@@ -659,6 +661,42 @@
   SymlinkStatus = symlink_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());
+  int 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.
+      result = false;
+      return success;
+    }
+    return error_code(error, posix_category());
+  }
+  std::fclose(file);
+
+  if (std::memcmp(BufferStorage.data(), Magic.data(), Magic.size()) != 0)
+    result = false;
+  else
+    result = true;
+  return success;
+}
+
 } // end namespace fs
 } // end namespace sys
 } // end namespace llvm





More information about the llvm-commits mailing list