xref: /aosp_15_r20/external/curl/docs/libcurl/opts/CURLOPT_SEEKFUNCTION.md (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker---
2*6236dae4SAndroid Build Coastguard Workerc: Copyright (C) Daniel Stenberg, <[email protected]>, et al.
3*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl
4*6236dae4SAndroid Build Coastguard WorkerTitle: CURLOPT_SEEKFUNCTION
5*6236dae4SAndroid Build Coastguard WorkerSection: 3
6*6236dae4SAndroid Build Coastguard WorkerSource: libcurl
7*6236dae4SAndroid Build Coastguard WorkerSee-also:
8*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_DEBUGFUNCTION (3)
9*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_IOCTLFUNCTION (3)
10*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_SEEKDATA (3)
11*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_STDERR (3)
12*6236dae4SAndroid Build Coastguard WorkerProtocol:
13*6236dae4SAndroid Build Coastguard Worker  - All
14*6236dae4SAndroid Build Coastguard WorkerAdded-in: 7.18.0
15*6236dae4SAndroid Build Coastguard Worker---
16*6236dae4SAndroid Build Coastguard Worker
17*6236dae4SAndroid Build Coastguard Worker# NAME
18*6236dae4SAndroid Build Coastguard Worker
19*6236dae4SAndroid Build Coastguard WorkerCURLOPT_SEEKFUNCTION - user callback for seeking in input stream
20*6236dae4SAndroid Build Coastguard Worker
21*6236dae4SAndroid Build Coastguard Worker# SYNOPSIS
22*6236dae4SAndroid Build Coastguard Worker
23*6236dae4SAndroid Build Coastguard Worker~~~c
24*6236dae4SAndroid Build Coastguard Worker#include <curl/curl.h>
25*6236dae4SAndroid Build Coastguard Worker
26*6236dae4SAndroid Build Coastguard Worker/* These are the return codes for the seek callbacks */
27*6236dae4SAndroid Build Coastguard Worker#define CURL_SEEKFUNC_OK       0
28*6236dae4SAndroid Build Coastguard Worker#define CURL_SEEKFUNC_FAIL     1 /* fail the entire transfer */
29*6236dae4SAndroid Build Coastguard Worker#define CURL_SEEKFUNC_CANTSEEK 2 /* tell libcurl seeking cannot be done, so
30*6236dae4SAndroid Build Coastguard Worker                                    libcurl might try other means instead */
31*6236dae4SAndroid Build Coastguard Worker
32*6236dae4SAndroid Build Coastguard Workerint seek_callback(void *clientp, curl_off_t offset, int origin);
33*6236dae4SAndroid Build Coastguard Worker
34*6236dae4SAndroid Build Coastguard WorkerCURLcode curl_easy_setopt(CURL *handle, CURLOPT_SEEKFUNCTION, seek_callback);
35*6236dae4SAndroid Build Coastguard Worker~~~
36*6236dae4SAndroid Build Coastguard Worker
37*6236dae4SAndroid Build Coastguard Worker# DESCRIPTION
38*6236dae4SAndroid Build Coastguard Worker
39*6236dae4SAndroid Build Coastguard WorkerPass a pointer to your callback function, which should match the prototype
40*6236dae4SAndroid Build Coastguard Workershown above.
41*6236dae4SAndroid Build Coastguard Worker
42*6236dae4SAndroid Build Coastguard WorkerThis function gets called by libcurl to seek to a certain position in the
43*6236dae4SAndroid Build Coastguard Workerinput stream and can be used to fast forward a file in a resumed upload
44*6236dae4SAndroid Build Coastguard Worker(instead of reading all uploaded bytes with the normal read
45*6236dae4SAndroid Build Coastguard Workerfunction/callback). It is also called to rewind a stream when data has already
46*6236dae4SAndroid Build Coastguard Workerbeen sent to the server and needs to be sent again. This may happen when doing
47*6236dae4SAndroid Build Coastguard Workeran HTTP PUT or POST with a multi-pass authentication method, or when an
48*6236dae4SAndroid Build Coastguard Workerexisting HTTP connection is reused too late and the server closes the
49*6236dae4SAndroid Build Coastguard Workerconnection. The function shall work like fseek(3) or lseek(3) and it gets
50*6236dae4SAndroid Build Coastguard WorkerSEEK_SET, SEEK_CUR or SEEK_END as argument for *origin*, although libcurl
51*6236dae4SAndroid Build Coastguard Workercurrently only passes SEEK_SET.
52*6236dae4SAndroid Build Coastguard Worker
53*6236dae4SAndroid Build Coastguard Worker*clientp* is the pointer you set with CURLOPT_SEEKDATA(3).
54*6236dae4SAndroid Build Coastguard Worker
55*6236dae4SAndroid Build Coastguard WorkerThe callback function must return *CURL_SEEKFUNC_OK* on success,
56*6236dae4SAndroid Build Coastguard Worker*CURL_SEEKFUNC_FAIL* to cause the upload operation to fail or
57*6236dae4SAndroid Build Coastguard Worker*CURL_SEEKFUNC_CANTSEEK* to indicate that while the seek failed, libcurl
58*6236dae4SAndroid Build Coastguard Workeris free to work around the problem if possible. The latter can sometimes be
59*6236dae4SAndroid Build Coastguard Workerdone by instead reading from the input or similar.
60*6236dae4SAndroid Build Coastguard Worker
61*6236dae4SAndroid Build Coastguard WorkerIf you forward the input arguments directly to fseek(3) or lseek(3), note that
62*6236dae4SAndroid Build Coastguard Workerthe data type for *offset* is not the same as defined for curl_off_t on
63*6236dae4SAndroid Build Coastguard Workermany systems.
64*6236dae4SAndroid Build Coastguard Worker
65*6236dae4SAndroid Build Coastguard Worker# DEFAULT
66*6236dae4SAndroid Build Coastguard Worker
67*6236dae4SAndroid Build Coastguard WorkerNULL
68*6236dae4SAndroid Build Coastguard Worker
69*6236dae4SAndroid Build Coastguard Worker# %PROTOCOLS%
70*6236dae4SAndroid Build Coastguard Worker
71*6236dae4SAndroid Build Coastguard Worker# EXAMPLE
72*6236dae4SAndroid Build Coastguard Worker
73*6236dae4SAndroid Build Coastguard Worker~~~c
74*6236dae4SAndroid Build Coastguard Worker#include <unistd.h> /* for lseek */
75*6236dae4SAndroid Build Coastguard Worker
76*6236dae4SAndroid Build Coastguard Workerstruct data {
77*6236dae4SAndroid Build Coastguard Worker  int our_fd;
78*6236dae4SAndroid Build Coastguard Worker};
79*6236dae4SAndroid Build Coastguard Workerstatic int seek_cb(void *clientp, curl_off_t offset, int origin)
80*6236dae4SAndroid Build Coastguard Worker{
81*6236dae4SAndroid Build Coastguard Worker  struct data *d = (struct data *)clientp;
82*6236dae4SAndroid Build Coastguard Worker  lseek(d->our_fd, offset, origin);
83*6236dae4SAndroid Build Coastguard Worker  return CURL_SEEKFUNC_OK;
84*6236dae4SAndroid Build Coastguard Worker}
85*6236dae4SAndroid Build Coastguard Worker
86*6236dae4SAndroid Build Coastguard Workerint main(void)
87*6236dae4SAndroid Build Coastguard Worker{
88*6236dae4SAndroid Build Coastguard Worker  struct data seek_data;
89*6236dae4SAndroid Build Coastguard Worker  CURL *curl = curl_easy_init();
90*6236dae4SAndroid Build Coastguard Worker  if(curl) {
91*6236dae4SAndroid Build Coastguard Worker    curl_easy_setopt(curl, CURLOPT_SEEKFUNCTION, seek_cb);
92*6236dae4SAndroid Build Coastguard Worker    curl_easy_setopt(curl, CURLOPT_SEEKDATA, &seek_data);
93*6236dae4SAndroid Build Coastguard Worker  }
94*6236dae4SAndroid Build Coastguard Worker}
95*6236dae4SAndroid Build Coastguard Worker~~~
96*6236dae4SAndroid Build Coastguard Worker
97*6236dae4SAndroid Build Coastguard Worker# %AVAILABILITY%
98*6236dae4SAndroid Build Coastguard Worker
99*6236dae4SAndroid Build Coastguard Worker# RETURN VALUE
100*6236dae4SAndroid Build Coastguard Worker
101*6236dae4SAndroid Build Coastguard WorkerReturns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not.
102