This is an archive of the discontinued LLVM Phabricator instance.

Introduce FileSystem::CalculateMD5AsString that supports any platform and make existing FileSystem::CalculateMD5 to use it.
ClosedPublic

Authored by ovyalov on Feb 19 2015, 2:03 PM.

Details

Summary

Introduce FileSystem::CalculateMD5AsString that supports any platform and make existing FileSystem::CalculateMD5 to use it.
Code from this CL will be used in http://reviews.llvm.org/D7709 to get MD5 string by a file path.

Diff Detail

Event Timeline

ovyalov updated this revision to Diff 20341.Feb 19 2015, 2:03 PM
ovyalov retitled this revision from to Introduce FileSystem::CalculateMD5AsString that supports any platform and make existing FileSystem::CalculateMD5 to use it..
ovyalov updated this object.
ovyalov edited the test plan for this revision. (Show Details)
ovyalov added reviewers: clayborg, vharron, zturner.
ovyalov added a subscriber: Unknown Object (MLST).
zturner edited edge metadata.Feb 19 2015, 2:23 PM

Thanks for working on this. It always bothered me that this was platform-specific.

include/lldb/Host/FileSystem.h
39–41

I think it might be more useful to put these functions in LLVM. I'm thinking we should delete both of these and add

void llvm::sys::fs::md5_contents(const Twine &Path, llvm::MD5Result &Result);

On second though, maybe this can stay here. Most applications MD5 memory buffers, not entire files, because they're the ones writing the files to begin with. I will put some comments in a followup

zturner added inline comments.Feb 19 2015, 3:06 PM
source/Host/common/FileSystem.cpp
23

I think it would be better if this file contained a function in an anonymous namespace with a signature like this:

bool CalculateMD5(StringRef path, MD5Result &result);

Then, both this function and the other function could call this to get an MD5Result. From there, this whole function becomes:

llvm::MD5::MD5Result result;
CalculateMD5(StringRef(file_spec.GetPath()), result);
high = ((uint64_t*)result)[0];
low = ((uint64_t*)result)[1];

which is much nicer than first converting a number to a string, and then converting two strings back to numbers.

47

I would prefer if we used a vector<char> here. Taking up 4K of stack is a little bit obnoxious for a simple function.

62

Similarly, with the aforementioned change, this function becomes:

llvm::MD5::MD5Result result;
CalculateMD5(StringRef(file_spec.GetPath()), result);
llvm::MD5::stringifyResult(result, str);
clayborg requested changes to this revision.Feb 19 2015, 3:23 PM
clayborg edited edge metadata.

Move the code that calculates the MD5 into a static function that both FileSystem::CalculateMD5AsString() and FileSystem::CalculateMD5() use. See inlined comments for details.

include/lldb/Host/FileSystem.h
40

I would rather not move this to LLVM just yet. We merge the top of tree SVN to many other branches internally here at apple and these internal branches link against older versions of llvm/clang that won't have this new functionality, so please leave this in FileSystem for now.

source/Host/common/FileSystem.cpp
39–59

Move the MD5 calculation into a static function:

bool
CalculateMD5 (const FileSpec &file_spec, llvm::MD5::MD5Result &md5_result);

And have both versions use it. The version that wants to return two uint64 values should be able to cast the llvm::MD5::MD5Result to a "uint64_t *' and access both numbers and it then avoids creating a string and calling strtoull on the two strings.

This revision now requires changes to proceed.Feb 19 2015, 3:23 PM
ovyalov updated this revision to Diff 20354.Feb 19 2015, 4:05 PM
ovyalov edited edge metadata.

Fixed accordingly to your suggestions.
Please take another look.

Thank you.

zturner accepted this revision.Feb 19 2015, 4:14 PM
zturner edited edge metadata.
zturner added inline comments.
source/Host/common/FileSystem.cpp
62

I'm not sure how important it is for you to have the result in an std::string. For example, are you going to pass it in to another API that accepts an std::string? Or are you just going to print it or log it? As a result, you might consider changing the second argument to an llvm::SmallString<32> & which will save a heap allocation and a string copy. But this is minor, so up to you. Otherwise LGTM

clayborg accepted this revision.Feb 19 2015, 4:38 PM
clayborg edited edge metadata.

Looks good.

This revision is now accepted and ready to land.Feb 19 2015, 4:38 PM
ovyalov added inline comments.Feb 19 2015, 5:06 PM
source/Host/common/FileSystem.cpp
62

Let me leave it as std::string since I'd prefer to keep this function signature more generic.

ovyalov closed this revision.Feb 20 2015, 10:37 AM

AFFECTED FILES

/lldb/trunk/include/lldb/Host/FileSystem.h
/lldb/trunk/lldb.xcodeproj/project.pbxproj
/lldb/trunk/source/Host/CMakeLists.txt
/lldb/trunk/source/Host/common/FileSystem.cpp
/lldb/trunk/source/Host/posix/FileSystem.cpp
/lldb/trunk/source/Host/windows/FileSystem.cpp

USERS

ovyalov (Author)

http://reviews.llvm.org/rL230036