From: Oleksiy Vyalov Date: Fri, 20 Feb 2015 18:34:03 +0000 (+0000) Subject: Introduce FileSystem::CalculateMD5AsString that supports any platform and make existi... X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7a9e76216b4ce12ce29135cd851fb5f22f77fe22;p=platform%2Fupstream%2Fllvm.git Introduce FileSystem::CalculateMD5AsString that supports any platform and make existing FileSystem::CalculateMD5 to use it. http://reviews.llvm.org/D7771 llvm-svn: 230036 --- diff --git a/lldb/include/lldb/Host/FileSystem.h b/lldb/include/lldb/Host/FileSystem.h index adcbfc9..ddf82b3 100644 --- a/lldb/include/lldb/Host/FileSystem.h +++ b/lldb/include/lldb/Host/FileSystem.h @@ -37,6 +37,7 @@ class FileSystem static Error Unlink(const char *path); static bool CalculateMD5(const FileSpec &file_spec, uint64_t &low, uint64_t &high); + static bool CalculateMD5AsString(const FileSpec &file_spec, std::string& digest_str); }; } diff --git a/lldb/lldb.xcodeproj/project.pbxproj b/lldb/lldb.xcodeproj/project.pbxproj index 3f6498b..8b9ae79 100644 --- a/lldb/lldb.xcodeproj/project.pbxproj +++ b/lldb/lldb.xcodeproj/project.pbxproj @@ -90,6 +90,7 @@ 23EFE389193D1ABC00E54E54 /* SBTypeEnumMember.h in Headers */ = {isa = PBXBuildFile; fileRef = 23EFE388193D1ABC00E54E54 /* SBTypeEnumMember.h */; settings = {ATTRIBUTES = (Public, ); }; }; 23EFE38B193D1AEC00E54E54 /* SBTypeEnumMember.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 23EFE38A193D1AEC00E54E54 /* SBTypeEnumMember.cpp */; }; 23F4034D1926E0F60046DC9B /* NativeRegisterContextRegisterInfo.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 23F403481926CC250046DC9B /* NativeRegisterContextRegisterInfo.cpp */; }; + 250D6AE31A9679440049CC70 /* FileSystem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 250D6AE11A9679270049CC70 /* FileSystem.cpp */; }; 25420ECD1A6490B8009ADBCB /* OptionValueChar.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 25420ECC1A6490B8009ADBCB /* OptionValueChar.cpp */; }; 25420ED21A649D88009ADBCB /* PipeBase.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 25420ED11A649D88009ADBCB /* PipeBase.cpp */; }; 254FBB951A81AA7F00BD6378 /* SBLaunchInfo.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 254FBB941A81AA7F00BD6378 /* SBLaunchInfo.cpp */; }; @@ -1199,6 +1200,7 @@ 23EFE38A193D1AEC00E54E54 /* SBTypeEnumMember.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = SBTypeEnumMember.cpp; path = source/API/SBTypeEnumMember.cpp; sourceTree = ""; }; 23F403471926C8D50046DC9B /* NativeRegisterContextRegisterInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NativeRegisterContextRegisterInfo.h; path = include/lldb/Host/common/NativeRegisterContextRegisterInfo.h; sourceTree = ""; }; 23F403481926CC250046DC9B /* NativeRegisterContextRegisterInfo.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = NativeRegisterContextRegisterInfo.cpp; path = source/Host/common/NativeRegisterContextRegisterInfo.cpp; sourceTree = ""; }; + 250D6AE11A9679270049CC70 /* FileSystem.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FileSystem.cpp; sourceTree = ""; }; 25420ECC1A6490B8009ADBCB /* OptionValueChar.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = OptionValueChar.cpp; path = source/Interpreter/OptionValueChar.cpp; sourceTree = ""; }; 25420ECE1A64911B009ADBCB /* OptionValueChar.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = OptionValueChar.h; path = include/lldb/Interpreter/OptionValueChar.h; sourceTree = ""; }; 25420ED11A649D88009ADBCB /* PipeBase.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PipeBase.cpp; sourceTree = ""; }; @@ -4884,6 +4886,7 @@ 69A01E1A1236C5D400C660B5 /* common */ = { isa = PBXGroup; children = ( + 250D6AE11A9679270049CC70 /* FileSystem.cpp */, 33E5E8411A672A240024ED68 /* StringConvert.cpp */, 25420ED11A649D88009ADBCB /* PipeBase.cpp */, 26CFDCA2186163A4000E63E5 /* Editline.cpp */, @@ -5870,6 +5873,7 @@ 2689002E13353E0400698AC0 /* AddressResolverName.cpp in Sources */, 2689002F13353E0400698AC0 /* ArchSpec.cpp in Sources */, 2689003013353E0400698AC0 /* Baton.cpp in Sources */, + 250D6AE31A9679440049CC70 /* FileSystem.cpp in Sources */, 2689003113353E0400698AC0 /* Broadcaster.cpp in Sources */, 49DCF702170E70120092F75E /* Materializer.cpp in Sources */, 2689003213353E0400698AC0 /* Communication.cpp in Sources */, diff --git a/lldb/source/Host/CMakeLists.txt b/lldb/source/Host/CMakeLists.txt index 19f95b2..cdc79bc 100644 --- a/lldb/source/Host/CMakeLists.txt +++ b/lldb/source/Host/CMakeLists.txt @@ -8,6 +8,7 @@ add_host_subdirectory(common common/File.cpp common/FileCache.cpp common/FileSpec.cpp + common/FileSystem.cpp common/Host.cpp common/HostInfoBase.cpp common/HostNativeThreadBase.cpp diff --git a/lldb/source/Host/common/FileSystem.cpp b/lldb/source/Host/common/FileSystem.cpp new file mode 100644 index 0000000..509a187 --- /dev/null +++ b/lldb/source/Host/common/FileSystem.cpp @@ -0,0 +1,72 @@ +//===-- FileSystem.cpp ------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/Host/FileSystem.h" + +#include "llvm/Support/MD5.h" + +#include +#include + +using namespace lldb; +using namespace lldb_private; + +namespace { + +bool +CalcMD5(const FileSpec &file_spec, llvm::MD5::MD5Result &md5_result) +{ + llvm::MD5 md5_hash; + std::ifstream file(file_spec.GetPath(), std::ios::binary); + if (!file.is_open()) + return false; + + std::vector read_buf(4096); + while (!file.eof()) + { + file.read(&read_buf[0], read_buf.size()); + const auto read_bytes = file.gcount(); + if (read_bytes == 0) + break; + + md5_hash.update(llvm::StringRef(&read_buf[0], read_bytes)); + } + + md5_hash.final(md5_result); + return true; +} + +} // namespace + +bool +FileSystem::CalculateMD5(const FileSpec &file_spec, uint64_t &low, uint64_t &high) +{ + llvm::MD5::MD5Result md5_result; + if (!CalcMD5(file_spec, md5_result)) + return false; + + const auto uint64_res = reinterpret_cast(md5_result); + high = uint64_res[0]; + low = uint64_res[1]; + + return true; +} + +bool +FileSystem::CalculateMD5AsString(const FileSpec &file_spec, std::string& digest_str) +{ + llvm::MD5::MD5Result md5_result; + if (!CalcMD5(file_spec, md5_result)) + return false; + + llvm::SmallString<32> result_str; + llvm::MD5::stringifyResult(md5_result, result_str); + digest_str = result_str.c_str(); + return true; +} diff --git a/lldb/source/Host/posix/FileSystem.cpp b/lldb/source/Host/posix/FileSystem.cpp index 5713168..b55b01b4 100644 --- a/lldb/source/Host/posix/FileSystem.cpp +++ b/lldb/source/Host/posix/FileSystem.cpp @@ -172,30 +172,3 @@ FileSystem::Readlink(const char *path, char *buf, size_t buf_len) error.SetErrorString("'buf' buffer is too small to contain link contents"); return error; } - -bool -FileSystem::CalculateMD5(const FileSpec &file_spec, uint64_t &low, uint64_t &high) -{ -#if defined(__APPLE__) - StreamString md5_cmd_line; - md5_cmd_line.Printf("md5 -q '%s'", file_spec.GetPath().c_str()); - std::string hash_string; - Error err = Host::RunShellCommand(md5_cmd_line.GetData(), NULL, NULL, NULL, &hash_string, 60); - if (err.Fail()) - return false; - // a correctly formed MD5 is 16-bytes, that is 32 hex digits - // if the output is any other length it is probably wrong - if (hash_string.size() != 32) - return false; - std::string part1(hash_string, 0, 16); - std::string part2(hash_string, 16); - const char *part1_cstr = part1.c_str(); - const char *part2_cstr = part2.c_str(); - high = ::strtoull(part1_cstr, NULL, 16); - low = ::strtoull(part2_cstr, NULL, 16); - return true; -#else - // your own MD5 implementation here - return false; -#endif -} diff --git a/lldb/source/Host/windows/FileSystem.cpp b/lldb/source/Host/windows/FileSystem.cpp index f641ff5..a58ddef 100644 --- a/lldb/source/Host/windows/FileSystem.cpp +++ b/lldb/source/Host/windows/FileSystem.cpp @@ -138,9 +138,3 @@ FileSystem::Readlink(const char *path, char *buf, size_t buf_len) ::CloseHandle(h); return error; } - -bool -FileSystem::CalculateMD5(const FileSpec &file_spec, uint64_t &low, uint64_t &high) -{ - return false; -}