tool_cb_wrt.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "tool_setup.h"
  23. #define ENABLE_CURLX_PRINTF
  24. /* use our own printf() functions */
  25. #include "curlx.h"
  26. #include "tool_cfgable.h"
  27. #include "tool_msgs.h"
  28. #include "tool_cb_wrt.h"
  29. #include "tool_operate.h"
  30. #include "memdebug.h" /* keep this as LAST include */
  31. /* create a local file for writing, return TRUE on success */
  32. bool tool_create_output_file(struct OutStruct *outs)
  33. {
  34. struct GlobalConfig *global = outs->config->global;
  35. FILE *file;
  36. if(!outs->filename || !*outs->filename) {
  37. warnf(global, "Remote filename has no length!\n");
  38. return FALSE;
  39. }
  40. if(outs->is_cd_filename) {
  41. /* don't overwrite existing files */
  42. file = fopen(outs->filename, "rb");
  43. if(file) {
  44. fclose(file);
  45. warnf(global, "Refusing to overwrite %s: %s\n", outs->filename,
  46. strerror(EEXIST));
  47. return FALSE;
  48. }
  49. }
  50. /* open file for writing */
  51. file = fopen(outs->filename, "wb");
  52. if(!file) {
  53. warnf(global, "Failed to create the file %s: %s\n", outs->filename,
  54. strerror(errno));
  55. return FALSE;
  56. }
  57. outs->s_isreg = TRUE;
  58. outs->fopened = TRUE;
  59. outs->stream = file;
  60. outs->bytes = 0;
  61. outs->init = 0;
  62. return TRUE;
  63. }
  64. /*
  65. ** callback for CURLOPT_WRITEFUNCTION
  66. */
  67. size_t tool_write_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
  68. {
  69. size_t rc;
  70. struct per_transfer *per = userdata;
  71. struct OutStruct *outs = &per->outs;
  72. struct OperationConfig *config = outs->config;
  73. size_t bytes = sz * nmemb;
  74. bool is_tty = config->global->isatty;
  75. #ifdef WIN32
  76. CONSOLE_SCREEN_BUFFER_INFO console_info;
  77. intptr_t fhnd;
  78. #endif
  79. /*
  80. * Once that libcurl has called back tool_write_cb() the returned value
  81. * is checked against the amount that was intended to be written, if
  82. * it does not match then it fails with CURLE_WRITE_ERROR. So at this
  83. * point returning a value different from sz*nmemb indicates failure.
  84. */
  85. const size_t failure = bytes ? 0 : 1;
  86. #ifdef DEBUGBUILD
  87. {
  88. char *tty = curlx_getenv("CURL_ISATTY");
  89. if(tty) {
  90. is_tty = TRUE;
  91. curl_free(tty);
  92. }
  93. }
  94. if(config->show_headers) {
  95. if(bytes > (size_t)CURL_MAX_HTTP_HEADER) {
  96. warnf(config->global, "Header data size exceeds single call write "
  97. "limit!\n");
  98. return failure;
  99. }
  100. }
  101. else {
  102. if(bytes > (size_t)CURL_MAX_WRITE_SIZE) {
  103. warnf(config->global, "Data size exceeds single call write limit!\n");
  104. return failure;
  105. }
  106. }
  107. {
  108. /* Some internal congruency checks on received OutStruct */
  109. bool check_fails = FALSE;
  110. if(outs->filename) {
  111. /* regular file */
  112. if(!*outs->filename)
  113. check_fails = TRUE;
  114. if(!outs->s_isreg)
  115. check_fails = TRUE;
  116. if(outs->fopened && !outs->stream)
  117. check_fails = TRUE;
  118. if(!outs->fopened && outs->stream)
  119. check_fails = TRUE;
  120. if(!outs->fopened && outs->bytes)
  121. check_fails = TRUE;
  122. }
  123. else {
  124. /* standard stream */
  125. if(!outs->stream || outs->s_isreg || outs->fopened)
  126. check_fails = TRUE;
  127. if(outs->alloc_filename || outs->is_cd_filename || outs->init)
  128. check_fails = TRUE;
  129. }
  130. if(check_fails) {
  131. warnf(config->global, "Invalid output struct data for write callback\n");
  132. return failure;
  133. }
  134. }
  135. #endif
  136. if(!outs->stream && !tool_create_output_file(outs))
  137. return failure;
  138. if(is_tty && (outs->bytes < 2000) && !config->terminal_binary_ok) {
  139. /* binary output to terminal? */
  140. if(memchr(buffer, 0, bytes)) {
  141. warnf(config->global, "Binary output can mess up your terminal. "
  142. "Use \"--output -\" to tell curl to output it to your terminal "
  143. "anyway, or consider \"--output <FILE>\" to save to a file.\n");
  144. config->synthetic_error = ERR_BINARY_TERMINAL;
  145. return failure;
  146. }
  147. }
  148. #ifdef WIN32
  149. fhnd = _get_osfhandle(fileno(outs->stream));
  150. if(isatty(fileno(outs->stream)) &&
  151. GetConsoleScreenBufferInfo((HANDLE)fhnd, &console_info)) {
  152. DWORD in_len = (DWORD)(sz * nmemb);
  153. wchar_t* wc_buf;
  154. DWORD wc_len;
  155. /* calculate buffer size for wide characters */
  156. wc_len = MultiByteToWideChar(CP_UTF8, 0, buffer, in_len, NULL, 0);
  157. wc_buf = (wchar_t*) malloc(wc_len * sizeof(wchar_t));
  158. if(!wc_buf)
  159. return failure;
  160. /* calculate buffer size for multi-byte characters */
  161. wc_len = MultiByteToWideChar(CP_UTF8, 0, buffer, in_len, wc_buf, wc_len);
  162. if(!wc_len) {
  163. free(wc_buf);
  164. return failure;
  165. }
  166. if(!WriteConsoleW(
  167. (HANDLE) fhnd,
  168. wc_buf,
  169. wc_len,
  170. &wc_len,
  171. NULL)) {
  172. free(wc_buf);
  173. return failure;
  174. }
  175. free(wc_buf);
  176. rc = bytes;
  177. }
  178. else
  179. #endif
  180. rc = fwrite(buffer, sz, nmemb, outs->stream);
  181. if(bytes == rc)
  182. /* we added this amount of data to the output */
  183. outs->bytes += bytes;
  184. if(config->readbusy) {
  185. config->readbusy = FALSE;
  186. curl_easy_pause(per->curl, CURLPAUSE_CONT);
  187. }
  188. if(config->nobuffer) {
  189. /* output buffering disabled */
  190. int res = fflush(outs->stream);
  191. if(res)
  192. return failure;
  193. }
  194. return rc;
  195. }