aboutsummaryrefslogtreecommitdiff
path: root/mysql/mysys/my_delete.c
blob: 7f82e6078b5a4315871f74aa19e7e6ea79d2b018 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include "mysys_priv.h"
#include "mysys_err.h"
#include <my_sys.h>
#include "my_thread_local.h"


int my_delete(const char *name, myf MyFlags)
{
  int err;
  DBUG_ENTER("my_delete");
  DBUG_PRINT("my",("name %s MyFlags %d", name, MyFlags));

  if ((err = unlink(name)) == -1)
  {
    set_my_errno(errno);
    if (MyFlags & (MY_FAE+MY_WME))
    {
      char errbuf[MYSYS_STRERROR_SIZE];
      my_error(EE_DELETE, MYF(0),
               name, errno, my_strerror(errbuf, sizeof(errbuf), errno));
    }
  }
  else if ((MyFlags & MY_SYNC_DIR) &&
           my_sync_dir_by_file(name, MyFlags))
    err= -1;
  DBUG_RETURN(err);
} /* my_delete */

#if defined(_WIN32)
/**
  Delete file which is possibly not closed.

  This function is intended to be used exclusively as a temporal solution
  for Win NT in case when it is needed to delete a not closed file (note
  that the file must be opened everywhere with FILE_SHARE_DELETE mode).
  Deleting not-closed files can not be supported on Win 98|ME (and because
  of that is considered harmful).
  
  The function deletes the file with its preliminary renaming. This is
  because when not-closed share-delete file is deleted it still lives on
  a disk until it will not be closed everwhere. This may conflict with an
  attempt to create a new file with the same name. The deleted file is
  renamed to <name>.<num>.deleted where <name> - the initial name of the
  file, <num> - a hexadecimal number chosen to make the temporal name to
  be unique.

  @param the name of the being deleted file
  @param the flags instructing how to react on an error internally in
         the function

  @note The per-thread @c my_errno holds additional info for a caller to
        decide how critical the error can be.

  @retval
    0	ok
  @retval
    1   error


*/
int nt_share_delete(const char *name, myf MyFlags)
{
  char buf[MAX_PATH + 20];
  ulong cnt;
  DBUG_ENTER("nt_share_delete");
  DBUG_PRINT("my",("name %s MyFlags %d", name, MyFlags));

  for (cnt= GetTickCount(); cnt; cnt--)
  {
    errno= 0;
    sprintf(buf, "%s.%08X.deleted", name, cnt);
    if (MoveFile(name, buf))
      break;

    if ((errno= GetLastError()) == ERROR_ALREADY_EXISTS)
      continue;

    /* This happened during tests with MERGE tables. */
    if (errno == ERROR_ACCESS_DENIED)
      continue;

    DBUG_PRINT("warning", ("Failed to rename %s to %s, errno: %d",
                           name, buf, errno));
    break;
  }

  if (errno == ERROR_FILE_NOT_FOUND)
  {
    set_my_errno(ENOENT);    // marking, that `name' doesn't exist 
  }
  else if (errno == 0)
  {
    if (DeleteFile(buf))
      DBUG_RETURN(0);
    /*
      The below is more complicated than necessary. For some reason, the
      assignment to my_errno clears the error number, which is retrieved
      by GetLastError() (VC2005EE). Assigning to errno first, allows to
      retrieve the correct value.
    */
    errno= GetLastError();
    if (errno == 0)
      set_my_errno(ENOENT); // marking, that `buf' doesn't exist
    else
      set_my_errno(errno);
  }
  else
    set_my_errno(errno);

  if (MyFlags & (MY_FAE+MY_WME))
  {
    char errbuf[MYSYS_STRERROR_SIZE];
    my_error(EE_DELETE, MYF(0),
             name, my_errno(), my_strerror(errbuf, sizeof(errbuf), my_errno()));
  }
  DBUG_RETURN(-1);
}
#endif