mf_iocache2.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* Copyright (c) 2000, 2019, 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/mf_iocache2.cc
  24. More functions to be used with IO_CACHE files
  25. */
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <sys/types.h>
  30. #include "m_ctype.h"
  31. #include "m_string.h"
  32. #include "my_compiler.h"
  33. #include "my_dbug.h"
  34. #include "my_inttypes.h"
  35. #include "my_io.h"
  36. #include "my_sys.h"
  37. #include "mysql/psi/mysql_file.h"
  38. #include "mysql/psi/mysql_mutex.h"
  39. #include "template_utils.h"
  40. /*
  41. Copy contents of an IO_CACHE to a file.
  42. SYNOPSIS
  43. my_b_copy_to_file()
  44. cache IO_CACHE to copy from
  45. file File to copy to
  46. DESCRIPTION
  47. Copy the contents of the cache to the file. The cache will be
  48. re-inited to a read cache and will read from the beginning of the
  49. cache.
  50. If a failure to write fully occurs, the cache is only copied
  51. partially.
  52. TODO
  53. Make this function solid by handling partial reads from the cache
  54. in a correct manner: it should be atomic.
  55. RETURN VALUE
  56. 0 All OK
  57. 1 An error occurred
  58. */
  59. int my_b_copy_to_file(IO_CACHE *cache, FILE *file) {
  60. size_t bytes_in_cache;
  61. DBUG_ENTER("my_b_copy_to_file");
  62. /* Reinit the cache to read from the beginning of the cache */
  63. if (reinit_io_cache(cache, READ_CACHE, 0L, false, false)) DBUG_RETURN(1);
  64. bytes_in_cache = my_b_bytes_in_cache(cache);
  65. do {
  66. if (my_fwrite(file, cache->read_pos, bytes_in_cache,
  67. MYF(MY_WME | MY_NABP)) == (size_t)-1)
  68. DBUG_RETURN(1);
  69. cache->read_pos = cache->read_end;
  70. } while ((bytes_in_cache = my_b_fill(cache)));
  71. if (cache->error == -1) DBUG_RETURN(1);
  72. DBUG_RETURN(0);
  73. }
  74. /*
  75. Make next read happen at the given position
  76. For write cache, make next write happen at the given position
  77. */
  78. void my_b_seek(IO_CACHE *info, my_off_t pos) {
  79. my_off_t offset;
  80. DBUG_ENTER("my_b_seek");
  81. DBUG_PRINT("enter", ("pos: %lu", (ulong)pos));
  82. /*
  83. TODO:
  84. Verify that it is OK to do seek in the non-append
  85. area in SEQ_READ_APPEND cache
  86. a) see if this always works
  87. b) see if there is a better way to make it work
  88. */
  89. if (info->type == SEQ_READ_APPEND) (void)flush_io_cache(info);
  90. offset = (pos - info->pos_in_file);
  91. if (info->type == READ_CACHE || info->type == SEQ_READ_APPEND) {
  92. /* TODO: explain why this works if pos < info->pos_in_file */
  93. if ((ulonglong)offset < (ulonglong)(info->read_end - info->buffer)) {
  94. /* The read is in the current buffer; Reuse it */
  95. info->read_pos = info->buffer + offset;
  96. DBUG_VOID_RETURN;
  97. } else {
  98. /* Force a new read on next my_b_read */
  99. info->read_pos = info->read_end = info->buffer;
  100. }
  101. } else if (info->type == WRITE_CACHE) {
  102. /* If write is in current buffer, reuse it */
  103. if ((ulonglong)offset <=
  104. (ulonglong)(info->write_end - info->write_buffer)) {
  105. info->write_pos = info->write_buffer + offset;
  106. DBUG_VOID_RETURN;
  107. }
  108. (void)flush_io_cache(info);
  109. /* Correct buffer end so that we write in increments of IO_SIZE */
  110. info->write_end =
  111. (info->write_buffer + info->buffer_length - (pos & (IO_SIZE - 1)));
  112. }
  113. info->pos_in_file = pos;
  114. info->seek_not_done = 1;
  115. DBUG_VOID_RETURN;
  116. }
  117. /*
  118. Fill buffer of the cache.
  119. NOTES
  120. This assumes that you have already used all characters in the CACHE,
  121. independent of the read_pos value!
  122. RETURN
  123. 0 On error or EOF (info->error = -1 on error)
  124. # Number of characters
  125. */
  126. size_t my_b_fill(IO_CACHE *info) {
  127. my_off_t pos_in_file =
  128. (info->pos_in_file + (size_t)(info->read_end - info->buffer));
  129. size_t diff_length, length, max_length;
  130. if (info->seek_not_done) { /* File touched, do seek */
  131. if (mysql_encryption_file_seek(info, pos_in_file, MY_SEEK_SET, MYF(0)) ==
  132. MY_FILEPOS_ERROR) {
  133. info->error = 0;
  134. return 0;
  135. }
  136. info->seek_not_done = 0;
  137. }
  138. diff_length = (size_t)(pos_in_file & (IO_SIZE - 1));
  139. max_length = (info->read_length - diff_length);
  140. if (max_length >= (info->end_of_file - pos_in_file))
  141. max_length = (size_t)(info->end_of_file - pos_in_file);
  142. if (!max_length) {
  143. info->error = 0;
  144. return 0; /* EOF */
  145. }
  146. DBUG_EXECUTE_IF("simulate_my_b_fill_error",
  147. { DBUG_SET("+d,simulate_file_read_error"); });
  148. if ((length = mysql_encryption_file_read(info, info->buffer, max_length,
  149. info->myflags)) == (size_t)-1) {
  150. info->error = -1;
  151. return 0;
  152. }
  153. info->read_pos = info->buffer;
  154. info->read_end = info->buffer + length;
  155. info->pos_in_file = pos_in_file;
  156. return length;
  157. }
  158. /*
  159. Read a string ended by '\n' into a buffer of 'max_length' size.
  160. Returns number of characters read, 0 on error.
  161. last byte is set to '\0'
  162. If buffer is full then to[max_length-1] will be set to \0.
  163. */
  164. size_t my_b_gets(IO_CACHE *info, char *to, size_t max_length) {
  165. char *start = to;
  166. size_t length;
  167. max_length--; /* Save place for end \0 */
  168. /* Calculate number of characters in buffer */
  169. if (!(length = my_b_bytes_in_cache(info)) && !(length = my_b_fill(info)))
  170. return 0;
  171. for (;;) {
  172. uchar *pos, *end;
  173. if (length > max_length) length = max_length;
  174. for (pos = info->read_pos, end = pos + length; pos < end;) {
  175. if ((*to++ = *pos++) == '\n') {
  176. info->read_pos = pos;
  177. *to = '\0';
  178. return (size_t)(to - start);
  179. }
  180. }
  181. if (!(max_length -= length)) {
  182. /* Found enough charcters; Return found string */
  183. info->read_pos = pos;
  184. *to = '\0';
  185. return (size_t)(to - start);
  186. }
  187. if (!(length = my_b_fill(info))) return 0;
  188. }
  189. }
  190. my_off_t my_b_filelength(IO_CACHE *info) {
  191. if (info->type == WRITE_CACHE) return my_b_tell(info);
  192. info->seek_not_done = 1;
  193. return mysql_file_seek(info->file, 0L, MY_SEEK_END, MYF(0));
  194. }
  195. /**
  196. Simple printf version. Used for logging in MySQL.
  197. Supports '%c', '%s', '%b', '%d', '%u', '%ld', '%lu' and '%llu'.
  198. Returns number of written characters, or (size_t) -1 on error.
  199. @param info The IO_CACHE to write to
  200. @param fmt format string
  201. @param ... variable list of arguments
  202. @return number of bytes written, -1 if an error occurred
  203. */
  204. size_t my_b_printf(IO_CACHE *info, const char *fmt, ...) {
  205. size_t result;
  206. va_list args;
  207. va_start(args, fmt);
  208. result = my_b_vprintf(info, fmt, args);
  209. va_end(args);
  210. return result;
  211. }
  212. /**
  213. Implementation of my_b_printf.
  214. @param info The IO_CACHE to write to
  215. @param fmt format string
  216. @param args variable list of arguments
  217. @return number of bytes written, -1 if an error occurred
  218. */
  219. size_t my_b_vprintf(IO_CACHE *info, const char *fmt, va_list args) {
  220. size_t out_length = 0;
  221. uint minimum_width; /* as yet unimplemented */
  222. uint minimum_width_sign;
  223. uint precision; /* as yet unimplemented for anything but %b */
  224. bool is_zero_padded;
  225. /*
  226. Store the location of the beginning of a format directive, for the
  227. case where we learn we shouldn't have been parsing a format string
  228. at all, and we don't want to lose the flag/precision/width/size
  229. information.
  230. */
  231. const char *backtrack;
  232. for (; *fmt != '\0'; fmt++) {
  233. /* Copy everything until '%' or end of string */
  234. const char *start = fmt;
  235. size_t length;
  236. for (; (*fmt != '\0') && (*fmt != '%'); fmt++)
  237. ;
  238. length = (size_t)(fmt - start);
  239. out_length += length;
  240. if (my_b_write(info, (const uchar *)start, length)) goto err;
  241. if (*fmt == '\0') /* End of format */
  242. return out_length;
  243. /*
  244. By this point, *fmt must be a percent; Keep track of this location and
  245. skip over the percent character.
  246. */
  247. DBUG_ASSERT(*fmt == '%');
  248. backtrack = fmt;
  249. fmt++;
  250. is_zero_padded = false;
  251. minimum_width_sign = 1;
  252. minimum_width = 0;
  253. precision = 0;
  254. /* Skip if max size is used (to be compatible with printf) */
  255. process_flags:
  256. switch (*fmt) {
  257. case '-':
  258. minimum_width_sign = -1;
  259. fmt++;
  260. goto process_flags;
  261. case '0':
  262. is_zero_padded = true;
  263. fmt++;
  264. goto process_flags;
  265. case '#':
  266. /** @todo Implement "#" conversion flag. */ fmt++;
  267. goto process_flags;
  268. case ' ':
  269. /** @todo Implement " " conversion flag. */ fmt++;
  270. goto process_flags;
  271. case '+':
  272. /** @todo Implement "+" conversion flag. */ fmt++;
  273. goto process_flags;
  274. }
  275. if (*fmt == '*') {
  276. minimum_width = (int)va_arg(args, int);
  277. fmt++;
  278. } else {
  279. while (my_isdigit(&my_charset_latin1, *fmt)) {
  280. minimum_width = (minimum_width * 10) + (*fmt - '0');
  281. fmt++;
  282. }
  283. }
  284. minimum_width *= minimum_width_sign;
  285. if (*fmt == '.') {
  286. fmt++;
  287. if (*fmt == '*') {
  288. precision = (int)va_arg(args, int);
  289. fmt++;
  290. } else {
  291. while (my_isdigit(&my_charset_latin1, *fmt)) {
  292. precision = (precision * 10) + (*fmt - '0');
  293. fmt++;
  294. }
  295. }
  296. }
  297. if (*fmt == 's') /* String parameter */
  298. {
  299. char *par = va_arg(args, char *);
  300. size_t length2 = strlen(par);
  301. /* TODO: implement precision */
  302. out_length += length2;
  303. if (my_b_write(info, (uchar *)par, length2)) goto err;
  304. } else if (*fmt == 'c') /* char type parameter */
  305. {
  306. char par[2];
  307. par[0] = va_arg(args, int);
  308. out_length++;
  309. if (my_b_write(info, (uchar *)par, 1)) goto err;
  310. } else if (*fmt ==
  311. 'b') /* Sized buffer parameter, only precision makes sense */
  312. {
  313. char *par = va_arg(args, char *);
  314. out_length += precision;
  315. if (my_b_write(info, (uchar *)par, precision)) goto err;
  316. } else if (*fmt == 'd' || *fmt == 'u') /* Integer parameter */
  317. {
  318. int iarg;
  319. size_t length2;
  320. char buff[32];
  321. iarg = va_arg(args, int);
  322. if (*fmt == 'd')
  323. length2 = (size_t)(int10_to_str((long)iarg, buff, -10) - buff);
  324. else
  325. length2 = (uint)(int10_to_str((long)(uint)iarg, buff, 10) - buff);
  326. /* minimum width padding */
  327. if (minimum_width > length2) {
  328. char *buffz;
  329. buffz = static_cast<char *>(my_alloca(minimum_width - length2));
  330. if (is_zero_padded)
  331. memset(buffz, '0', minimum_width - length2);
  332. else
  333. memset(buffz, ' ', minimum_width - length2);
  334. if (my_b_write(info, pointer_cast<uchar *>(buffz),
  335. minimum_width - length2)) {
  336. goto err;
  337. }
  338. }
  339. out_length += length2;
  340. if (my_b_write(info, (uchar *)buff, length2)) goto err;
  341. } else if ((*fmt == 'l' && fmt[1] == 'd') || fmt[1] == 'u')
  342. /* long parameter */
  343. {
  344. long iarg;
  345. size_t length2;
  346. char buff[32];
  347. iarg = va_arg(args, long);
  348. if (*++fmt == 'd')
  349. length2 = (size_t)(int10_to_str(iarg, buff, -10) - buff);
  350. else
  351. length2 = (size_t)(int10_to_str(iarg, buff, 10) - buff);
  352. out_length += length2;
  353. if (my_b_write(info, (uchar *)buff, length2)) goto err;
  354. } else if (fmt[0] == 'l' && fmt[1] == 'l' && fmt[2] == 'u') {
  355. ulonglong iarg;
  356. size_t length2;
  357. char buff[32];
  358. iarg = va_arg(args, ulonglong);
  359. length2 = (size_t)(longlong10_to_str(iarg, buff, 10) - buff);
  360. out_length += length2;
  361. fmt += 2;
  362. if (my_b_write(info, (uchar *)buff, length2)) goto err;
  363. } else {
  364. /* %% or unknown code */
  365. if (my_b_write(info, pointer_cast<const uchar *>(backtrack),
  366. fmt - backtrack))
  367. goto err;
  368. out_length += fmt - backtrack;
  369. }
  370. }
  371. return out_length;
  372. err:
  373. return (size_t)-1;
  374. }