my_fstream.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. Without limiting anything contained in the foregoing, this file,
  12. which is part of C Driver for MySQL (Connector/C), is also subject to the
  13. Universal FOSS Exception, version 1.0, a copy of which can be found at
  14. http://oss.oracle.com/licenses/universal-foss-exception.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License, version 2.0, for more details.
  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. @file mysys/my_fstream.cc
  24. */
  25. #include "my_config.h"
  26. #include <errno.h>
  27. #include <stdio.h>
  28. #include <sys/types.h>
  29. #include "my_dbug.h"
  30. #include "my_inttypes.h"
  31. #include "my_sys.h"
  32. #include "my_thread_local.h"
  33. #include "mysys_err.h"
  34. #include "template_utils.h"
  35. #if defined(_WIN32)
  36. #include "mysys/mysys_priv.h"
  37. #endif
  38. #ifdef HAVE_FSEEKO
  39. #undef ftell
  40. #undef fseek
  41. #define ftell(A) ftello(A)
  42. #define fseek(A, B, C) fseeko((A), (B), (C))
  43. #endif
  44. /*
  45. Read a chunk of bytes from a FILE
  46. SYNOPSIS
  47. my_fread()
  48. stream File descriptor
  49. Buffer Buffer to read to
  50. Count Number of bytes to read
  51. MyFlags Flags on what to do on error
  52. RETURN
  53. (size_t) -1 Error
  54. # Number of bytes read
  55. */
  56. size_t my_fread(FILE *stream, uchar *Buffer, size_t Count, myf MyFlags) {
  57. size_t readbytes;
  58. DBUG_ENTER("my_fread");
  59. DBUG_PRINT("my", ("stream: %p Buffer: %p Count: %u MyFlags: %d", stream,
  60. Buffer, (uint)Count, MyFlags));
  61. if ((readbytes = fread(Buffer, sizeof(char), Count, stream)) != Count) {
  62. DBUG_PRINT("error", ("Read only %d bytes", (int)readbytes));
  63. if (MyFlags & (MY_WME | MY_FAE | MY_FNABP)) {
  64. if (ferror(stream)) {
  65. char errbuf[MYSYS_STRERROR_SIZE];
  66. my_error(EE_READ, MYF(0), my_filename(my_fileno(stream)), errno,
  67. my_strerror(errbuf, sizeof(errbuf), errno));
  68. } else if (MyFlags & (MY_NABP | MY_FNABP)) {
  69. char errbuf[MYSYS_STRERROR_SIZE];
  70. my_error(EE_EOFERR, MYF(0), my_filename(my_fileno(stream)), errno,
  71. my_strerror(errbuf, sizeof(errbuf), errno));
  72. }
  73. }
  74. set_my_errno(errno ? errno : -1);
  75. if (ferror(stream) || MyFlags & (MY_NABP | MY_FNABP))
  76. DBUG_RETURN((size_t)-1); /* Return with error */
  77. }
  78. if (MyFlags & (MY_NABP | MY_FNABP)) DBUG_RETURN(0); /* Read ok */
  79. DBUG_RETURN(readbytes);
  80. } /* my_fread */
  81. /*
  82. Write a chunk of bytes to a stream
  83. my_fwrite()
  84. stream File descriptor
  85. Buffer Buffer to write from
  86. Count Number of bytes to write
  87. MyFlags Flags on what to do on error
  88. RETURN
  89. (size_t) -1 Error
  90. # Number of bytes written
  91. */
  92. size_t my_fwrite(FILE *stream, const uchar *Buffer, size_t Count, myf MyFlags) {
  93. size_t writtenbytes = 0;
  94. my_off_t seekptr;
  95. DBUG_ENTER("my_fwrite");
  96. DBUG_PRINT("my", ("stream: %p Buffer: %p Count: %u MyFlags: %d", stream,
  97. Buffer, (uint)Count, MyFlags));
  98. DBUG_EXECUTE_IF("simulate_fwrite_error", DBUG_RETURN(-1););
  99. seekptr = ftell(stream);
  100. for (;;) {
  101. size_t written =
  102. fwrite(pointer_cast<const char *>(Buffer), sizeof(char), Count, stream);
  103. if (written != Count) {
  104. DBUG_PRINT("error", ("Write only %d bytes", (int)writtenbytes));
  105. set_my_errno(errno);
  106. if (written != (size_t)-1) {
  107. seekptr += written;
  108. Buffer += written;
  109. writtenbytes += written;
  110. Count -= written;
  111. }
  112. if (errno == EINTR) {
  113. (void)my_fseek(stream, seekptr, MY_SEEK_SET);
  114. continue;
  115. }
  116. if (ferror(stream) || (MyFlags & (MY_NABP | MY_FNABP))) {
  117. if (MyFlags & (MY_WME | MY_FAE | MY_FNABP)) {
  118. char errbuf[MYSYS_STRERROR_SIZE];
  119. my_error(EE_WRITE, MYF(0), my_filename(my_fileno(stream)), errno,
  120. my_strerror(errbuf, sizeof(errbuf), errno));
  121. }
  122. writtenbytes = (size_t)-1; /* Return that we got error */
  123. break;
  124. }
  125. }
  126. if (MyFlags & (MY_NABP | MY_FNABP))
  127. writtenbytes = 0; /* Everything OK */
  128. else
  129. writtenbytes += written;
  130. break;
  131. }
  132. DBUG_RETURN(writtenbytes);
  133. } /* my_fwrite */
  134. /* Seek to position in file */
  135. my_off_t my_fseek(FILE *stream, my_off_t pos, int whence) {
  136. DBUG_ENTER("my_fseek");
  137. DBUG_PRINT("my",
  138. ("stream: %p pos: %lu whence: %d", stream, (long)pos, whence));
  139. DBUG_RETURN(fseek(stream, (off_t)pos, whence) ? MY_FILEPOS_ERROR
  140. : (my_off_t)ftell(stream));
  141. } /* my_seek */
  142. /* Tell current position of file */
  143. my_off_t my_ftell(FILE *stream) {
  144. off_t pos;
  145. DBUG_ENTER("my_ftell");
  146. DBUG_PRINT("my", ("stream: %p", stream));
  147. pos = ftell(stream);
  148. DBUG_PRINT("exit", ("ftell: %lu", (ulong)pos));
  149. DBUG_RETURN((my_off_t)pos);
  150. } /* my_ftell */
  151. /* Get a File corresponding to the stream*/
  152. int my_fileno(FILE *f) {
  153. #ifdef _WIN32
  154. return my_win_fileno(f);
  155. #else
  156. return fileno(f);
  157. #endif
  158. }