1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2020, 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  ***************************************************************************/
22 #include "tool_setup.h"
23 
24 #define ENABLE_CURLX_PRINTF
25 /* use our own printf() functions */
26 #include "curlx.h"
27 
28 #include "tool_cfgable.h"
29 #include "tool_cb_rea.h"
30 #include "tool_operate.h"
31 
32 #include "memdebug.h" /* keep this as LAST include */
33 
34 /*
35 ** callback for CURLOPT_READFUNCTION
36 */
37 
tool_read_cb(char * buffer,size_t sz,size_t nmemb,void * userdata)38 size_t tool_read_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
39 {
40   ssize_t rc;
41   struct InStruct *in = userdata;
42 
43   rc = read(in->fd, buffer, sz*nmemb);
44   if(rc < 0) {
45     if(errno == EAGAIN) {
46       errno = 0;
47       in->config->readbusy = TRUE;
48       return CURL_READFUNC_PAUSE;
49     }
50     /* since size_t is unsigned we can't return negative values fine */
51     rc = 0;
52   }
53   in->config->readbusy = FALSE;
54   return (size_t)rc;
55 }
56 
57 /*
58 ** callback for CURLOPT_XFERINFOFUNCTION used to unpause busy reads
59 */
60 
tool_readbusy_cb(void * clientp,curl_off_t dltotal,curl_off_t dlnow,curl_off_t ultotal,curl_off_t ulnow)61 int tool_readbusy_cb(void *clientp,
62                      curl_off_t dltotal, curl_off_t dlnow,
63                      curl_off_t ultotal, curl_off_t ulnow)
64 {
65   struct per_transfer *per = clientp;
66   struct OperationConfig *config = per->config;
67 
68   (void)dltotal;  /* unused */
69   (void)dlnow;  /* unused */
70   (void)ultotal;  /* unused */
71   (void)ulnow;  /* unused */
72 
73   if(config->readbusy) {
74     config->readbusy = FALSE;
75     curl_easy_pause(per->curl, CURLPAUSE_CONT);
76   }
77 
78   return per->noprogress? 0 : CURL_PROGRESSFUNC_CONTINUE;
79 }
80