using std::numeric_limits;
using std::pair;
using std::cout;
+using std::streampos;
using boost::shared_ptr;
using boost::thread;
using boost::lexical_cast;
return s.str ();
}
-/** @param file File name.
- * @return MD5 digest of file's contents.
- */
-string
-md5_digest (boost::filesystem::path file)
-{
- FILE* f = fopen_boost (file, "rb");
- if (!f) {
- throw OpenFileError (file.string());
- }
-
- boost::uintmax_t bytes = boost::filesystem::file_size (file);
-
- boost::uintmax_t const buffer_size = 64 * 1024;
- char buffer[buffer_size];
-
- MD5_CTX md5_context;
- MD5_Init (&md5_context);
- while (bytes > 0) {
- int const t = min (bytes, buffer_size);
- fread (buffer, 1, t, f);
- MD5_Update (&md5_context, buffer, t);
- bytes -= t;
- }
-
- unsigned char digest[MD5_DIGEST_LENGTH];
- MD5_Final (digest, &md5_context);
- fclose (f);
-
- 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 ();
-}
-
/** @param job Optional job for which to report progress */
string
-md5_digest_directory (boost::filesystem::path directory, shared_ptr<Job> job)
+md5_digest (vector<boost::filesystem::path> files, shared_ptr<Job> job)
{
boost::uintmax_t const buffer_size = 64 * 1024;
char buffer[buffer_size];
MD5_CTX md5_context;
MD5_Init (&md5_context);
- int files = 0;
- if (job) {
- for (boost::filesystem::directory_iterator i(directory); i != boost::filesystem::directory_iterator(); ++i) {
- ++files;
- }
+ vector<int64_t> sizes;
+ for (size_t i = 0; i < files.size(); ++i) {
+ sizes.push_back (boost::filesystem::file_size (files[i]));
}
- int j = 0;
- for (boost::filesystem::directory_iterator i(directory); i != boost::filesystem::directory_iterator(); ++i) {
- FILE* f = fopen_boost (i->path(), "rb");
+ for (size_t i = 0; i < files.size(); ++i) {
+ FILE* f = fopen_boost (files[i], "rb");
if (!f) {
- throw OpenFileError (i->path().string());
+ throw OpenFileError (files[i].string());
}
- boost::uintmax_t bytes = boost::filesystem::file_size (i->path ());
+ boost::uintmax_t const bytes = boost::filesystem::file_size (files[i]);
+ boost::uintmax_t remaining = bytes;
- while (bytes > 0) {
- int const t = min (bytes, buffer_size);
+ while (remaining > 0) {
+ int const t = min (remaining, buffer_size);
fread (buffer, 1, t, f);
MD5_Update (&md5_context, buffer, t);
- bytes -= t;
- }
+ remaining -= t;
- if (job) {
- job->set_progress (float (j) / files);
- ++j;
+ if (job) {
+ job->set_progress ((float (i) + 1 - float(remaining) / bytes) / files.size ());
+ }
}
fclose (f);