You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1821 lines
61 KiB

23 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
23 years ago
26 years ago
26 years ago
23 years ago
24 years ago
24 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
21 years ago
21 years ago
21 years ago
26 years ago
26 years ago
26 years ago
21 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
21 years ago
26 years ago
26 years ago
26 years ago
26 years ago
24 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
24 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
24 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
21 years ago
22 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
  1. /* Copyright (C) 2000-2003 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. /* Describe, check and repair of MyISAM tables */
  13. #include "fulltext.h"
  14. #include <m_ctype.h>
  15. #include <stdarg.h>
  16. #include <my_getopt.h>
  17. #ifdef HAVE_SYS_VADVICE_H
  18. #include <sys/vadvise.h>
  19. #endif
  20. #ifdef HAVE_SYS_MMAN_H
  21. #include <sys/mman.h>
  22. #endif
  23. SET_STACK_SIZE(9000) /* Minimum stack size for program */
  24. #ifndef USE_RAID
  25. #define my_raid_create(A,B,C,D,E,F,G) my_create(A,B,C,G)
  26. #define my_raid_delete(A,B,C) my_delete(A,B)
  27. #endif
  28. #ifdef OS2
  29. #define _sanity(a,b)
  30. #endif
  31. static uint decode_bits;
  32. static char **default_argv;
  33. static const char *load_default_groups[]= { "myisamchk", 0 };
  34. static const char *set_collation_name, *opt_tmpdir;
  35. static CHARSET_INFO *set_collation;
  36. static long opt_myisam_block_size;
  37. static long opt_key_cache_block_size;
  38. static const char *my_progname_short;
  39. static int stopwords_inited= 0;
  40. static MY_TMPDIR myisamchk_tmpdir;
  41. static const char *type_names[]=
  42. { "impossible","char","binary", "short", "long", "float",
  43. "double","number","unsigned short",
  44. "unsigned long","longlong","ulonglong","int24",
  45. "uint24","int8","varchar", "varbin","?",
  46. "?"};
  47. static const char *prefix_packed_txt="packed ",
  48. *bin_packed_txt="prefix ",
  49. *diff_txt="stripped ",
  50. *null_txt="NULL",
  51. *blob_txt="BLOB ";
  52. static const char *field_pack[]=
  53. {"","no endspace", "no prespace",
  54. "no zeros", "blob", "constant", "table-lockup",
  55. "always zero","varchar","unique-hash","?","?"};
  56. static const char *myisam_stats_method_str="nulls_unequal";
  57. static void get_options(int *argc,char * * *argv);
  58. static void print_version(void);
  59. static void usage(void);
  60. static int myisamchk(MI_CHECK *param, char *filename);
  61. static void descript(MI_CHECK *param, register MI_INFO *info, my_string name);
  62. static int mi_sort_records(MI_CHECK *param, register MI_INFO *info,
  63. my_string name, uint sort_key,
  64. my_bool write_info, my_bool update_index);
  65. static int sort_record_index(MI_SORT_PARAM *sort_param, MI_INFO *info,
  66. MI_KEYDEF *keyinfo,
  67. my_off_t page,uchar *buff,uint sortkey,
  68. File new_file, my_bool update_index);
  69. MI_CHECK check_param;
  70. /* Main program */
  71. int main(int argc, char **argv)
  72. {
  73. int error;
  74. MY_INIT(argv[0]);
  75. my_progname_short= my_progname+dirname_length(my_progname);
  76. #ifdef __EMX__
  77. _wildcard (&argc, &argv);
  78. #endif
  79. myisamchk_init(&check_param);
  80. check_param.opt_lock_memory=1; /* Lock memory if possible */
  81. check_param.using_global_keycache = 0;
  82. get_options(&argc,(char***) &argv);
  83. myisam_quick_table_bits=decode_bits;
  84. error=0;
  85. while (--argc >= 0)
  86. {
  87. int new_error=myisamchk(&check_param, *(argv++));
  88. if ((check_param.testflag & T_REP_ANY) != T_REP)
  89. check_param.testflag&= ~T_REP;
  90. VOID(fflush(stdout));
  91. VOID(fflush(stderr));
  92. if ((check_param.error_printed | check_param.warning_printed) &&
  93. (check_param.testflag & T_FORCE_CREATE) &&
  94. (!(check_param.testflag & (T_REP | T_REP_BY_SORT | T_SORT_RECORDS |
  95. T_SORT_INDEX))))
  96. {
  97. uint old_testflag=check_param.testflag;
  98. if (!(check_param.testflag & T_REP))
  99. check_param.testflag|= T_REP_BY_SORT;
  100. check_param.testflag&= ~T_EXTEND; /* Don't needed */
  101. error|=myisamchk(&check_param, argv[-1]);
  102. check_param.testflag= old_testflag;
  103. VOID(fflush(stdout));
  104. VOID(fflush(stderr));
  105. }
  106. else
  107. error|=new_error;
  108. if (argc && (!(check_param.testflag & T_SILENT) || check_param.testflag & T_INFO))
  109. {
  110. puts("\n---------\n");
  111. VOID(fflush(stdout));
  112. }
  113. }
  114. if (check_param.total_files > 1)
  115. { /* Only if descript */
  116. char buff[22],buff2[22];
  117. if (!(check_param.testflag & T_SILENT) || check_param.testflag & T_INFO)
  118. puts("\n---------\n");
  119. printf("\nTotal of all %d MyISAM-files:\nData records: %9s Deleted blocks: %9s\n",check_param.total_files,llstr(check_param.total_records,buff),
  120. llstr(check_param.total_deleted,buff2));
  121. }
  122. free_defaults(default_argv);
  123. free_tmpdir(&myisamchk_tmpdir);
  124. ft_free_stopwords();
  125. my_end(check_param.testflag & T_INFO ? MY_CHECK_ERROR | MY_GIVE_INFO : MY_CHECK_ERROR);
  126. exit(error);
  127. #ifndef _lint
  128. return 0; /* No compiler warning */
  129. #endif
  130. } /* main */
  131. enum options_mc {
  132. OPT_CHARSETS_DIR=256, OPT_SET_COLLATION,OPT_START_CHECK_POS,
  133. OPT_CORRECT_CHECKSUM, OPT_KEY_BUFFER_SIZE,
  134. OPT_KEY_CACHE_BLOCK_SIZE, OPT_MYISAM_BLOCK_SIZE,
  135. OPT_READ_BUFFER_SIZE, OPT_WRITE_BUFFER_SIZE, OPT_SORT_BUFFER_SIZE,
  136. OPT_SORT_KEY_BLOCKS, OPT_DECODE_BITS, OPT_FT_MIN_WORD_LEN,
  137. OPT_FT_MAX_WORD_LEN, OPT_FT_STOPWORD_FILE,
  138. OPT_MAX_RECORD_LENGTH, OPT_AUTO_CLOSE, OPT_STATS_METHOD
  139. };
  140. static struct my_option my_long_options[] =
  141. {
  142. {"analyze", 'a',
  143. "Analyze distribution of keys. Will make some joins in MySQL faster. You can check the calculated distribution.",
  144. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  145. #ifdef __NETWARE__
  146. {"autoclose", OPT_AUTO_CLOSE, "Auto close the screen on exit for Netware.",
  147. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  148. #endif
  149. {"block-search", 'b',
  150. "No help available.",
  151. 0, 0, 0, GET_ULONG, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  152. {"backup", 'B',
  153. "Make a backup of the .MYD file as 'filename-time.BAK'.",
  154. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  155. {"character-sets-dir", OPT_CHARSETS_DIR,
  156. "Directory where character sets are.",
  157. (gptr*) &charsets_dir, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  158. {"check", 'c',
  159. "Check table for errors.",
  160. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  161. {"check-only-changed", 'C',
  162. "Check only tables that have changed since last check. It also applies to other requested actions (e.g. --analyze will be ignored if the table is already analyzed).",
  163. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  164. {"correct-checksum", OPT_CORRECT_CHECKSUM,
  165. "Correct checksum information for table.",
  166. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  167. #ifndef DBUG_OFF
  168. {"debug", '#',
  169. "Output debug log. Often this is 'd:t:o,filename'.",
  170. 0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
  171. #endif
  172. {"description", 'd',
  173. "Prints some information about table.",
  174. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  175. {"data-file-length", 'D',
  176. "Max length of data file (when recreating data-file when it's full).",
  177. (gptr*) &check_param.max_data_file_length,
  178. (gptr*) &check_param.max_data_file_length,
  179. 0, GET_LL, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  180. {"extend-check", 'e',
  181. "If used when checking a table, ensure that the table is 100 percent consistent, which will take a long time. If used when repairing a table, try to recover every possible row from the data file. Normally this will also find a lot of garbage rows; Don't use this option with repair if you are not totally desperate.",
  182. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  183. {"fast", 'F',
  184. "Check only tables that haven't been closed properly. It also applies to other requested actions (e.g. --analyze will be ignored if the table is already analyzed).",
  185. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  186. {"force", 'f',
  187. "Restart with -r if there are any errors in the table. States will be updated as with --update-state.",
  188. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  189. {"HELP", 'H',
  190. "Display this help and exit.",
  191. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  192. {"help", '?',
  193. "Display this help and exit.",
  194. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  195. {"information", 'i',
  196. "Print statistics information about table that is checked.",
  197. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  198. {"keys-used", 'k',
  199. "Tell MyISAM to update only some specific keys. # is a bit mask of which keys to use. This can be used to get faster inserts.",
  200. (gptr*) &check_param.keys_in_use,
  201. (gptr*) &check_param.keys_in_use,
  202. 0, GET_ULL, REQUIRED_ARG, -1, 0, 0, 0, 0, 0},
  203. {"max-record-length", OPT_MAX_RECORD_LENGTH,
  204. "Skip rows bigger than this if myisamchk can't allocate memory to hold it",
  205. (gptr*) &check_param.max_record_length,
  206. (gptr*) &check_param.max_record_length,
  207. 0, GET_ULL, REQUIRED_ARG, LONGLONG_MAX, 0, LONGLONG_MAX, 0, 0, 0},
  208. {"medium-check", 'm',
  209. "Faster than extend-check, but only finds 99.99% of all errors. Should be good enough for most cases.",
  210. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  211. {"quick", 'q', "Faster repair by not modifying the data file.",
  212. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  213. {"read-only", 'T',
  214. "Don't mark table as checked.",
  215. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  216. {"recover", 'r',
  217. "Can fix almost anything except unique keys that aren't unique.",
  218. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  219. {"parallel-recover", 'p',
  220. "Same as '-r' but creates all the keys in parallel.",
  221. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  222. {"safe-recover", 'o',
  223. "Uses old recovery method; Slower than '-r' but can handle a couple of cases where '-r' reports that it can't fix the data file.",
  224. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  225. {"sort-recover", 'n',
  226. "Force recovering with sorting even if the temporary file was very big.",
  227. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  228. #ifdef DEBUG
  229. {"start-check-pos", OPT_START_CHECK_POS,
  230. "No help available.",
  231. 0, 0, 0, GET_ULL, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  232. #endif
  233. {"set-auto-increment", 'A',
  234. "Force auto_increment to start at this or higher value. If no value is given, then sets the next auto_increment value to the highest used value for the auto key + 1.",
  235. (gptr*) &check_param.auto_increment_value,
  236. (gptr*) &check_param.auto_increment_value,
  237. 0, GET_ULL, OPT_ARG, 0, 0, 0, 0, 0, 0},
  238. {"set-collation", OPT_SET_COLLATION,
  239. "Change the collation used by the index",
  240. (gptr*) &set_collation_name, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  241. {"set-variable", 'O',
  242. "Change the value of a variable. Please note that this option is deprecated; you can set variables directly with --variable-name=value.",
  243. 0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  244. {"silent", 's',
  245. "Only print errors. One can use two -s to make myisamchk very silent.",
  246. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  247. {"sort-index", 'S',
  248. "Sort index blocks. This speeds up 'read-next' in applications.",
  249. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  250. {"sort-records", 'R',
  251. "Sort records according to an index. This makes your data much more localized and may speed up things. (It may be VERY slow to do a sort the first time!)",
  252. (gptr*) &check_param.opt_sort_key,
  253. (gptr*) &check_param.opt_sort_key,
  254. 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  255. {"tmpdir", 't',
  256. "Path for temporary files.",
  257. (gptr*) &opt_tmpdir,
  258. 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  259. {"update-state", 'U',
  260. "Mark tables as crashed if any errors were found.",
  261. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  262. {"unpack", 'u',
  263. "Unpack file packed with myisampack.",
  264. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  265. {"verbose", 'v',
  266. "Print more information. This can be used with --description and --check. Use many -v for more verbosity!",
  267. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  268. {"version", 'V',
  269. "Print version and exit.",
  270. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  271. {"wait", 'w',
  272. "Wait if table is locked.",
  273. 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  274. { "key_buffer_size", OPT_KEY_BUFFER_SIZE, "",
  275. (gptr*) &check_param.use_buffers, (gptr*) &check_param.use_buffers, 0,
  276. GET_ULONG, REQUIRED_ARG, (long) USE_BUFFER_INIT, (long) MALLOC_OVERHEAD,
  277. (long) ~0L, (long) MALLOC_OVERHEAD, (long) IO_SIZE, 0},
  278. { "key_cache_block_size", OPT_KEY_CACHE_BLOCK_SIZE, "",
  279. (gptr*) &opt_key_cache_block_size,
  280. (gptr*) &opt_key_cache_block_size, 0,
  281. GET_LONG, REQUIRED_ARG, MI_KEY_BLOCK_LENGTH, MI_MIN_KEY_BLOCK_LENGTH,
  282. MI_MAX_KEY_BLOCK_LENGTH, 0, MI_MIN_KEY_BLOCK_LENGTH, 0},
  283. { "myisam_block_size", OPT_MYISAM_BLOCK_SIZE, "",
  284. (gptr*) &opt_myisam_block_size, (gptr*) &opt_myisam_block_size, 0,
  285. GET_LONG, REQUIRED_ARG, MI_KEY_BLOCK_LENGTH, MI_MIN_KEY_BLOCK_LENGTH,
  286. MI_MAX_KEY_BLOCK_LENGTH, 0, MI_MIN_KEY_BLOCK_LENGTH, 0},
  287. { "read_buffer_size", OPT_READ_BUFFER_SIZE, "",
  288. (gptr*) &check_param.read_buffer_length,
  289. (gptr*) &check_param.read_buffer_length, 0, GET_ULONG, REQUIRED_ARG,
  290. (long) READ_BUFFER_INIT, (long) MALLOC_OVERHEAD,
  291. (long) ~0L, (long) MALLOC_OVERHEAD, (long) 1L, 0},
  292. { "write_buffer_size", OPT_WRITE_BUFFER_SIZE, "",
  293. (gptr*) &check_param.write_buffer_length,
  294. (gptr*) &check_param.write_buffer_length, 0, GET_ULONG, REQUIRED_ARG,
  295. (long) READ_BUFFER_INIT, (long) MALLOC_OVERHEAD,
  296. (long) ~0L, (long) MALLOC_OVERHEAD, (long) 1L, 0},
  297. { "sort_buffer_size", OPT_SORT_BUFFER_SIZE, "",
  298. (gptr*) &check_param.sort_buffer_length,
  299. (gptr*) &check_param.sort_buffer_length, 0, GET_ULONG, REQUIRED_ARG,
  300. (long) SORT_BUFFER_INIT, (long) (MIN_SORT_BUFFER + MALLOC_OVERHEAD),
  301. (long) ~0L, (long) MALLOC_OVERHEAD, (long) 1L, 0},
  302. { "sort_key_blocks", OPT_SORT_KEY_BLOCKS, "",
  303. (gptr*) &check_param.sort_key_blocks,
  304. (gptr*) &check_param.sort_key_blocks, 0, GET_ULONG, REQUIRED_ARG,
  305. BUFFERS_WHEN_SORTING, 4L, 100L, 0L, 1L, 0},
  306. { "decode_bits", OPT_DECODE_BITS, "", (gptr*) &decode_bits,
  307. (gptr*) &decode_bits, 0, GET_UINT, REQUIRED_ARG, 9L, 4L, 17L, 0L, 1L, 0},
  308. { "ft_min_word_len", OPT_FT_MIN_WORD_LEN, "", (gptr*) &ft_min_word_len,
  309. (gptr*) &ft_min_word_len, 0, GET_ULONG, REQUIRED_ARG, 4, 1, HA_FT_MAXCHARLEN,
  310. 0, 1, 0},
  311. { "ft_max_word_len", OPT_FT_MAX_WORD_LEN, "", (gptr*) &ft_max_word_len,
  312. (gptr*) &ft_max_word_len, 0, GET_ULONG, REQUIRED_ARG, HA_FT_MAXCHARLEN, 10,
  313. HA_FT_MAXCHARLEN, 0, 1, 0},
  314. { "ft_stopword_file", OPT_FT_STOPWORD_FILE,
  315. "Use stopwords from this file instead of built-in list.",
  316. (gptr*) &ft_stopword_file, (gptr*) &ft_stopword_file, 0, GET_STR,
  317. REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  318. {"stats_method", OPT_STATS_METHOD,
  319. "Specifies how index statistics collection code should treat NULLs. "
  320. "Possible values of name are \"nulls_unequal\" (default behavior for 4.1/5.0), "
  321. "\"nulls_equal\" (emulate 4.0 behavior), and \"nulls_ignored\".",
  322. (gptr*) &myisam_stats_method_str, (gptr*) &myisam_stats_method_str, 0,
  323. GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  324. { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
  325. };
  326. #include <help_start.h>
  327. static void print_version(void)
  328. {
  329. printf("%s Ver 2.7 for %s at %s\n", my_progname, SYSTEM_TYPE,
  330. MACHINE_TYPE);
  331. NETWARE_SET_SCREEN_MODE(1);
  332. }
  333. static void usage(void)
  334. {
  335. print_version();
  336. puts("By Monty, for your professional use");
  337. puts("This software comes with NO WARRANTY: see the PUBLIC for details.\n");
  338. puts("Description, check and repair of MyISAM tables.");
  339. puts("Used without options all tables on the command will be checked for errors");
  340. printf("Usage: %s [OPTIONS] tables[.MYI]\n", my_progname_short);
  341. printf("\nGlobal options:\n");
  342. #ifndef DBUG_OFF
  343. printf("\
  344. -#, --debug=... Output debug log. Often this is 'd:t:o,filename'.\n");
  345. #endif
  346. printf("\
  347. -?, --help Display this help and exit.\n\
  348. -O, --set-variable var=option.\n\
  349. Change the value of a variable. Please note that\n\
  350. this option is deprecated; you can set variables\n\
  351. directly with '--variable-name=value'.\n\
  352. -t, --tmpdir=path Path for temporary files. Multiple paths can be\n\
  353. specified, separated by ");
  354. #if defined( __WIN__) || defined(OS2) || defined(__NETWARE__)
  355. printf("semicolon (;)");
  356. #else
  357. printf("colon (:)");
  358. #endif
  359. printf(", they will be used\n\
  360. in a round-robin fashion.\n\
  361. -s, --silent Only print errors. One can use two -s to make\n\
  362. myisamchk very silent.\n\
  363. -v, --verbose Print more information. This can be used with\n\
  364. --description and --check. Use many -v for more verbosity.\n\
  365. -V, --version Print version and exit.\n\
  366. -w, --wait Wait if table is locked.\n\n");
  367. #ifdef DEBUG
  368. puts(" --start-check-pos=# Start reading file at given offset.\n");
  369. #endif
  370. puts("Check options (check is the default action for myisamchk):\n\
  371. -c, --check Check table for errors.\n\
  372. -e, --extend-check Check the table VERY throughly. Only use this in\n\
  373. extreme cases as myisamchk should normally be able to\n\
  374. find out if the table is ok even without this switch.\n\
  375. -F, --fast Check only tables that haven't been closed properly.\n\
  376. -C, --check-only-changed\n\
  377. Check only tables that have changed since last check.\n\
  378. -f, --force Restart with '-r' if there are any errors in the table.\n\
  379. States will be updated as with '--update-state'.\n\
  380. -i, --information Print statistics information about table that is checked.\n\
  381. -m, --medium-check Faster than extend-check, but only finds 99.99% of\n\
  382. all errors. Should be good enough for most cases.\n\
  383. -U --update-state Mark tables as crashed if you find any errors.\n\
  384. -T, --read-only Don't mark table as checked.\n");
  385. puts("Repair options (When using '-r' or '-o'):\n\
  386. -B, --backup Make a backup of the .MYD file as 'filename-time.BAK'.\n\
  387. --correct-checksum Correct checksum information for table.\n\
  388. -D, --data-file-length=# Max length of data file (when recreating data\n\
  389. file when it's full).\n\
  390. -e, --extend-check Try to recover every possible row from the data file\n\
  391. Normally this will also find a lot of garbage rows;\n\
  392. Don't use this option if you are not totally desperate.\n\
  393. -f, --force Overwrite old temporary files.\n\
  394. -k, --keys-used=# Tell MyISAM to update only some specific keys. # is a\n\
  395. bit mask of which keys to use. This can be used to\n\
  396. get faster inserts.\n\
  397. --max-record-length=#\n\
  398. Skip rows bigger than this if myisamchk can't allocate\n\
  399. memory to hold it.\n\
  400. -r, --recover Can fix almost anything except unique keys that aren't\n\
  401. unique.\n\
  402. -n, --sort-recover Forces recovering with sorting even if the temporary\n\
  403. file would be very big.\n\
  404. -p, --parallel-recover\n\
  405. Uses the same technique as '-r' and '-n', but creates\n\
  406. all the keys in parallel, in different threads.\n\
  407. -o, --safe-recover Uses old recovery method; Slower than '-r' but can\n\
  408. handle a couple of cases where '-r' reports that it\n\
  409. can't fix the data file.\n\
  410. --character-sets-dir=...\n\
  411. Directory where character sets are.\n\
  412. --set-collation=name\n\
  413. Change the collation used by the index.\n\
  414. -q, --quick Faster repair by not modifying the data file.\n\
  415. One can give a second '-q' to force myisamchk to\n\
  416. modify the original datafile in case of duplicate keys.\n\
  417. NOTE: Tables where the data file is currupted can't be\n\
  418. fixed with this option.\n\
  419. -u, --unpack Unpack file packed with myisampack.\n\
  420. ");
  421. puts("Other actions:\n\
  422. -a, --analyze Analyze distribution of keys. Will make some joins in\n\
  423. MySQL faster. You can check the calculated distribution\n\
  424. by using '--description --verbose table_name'.\n\
  425. --stats_method=name Specifies how index statistics collection code should\n\
  426. treat NULLs. Possible values of name are \"nulls_unequal\"\n\
  427. (default for 4.1/5.0), \"nulls_equal\" (emulate 4.0), and \n\
  428. \"nulls_ignored\".\n\
  429. -d, --description Prints some information about table.\n\
  430. -A, --set-auto-increment[=value]\n\
  431. Force auto_increment to start at this or higher value\n\
  432. If no value is given, then sets the next auto_increment\n\
  433. value to the highest used value for the auto key + 1.\n\
  434. -S, --sort-index Sort index blocks. This speeds up 'read-next' in\n\
  435. applications.\n\
  436. -R, --sort-records=#\n\
  437. Sort records according to an index. This makes your\n\
  438. data much more localized and may speed up things\n\
  439. (It may be VERY slow to do a sort the first time!).\n\
  440. -b, --block-search=#\n\
  441. Find a record, a block at given offset belongs to.");
  442. print_defaults("my", load_default_groups);
  443. my_print_variables(my_long_options);
  444. }
  445. #include <help_end.h>
  446. const char *myisam_stats_method_names[] = {"nulls_unequal", "nulls_equal",
  447. "nulls_ignored", NullS};
  448. TYPELIB myisam_stats_method_typelib= {
  449. array_elements(myisam_stats_method_names) - 1, "",
  450. myisam_stats_method_names, NULL};
  451. /* Read options */
  452. static my_bool
  453. get_one_option(int optid,
  454. const struct my_option *opt __attribute__((unused)),
  455. char *argument)
  456. {
  457. switch (optid) {
  458. #ifdef __NETWARE__
  459. case OPT_AUTO_CLOSE:
  460. setscreenmode(SCR_AUTOCLOSE_ON_EXIT);
  461. break;
  462. #endif
  463. case 'a':
  464. if (argument == disabled_my_option)
  465. check_param.testflag&= ~T_STATISTICS;
  466. else
  467. check_param.testflag|= T_STATISTICS;
  468. break;
  469. case 'A':
  470. if (argument)
  471. check_param.auto_increment_value= strtoull(argument, NULL, 0);
  472. else
  473. check_param.auto_increment_value= 0; /* Set to max used value */
  474. check_param.testflag|= T_AUTO_INC;
  475. break;
  476. case 'b':
  477. check_param.search_after_block= strtoul(argument, NULL, 10);
  478. break;
  479. case 'B':
  480. if (argument == disabled_my_option)
  481. check_param.testflag&= ~T_BACKUP_DATA;
  482. else
  483. check_param.testflag|= T_BACKUP_DATA;
  484. break;
  485. case 'c':
  486. if (argument == disabled_my_option)
  487. check_param.testflag&= ~T_CHECK;
  488. else
  489. check_param.testflag|= T_CHECK;
  490. break;
  491. case 'C':
  492. if (argument == disabled_my_option)
  493. check_param.testflag&= ~(T_CHECK | T_CHECK_ONLY_CHANGED);
  494. else
  495. check_param.testflag|= T_CHECK | T_CHECK_ONLY_CHANGED;
  496. break;
  497. case 'D':
  498. check_param.max_data_file_length=strtoll(argument, NULL, 10);
  499. break;
  500. case 's': /* silent */
  501. if (argument == disabled_my_option)
  502. check_param.testflag&= ~(T_SILENT | T_VERY_SILENT);
  503. else
  504. {
  505. if (check_param.testflag & T_SILENT)
  506. check_param.testflag|= T_VERY_SILENT;
  507. check_param.testflag|= T_SILENT;
  508. check_param.testflag&= ~T_WRITE_LOOP;
  509. }
  510. break;
  511. case 'w':
  512. if (argument == disabled_my_option)
  513. check_param.testflag&= ~T_WAIT_FOREVER;
  514. else
  515. check_param.testflag|= T_WAIT_FOREVER;
  516. break;
  517. case 'd': /* description if isam-file */
  518. if (argument == disabled_my_option)
  519. check_param.testflag&= ~T_DESCRIPT;
  520. else
  521. check_param.testflag|= T_DESCRIPT;
  522. break;
  523. case 'e': /* extend check */
  524. if (argument == disabled_my_option)
  525. check_param.testflag&= ~T_EXTEND;
  526. else
  527. check_param.testflag|= T_EXTEND;
  528. break;
  529. case 'i':
  530. if (argument == disabled_my_option)
  531. check_param.testflag&= ~T_INFO;
  532. else
  533. check_param.testflag|= T_INFO;
  534. break;
  535. case 'f':
  536. if (argument == disabled_my_option)
  537. {
  538. check_param.tmpfile_createflag= O_RDWR | O_TRUNC | O_EXCL;
  539. check_param.testflag&= ~(T_FORCE_CREATE | T_UPDATE_STATE);
  540. }
  541. else
  542. {
  543. check_param.tmpfile_createflag= O_RDWR | O_TRUNC;
  544. check_param.testflag|= T_FORCE_CREATE | T_UPDATE_STATE;
  545. }
  546. break;
  547. case 'F':
  548. if (argument == disabled_my_option)
  549. check_param.testflag&= ~T_FAST;
  550. else
  551. check_param.testflag|= T_FAST;
  552. break;
  553. case 'k':
  554. check_param.keys_in_use= (ulonglong) strtoll(argument, NULL, 10);
  555. break;
  556. case 'm':
  557. if (argument == disabled_my_option)
  558. check_param.testflag&= ~T_MEDIUM;
  559. else
  560. check_param.testflag|= T_MEDIUM; /* Medium check */
  561. break;
  562. case 'r': /* Repair table */
  563. check_param.testflag&= ~T_REP_ANY;
  564. if (argument != disabled_my_option)
  565. check_param.testflag|= T_REP_BY_SORT;
  566. break;
  567. case 'p':
  568. check_param.testflag&= ~T_REP_ANY;
  569. if (argument != disabled_my_option)
  570. check_param.testflag|= T_REP_PARALLEL;
  571. break;
  572. case 'o':
  573. check_param.testflag&= ~T_REP_ANY;
  574. check_param.force_sort= 0;
  575. if (argument != disabled_my_option)
  576. {
  577. check_param.testflag|= T_REP;
  578. my_disable_async_io= 1; /* More safety */
  579. }
  580. break;
  581. case 'n':
  582. check_param.testflag&= ~T_REP_ANY;
  583. if (argument == disabled_my_option)
  584. check_param.force_sort= 0;
  585. else
  586. {
  587. check_param.testflag|= T_REP_BY_SORT;
  588. check_param.force_sort= 1;
  589. }
  590. break;
  591. case 'q':
  592. if (argument == disabled_my_option)
  593. check_param.testflag&= ~(T_QUICK | T_FORCE_UNIQUENESS);
  594. else
  595. check_param.testflag|=
  596. (check_param.testflag & T_QUICK) ? T_FORCE_UNIQUENESS : T_QUICK;
  597. break;
  598. case 'u':
  599. if (argument == disabled_my_option)
  600. check_param.testflag&= ~(T_UNPACK | T_REP_BY_SORT);
  601. else
  602. check_param.testflag|= T_UNPACK | T_REP_BY_SORT;
  603. break;
  604. case 'v': /* Verbose */
  605. if (argument == disabled_my_option)
  606. {
  607. check_param.testflag&= ~T_VERBOSE;
  608. check_param.verbose=0;
  609. }
  610. else
  611. {
  612. check_param.testflag|= T_VERBOSE;
  613. check_param.verbose++;
  614. }
  615. break;
  616. case 'R': /* Sort records */
  617. if (argument == disabled_my_option)
  618. check_param.testflag&= ~T_SORT_RECORDS;
  619. else
  620. {
  621. check_param.testflag|= T_SORT_RECORDS;
  622. check_param.opt_sort_key= (uint) atoi(argument) - 1;
  623. if (check_param.opt_sort_key >= MI_MAX_KEY)
  624. {
  625. fprintf(stderr,
  626. "The value of the sort key is bigger than max key: %d.\n",
  627. MI_MAX_KEY);
  628. exit(1);
  629. }
  630. }
  631. break;
  632. case 'S': /* Sort index */
  633. if (argument == disabled_my_option)
  634. check_param.testflag&= ~T_SORT_INDEX;
  635. else
  636. check_param.testflag|= T_SORT_INDEX;
  637. break;
  638. case 'T':
  639. if (argument == disabled_my_option)
  640. check_param.testflag&= ~T_READONLY;
  641. else
  642. check_param.testflag|= T_READONLY;
  643. break;
  644. case 'U':
  645. if (argument == disabled_my_option)
  646. check_param.testflag&= ~T_UPDATE_STATE;
  647. else
  648. check_param.testflag|= T_UPDATE_STATE;
  649. break;
  650. case '#':
  651. if (argument == disabled_my_option)
  652. {
  653. DBUG_POP();
  654. }
  655. else
  656. {
  657. DBUG_PUSH(argument ? argument : "d:t:o,/tmp/myisamchk.trace");
  658. }
  659. break;
  660. case 'V':
  661. print_version();
  662. exit(0);
  663. case OPT_CORRECT_CHECKSUM:
  664. if (argument == disabled_my_option)
  665. check_param.testflag&= ~T_CALC_CHECKSUM;
  666. else
  667. check_param.testflag|= T_CALC_CHECKSUM;
  668. break;
  669. case OPT_STATS_METHOD:
  670. {
  671. int method;
  672. enum_mi_stats_method method_conv;
  673. myisam_stats_method_str= argument;
  674. if ((method=find_type(argument, &myisam_stats_method_typelib, 2)) <= 0)
  675. {
  676. fprintf(stderr, "Invalid value of stats_method: %s.\n", argument);
  677. exit(1);
  678. }
  679. switch (method-1) {
  680. case 0:
  681. method_conv= MI_STATS_METHOD_NULLS_EQUAL;
  682. break;
  683. case 1:
  684. method_conv= MI_STATS_METHOD_NULLS_NOT_EQUAL;
  685. break;
  686. case 2:
  687. method_conv= MI_STATS_METHOD_IGNORE_NULLS;
  688. break;
  689. default: assert(0); /* Impossible */
  690. }
  691. check_param.stats_method= method_conv;
  692. break;
  693. }
  694. #ifdef DEBUG /* Only useful if debugging */
  695. case OPT_START_CHECK_POS:
  696. check_param.start_check_pos= strtoull(argument, NULL, 0);
  697. break;
  698. #endif
  699. case 'H':
  700. my_print_help(my_long_options);
  701. exit(0);
  702. case '?':
  703. usage();
  704. exit(0);
  705. }
  706. return 0;
  707. }
  708. static void get_options(register int *argc,register char ***argv)
  709. {
  710. int ho_error;
  711. load_defaults("my", load_default_groups, argc, argv);
  712. default_argv= *argv;
  713. if (isatty(fileno(stdout)))
  714. check_param.testflag|=T_WRITE_LOOP;
  715. if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
  716. exit(ho_error);
  717. /* If using repair, then update checksum if one uses --update-state */
  718. if ((check_param.testflag & T_UPDATE_STATE) &&
  719. (check_param.testflag & T_REP_ANY))
  720. check_param.testflag|= T_CALC_CHECKSUM;
  721. if (*argc == 0)
  722. {
  723. usage();
  724. exit(-1);
  725. }
  726. if ((check_param.testflag & T_UNPACK) &&
  727. (check_param.testflag & (T_QUICK | T_SORT_RECORDS)))
  728. {
  729. VOID(fprintf(stderr,
  730. "%s: --unpack can't be used with --quick or --sort-records\n",
  731. my_progname_short));
  732. exit(1);
  733. }
  734. if ((check_param.testflag & T_READONLY) &&
  735. (check_param.testflag &
  736. (T_REP_ANY | T_STATISTICS | T_AUTO_INC |
  737. T_SORT_RECORDS | T_SORT_INDEX | T_FORCE_CREATE)))
  738. {
  739. VOID(fprintf(stderr,
  740. "%s: Can't use --readonly when repairing or sorting\n",
  741. my_progname_short));
  742. exit(1);
  743. }
  744. if (init_tmpdir(&myisamchk_tmpdir, opt_tmpdir))
  745. exit(1);
  746. check_param.tmpdir=&myisamchk_tmpdir;
  747. check_param.key_cache_block_size= opt_key_cache_block_size;
  748. if (set_collation_name)
  749. if (!(set_collation= get_charset_by_name(set_collation_name,
  750. MYF(MY_WME))))
  751. exit(1);
  752. myisam_block_size=(uint) 1 << my_bit_log2(opt_myisam_block_size);
  753. return;
  754. } /* get options */
  755. /* Check table */
  756. static int myisamchk(MI_CHECK *param, my_string filename)
  757. {
  758. int error,lock_type,recreate;
  759. int rep_quick= param->testflag & (T_QUICK | T_FORCE_UNIQUENESS);
  760. uint raid_chunks;
  761. MI_INFO *info;
  762. File datafile;
  763. char llbuff[22],llbuff2[22];
  764. my_bool state_updated=0;
  765. MYISAM_SHARE *share;
  766. DBUG_ENTER("myisamchk");
  767. param->out_flag=error=param->warning_printed=param->error_printed=
  768. recreate=0;
  769. datafile=0;
  770. param->isam_file_name=filename; /* For error messages */
  771. if (!(info=mi_open(filename,
  772. (param->testflag & (T_DESCRIPT | T_READONLY)) ?
  773. O_RDONLY : O_RDWR,
  774. HA_OPEN_FOR_REPAIR |
  775. ((param->testflag & T_WAIT_FOREVER) ?
  776. HA_OPEN_WAIT_IF_LOCKED :
  777. (param->testflag & T_DESCRIPT) ?
  778. HA_OPEN_IGNORE_IF_LOCKED : HA_OPEN_ABORT_IF_LOCKED))))
  779. {
  780. /* Avoid twice printing of isam file name */
  781. param->error_printed=1;
  782. switch (my_errno) {
  783. case HA_ERR_CRASHED:
  784. mi_check_print_error(param,"'%s' doesn't have a correct index definition. You need to recreate it before you can do a repair",filename);
  785. break;
  786. case HA_ERR_NOT_A_TABLE:
  787. mi_check_print_error(param,"'%s' is not a MyISAM-table",filename);
  788. break;
  789. case HA_ERR_CRASHED_ON_USAGE:
  790. mi_check_print_error(param,"'%s' is marked as crashed",filename);
  791. break;
  792. case HA_ERR_CRASHED_ON_REPAIR:
  793. mi_check_print_error(param,"'%s' is marked as crashed after last repair",filename);
  794. break;
  795. case HA_ERR_OLD_FILE:
  796. mi_check_print_error(param,"'%s' is a old type of MyISAM-table", filename);
  797. break;
  798. case HA_ERR_END_OF_FILE:
  799. mi_check_print_error(param,"Couldn't read complete header from '%s'", filename);
  800. break;
  801. case EAGAIN:
  802. mi_check_print_error(param,"'%s' is locked. Use -w to wait until unlocked",filename);
  803. break;
  804. case ENOENT:
  805. mi_check_print_error(param,"File '%s' doesn't exist",filename);
  806. break;
  807. case EACCES:
  808. mi_check_print_error(param,"You don't have permission to use '%s'",filename);
  809. break;
  810. default:
  811. mi_check_print_error(param,"%d when opening MyISAM-table '%s'",
  812. my_errno,filename);
  813. break;
  814. }
  815. DBUG_RETURN(1);
  816. }
  817. share=info->s;
  818. share->options&= ~HA_OPTION_READ_ONLY_DATA; /* We are modifing it */
  819. share->tot_locks-= share->r_locks;
  820. share->r_locks=0;
  821. raid_chunks=share->base.raid_chunks;
  822. /*
  823. Skip the checking of the file if:
  824. We are using --fast and the table is closed properly
  825. We are using --check-only-changed-tables and the table hasn't changed
  826. */
  827. if (param->testflag & (T_FAST | T_CHECK_ONLY_CHANGED))
  828. {
  829. my_bool need_to_check= mi_is_crashed(info) || share->state.open_count != 0;
  830. if ((param->testflag & (T_REP_ANY | T_SORT_RECORDS)) &&
  831. ((share->state.changed & (STATE_CHANGED | STATE_CRASHED |
  832. STATE_CRASHED_ON_REPAIR) ||
  833. !(param->testflag & T_CHECK_ONLY_CHANGED))))
  834. need_to_check=1;
  835. if (info->s->base.keys && info->state->records)
  836. {
  837. if ((param->testflag & T_STATISTICS) &&
  838. (share->state.changed & STATE_NOT_ANALYZED))
  839. need_to_check=1;
  840. if ((param->testflag & T_SORT_INDEX) &&
  841. (share->state.changed & STATE_NOT_SORTED_PAGES))
  842. need_to_check=1;
  843. if ((param->testflag & T_REP_BY_SORT) &&
  844. (share->state.changed & STATE_NOT_OPTIMIZED_KEYS))
  845. need_to_check=1;
  846. }
  847. if ((param->testflag & T_CHECK_ONLY_CHANGED) &&
  848. (share->state.changed & (STATE_CHANGED | STATE_CRASHED |
  849. STATE_CRASHED_ON_REPAIR)))
  850. need_to_check=1;
  851. if (!need_to_check)
  852. {
  853. if (!(param->testflag & T_SILENT) || param->testflag & T_INFO)
  854. printf("MyISAM file: %s is already checked\n",filename);
  855. if (mi_close(info))
  856. {
  857. mi_check_print_error(param,"%d when closing MyISAM-table '%s'",
  858. my_errno,filename);
  859. DBUG_RETURN(1);
  860. }
  861. DBUG_RETURN(0);
  862. }
  863. }
  864. if ((param->testflag & (T_REP_ANY | T_STATISTICS |
  865. T_SORT_RECORDS | T_SORT_INDEX)) &&
  866. (((param->testflag & T_UNPACK) &&
  867. share->data_file_type == COMPRESSED_RECORD) ||
  868. mi_uint2korr(share->state.header.state_info_length) !=
  869. MI_STATE_INFO_SIZE ||
  870. mi_uint2korr(share->state.header.base_info_length) !=
  871. MI_BASE_INFO_SIZE ||
  872. mi_is_any_intersect_keys_active(param->keys_in_use, share->base.keys,
  873. ~share->state.key_map) ||
  874. test_if_almost_full(info) ||
  875. info->s->state.header.file_version[3] != myisam_file_magic[3] ||
  876. (set_collation &&
  877. set_collation->number != share->state.header.language) ||
  878. myisam_block_size != MI_KEY_BLOCK_LENGTH))
  879. {
  880. if (set_collation)
  881. param->language= set_collation->number;
  882. if (recreate_table(param, &info,filename))
  883. {
  884. VOID(fprintf(stderr,
  885. "MyISAM-table '%s' is not fixed because of errors\n",
  886. filename));
  887. return(-1);
  888. }
  889. recreate=1;
  890. if (!(param->testflag & T_REP_ANY))
  891. {
  892. param->testflag|=T_REP_BY_SORT; /* if only STATISTICS */
  893. if (!(param->testflag & T_SILENT))
  894. printf("- '%s' has old table-format. Recreating index\n",filename);
  895. rep_quick|=T_QUICK;
  896. }
  897. share=info->s;
  898. share->tot_locks-= share->r_locks;
  899. share->r_locks=0;
  900. }
  901. if (param->testflag & T_DESCRIPT)
  902. {
  903. param->total_files++;
  904. param->total_records+=info->state->records;
  905. param->total_deleted+=info->state->del;
  906. descript(param, info, filename);
  907. }
  908. else
  909. {
  910. if (!stopwords_inited++)
  911. ft_init_stopwords();
  912. if (!(param->testflag & T_READONLY))
  913. lock_type = F_WRLCK; /* table is changed */
  914. else
  915. lock_type= F_RDLCK;
  916. if (info->lock_type == F_RDLCK)
  917. info->lock_type=F_UNLCK; /* Read only table */
  918. if (_mi_readinfo(info,lock_type,0))
  919. {
  920. mi_check_print_error(param,"Can't lock indexfile of '%s', error: %d",
  921. filename,my_errno);
  922. param->error_printed=0;
  923. goto end2;
  924. }
  925. /*
  926. _mi_readinfo() has locked the table.
  927. We mark the table as locked (without doing file locks) to be able to
  928. use functions that only works on locked tables (like row caching).
  929. */
  930. mi_lock_database(info, F_EXTRA_LCK);
  931. datafile=info->dfile;
  932. if (param->testflag & (T_REP_ANY | T_SORT_RECORDS | T_SORT_INDEX))
  933. {
  934. if (param->testflag & T_REP_ANY)
  935. {
  936. ulonglong tmp=share->state.key_map;
  937. mi_copy_keys_active(share->state.key_map, share->base.keys,
  938. param->keys_in_use);
  939. if (tmp != share->state.key_map)
  940. info->update|=HA_STATE_CHANGED;
  941. }
  942. if (rep_quick && chk_del(param, info, param->testflag & ~T_VERBOSE))
  943. {
  944. if (param->testflag & T_FORCE_CREATE)
  945. {
  946. rep_quick=0;
  947. mi_check_print_info(param,"Creating new data file\n");
  948. }
  949. else
  950. {
  951. error=1;
  952. mi_check_print_error(param,
  953. "Quick-recover aborted; Run recovery without switch 'q'");
  954. }
  955. }
  956. if (!error)
  957. {
  958. if ((param->testflag & (T_REP_BY_SORT | T_REP_PARALLEL)) &&
  959. (mi_is_any_key_active(share->state.key_map) ||
  960. (rep_quick && !param->keys_in_use && !recreate)) &&
  961. mi_test_if_sort_rep(info, info->state->records,
  962. info->s->state.key_map,
  963. param->force_sort))
  964. {
  965. if (param->testflag & T_REP_BY_SORT)
  966. error=mi_repair_by_sort(param,info,filename,rep_quick);
  967. else
  968. error=mi_repair_parallel(param,info,filename,rep_quick);
  969. state_updated=1;
  970. }
  971. else if (param->testflag & T_REP_ANY)
  972. error=mi_repair(param, info,filename,rep_quick);
  973. }
  974. if (!error && param->testflag & T_SORT_RECORDS)
  975. {
  976. /*
  977. The data file is nowadays reopened in the repair code so we should
  978. soon remove the following reopen-code
  979. */
  980. #ifndef TO_BE_REMOVED
  981. if (param->out_flag & O_NEW_DATA)
  982. { /* Change temp file to org file */
  983. VOID(my_close(info->dfile,MYF(MY_WME))); /* Close new file */
  984. error|=change_to_newfile(filename,MI_NAME_DEXT,DATA_TMP_EXT,
  985. raid_chunks,
  986. MYF(0));
  987. if (mi_open_datafile(info,info->s, -1))
  988. error=1;
  989. param->out_flag&= ~O_NEW_DATA; /* We are using new datafile */
  990. param->read_cache.file=info->dfile;
  991. }
  992. #endif
  993. if (! error)
  994. {
  995. uint key;
  996. /*
  997. We can't update the index in mi_sort_records if we have a
  998. prefix compressed or fulltext index
  999. */
  1000. my_bool update_index=1;
  1001. for (key=0 ; key < share->base.keys; key++)
  1002. if (share->keyinfo[key].flag & (HA_BINARY_PACK_KEY|HA_FULLTEXT))
  1003. update_index=0;
  1004. error=mi_sort_records(param,info,filename,param->opt_sort_key,
  1005. /* what is the following parameter for ? */
  1006. (my_bool) !(param->testflag & T_REP),
  1007. update_index);
  1008. datafile=info->dfile; /* This is now locked */
  1009. if (!error && !update_index)
  1010. {
  1011. if (param->verbose)
  1012. puts("Table had a compressed index; We must now recreate the index");
  1013. error=mi_repair_by_sort(param,info,filename,1);
  1014. }
  1015. }
  1016. }
  1017. if (!error && param->testflag & T_SORT_INDEX)
  1018. error=mi_sort_index(param,info,filename);
  1019. if (!error)
  1020. share->state.changed&= ~(STATE_CHANGED | STATE_CRASHED |
  1021. STATE_CRASHED_ON_REPAIR);
  1022. else
  1023. mi_mark_crashed(info);
  1024. }
  1025. else if ((param->testflag & T_CHECK) || !(param->testflag & T_AUTO_INC))
  1026. {
  1027. if (!(param->testflag & T_SILENT) || param->testflag & T_INFO)
  1028. printf("Checking MyISAM file: %s\n",filename);
  1029. if (!(param->testflag & T_SILENT))
  1030. printf("Data records: %7s Deleted blocks: %7s\n",
  1031. llstr(info->state->records,llbuff),
  1032. llstr(info->state->del,llbuff2));
  1033. error =chk_status(param,info);
  1034. mi_intersect_keys_active(share->state.key_map, param->keys_in_use);
  1035. error =chk_size(param,info);
  1036. if (!error || !(param->testflag & (T_FAST | T_FORCE_CREATE)))
  1037. error|=chk_del(param, info,param->testflag);
  1038. if ((!error || (!(param->testflag & (T_FAST | T_FORCE_CREATE)) &&
  1039. !param->start_check_pos)))
  1040. {
  1041. error|=chk_key(param, info);
  1042. if (!error && (param->testflag & (T_STATISTICS | T_AUTO_INC)))
  1043. error=update_state_info(param, info,
  1044. ((param->testflag & T_STATISTICS) ?
  1045. UPDATE_STAT : 0) |
  1046. ((param->testflag & T_AUTO_INC) ?
  1047. UPDATE_AUTO_INC : 0));
  1048. }
  1049. if ((!rep_quick && !error) ||
  1050. !(param->testflag & (T_FAST | T_FORCE_CREATE)))
  1051. {
  1052. if (param->testflag & (T_EXTEND | T_MEDIUM))
  1053. VOID(init_key_cache(dflt_key_cache,opt_key_cache_block_size,
  1054. param->use_buffers, 0, 0));
  1055. VOID(init_io_cache(&param->read_cache,datafile,
  1056. (uint) param->read_buffer_length,
  1057. READ_CACHE,
  1058. (param->start_check_pos ?
  1059. param->start_check_pos :
  1060. share->pack.header_length),
  1061. 1,
  1062. MYF(MY_WME)));
  1063. lock_memory(param);
  1064. if ((info->s->options & (HA_OPTION_PACK_RECORD |
  1065. HA_OPTION_COMPRESS_RECORD)) ||
  1066. (param->testflag & (T_EXTEND | T_MEDIUM)))
  1067. error|=chk_data_link(param, info, param->testflag & T_EXTEND);
  1068. error|=flush_blocks(param, share->key_cache, share->kfile);
  1069. VOID(end_io_cache(&param->read_cache));
  1070. }
  1071. if (!error)
  1072. {
  1073. if ((share->state.changed & STATE_CHANGED) &&
  1074. (param->testflag & T_UPDATE_STATE))
  1075. info->update|=HA_STATE_CHANGED | HA_STATE_ROW_CHANGED;
  1076. share->state.changed&= ~(STATE_CHANGED | STATE_CRASHED |
  1077. STATE_CRASHED_ON_REPAIR);
  1078. }
  1079. else if (!mi_is_crashed(info) &&
  1080. (param->testflag & T_UPDATE_STATE))
  1081. { /* Mark crashed */
  1082. mi_mark_crashed(info);
  1083. info->update|=HA_STATE_CHANGED | HA_STATE_ROW_CHANGED;
  1084. }
  1085. }
  1086. }
  1087. if ((param->testflag & T_AUTO_INC) ||
  1088. ((param->testflag & T_REP_ANY) && info->s->base.auto_key))
  1089. update_auto_increment_key(param, info,
  1090. (my_bool) !test(param->testflag & T_AUTO_INC));
  1091. if (!(param->testflag & T_DESCRIPT))
  1092. {
  1093. if (info->update & HA_STATE_CHANGED && ! (param->testflag & T_READONLY))
  1094. error|=update_state_info(param, info,
  1095. UPDATE_OPEN_COUNT |
  1096. (((param->testflag & T_REP_ANY) ?
  1097. UPDATE_TIME : 0) |
  1098. (state_updated ? UPDATE_STAT : 0) |
  1099. ((param->testflag & T_SORT_RECORDS) ?
  1100. UPDATE_SORT : 0)));
  1101. VOID(lock_file(param, share->kfile,0L,F_UNLCK,"indexfile",filename));
  1102. info->update&= ~HA_STATE_CHANGED;
  1103. }
  1104. mi_lock_database(info, F_UNLCK);
  1105. end2:
  1106. if (mi_close(info))
  1107. {
  1108. mi_check_print_error(param,"%d when closing MyISAM-table '%s'",my_errno,filename);
  1109. DBUG_RETURN(1);
  1110. }
  1111. if (error == 0)
  1112. {
  1113. if (param->out_flag & O_NEW_DATA)
  1114. error|=change_to_newfile(filename,MI_NAME_DEXT,DATA_TMP_EXT,
  1115. raid_chunks,
  1116. ((param->testflag & T_BACKUP_DATA) ?
  1117. MYF(MY_REDEL_MAKE_BACKUP) : MYF(0)));
  1118. if (param->out_flag & O_NEW_INDEX)
  1119. error|=change_to_newfile(filename,MI_NAME_IEXT,INDEX_TMP_EXT,0,
  1120. MYF(0));
  1121. }
  1122. VOID(fflush(stdout)); VOID(fflush(stderr));
  1123. if (param->error_printed)
  1124. {
  1125. if (param->testflag & (T_REP_ANY | T_SORT_RECORDS | T_SORT_INDEX))
  1126. {
  1127. VOID(fprintf(stderr,
  1128. "MyISAM-table '%s' is not fixed because of errors\n",
  1129. filename));
  1130. if (param->testflag & T_REP_ANY)
  1131. VOID(fprintf(stderr,
  1132. "Try fixing it by using the --safe-recover (-o), the --force (-f) option or by not using the --quick (-q) flag\n"));
  1133. }
  1134. else if (!(param->error_printed & 2) &&
  1135. !(param->testflag & T_FORCE_CREATE))
  1136. VOID(fprintf(stderr,
  1137. "MyISAM-table '%s' is corrupted\nFix it using switch \"-r\" or \"-o\"\n",
  1138. filename));
  1139. }
  1140. else if (param->warning_printed &&
  1141. ! (param->testflag & (T_REP_ANY | T_SORT_RECORDS | T_SORT_INDEX |
  1142. T_FORCE_CREATE)))
  1143. VOID(fprintf(stderr, "MyISAM-table '%s' is usable but should be fixed\n",
  1144. filename));
  1145. VOID(fflush(stderr));
  1146. DBUG_RETURN(error);
  1147. } /* myisamchk */
  1148. /* Write info about table */
  1149. static void descript(MI_CHECK *param, register MI_INFO *info, my_string name)
  1150. {
  1151. uint key,keyseg_nr,field,start;
  1152. reg3 MI_KEYDEF *keyinfo;
  1153. reg2 HA_KEYSEG *keyseg;
  1154. reg4 const char *text;
  1155. char buff[160],length[10],*pos,*end;
  1156. enum en_fieldtype type;
  1157. MYISAM_SHARE *share=info->s;
  1158. char llbuff[22],llbuff2[22];
  1159. DBUG_ENTER("describe");
  1160. printf("\nMyISAM file: %s\n",name);
  1161. fputs("Record format: ",stdout);
  1162. if (share->options & HA_OPTION_COMPRESS_RECORD)
  1163. puts("Compressed");
  1164. else if (share->options & HA_OPTION_PACK_RECORD)
  1165. puts("Packed");
  1166. else
  1167. puts("Fixed length");
  1168. printf("Character set: %s (%d)\n",
  1169. get_charset_name(share->state.header.language),
  1170. share->state.header.language);
  1171. if (param->testflag & T_VERBOSE)
  1172. {
  1173. printf("File-version: %d\n",
  1174. (int) share->state.header.file_version[3]);
  1175. if (share->state.create_time)
  1176. {
  1177. get_date(buff,1,share->state.create_time);
  1178. printf("Creation time: %s\n",buff);
  1179. }
  1180. if (share->state.check_time)
  1181. {
  1182. get_date(buff,1,share->state.check_time);
  1183. printf("Recover time: %s\n",buff);
  1184. }
  1185. pos=buff;
  1186. if (share->state.changed & STATE_CRASHED)
  1187. strmov(buff,"crashed");
  1188. else
  1189. {
  1190. if (share->state.open_count)
  1191. pos=strmov(pos,"open,");
  1192. if (share->state.changed & STATE_CHANGED)
  1193. pos=strmov(pos,"changed,");
  1194. else
  1195. pos=strmov(pos,"checked,");
  1196. if (!(share->state.changed & STATE_NOT_ANALYZED))
  1197. pos=strmov(pos,"analyzed,");
  1198. if (!(share->state.changed & STATE_NOT_OPTIMIZED_KEYS))
  1199. pos=strmov(pos,"optimized keys,");
  1200. if (!(share->state.changed & STATE_NOT_SORTED_PAGES))
  1201. pos=strmov(pos,"sorted index pages,");
  1202. pos[-1]=0; /* Remove extra ',' */
  1203. }
  1204. printf("Status: %s\n",buff);
  1205. if (share->base.auto_key)
  1206. {
  1207. printf("Auto increment key: %13d Last value: %13s\n",
  1208. share->base.auto_key,
  1209. llstr(share->state.auto_increment,llbuff));
  1210. }
  1211. if (share->base.raid_type)
  1212. {
  1213. printf("RAID: Type: %u Chunks: %u Chunksize: %lu\n",
  1214. share->base.raid_type,
  1215. share->base.raid_chunks,
  1216. share->base.raid_chunksize);
  1217. }
  1218. if (share->options & (HA_OPTION_CHECKSUM | HA_OPTION_COMPRESS_RECORD))
  1219. printf("Checksum: %23s\n",llstr(info->state->checksum,llbuff));
  1220. ;
  1221. if (share->options & HA_OPTION_DELAY_KEY_WRITE)
  1222. printf("Keys are only flushed at close\n");
  1223. }
  1224. printf("Data records: %13s Deleted blocks: %13s\n",
  1225. llstr(info->state->records,llbuff),llstr(info->state->del,llbuff2));
  1226. if (param->testflag & T_SILENT)
  1227. DBUG_VOID_RETURN; /* This is enough */
  1228. if (param->testflag & T_VERBOSE)
  1229. {
  1230. #ifdef USE_RELOC
  1231. printf("Init-relocation: %13s\n",llstr(share->base.reloc,llbuff));
  1232. #endif
  1233. printf("Datafile parts: %13s Deleted data: %13s\n",
  1234. llstr(share->state.split,llbuff),
  1235. llstr(info->state->empty,llbuff2));
  1236. printf("Datafile pointer (bytes):%9d Keyfile pointer (bytes):%9d\n",
  1237. share->rec_reflength,share->base.key_reflength);
  1238. printf("Datafile length: %13s Keyfile length: %13s\n",
  1239. llstr(info->state->data_file_length,llbuff),
  1240. llstr(info->state->key_file_length,llbuff2));
  1241. if (info->s->base.reloc == 1L && info->s->base.records == 1L)
  1242. puts("This is a one-record table");
  1243. else
  1244. {
  1245. if (share->base.max_data_file_length != HA_OFFSET_ERROR ||
  1246. share->base.max_key_file_length != HA_OFFSET_ERROR)
  1247. printf("Max datafile length: %13s Max keyfile length: %13s\n",
  1248. llstr(share->base.max_data_file_length-1,llbuff),
  1249. llstr(share->base.max_key_file_length-1,llbuff2));
  1250. }
  1251. }
  1252. printf("Recordlength: %13d\n",(int) share->base.pack_reclength);
  1253. if (! mi_is_all_keys_active(share->state.key_map, share->base.keys))
  1254. {
  1255. longlong2str(share->state.key_map,buff,2);
  1256. printf("Using only keys '%s' of %d possibly keys\n",
  1257. buff, share->base.keys);
  1258. }
  1259. puts("\ntable description:");
  1260. printf("Key Start Len Index Type");
  1261. if (param->testflag & T_VERBOSE)
  1262. printf(" Rec/key Root Blocksize");
  1263. VOID(putchar('\n'));
  1264. for (key=keyseg_nr=0, keyinfo= &share->keyinfo[0] ;
  1265. key < share->base.keys;
  1266. key++,keyinfo++)
  1267. {
  1268. keyseg=keyinfo->seg;
  1269. if (keyinfo->flag & HA_NOSAME) text="unique ";
  1270. else if (keyinfo->flag & HA_FULLTEXT) text="fulltext ";
  1271. else text="multip.";
  1272. pos=buff;
  1273. if (keyseg->flag & HA_REVERSE_SORT)
  1274. *pos++ = '-';
  1275. pos=strmov(pos,type_names[keyseg->type]);
  1276. *pos++ = ' ';
  1277. *pos=0;
  1278. if (keyinfo->flag & HA_PACK_KEY)
  1279. pos=strmov(pos,prefix_packed_txt);
  1280. if (keyinfo->flag & HA_BINARY_PACK_KEY)
  1281. pos=strmov(pos,bin_packed_txt);
  1282. if (keyseg->flag & HA_SPACE_PACK)
  1283. pos=strmov(pos,diff_txt);
  1284. if (keyseg->flag & HA_BLOB_PART)
  1285. pos=strmov(pos,blob_txt);
  1286. if (keyseg->flag & HA_NULL_PART)
  1287. pos=strmov(pos,null_txt);
  1288. *pos=0;
  1289. printf("%-4d%-6ld%-3d %-8s%-21s",
  1290. key+1,(long) keyseg->start+1,keyseg->length,text,buff);
  1291. if (share->state.key_root[key] != HA_OFFSET_ERROR)
  1292. llstr(share->state.key_root[key],buff);
  1293. else
  1294. buff[0]=0;
  1295. if (param->testflag & T_VERBOSE)
  1296. printf("%11lu %12s %10d",
  1297. share->state.rec_per_key_part[keyseg_nr++],
  1298. buff,keyinfo->block_length);
  1299. VOID(putchar('\n'));
  1300. while ((++keyseg)->type != HA_KEYTYPE_END)
  1301. {
  1302. pos=buff;
  1303. if (keyseg->flag & HA_REVERSE_SORT)
  1304. *pos++ = '-';
  1305. pos=strmov(pos,type_names[keyseg->type]);
  1306. *pos++= ' ';
  1307. if (keyseg->flag & HA_SPACE_PACK)
  1308. pos=strmov(pos,diff_txt);
  1309. if (keyseg->flag & HA_BLOB_PART)
  1310. pos=strmov(pos,blob_txt);
  1311. if (keyseg->flag & HA_NULL_PART)
  1312. pos=strmov(pos,null_txt);
  1313. *pos=0;
  1314. printf(" %-6ld%-3d %-21s",
  1315. (long) keyseg->start+1,keyseg->length,buff);
  1316. if (param->testflag & T_VERBOSE)
  1317. printf("%11lu", share->state.rec_per_key_part[keyseg_nr++]);
  1318. VOID(putchar('\n'));
  1319. }
  1320. keyseg++;
  1321. }
  1322. if (share->state.header.uniques)
  1323. {
  1324. MI_UNIQUEDEF *uniqueinfo;
  1325. puts("\nUnique Key Start Len Nullpos Nullbit Type");
  1326. for (key=0,uniqueinfo= &share->uniqueinfo[0] ;
  1327. key < share->state.header.uniques; key++, uniqueinfo++)
  1328. {
  1329. my_bool new_row=0;
  1330. char null_bit[8],null_pos[8];
  1331. printf("%-8d%-5d",key+1,uniqueinfo->key+1);
  1332. for (keyseg=uniqueinfo->seg ; keyseg->type != HA_KEYTYPE_END ; keyseg++)
  1333. {
  1334. if (new_row)
  1335. fputs(" ",stdout);
  1336. null_bit[0]=null_pos[0]=0;
  1337. if (keyseg->null_bit)
  1338. {
  1339. sprintf(null_bit,"%d",keyseg->null_bit);
  1340. sprintf(null_pos,"%ld",(long) keyseg->null_pos+1);
  1341. }
  1342. printf("%-7ld%-5d%-9s%-10s%-30s\n",
  1343. (long) keyseg->start+1,keyseg->length,
  1344. null_pos,null_bit,
  1345. type_names[keyseg->type]);
  1346. new_row=1;
  1347. }
  1348. }
  1349. }
  1350. if (param->verbose > 1)
  1351. {
  1352. char null_bit[8],null_pos[8];
  1353. printf("\nField Start Length Nullpos Nullbit Type");
  1354. if (share->options & HA_OPTION_COMPRESS_RECORD)
  1355. printf(" Huff tree Bits");
  1356. VOID(putchar('\n'));
  1357. start=1;
  1358. for (field=0 ; field < share->base.fields ; field++)
  1359. {
  1360. if (share->options & HA_OPTION_COMPRESS_RECORD)
  1361. type=share->rec[field].base_type;
  1362. else
  1363. type=(enum en_fieldtype) share->rec[field].type;
  1364. end=strmov(buff,field_pack[type]);
  1365. if (share->options & HA_OPTION_COMPRESS_RECORD)
  1366. {
  1367. if (share->rec[field].pack_type & PACK_TYPE_SELECTED)
  1368. end=strmov(end,", not_always");
  1369. if (share->rec[field].pack_type & PACK_TYPE_SPACE_FIELDS)
  1370. end=strmov(end,", no empty");
  1371. if (share->rec[field].pack_type & PACK_TYPE_ZERO_FILL)
  1372. {
  1373. sprintf(end,", zerofill(%d)",share->rec[field].space_length_bits);
  1374. end=strend(end);
  1375. }
  1376. }
  1377. if (buff[0] == ',')
  1378. strmov(buff,buff+2);
  1379. int10_to_str((long) share->rec[field].length,length,10);
  1380. null_bit[0]=null_pos[0]=0;
  1381. if (share->rec[field].null_bit)
  1382. {
  1383. sprintf(null_bit,"%d",share->rec[field].null_bit);
  1384. sprintf(null_pos,"%d",share->rec[field].null_pos+1);
  1385. }
  1386. printf("%-6d%-6d%-7s%-8s%-8s%-35s",field+1,start,length,
  1387. null_pos, null_bit, buff);
  1388. if (share->options & HA_OPTION_COMPRESS_RECORD)
  1389. {
  1390. if (share->rec[field].huff_tree)
  1391. printf("%3d %2d",
  1392. (uint) (share->rec[field].huff_tree-share->decode_trees)+1,
  1393. share->rec[field].huff_tree->quick_table_bits);
  1394. }
  1395. VOID(putchar('\n'));
  1396. start+=share->rec[field].length;
  1397. }
  1398. }
  1399. DBUG_VOID_RETURN;
  1400. } /* describe */
  1401. /* Sort records according to one key */
  1402. static int mi_sort_records(MI_CHECK *param,
  1403. register MI_INFO *info, my_string name,
  1404. uint sort_key,
  1405. my_bool write_info,
  1406. my_bool update_index)
  1407. {
  1408. int got_error;
  1409. uint key;
  1410. MI_KEYDEF *keyinfo;
  1411. File new_file;
  1412. uchar *temp_buff;
  1413. ha_rows old_record_count;
  1414. MYISAM_SHARE *share=info->s;
  1415. char llbuff[22],llbuff2[22];
  1416. SORT_INFO sort_info;
  1417. MI_SORT_PARAM sort_param;
  1418. DBUG_ENTER("sort_records");
  1419. bzero((char*)&sort_info,sizeof(sort_info));
  1420. bzero((char*)&sort_param,sizeof(sort_param));
  1421. sort_param.sort_info=&sort_info;
  1422. sort_info.param=param;
  1423. keyinfo= &share->keyinfo[sort_key];
  1424. got_error=1;
  1425. temp_buff=0;
  1426. new_file= -1;
  1427. if (! mi_is_key_active(share->state.key_map, sort_key))
  1428. {
  1429. mi_check_print_warning(param,
  1430. "Can't sort table '%s' on key %d; No such key",
  1431. name,sort_key+1);
  1432. param->error_printed=0;
  1433. DBUG_RETURN(0); /* Nothing to do */
  1434. }
  1435. if (keyinfo->flag & HA_FULLTEXT)
  1436. {
  1437. mi_check_print_warning(param,"Can't sort table '%s' on FULLTEXT key %d",
  1438. name,sort_key+1);
  1439. param->error_printed=0;
  1440. DBUG_RETURN(0); /* Nothing to do */
  1441. }
  1442. if (share->data_file_type == COMPRESSED_RECORD)
  1443. {
  1444. mi_check_print_warning(param,"Can't sort read-only table '%s'", name);
  1445. param->error_printed=0;
  1446. DBUG_RETURN(0); /* Nothing to do */
  1447. }
  1448. if (!(param->testflag & T_SILENT))
  1449. {
  1450. printf("- Sorting records for MyISAM-table '%s'\n",name);
  1451. if (write_info)
  1452. printf("Data records: %9s Deleted: %9s\n",
  1453. llstr(info->state->records,llbuff),
  1454. llstr(info->state->del,llbuff2));
  1455. }
  1456. if (share->state.key_root[sort_key] == HA_OFFSET_ERROR)
  1457. DBUG_RETURN(0); /* Nothing to do */
  1458. init_key_cache(dflt_key_cache, opt_key_cache_block_size, param->use_buffers,
  1459. 0, 0);
  1460. if (init_io_cache(&info->rec_cache,-1,(uint) param->write_buffer_length,
  1461. WRITE_CACHE,share->pack.header_length,1,
  1462. MYF(MY_WME | MY_WAIT_IF_FULL)))
  1463. goto err;
  1464. info->opt_flag|=WRITE_CACHE_USED;
  1465. if (!(temp_buff=(uchar*) my_alloca((uint) keyinfo->block_length)))
  1466. {
  1467. mi_check_print_error(param,"Not enough memory for key block");
  1468. goto err;
  1469. }
  1470. if (!(sort_param.record=(byte*) my_malloc((uint) share->base.pack_reclength,
  1471. MYF(0))))
  1472. {
  1473. mi_check_print_error(param,"Not enough memory for record");
  1474. goto err;
  1475. }
  1476. fn_format(param->temp_filename,name,"", MI_NAME_DEXT,2+4+32);
  1477. new_file=my_raid_create(fn_format(param->temp_filename,
  1478. param->temp_filename,"",
  1479. DATA_TMP_EXT,2+4),
  1480. 0,param->tmpfile_createflag,
  1481. share->base.raid_type,
  1482. share->base.raid_chunks,
  1483. share->base.raid_chunksize,
  1484. MYF(0));
  1485. if (new_file < 0)
  1486. {
  1487. mi_check_print_error(param,"Can't create new tempfile: '%s'",
  1488. param->temp_filename);
  1489. goto err;
  1490. }
  1491. if (share->pack.header_length)
  1492. if (filecopy(param,new_file,info->dfile,0L,share->pack.header_length,
  1493. "datafile-header"))
  1494. goto err;
  1495. info->rec_cache.file=new_file; /* Use this file for cacheing*/
  1496. lock_memory(param);
  1497. for (key=0 ; key < share->base.keys ; key++)
  1498. share->keyinfo[key].flag|= HA_SORT_ALLOWS_SAME;
  1499. if (my_pread(share->kfile,(byte*) temp_buff,
  1500. (uint) keyinfo->block_length,
  1501. share->state.key_root[sort_key],
  1502. MYF(MY_NABP+MY_WME)))
  1503. {
  1504. mi_check_print_error(param,"Can't read indexpage from filepos: %s",
  1505. (ulong) share->state.key_root[sort_key]);
  1506. goto err;
  1507. }
  1508. /* Setup param for sort_write_record */
  1509. sort_info.info=info;
  1510. sort_info.new_data_file_type=share->data_file_type;
  1511. sort_param.fix_datafile=1;
  1512. sort_param.master=1;
  1513. sort_param.filepos=share->pack.header_length;
  1514. old_record_count=info->state->records;
  1515. info->state->records=0;
  1516. if (sort_info.new_data_file_type != COMPRESSED_RECORD)
  1517. info->state->checksum=0;
  1518. if (sort_record_index(&sort_param,info,keyinfo,share->state.key_root[sort_key],
  1519. temp_buff, sort_key,new_file,update_index) ||
  1520. write_data_suffix(&sort_info,1) ||
  1521. flush_io_cache(&info->rec_cache))
  1522. goto err;
  1523. if (info->state->records != old_record_count)
  1524. {
  1525. mi_check_print_error(param,"found %s of %s records",
  1526. llstr(info->state->records,llbuff),
  1527. llstr(old_record_count,llbuff2));
  1528. goto err;
  1529. }
  1530. VOID(my_close(info->dfile,MYF(MY_WME)));
  1531. param->out_flag|=O_NEW_DATA; /* Data in new file */
  1532. info->dfile=new_file; /* Use new datafile */
  1533. info->state->del=0;
  1534. info->state->empty=0;
  1535. share->state.dellink= HA_OFFSET_ERROR;
  1536. info->state->data_file_length=sort_param.filepos;
  1537. share->state.split=info->state->records; /* Only hole records */
  1538. share->state.version=(ulong) time((time_t*) 0);
  1539. info->update= (short) (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED);
  1540. if (param->testflag & T_WRITE_LOOP)
  1541. {
  1542. VOID(fputs(" \r",stdout)); VOID(fflush(stdout));
  1543. }
  1544. got_error=0;
  1545. err:
  1546. if (got_error && new_file >= 0)
  1547. {
  1548. VOID(end_io_cache(&info->rec_cache));
  1549. (void) my_close(new_file,MYF(MY_WME));
  1550. (void) my_raid_delete(param->temp_filename, share->base.raid_chunks,
  1551. MYF(MY_WME));
  1552. }
  1553. if (temp_buff)
  1554. {
  1555. my_afree((gptr) temp_buff);
  1556. }
  1557. my_free(sort_param.record,MYF(MY_ALLOW_ZERO_PTR));
  1558. info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
  1559. VOID(end_io_cache(&info->rec_cache));
  1560. my_free(sort_info.buff,MYF(MY_ALLOW_ZERO_PTR));
  1561. sort_info.buff=0;
  1562. share->state.sortkey=sort_key;
  1563. DBUG_RETURN(flush_blocks(param, share->key_cache, share->kfile) |
  1564. got_error);
  1565. } /* sort_records */
  1566. /* Sort records recursive using one index */
  1567. static int sort_record_index(MI_SORT_PARAM *sort_param,MI_INFO *info,
  1568. MI_KEYDEF *keyinfo,
  1569. my_off_t page, uchar *buff, uint sort_key,
  1570. File new_file,my_bool update_index)
  1571. {
  1572. uint nod_flag,used_length,key_length;
  1573. uchar *temp_buff,*keypos,*endpos;
  1574. my_off_t next_page,rec_pos;
  1575. uchar lastkey[MI_MAX_KEY_BUFF];
  1576. char llbuff[22];
  1577. SORT_INFO *sort_info= sort_param->sort_info;
  1578. MI_CHECK *param=sort_info->param;
  1579. DBUG_ENTER("sort_record_index");
  1580. nod_flag=mi_test_if_nod(buff);
  1581. temp_buff=0;
  1582. if (nod_flag)
  1583. {
  1584. if (!(temp_buff=(uchar*) my_alloca((uint) keyinfo->block_length)))
  1585. {
  1586. mi_check_print_error(param,"Not Enough memory");
  1587. DBUG_RETURN(-1);
  1588. }
  1589. }
  1590. used_length=mi_getint(buff);
  1591. keypos=buff+2+nod_flag;
  1592. endpos=buff+used_length;
  1593. for ( ;; )
  1594. {
  1595. _sanity(__FILE__,__LINE__);
  1596. if (nod_flag)
  1597. {
  1598. next_page=_mi_kpos(nod_flag,keypos);
  1599. if (my_pread(info->s->kfile,(byte*) temp_buff,
  1600. (uint) keyinfo->block_length, next_page,
  1601. MYF(MY_NABP+MY_WME)))
  1602. {
  1603. mi_check_print_error(param,"Can't read keys from filepos: %s",
  1604. llstr(next_page,llbuff));
  1605. goto err;
  1606. }
  1607. if (sort_record_index(sort_param, info,keyinfo,next_page,temp_buff,sort_key,
  1608. new_file, update_index))
  1609. goto err;
  1610. }
  1611. _sanity(__FILE__,__LINE__);
  1612. if (keypos >= endpos ||
  1613. (key_length=(*keyinfo->get_key)(keyinfo,nod_flag,&keypos,lastkey))
  1614. == 0)
  1615. break;
  1616. rec_pos= _mi_dpos(info,0,lastkey+key_length);
  1617. if ((*info->s->read_rnd)(info,sort_param->record,rec_pos,0))
  1618. {
  1619. mi_check_print_error(param,"%d when reading datafile",my_errno);
  1620. goto err;
  1621. }
  1622. if (rec_pos != sort_param->filepos && update_index)
  1623. {
  1624. _mi_dpointer(info,keypos-nod_flag-info->s->rec_reflength,
  1625. sort_param->filepos);
  1626. if (movepoint(info,sort_param->record,rec_pos,sort_param->filepos,
  1627. sort_key))
  1628. {
  1629. mi_check_print_error(param,"%d when updating key-pointers",my_errno);
  1630. goto err;
  1631. }
  1632. }
  1633. if (sort_write_record(sort_param))
  1634. goto err;
  1635. }
  1636. /* Clear end of block to get better compression if the table is backuped */
  1637. bzero((byte*) buff+used_length,keyinfo->block_length-used_length);
  1638. if (my_pwrite(info->s->kfile,(byte*) buff,(uint) keyinfo->block_length,
  1639. page,param->myf_rw))
  1640. {
  1641. mi_check_print_error(param,"%d when updating keyblock",my_errno);
  1642. goto err;
  1643. }
  1644. if (temp_buff)
  1645. my_afree((gptr) temp_buff);
  1646. DBUG_RETURN(0);
  1647. err:
  1648. if (temp_buff)
  1649. my_afree((gptr) temp_buff);
  1650. DBUG_RETURN(1);
  1651. } /* sort_record_index */
  1652. /*
  1653. Check if myisamchk was killed by a signal
  1654. This is overloaded by other programs that want to be able to abort
  1655. sorting
  1656. */
  1657. static int not_killed= 0;
  1658. volatile int *killed_ptr(MI_CHECK *param __attribute__((unused)))
  1659. {
  1660. return &not_killed; /* always NULL */
  1661. }
  1662. /* print warnings and errors */
  1663. /* VARARGS */
  1664. void mi_check_print_info(MI_CHECK *param __attribute__((unused)),
  1665. const char *fmt,...)
  1666. {
  1667. va_list args;
  1668. va_start(args,fmt);
  1669. VOID(vfprintf(stdout, fmt, args));
  1670. VOID(fputc('\n',stdout));
  1671. va_end(args);
  1672. }
  1673. /* VARARGS */
  1674. void mi_check_print_warning(MI_CHECK *param, const char *fmt,...)
  1675. {
  1676. va_list args;
  1677. DBUG_ENTER("mi_check_print_warning");
  1678. fflush(stdout);
  1679. if (!param->warning_printed && !param->error_printed)
  1680. {
  1681. if (param->testflag & T_SILENT)
  1682. fprintf(stderr,"%s: MyISAM file %s\n",my_progname_short,
  1683. param->isam_file_name);
  1684. param->out_flag|= O_DATA_LOST;
  1685. }
  1686. param->warning_printed=1;
  1687. va_start(args,fmt);
  1688. fprintf(stderr,"%s: warning: ",my_progname_short);
  1689. VOID(vfprintf(stderr, fmt, args));
  1690. VOID(fputc('\n',stderr));
  1691. fflush(stderr);
  1692. va_end(args);
  1693. DBUG_VOID_RETURN;
  1694. }
  1695. /* VARARGS */
  1696. void mi_check_print_error(MI_CHECK *param, const char *fmt,...)
  1697. {
  1698. va_list args;
  1699. DBUG_ENTER("mi_check_print_error");
  1700. DBUG_PRINT("enter",("format: %s",fmt));
  1701. fflush(stdout);
  1702. if (!param->warning_printed && !param->error_printed)
  1703. {
  1704. if (param->testflag & T_SILENT)
  1705. fprintf(stderr,"%s: MyISAM file %s\n",my_progname_short,param->isam_file_name);
  1706. param->out_flag|= O_DATA_LOST;
  1707. }
  1708. param->error_printed|=1;
  1709. va_start(args,fmt);
  1710. fprintf(stderr,"%s: error: ",my_progname_short);
  1711. VOID(vfprintf(stderr, fmt, args));
  1712. VOID(fputc('\n',stderr));
  1713. fflush(stderr);
  1714. va_end(args);
  1715. DBUG_VOID_RETURN;
  1716. }