Coverage Report

Created: 2024-06-18 07:03

/src/server/mysys/my_open.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (c) 2000, 2011, 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-1335  USA */
15
16
#include "mysys_priv.h"
17
#include "mysys_err.h"
18
#include <m_string.h>
19
#include <errno.h>
20
#include "my_atomic.h"
21
22
CREATE_NOSYMLINK_FUNCTION(
23
  open_nosymlinks(const char *pathname, int flags, int mode),
24
  openat(dfd, filename, O_NOFOLLOW | flags, mode),
25
  open(pathname, O_NOFOLLOW | flags, mode)
26
);
27
28
/*
29
  Open a file
30
31
  SYNOPSIS
32
    my_open()
33
      FileName  Fully qualified file name
34
      Flags Read | write 
35
      MyFlags Special flags
36
37
  RETURN VALUE
38
    File descriptor
39
*/
40
41
File my_open(const char *FileName, int Flags, myf MyFlags)
42
        /* Path-name of file */
43
        /* Read | write .. */
44
        /* Special flags */
45
0
{
46
0
  File fd;
47
0
  DBUG_ENTER("my_open");
48
0
  DBUG_PRINT("my",("Name: '%s'  Flags: %d  MyFlags: %lu",
49
0
       FileName, Flags, MyFlags));
50
0
  if (!(MyFlags & (MY_WME | MY_FAE | MY_FFNF)))
51
0
    MyFlags|= my_global_flags;
52
#if defined(_WIN32)
53
  fd= my_win_open(FileName, Flags);
54
#else
55
0
  if (MyFlags & MY_NOSYMLINKS)
56
0
    fd = open_nosymlinks(FileName, Flags | O_CLOEXEC, my_umask);
57
0
  else
58
0
    fd = open(FileName, Flags | O_CLOEXEC, my_umask);
59
0
#endif
60
61
0
  fd= my_register_filename(fd, FileName, FILE_BY_OPEN,
62
0
         EE_FILENOTFOUND, MyFlags);
63
0
  DBUG_RETURN(fd);
64
0
} /* my_open */
65
66
67
/*
68
  Close a file
69
70
  SYNOPSIS
71
    my_close()
72
      fd  File sescriptor
73
      myf Special Flags
74
75
*/
76
77
int my_close(File fd, myf MyFlags)
78
0
{
79
0
  int err;
80
0
  char *name= NULL;
81
0
  DBUG_ENTER("my_close");
82
0
  DBUG_PRINT("my",("fd: %d  MyFlags: %lu",fd, MyFlags));
83
0
  if (!(MyFlags & (MY_WME | MY_FAE)))
84
0
    MyFlags|= my_global_flags;
85
86
0
  if ((uint) fd < my_file_limit && my_file_info[fd].type != UNOPEN)
87
0
  {
88
0
    name= my_file_info[fd].name;
89
0
    my_file_info[fd].name= NULL;
90
0
    my_file_info[fd].type= UNOPEN;
91
0
  }
92
0
#ifndef _WIN32
93
0
  err= close(fd);
94
#else
95
  err= my_win_close(fd);
96
#endif
97
0
  if (err)
98
0
  {
99
0
    DBUG_PRINT("error",("Got error %d on close",err));
100
0
    my_errno=errno;
101
0
    if (MyFlags & (MY_FAE | MY_WME))
102
0
      my_error(EE_BADCLOSE, MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_ERROR_LOG))),
103
0
               name,errno);
104
0
  }
105
0
  if (name)
106
0
  {
107
0
    my_free(name);
108
0
  }
109
0
  my_atomic_add32_explicit(&my_file_opened, -1, MY_MEMORY_ORDER_RELAXED);
110
0
  DBUG_RETURN(err);
111
0
} /* my_close */
112
113
114
/*
115
  Register file in my_file_info[]
116
   
117
  SYNOPSIS
118
    my_register_filename()
119
    fd         File number opened, -1 if error on open
120
    FileName       File name
121
    type_file_type     How file was created
122
    error_message_number   Error message number if caller got error (fd == -1)
123
    MyFlags      Flags for my_close()
124
125
  RETURN
126
    -1   error
127
     #   Filenumber
128
129
*/
130
131
File my_register_filename(File fd, const char *FileName, enum file_type
132
        type_of_file, uint error_message_number, myf MyFlags)
133
0
{
134
0
  DBUG_ENTER("my_register_filename");
135
0
  if ((int) fd >= MY_FILE_MIN)
136
0
  {
137
0
    my_atomic_add32_explicit(&my_file_opened, 1, MY_MEMORY_ORDER_RELAXED);
138
0
    if ((uint) fd >= my_file_limit || (MyFlags & MY_NO_REGISTER))
139
0
      DBUG_RETURN(fd);
140
0
    my_file_info[fd].name = my_strdup(key_memory_my_file_info, FileName, MyFlags);
141
0
    statistic_increment(my_file_total_opened,&THR_LOCK_open);
142
0
    my_file_info[fd].type = type_of_file;
143
0
    DBUG_PRINT("exit",("fd: %d",fd));
144
0
    DBUG_RETURN(fd);
145
0
  }
146
0
  my_errno= errno;
147
148
0
  DBUG_PRINT("error",("Got error %d on open", my_errno));
149
0
  if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
150
0
  {
151
0
    if (my_errno == EMFILE)
152
0
      error_message_number= EE_OUT_OF_FILERESOURCES;
153
0
    my_error(error_message_number,
154
0
             MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_ERROR_LOG))),
155
0
             FileName, my_errno);
156
0
  }
157
0
  DBUG_RETURN(-1);
158
0
}