1---
2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al.
3SPDX-License-Identifier: curl
4Title: CURLOPT_CHUNK_BGN_FUNCTION
5Section: 3
6Source: libcurl
7See-also:
8  - CURLOPT_CHUNK_END_FUNCTION (3)
9  - CURLOPT_WILDCARDMATCH (3)
10---
11
12# NAME
13
14CURLOPT_CHUNK_BGN_FUNCTION - callback before a transfer with FTP wildcard match
15
16# SYNOPSIS
17
18~~~c
19#include <curl/curl.h>
20
21struct curl_fileinfo {
22  char *filename;
23  curlfiletype filetype;
24  time_t time;   /* always zero! */
25  unsigned int perm;
26  int uid;
27  int gid;
28  curl_off_t size;
29  long int hardlinks;
30
31  struct {
32    /* If some of these fields is not NULL, it is a pointer to b_data. */
33    char *time;
34    char *perm;
35    char *user;
36    char *group;
37    char *target; /* pointer to the target filename of a symlink */
38  } strings;
39
40  unsigned int flags;
41
42  /* used internally */
43  char *b_data;
44  size_t b_size;
45  size_t b_used;
46};
47
48long chunk_bgn_callback(const void *transfer_info, void *ptr,
49                        int remains);
50
51CURLcode curl_easy_setopt(CURL *handle, CURLOPT_CHUNK_BGN_FUNCTION,
52                          chunk_bgn_callback);
53~~~
54
55# DESCRIPTION
56
57Pass a pointer to your callback function, which should match the prototype
58shown above.
59
60This callback function gets called by libcurl before a part of the stream is
61going to be transferred (if the transfer supports chunks).
62
63The *transfer_info* pointer points to a **curl_fileinfo** struct with
64details about the file that is about to get transferred.
65
66This callback makes sense only when using the CURLOPT_WILDCARDMATCH(3)
67option for now.
68
69The target of transfer_info parameter is a "feature depended" structure. For
70the FTP wildcard download, the target is **curl_fileinfo** structure (see
71*curl/curl.h*). The parameter *ptr* is a pointer given by
72CURLOPT_CHUNK_DATA(3). The parameter remains contains number of chunks
73remaining per the transfer. If the feature is not available, the parameter has
74zero value.
75
76Return *CURL_CHUNK_BGN_FUNC_OK* if everything is fine,
77*CURL_CHUNK_BGN_FUNC_SKIP* if you want to skip the concrete chunk or
78*CURL_CHUNK_BGN_FUNC_FAIL* to tell libcurl to stop if some error occurred.
79
80# DEFAULT
81
82NULL
83
84# PROTOCOLS
85
86FTP
87
88# EXAMPLE
89
90~~~c
91#include <stdio.h>
92
93struct callback_data {
94   FILE *output;
95};
96
97static long file_is_coming(struct curl_fileinfo *finfo,
98                           void *ptr,
99                           int remains)
100{
101  struct callback_data *data = ptr;
102  printf("%3d %40s %10luB ", remains, finfo->filename,
103         (unsigned long)finfo->size);
104
105  switch(finfo->filetype) {
106  case CURLFILETYPE_DIRECTORY:
107    printf(" DIR\n");
108    break;
109  case CURLFILETYPE_FILE:
110    printf("FILE ");
111    break;
112  default:
113    printf("OTHER\n");
114    break;
115  }
116
117  if(finfo->filetype == CURLFILETYPE_FILE) {
118    /* do not transfer files >= 50B */
119    if(finfo->size > 50) {
120      printf("SKIPPED\n");
121      return CURL_CHUNK_BGN_FUNC_SKIP;
122    }
123
124    data->output = fopen(finfo->filename, "wb");
125    if(!data->output) {
126      return CURL_CHUNK_BGN_FUNC_FAIL;
127    }
128  }
129
130  return CURL_CHUNK_BGN_FUNC_OK;
131}
132
133int main()
134{
135  /* data for callback */
136  struct callback_data callback_info;
137
138  CURL *curl = curl_easy_init();
139
140  /* callback is called before download of concrete file started */
141  curl_easy_setopt(curl, CURLOPT_CHUNK_BGN_FUNCTION, file_is_coming);
142  curl_easy_setopt(curl, CURLOPT_CHUNK_DATA, &callback_info);
143}
144~~~
145
146# AVAILABILITY
147
148This was added in 7.21.0
149
150# RETURN VALUE
151
152Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not.
153