From 79ae093c82ec6f714719ccdc97b03e1c184bdc01 Mon Sep 17 00:00:00 2001 From: James Christopher Adduono Date: Tue, 25 Oct 2016 02:18:32 -0400 Subject: MD5 verification sanity We can use switches and enums to make things more understandable and work with results in a more sensible manner. PS2: Move file MD5 verification and error handling into a separate function to reduce code. PS3: Default to false in Check_Restore_File_MD5 function Change-Id: Id3ee0bb444cf13be67bb5dec2c43f7c26a86b954 (cherry picked from commit 5c3950716e5c7c6279efd5f3f338d6d22e5215e2) --- partition.cpp | 58 +++++++++++++++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 31 deletions(-) (limited to 'partition.cpp') diff --git a/partition.cpp b/partition.cpp index f22a283df..d6cadf902 100644 --- a/partition.cpp +++ b/partition.cpp @@ -1612,52 +1612,48 @@ bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) return false; } +bool TWPartition::Check_Restore_File_MD5(const string& Filename) { + twrpDigest md5sum; + + md5sum.setfn(Filename); + switch (md5sum.verify_md5digest()) { + case MD5_OK: + gui_msg(Msg("md5_matched=MD5 matched for '{1}'.")(Filename)); + return true; + case MD5_FILE_UNREADABLE: + case MD5_NOT_FOUND: + gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(Filename)); + break; + case MD5_MATCH_FAIL: + gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(Filename)); + break; + } + return false; +} + bool TWPartition::Check_MD5(PartitionSettings *part_settings) { - string Full_Filename, md5file; + string Full_Filename; char split_filename[512]; int index = 0; - twrpDigest md5sum; sync(); - memset(split_filename, 0, sizeof(split_filename)); Full_Filename = part_settings->Backup_Folder + "/" + Backup_FileName; if (!TWFunc::Path_Exists(Full_Filename)) { // This is a split archive, we presume - sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index); - LOGINFO("split_filename: %s\n", split_filename); - md5file = split_filename; - md5file += ".md5"; - if (!TWFunc::Path_Exists(md5file)) { - gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(split_filename)); - return false; - } - md5sum.setfn(split_filename); + memset(split_filename, 0, sizeof(split_filename)); while (index < 1000) { - if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) { - gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename)); + sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index); + if (!TWFunc::Path_Exists(split_filename)) + break; + LOGINFO("split_filename: %s\n", split_filename); + if (!Check_Restore_File_MD5(split_filename)) return false; - } index++; - sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index); - md5sum.setfn(split_filename); } return true; - } else { - // Single file archive - md5file = Full_Filename + ".md5"; - if (!TWFunc::Path_Exists(md5file)) { - gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(md5file)); - return false; - } - md5sum.setfn(Full_Filename); - if (md5sum.verify_md5digest() != 0) { - gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename)); - return false; - } else - return true; } - return false; + return Check_Restore_File_MD5(Full_Filename); // Single file archive } bool TWPartition::Restore(PartitionSettings *part_settings) { -- cgit v1.2.3