From b40a6348c966bb734d034cb60a24bd7b91d5d38c Mon Sep 17 00:00:00 2001
From: unknown <monty@mysql.com/nosik.monty.fi>
Date: Wed, 23 Jan 2008 05:00:17 +0200
Subject: [PATCH] Fixed bug when using maria_chk --safe-recover with packed
 tables Fixed bug when using maria_chk --safe-recover and getting duplicated
 keys Fixed bug when using maria_pack --join

storage/maria/ma_check.c:
  Fixed bug when using --safe-recover with packed tables
  Fixed bug when using --safe-recover and getting duplicated keys
storage/maria/maria_pack.c:
  Fixed bug when using --join
storage/maria/unittest/ma_test_all-t:
  Added test to check maria_pack --join
---
 storage/maria/ma_check.c             |  8 ++++----
 storage/maria/maria_pack.c           |  7 +++++++
 storage/maria/unittest/ma_test_all-t | 10 +++++++++-
 3 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/storage/maria/ma_check.c b/storage/maria/ma_check.c
index 9c349c4ee7..d8c553a317 100644
--- a/storage/maria/ma_check.c
+++ b/storage/maria/ma_check.c
@@ -2249,7 +2249,7 @@ int maria_repair(HA_CHECK *param, register MARIA_HA *info,
   if (reenable_logging)
     _ma_tmp_disable_logging_for_table(info, 0);
 
-  sort_param.filepos= new_header_length=
+  sort_param.current_filepos= sort_param.filepos= new_header_length=
     ((param->testflag & T_UNPACK) ? 0L : share->pack.header_length);
 
   if (!rep_quick)
@@ -2389,9 +2389,9 @@ int maria_repair(HA_CHECK *param, register MARIA_HA *info,
           _ma_check_print_error(param,"Couldn't delete duplicate row");
           goto err;
         }
-        continue;
       }
       /* purecov: end */
+      continue;
     }
     if (!block_record)
     {
@@ -2501,7 +2501,7 @@ int maria_repair(HA_CHECK *param, register MARIA_HA *info,
   got_error= 0;
   /* If invoked by external program that uses thr_lock */
   if (&share->state.state != info->state)
-    memcpy( &share->state.state, info->state, sizeof(*info->state));
+    memcpy(&share->state.state, info->state, sizeof(*info->state));
 
 err:
   if (scan_inited)
@@ -3606,7 +3606,7 @@ int maria_repair_by_sort(HA_CHECK *param, register MARIA_HA *info,
   got_error=0;
 
   if (&share->state.state != info->state)
-    memcpy( &share->state.state, info->state, sizeof(*info->state));
+    memcpy(&share->state.state, info->state, sizeof(*info->state));
 
 err:
   if (scan_inited)
diff --git a/storage/maria/maria_pack.c b/storage/maria/maria_pack.c
index f5affeb18f..2cdd0eb468 100644
--- a/storage/maria/maria_pack.c
+++ b/storage/maria/maria_pack.c
@@ -3021,10 +3021,17 @@ static int save_state_mrg(File file,PACK_MRG_INFO *mrg,my_off_t new_length,
   options= (mi_uint2korr(state.header.options) | HA_OPTION_COMPRESS_RECORD |
 	    HA_OPTION_READ_ONLY_DATA);
   mi_int2store(state.header.options,options);
+  /* Save the original file type of we have to undo the packing later */
+  state.header.org_data_file_type= state.header.data_file_type;
+  state.header.data_file_type= COMPRESSED_RECORD;
+
   state.state.data_file_length=new_length;
   state.state.del=0;
   state.state.empty=0;
   state.state.records=state.split=(ha_rows) mrg->records;
+  state.create_rename_lsn= state.is_of_horizon= state.skip_redo_lsn=
+    LSN_REPAIRED_BY_MARIA_CHK;
+
   /* See comment above in save_state about key_file_length handling. */
   if (mrg->src_file_has_indexes_disabled)
   {
diff --git a/storage/maria/unittest/ma_test_all-t b/storage/maria/unittest/ma_test_all-t
index 0e9699ca6b..33a8d711ef 100755
--- a/storage/maria/unittest/ma_test_all-t
+++ b/storage/maria/unittest/ma_test_all-t
@@ -359,7 +359,15 @@ sub run_pack_tests()
           "$maria_path/maria_chk$suffix -se test2",
           "$maria_path/maria_pack$suffix --force -s test1",
           "$maria_path/maria_chk$suffix -s --unpack --parallel-recover test2",
-          "$maria_path/maria_chk$suffix -se test2"
+          "$maria_path/maria_chk$suffix -se test2",
+          $NEW_TEST,
+          "$maria_path/ma_test1$suffix $silent -c $row_type",
+	  "cp test1.MAD test2.MAD",
+	  "cp test1.MAI test2.MAI",
+          "$maria_path/maria_pack$suffix --force -s --join=test3 test1 test2",
+          "$maria_path/maria_chk -s test3",
+          "$maria_path/maria_chk -s --safe-recover test3",
+          "$maria_path/maria_chk -s test3"
          );
 
   return &count_tests(\@t) if ($count);
-- 
2.30.9