summaryrefslogtreecommitdiff
path: root/src/lib/util.cc
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2013-07-24 10:32:52 +0100
committerCarl Hetherington <cth@carlh.net>2013-07-24 10:32:52 +0100
commit49a51ff9778f3b72ee962d3e8bd9cf71944f3c2b (patch)
tree2b916b15eabd8132bbc80b00e4a4f09edfd9eb92 /src/lib/util.cc
parent084d5155d410eef68f87635876e136b224db91dc (diff)
Rename Content::_file to path and support md5sums of directories.
Diffstat (limited to 'src/lib/util.cc')
-rw-r--r--src/lib/util.cc38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/lib/util.cc b/src/lib/util.cc
index 7e7e579a4..affbe3b00 100644
--- a/src/lib/util.cc
+++ b/src/lib/util.cc
@@ -407,6 +407,44 @@ md5_digest (boost::filesystem::path file)
return s.str ();
}
+string
+md5_digest_directory (boost::filesystem::path directory)
+{
+ int const buffer_size = 64 * 1024;
+ char buffer[buffer_size];
+
+ MD5_CTX md5_context;
+ MD5_Init (&md5_context);
+
+ for (boost::filesystem::directory_iterator i(directory); i != boost::filesystem::directory_iterator(); ++i) {
+ ifstream f (i->path().string().c_str(), std::ios::binary);
+ if (!f.good ()) {
+ throw OpenFileError (i->path().string());
+ }
+
+ f.seekg (0, std::ios::end);
+ int bytes = f.tellg ();
+ f.seekg (0, std::ios::beg);
+
+ while (bytes > 0) {
+ int const t = min (bytes, buffer_size);
+ f.read (buffer, t);
+ MD5_Update (&md5_context, buffer, t);
+ bytes -= t;
+ }
+ }
+
+ unsigned char digest[MD5_DIGEST_LENGTH];
+ MD5_Final (digest, &md5_context);
+
+ stringstream s;
+ for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
+ s << std::hex << std::setfill('0') << std::setw(2) << ((int) digest[i]);
+ }
+
+ return s.str ();
+}
+
static bool
about_equal (float a, float b)
{