MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_delete.c
1 /* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
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 as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #include "mysys_priv.h"
17 #include "mysys_err.h"
18 #include <my_sys.h>
19 
20 int my_delete(const char *name, myf MyFlags)
21 {
22  int err;
23  DBUG_ENTER("my_delete");
24  DBUG_PRINT("my",("name %s MyFlags %d", name, MyFlags));
25 
26  if ((err = unlink(name)) == -1)
27  {
28  my_errno=errno;
29  if (MyFlags & (MY_FAE+MY_WME))
30  {
31  char errbuf[MYSYS_STRERROR_SIZE];
32  my_error(EE_DELETE, MYF(ME_BELL+ME_WAITTANG+(MyFlags & ME_NOINPUT)),
33  name, errno, my_strerror(errbuf, sizeof(errbuf), errno));
34  }
35  }
36  else if ((MyFlags & MY_SYNC_DIR) &&
37  my_sync_dir_by_file(name, MyFlags))
38  err= -1;
39  DBUG_RETURN(err);
40 } /* my_delete */
41 
42 #if defined(__WIN__)
43 
74 int nt_share_delete(const char *name, myf MyFlags)
75 {
76  char buf[MAX_PATH + 20];
77  ulong cnt;
78  DBUG_ENTER("nt_share_delete");
79  DBUG_PRINT("my",("name %s MyFlags %d", name, MyFlags));
80 
81  for (cnt= GetTickCount(); cnt; cnt--)
82  {
83  errno= 0;
84  sprintf(buf, "%s.%08X.deleted", name, cnt);
85  if (MoveFile(name, buf))
86  break;
87 
88  if ((errno= GetLastError()) == ERROR_ALREADY_EXISTS)
89  continue;
90 
91  /* This happened during tests with MERGE tables. */
92  if (errno == ERROR_ACCESS_DENIED)
93  continue;
94 
95  DBUG_PRINT("warning", ("Failed to rename %s to %s, errno: %d",
96  name, buf, errno));
97  break;
98  }
99 
100  if (errno == ERROR_FILE_NOT_FOUND)
101  {
102  my_errno= ENOENT; // marking, that `name' doesn't exist
103  }
104  else if (errno == 0)
105  {
106  if (DeleteFile(buf))
107  DBUG_RETURN(0);
108  /*
109  The below is more complicated than necessary. For some reason, the
110  assignment to my_errno clears the error number, which is retrieved
111  by GetLastError() (VC2005EE). Assigning to errno first, allows to
112  retrieve the correct value.
113  */
114  errno= GetLastError();
115  if (errno == 0)
116  my_errno= ENOENT; // marking, that `buf' doesn't exist
117  else
118  my_errno= errno;
119  }
120  else
121  my_errno= errno;
122 
123  if (MyFlags & (MY_FAE+MY_WME))
124  {
125  char errbuf[MYSYS_STRERROR_SIZE];
126  my_error(EE_DELETE, MYF(ME_BELL + ME_WAITTANG + (MyFlags & ME_NOINPUT)),
127  name, my_errno, my_strerror(errbuf, sizeof(errbuf), my_errno));
128  }
129  DBUG_RETURN(-1);
130 }
131 #endif