tool_cb_rea.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "tool_setup.h"
  25. #ifdef HAVE_SYS_SELECT_H
  26. #include <sys/select.h>
  27. #endif
  28. #define ENABLE_CURLX_PRINTF
  29. /* use our own printf() functions */
  30. #include "curlx.h"
  31. #include "tool_cfgable.h"
  32. #include "tool_cb_rea.h"
  33. #include "tool_operate.h"
  34. #include "tool_util.h"
  35. #include "tool_msgs.h"
  36. #include "memdebug.h" /* keep this as LAST include */
  37. /*
  38. ** callback for CURLOPT_READFUNCTION
  39. */
  40. size_t tool_read_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
  41. {
  42. ssize_t rc = 0;
  43. struct per_transfer *per = userdata;
  44. struct OperationConfig *config = per->config;
  45. if((per->uploadfilesize != -1) &&
  46. (per->uploadedsofar == per->uploadfilesize)) {
  47. /* done */
  48. return 0;
  49. }
  50. if(config->timeout_ms) {
  51. struct timeval now = tvnow();
  52. long msdelta = tvdiff(now, per->start);
  53. if(msdelta > config->timeout_ms)
  54. /* timeout */
  55. return 0;
  56. #ifndef _WIN32
  57. /* this logic waits on read activity on a file descriptor that is not a
  58. socket which makes it not work with select() on Windows */
  59. else {
  60. fd_set bits;
  61. struct timeval timeout;
  62. long wait = config->timeout_ms - msdelta;
  63. /* wait this long at the most */
  64. timeout.tv_sec = wait/1000;
  65. timeout.tv_usec = (int)((wait%1000)*1000);
  66. FD_ZERO(&bits);
  67. FD_SET(per->infd, &bits);
  68. if(!select(per->infd + 1, &bits, NULL, NULL, &timeout))
  69. return 0; /* timeout */
  70. }
  71. #endif
  72. }
  73. rc = read(per->infd, buffer, sz*nmemb);
  74. if(rc < 0) {
  75. if(errno == EAGAIN) {
  76. errno = 0;
  77. config->readbusy = TRUE;
  78. return CURL_READFUNC_PAUSE;
  79. }
  80. /* since size_t is unsigned we can't return negative values fine */
  81. rc = 0;
  82. }
  83. if((per->uploadfilesize != -1) &&
  84. (per->uploadedsofar + rc > per->uploadfilesize)) {
  85. /* do not allow uploading more than originally set out to do */
  86. curl_off_t delta = per->uploadedsofar + rc - per->uploadfilesize;
  87. warnf(per->config->global, "File size larger in the end than when "
  88. "started. Dropping at least %" CURL_FORMAT_CURL_OFF_T " bytes",
  89. delta);
  90. rc = (ssize_t)(per->uploadfilesize - per->uploadedsofar);
  91. }
  92. config->readbusy = FALSE;
  93. /* when select() returned zero here, it timed out */
  94. return (size_t)rc;
  95. }
  96. /*
  97. ** callback for CURLOPT_XFERINFOFUNCTION used to unpause busy reads
  98. */
  99. int tool_readbusy_cb(void *clientp,
  100. curl_off_t dltotal, curl_off_t dlnow,
  101. curl_off_t ultotal, curl_off_t ulnow)
  102. {
  103. struct per_transfer *per = clientp;
  104. struct OperationConfig *config = per->config;
  105. (void)dltotal; /* unused */
  106. (void)dlnow; /* unused */
  107. (void)ultotal; /* unused */
  108. (void)ulnow; /* unused */
  109. if(config->readbusy) {
  110. config->readbusy = FALSE;
  111. curl_easy_pause(per->curl, CURLPAUSE_CONT);
  112. }
  113. return per->noprogress? 0 : CURL_PROGRESSFUNC_CONTINUE;
  114. }