Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/src/tool_cb_rea.c
2652 views
1
/***************************************************************************
2
* _ _ ____ _
3
* Project ___| | | | _ \| |
4
* / __| | | | |_) | |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
7
*
8
* Copyright (C) Daniel Stenberg, <[email protected]>, 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
#ifdef HAVE_POLL_H
30
#include <poll.h>
31
#elif defined(HAVE_SYS_POLL_H)
32
#include <sys/poll.h>
33
#endif
34
35
#include "tool_cfgable.h"
36
#include "tool_cb_rea.h"
37
#include "tool_operate.h"
38
#include "tool_util.h"
39
#include "tool_msgs.h"
40
41
#include "memdebug.h" /* keep this as LAST include */
42
43
#ifndef _WIN32
44
/* Wait up to a number of milliseconds for socket activity. This function
45
waits on read activity on a file descriptor that is not a socket which
46
makes it not work with select() or poll() on Windows. */
47
static bool waitfd(int waitms, int fd)
48
{
49
#ifdef HAVE_POLL
50
struct pollfd set;
51
52
set.fd = fd;
53
set.events = POLLIN;
54
set.revents = 0;
55
if(poll(&set, 1, (int)waitms))
56
return TRUE; /* timeout */
57
return FALSE;
58
#else
59
fd_set bits;
60
struct timeval timeout;
61
62
if(fd >= FD_SETSIZE)
63
/* can't wait! */
64
return FALSE;
65
66
/* wait this long at the most */
67
timeout.tv_sec = waitms / 1000;
68
timeout.tv_usec = (int)((waitms % 1000) * 1000);
69
70
FD_ZERO(&bits);
71
#ifdef __DJGPP__
72
#pragma GCC diagnostic push
73
#pragma GCC diagnostic ignored "-Warith-conversion"
74
#endif
75
FD_SET(fd, &bits);
76
#ifdef __DJGPP__
77
#pragma GCC diagnostic pop
78
#endif
79
if(!select(fd + 1, &bits, NULL, NULL, &timeout))
80
return TRUE; /* timeout */
81
return FALSE;
82
#endif
83
}
84
#endif
85
86
/*
87
** callback for CURLOPT_READFUNCTION
88
*/
89
90
size_t tool_read_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
91
{
92
ssize_t rc = 0;
93
struct per_transfer *per = userdata;
94
struct OperationConfig *config = per->config;
95
96
if((per->uploadfilesize != -1) &&
97
(per->uploadedsofar == per->uploadfilesize)) {
98
/* done */
99
return 0;
100
}
101
102
if(config->timeout_ms) {
103
struct curltime now = curlx_now();
104
long msdelta = (long)curlx_timediff(now, per->start);
105
106
if(msdelta > config->timeout_ms)
107
/* timeout */
108
return 0;
109
#ifndef _WIN32
110
else {
111
long w = config->timeout_ms - msdelta;
112
if(w > INT_MAX)
113
w = INT_MAX;
114
waitfd((int)w, per->infd);
115
}
116
#endif
117
}
118
119
/* If we are on Windows, and using `-T .`, then per->infd points to a socket
120
connected to stdin via a reader thread, and needs to be read with recv()
121
Make sure we are in non-blocking mode and infd is not regular stdin
122
On Linux per->infd should be stdin (0) and the block below should not
123
execute */
124
if(per->uploadfile && !strcmp(per->uploadfile, ".") && per->infd > 0) {
125
#if defined(_WIN32) && !defined(CURL_WINDOWS_UWP) && !defined(UNDER_CE)
126
rc = CURL_RECV(per->infd, buffer, curlx_uztosi(sz * nmemb), 0);
127
if(rc < 0) {
128
if(SOCKERRNO == SOCKEWOULDBLOCK) {
129
CURL_SETERRNO(0);
130
config->readbusy = TRUE;
131
return CURL_READFUNC_PAUSE;
132
}
133
134
rc = 0;
135
}
136
#else
137
warnf("per->infd != 0: FD == %d. This behavior"
138
" is only supported on desktop Windows", per->infd);
139
#endif
140
}
141
else {
142
rc = read(per->infd, buffer, sz*nmemb);
143
if(rc < 0) {
144
if(errno == EAGAIN) {
145
CURL_SETERRNO(0);
146
config->readbusy = TRUE;
147
return CURL_READFUNC_PAUSE;
148
}
149
/* since size_t is unsigned we cannot return negative values fine */
150
rc = 0;
151
}
152
}
153
if((per->uploadfilesize != -1) &&
154
(per->uploadedsofar + rc > per->uploadfilesize)) {
155
/* do not allow uploading more than originally set out to do */
156
curl_off_t delta = per->uploadedsofar + rc - per->uploadfilesize;
157
warnf("File size larger in the end than when "
158
"started. Dropping at least %" CURL_FORMAT_CURL_OFF_T " bytes",
159
delta);
160
rc = (ssize_t)(per->uploadfilesize - per->uploadedsofar);
161
}
162
config->readbusy = FALSE;
163
164
/* when select() returned zero here, it timed out */
165
return (size_t)rc;
166
}
167
168
/*
169
** callback for CURLOPT_XFERINFOFUNCTION used to unpause busy reads
170
*/
171
172
int tool_readbusy_cb(void *clientp,
173
curl_off_t dltotal, curl_off_t dlnow,
174
curl_off_t ultotal, curl_off_t ulnow)
175
{
176
struct per_transfer *per = clientp;
177
struct OperationConfig *config = per->config;
178
static curl_off_t ulprev;
179
180
(void)dltotal;
181
(void)dlnow;
182
(void)ultotal;
183
(void)ulnow;
184
185
if(config->readbusy) {
186
if(ulprev == ulnow) {
187
#ifndef _WIN32
188
waitfd(1, per->infd);
189
#else
190
/* sleep */
191
curlx_wait_ms(1);
192
#endif
193
}
194
195
config->readbusy = FALSE;
196
curl_easy_pause(per->curl, CURLPAUSE_CONT);
197
}
198
199
ulprev = ulnow;
200
201
return per->noprogress ? 0 : CURL_PROGRESSFUNC_CONTINUE;
202
}
203
204