check backup image size before restoring

Change-Id: I206d34936388c1b072eaaec50e0492710cb80f2e
diff --git a/partition.cpp b/partition.cpp
index 0590d98..d391f98 100644
--- a/partition.cpp
+++ b/partition.cpp
@@ -1300,8 +1300,21 @@
 	string Full_FileName, Command;
 
 	TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
-	ui_print("Restoring %s...\n", Display_Name.c_str());
 	Full_FileName = restore_folder + "/" + Backup_FileName;
+
+	if (!Find_Partition_Size()) {
+		LOGE("Unable to find partition size for '%s'\n", Mount_Point.c_str());
+		return false;
+	}
+	unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
+	if (backup_size > Size) {
+		LOGE("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
+			(int)(backup_size / 1048576LLU), Full_FileName.c_str(),
+			Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
+		return false;
+	}
+
+	ui_print("Restoring %s...\n", Display_Name.c_str());
 	Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
 	LOGI("Restore command: '%s'\n", Command.c_str());
 	system(Command.c_str());