summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/Unix/PathV2.inc
diff options
context:
space:
mode:
authorMichael J. Spencer <bigcheesegs@gmail.com>2011-01-15 18:52:33 +0000
committerMichael J. Spencer <bigcheesegs@gmail.com>2011-01-15 18:52:33 +0000
commitee1699c3625cfbc2b122e11f4259860fff5b0e68 (patch)
tree38e3d29d53ba8e4ae782b1f41b7f96fbbccce9fd /llvm/lib/Support/Unix/PathV2.inc
parent4a1ff16b2922138333eb4251b7573ff076aa77b4 (diff)
downloadbcm5719-llvm-ee1699c3625cfbc2b122e11f4259860fff5b0e68.tar.gz
bcm5719-llvm-ee1699c3625cfbc2b122e11f4259860fff5b0e68.zip
Support/PathV2: Implement get_magic.
llvm-svn: 123544
Diffstat (limited to 'llvm/lib/Support/Unix/PathV2.inc')
-rw-r--r--llvm/lib/Support/Unix/PathV2.inc31
1 files changed, 31 insertions, 0 deletions
diff --git a/llvm/lib/Support/Unix/PathV2.inc b/llvm/lib/Support/Unix/PathV2.inc
index 80e75ecbf5a..056708a7b0b 100644
--- a/llvm/lib/Support/Unix/PathV2.inc
+++ b/llvm/lib/Support/Unix/PathV2.inc
@@ -462,6 +462,37 @@ error_code directory_iterator_increment(directory_iterator& it) {
return success;
}
+error_code get_magic(const Twine &path, uint32_t len,
+ SmallVectorImpl<char> &result) {
+ SmallString<128> PathStorage;
+ StringRef Path = path.toNullTerminatedStringRef(PathStorage);
+ result.set_size(0);
+
+ // Open path.
+ std::FILE *file = std::fopen(Path.data(), "rb");
+ if (file == 0)
+ return error_code(errno, system_category());
+
+ // Reserve storage.
+ result.reserve(len);
+
+ // Read magic!
+ size_t size = std::fread(result.data(), 1, len, file);
+ if (std::ferror(file) != 0) {
+ std::fclose(file);
+ return error_code(errno, system_category());
+ } else if (size != result.size()) {
+ if (std::feof(file) != 0) {
+ std::fclose(file);
+ result.set_size(size);
+ return make_error_code(errc::value_too_large);
+ }
+ }
+ std::fclose(file);
+ result.set_size(len);
+ return success;
+}
+
} // end namespace fs
} // end namespace sys
} // end namespace llvm
OpenPOWER on IntegriCloud