From 262f66f165516f2ae3923b708d860a8711fc7c58 Mon Sep 17 00:00:00 2001 From: Michael Vrable Date: Thu, 9 May 2013 21:53:07 -0700 Subject: [PATCH] Clean up the file hashing interface and segment metadata hashes. Switch the segment file hashes to the generic hashing code, and ensure that the metadata checkpoint file also has a hash computed and stored. --- hash.cc | 11 +++++++++++ hash.h | 3 +++ main.cc | 7 +++++++ store.cc | 7 ++----- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/hash.cc b/hash.cc index 7e2087a..2875814 100644 --- a/hash.cc +++ b/hash.cc @@ -50,6 +50,17 @@ Hash *Hash::New(const std::string& name) return constructor(); } +std::string Hash::hash_file(const char *filename) +{ + string result; + Hash *hash = Hash::New(); + if (hash->update_from_file(filename)) + result = hash->digest_str(); + + delete hash; + return result; +} + bool Hash::update_from_file(const char *filename) { FILE *f = fopen(filename, "rb"); diff --git a/hash.h b/hash.h index 12d5b47..5f14dab 100644 --- a/hash.h +++ b/hash.h @@ -53,6 +53,9 @@ public: static Hash *New(); static Hash *New(const std::string& name); + // Computes and returns the hash of a file on disk. + static std::string hash_file(const char *filename); + protected: virtual const uint8_t *finalize() = 0; diff --git a/main.cc b/main.cc index ede33c6..91d1cef 100644 --- a/main.cc +++ b/main.cc @@ -917,6 +917,9 @@ int main(int argc, char *argv[]) } } fclose(dbmeta); + + string dbmeta_csum + = Hash::hash_file(dbmeta_file->get_local_path().c_str()); dbmeta_file->send(); db->Close(); @@ -961,6 +964,10 @@ int main(int argc, char *argv[]) fprintf(descriptor, "Scheme: %s\n", backup_scheme.c_str()); fprintf(descriptor, "Root: %s\n", backup_root.c_str()); + if (dbmeta_csum.size() > 0) { + fprintf(descriptor, "Database-state: %s\n", dbmeta_csum.c_str()); + } + if (csum.size() > 0) { fprintf(descriptor, "Checksums: %s\n", csum.c_str()); } diff --git a/store.cc b/store.cc index ccd055d..31ff40f 100644 --- a/store.cc +++ b/store.cc @@ -309,11 +309,8 @@ void TarSegmentStore::close_segment(const string &group) group_sizes[segment->group].second += disk_size; } - SHA1Checksum segment_checksum; - string checksum; - if (segment_checksum.process_file(segment->rf->get_local_path().c_str())) { - checksum = segment_checksum.checksum_str(); - } + string checksum + = Hash::hash_file(segment->rf->get_local_path().c_str()); db->SetSegmentMetadata(segment->name, segment->rf->get_remote_path(), checksum, group, segment->data_size, disk_size); -- 2.20.1