xref: /curl/src/tool_cb_rea.c (revision 5f4aaf8b)
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 
26 #ifdef HAVE_SYS_SELECT_H
27 #include <sys/select.h>
28 #endif
29 
30 #define ENABLE_CURLX_PRINTF
31 /* use our own printf() functions */
32 #include "curlx.h"
33 
34 #include "tool_cfgable.h"
35 #include "tool_cb_rea.h"
36 #include "tool_operate.h"
37 #include "tool_util.h"
38 #include "tool_msgs.h"
39 #include "tool_sleep.h"
40 
41 #include "memdebug.h" /* keep this as LAST include */
42 
43 /*
44 ** callback for CURLOPT_READFUNCTION
45 */
46 
tool_read_cb(char * buffer,size_t sz,size_t nmemb,void * userdata)47 size_t tool_read_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
48 {
49   ssize_t rc = 0;
50   struct per_transfer *per = userdata;
51   struct OperationConfig *config = per->config;
52 
53   if((per->uploadfilesize != -1) &&
54      (per->uploadedsofar == per->uploadfilesize)) {
55     /* done */
56     return 0;
57   }
58 
59   if(config->timeout_ms) {
60     struct timeval now = tvnow();
61     long msdelta = tvdiff(now, per->start);
62 
63     if(msdelta > config->timeout_ms)
64       /* timeout */
65       return 0;
66 #ifndef _WIN32
67     /* this logic waits on read activity on a file descriptor that is not a
68        socket which makes it not work with select() on Windows */
69     else {
70       fd_set bits;
71       struct timeval timeout;
72       long wait = config->timeout_ms - msdelta;
73 
74       /* wait this long at the most */
75       timeout.tv_sec = wait/1000;
76       timeout.tv_usec = (int)((wait%1000)*1000);
77 
78       FD_ZERO(&bits);
79       FD_SET(per->infd, &bits);
80       if(!select(per->infd + 1, &bits, NULL, NULL, &timeout))
81         return 0; /* timeout */
82     }
83 #endif
84   }
85 
86   rc = read(per->infd, buffer, sz*nmemb);
87   if(rc < 0) {
88     if(errno == EAGAIN) {
89       errno = 0;
90       config->readbusy = TRUE;
91       return CURL_READFUNC_PAUSE;
92     }
93     /* since size_t is unsigned we can't return negative values fine */
94     rc = 0;
95   }
96   if((per->uploadfilesize != -1) &&
97      (per->uploadedsofar + rc > per->uploadfilesize)) {
98     /* do not allow uploading more than originally set out to do */
99     curl_off_t delta = per->uploadedsofar + rc - per->uploadfilesize;
100     warnf(per->config->global, "File size larger in the end than when "
101           "started. Dropping at least %" CURL_FORMAT_CURL_OFF_T " bytes",
102           delta);
103     rc = (ssize_t)(per->uploadfilesize - per->uploadedsofar);
104   }
105   config->readbusy = FALSE;
106 
107   /* when select() returned zero here, it timed out */
108   return (size_t)rc;
109 }
110 
111 /*
112 ** callback for CURLOPT_XFERINFOFUNCTION used to unpause busy reads
113 */
114 
tool_readbusy_cb(void * clientp,curl_off_t dltotal,curl_off_t dlnow,curl_off_t ultotal,curl_off_t ulnow)115 int tool_readbusy_cb(void *clientp,
116                      curl_off_t dltotal, curl_off_t dlnow,
117                      curl_off_t ultotal, curl_off_t ulnow)
118 {
119   struct per_transfer *per = clientp;
120   struct OperationConfig *config = per->config;
121 
122   (void)dltotal;  /* unused */
123   (void)dlnow;  /* unused */
124   (void)ultotal;  /* unused */
125   (void)ulnow;  /* unused */
126 
127   if(config->readbusy) {
128     /* lame code to keep the rate down because the input might not deliver
129        anything, get paused again and come back here immediately */
130     static long rate = 500;
131     static struct timeval prev;
132     static curl_off_t ulprev;
133 
134     if(ulprev == ulnow) {
135       /* it did not upload anything since last call */
136       struct timeval now = tvnow();
137       if(prev.tv_sec)
138         /* get a rolling average rate */
139         /* rate = rate - rate/4 + tvdiff(now, prev)/4; */
140         rate -= rate/4 - tvdiff(now, prev)/4;
141       prev = now;
142     }
143     else {
144       rate = 50;
145       ulprev = ulnow;
146     }
147     if(rate >= 50) {
148       /* keeps the looping down to 20 times per second in the crazy case */
149       config->readbusy = FALSE;
150       curl_easy_pause(per->curl, CURLPAUSE_CONT);
151     }
152     else
153       /* sleep half a period */
154       tool_go_sleep(25);
155   }
156 
157   return per->noprogress? 0 : CURL_PROGRESSFUNC_CONTINUE;
158 }
159