Coverage Report

Created: 2024-06-18 07:03

/src/server/mysys/my_read.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (c) 2000, 2013, 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 <my_base.h>
19
#include <errno.h>
20
21
/*
22
  Read a chunk of bytes from a file with retry's if needed
23
24
  The parameters are:
25
    File descriptor
26
    Buffer to hold at least Count bytes
27
    Bytes to read
28
    Flags on what to do on error
29
30
    Return:
31
      -1 on error
32
      0  if flag has bits MY_NABP or MY_FNABP set
33
      N  number of bytes read.
34
*/
35
36
size_t my_read(File Filedes, uchar *Buffer, size_t Count, myf MyFlags)
37
0
{
38
0
  size_t readbytes, save_count= 0;
39
0
  DBUG_ENTER("my_read");
40
0
  DBUG_PRINT("my",("fd: %d  Buffer: %p  Count: %lu  MyFlags: %lu",
41
0
                   Filedes, Buffer, (ulong) Count, MyFlags));
42
0
  if (!(MyFlags & (MY_WME | MY_FAE | MY_FNABP)))
43
0
    MyFlags|= my_global_flags;
44
45
0
  for (;;)
46
0
  {
47
0
    errno= 0;         /* Linux, Windows don't reset this on EOF/success */
48
#ifdef _WIN32
49
    readbytes= my_win_read(Filedes, Buffer, Count);
50
#else
51
0
    readbytes= read(Filedes, Buffer, Count);
52
0
#endif
53
0
    DBUG_EXECUTE_IF ("simulate_file_read_error",
54
0
                     {
55
0
                       errno= ENOSPC;
56
0
                       readbytes= (size_t) -1;
57
0
                       DBUG_SET("-d,simulate_file_read_error");
58
0
                       DBUG_SET("-d,simulate_my_b_fill_error");
59
0
                     });
60
61
0
    if (readbytes != Count)
62
0
    {
63
0
      int got_errno= my_errno= errno;
64
0
      DBUG_PRINT("warning",("Read only %d bytes off %lu from %d, errno: %d",
65
0
                            (int) readbytes, (ulong) Count, Filedes,
66
0
                            got_errno));
67
68
0
      if (got_errno == 0 || (readbytes != (size_t) -1 &&
69
0
                         (MyFlags & (MY_NABP | MY_FNABP))))
70
0
        my_errno= HA_ERR_FILE_TOO_SHORT;
71
72
0
      if ((readbytes == 0 || (int) readbytes == -1) && got_errno == EINTR)
73
0
      {  
74
0
        DBUG_PRINT("debug", ("my_read() was interrupted and returned %ld",
75
0
                             (long) readbytes));
76
0
        continue;                              /* Interrupted */
77
0
      }
78
79
      /* Do a read retry if we didn't get enough data on first read */
80
0
      if (readbytes != (size_t) -1 && readbytes != 0 &&
81
0
          (MyFlags & MY_FULL_IO))
82
0
      {
83
0
        Buffer+= readbytes;
84
0
        Count-= readbytes;
85
0
        save_count+= readbytes;
86
0
        continue;
87
0
      }
88
89
0
      if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
90
0
      {
91
0
        if (readbytes == (size_t) -1)
92
0
          my_error(EE_READ,
93
0
                   MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_ERROR_LOG))),
94
0
                   my_filename(Filedes), got_errno);
95
0
        else if (MyFlags & (MY_NABP | MY_FNABP))
96
0
          my_error(EE_EOFERR,
97
0
                   MYF(ME_BELL | (MyFlags & (ME_NOTE | ME_ERROR_LOG))),
98
0
                   my_filename(Filedes), got_errno);
99
0
      }
100
0
      if (readbytes == (size_t) -1 ||
101
0
          ((MyFlags & (MY_FNABP | MY_NABP)) && !(MyFlags & MY_FULL_IO)))
102
0
        DBUG_RETURN(MY_FILE_ERROR);  /* Return with error */
103
0
    }
104
105
0
    if (MyFlags & (MY_NABP | MY_FNABP))
106
0
      readbytes= 0;     /* Ok on read */
107
0
    else
108
0
      readbytes+= save_count;
109
0
    break;
110
0
  }
111
0
  DBUG_RETURN(readbytes);
112
0
} /* my_read */