1---
2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al.
3SPDX-License-Identifier: curl
4Title: CURLOPT_SEEKFUNCTION
5Section: 3
6Source: libcurl
7See-also:
8  - CURLOPT_DEBUGFUNCTION (3)
9  - CURLOPT_IOCTLFUNCTION (3)
10  - CURLOPT_SEEKDATA (3)
11  - CURLOPT_STDERR (3)
12---
13
14# NAME
15
16CURLOPT_SEEKFUNCTION - user callback for seeking in input stream
17
18# SYNOPSIS
19
20~~~c
21#include <curl/curl.h>
22
23/* These are the return codes for the seek callbacks */
24#define CURL_SEEKFUNC_OK       0
25#define CURL_SEEKFUNC_FAIL     1 /* fail the entire transfer */
26#define CURL_SEEKFUNC_CANTSEEK 2 /* tell libcurl seeking cannot be done, so
27                                    libcurl might try other means instead */
28
29int seek_callback(void *clientp, curl_off_t offset, int origin);
30
31CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SEEKFUNCTION, seek_callback);
32~~~
33
34# DESCRIPTION
35
36Pass a pointer to your callback function, which should match the prototype
37shown above.
38
39This function gets called by libcurl to seek to a certain position in the
40input stream and can be used to fast forward a file in a resumed upload
41(instead of reading all uploaded bytes with the normal read
42function/callback). It is also called to rewind a stream when data has already
43been sent to the server and needs to be sent again. This may happen when doing
44an HTTP PUT or POST with a multi-pass authentication method, or when an
45existing HTTP connection is reused too late and the server closes the
46connection. The function shall work like fseek(3) or lseek(3) and it gets
47SEEK_SET, SEEK_CUR or SEEK_END as argument for *origin*, although libcurl
48currently only passes SEEK_SET.
49
50*clientp* is the pointer you set with CURLOPT_SEEKDATA(3).
51
52The callback function must return *CURL_SEEKFUNC_OK* on success,
53*CURL_SEEKFUNC_FAIL* to cause the upload operation to fail or
54*CURL_SEEKFUNC_CANTSEEK* to indicate that while the seek failed, libcurl
55is free to work around the problem if possible. The latter can sometimes be
56done by instead reading from the input or similar.
57
58If you forward the input arguments directly to fseek(3) or lseek(3), note that
59the data type for *offset* is not the same as defined for curl_off_t on
60many systems!
61
62# DEFAULT
63
64By default, this is NULL and unused.
65
66# PROTOCOLS
67
68HTTP, FTP, SFTP
69
70# EXAMPLE
71
72~~~c
73#include <unistd.h> /* for lseek */
74
75struct data {
76  int our_fd;
77};
78static int seek_cb(void *clientp, curl_off_t offset, int origin)
79{
80  struct data *d = (struct data *)clientp;
81  lseek(d->our_fd, offset, origin);
82  return CURL_SEEKFUNC_OK;
83}
84
85int main(void)
86{
87  struct data seek_data;
88  CURL *curl = curl_easy_init();
89  if(curl) {
90    curl_easy_setopt(curl, CURLOPT_SEEKFUNCTION, seek_cb);
91    curl_easy_setopt(curl, CURLOPT_SEEKDATA, &seek_data);
92  }
93}
94~~~
95
96# AVAILABILITY
97
98Added in 7.18.0
99
100# RETURN VALUE
101
102Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not.
103