1 /* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 /* Remove all rows from a MyISAM table */
24 /* This clears the status information and truncates files */
25 
26 #include "myisamdef.h"
27 
mi_delete_all_rows(MI_INFO * info)28 int mi_delete_all_rows(MI_INFO *info)
29 {
30   uint i;
31   MYISAM_SHARE *share=info->s;
32   MI_STATE_INFO *state=&share->state;
33   DBUG_ENTER("mi_delete_all_rows");
34 
35   if (share->options & HA_OPTION_READ_ONLY_DATA)
36   {
37     set_my_errno(EACCES);
38     DBUG_RETURN(EACCES);
39   }
40   if (_mi_readinfo(info,F_WRLCK,1))
41     DBUG_RETURN(my_errno());
42   if (_mi_mark_file_changed(info))
43     goto err;
44 
45   info->state->records=info->state->del=state->split=0;
46   state->dellink = HA_OFFSET_ERROR;
47   state->sortkey=  (ushort) ~0;
48   info->state->key_file_length=share->base.keystart;
49   info->state->data_file_length=0;
50   info->state->empty=info->state->key_empty=0;
51   info->state->checksum=0;
52 
53   for (i=share->base.max_key_block_length/MI_MIN_KEY_BLOCK_LENGTH ; i-- ; )
54     state->key_del[i]= HA_OFFSET_ERROR;
55   for (i=0 ; i < share->base.keys ; i++)
56     state->key_root[i]= HA_OFFSET_ERROR;
57 
58   myisam_log_command(MI_LOG_DELETE_ALL,info,(uchar*) 0,0,0);
59   /*
60     If we are using delayed keys or if the user has done changes to the tables
61     since it was locked then there may be key blocks in the key cache
62   */
63   flush_key_blocks(share->key_cache, keycache_thread_var(),
64                    share->kfile, FLUSH_IGNORE_CHANGED);
65   if (share->file_map)
66     mi_munmap_file(info);
67   if (mysql_file_chsize(info->dfile, 0, 0, MYF(MY_WME)) ||
68       mysql_file_chsize(share->kfile, share->base.keystart, 0, MYF(MY_WME)))
69     goto err;
70   (void) _mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
71   DBUG_RETURN(0);
72 
73 err:
74   {
75     int save_errno=my_errno();
76     (void) _mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
77     info->update|=HA_STATE_WRITTEN;	/* Buffer changed */
78     set_my_errno(save_errno);
79     DBUG_RETURN(save_errno);
80   }
81 } /* mi_delete */
82