1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
24 #include <curl/curl.h>
26 #define ENABLE_CURLX_PRINTF
27 /* use our own printf() functions */
30 #include "tool_cfgable.h"
31 #include "tool_msgs.h"
32 #include "tool_cb_wrt.h"
34 #include "memdebug.h" /* keep this as LAST include */
37 ** callback for CURLOPT_WRITEFUNCTION
40 size_t tool_write_cb(void *buffer, size_t sz, size_t nmemb, void *userdata)
43 struct OutStruct *outs = userdata;
44 struct Configurable *config = outs->config;
47 * Once that libcurl has called back tool_write_cb() the returned value
48 * is checked against the amount that was intended to be written, if
49 * it does not match then it fails with CURLE_WRITE_ERROR. So at this
50 * point returning a value different from sz*nmemb indicates failure.
52 const size_t failure = (sz * nmemb) ? 0 : 1;
58 if(config->include_headers) {
59 if(sz * nmemb > (size_t)CURL_MAX_HTTP_HEADER) {
60 warnf(config, "Data size exceeds single call write limit!\n");
65 if(sz * nmemb > (size_t)CURL_MAX_WRITE_SIZE) {
66 warnf(config, "Data size exceeds single call write limit!\n");
72 /* Some internal congruency checks on received OutStruct */
73 bool check_fails = FALSE;
80 if(outs->fopened && !outs->stream)
82 if(!outs->fopened && outs->stream)
84 if(!outs->fopened && outs->bytes)
89 if(!outs->stream || outs->s_isreg || outs->fopened)
91 if(outs->alloc_filename || outs->init)
95 warnf(config, "Invalid output struct data for write callback\n");
104 if(!outs->filename || !*outs->filename) {
105 warnf(config, "Remote filename has no length!\n");
109 if(config->content_disposition) {
110 /* don't overwrite existing files */
111 file = fopen(outs->filename, "rb");
114 warnf(config, "Refusing to overwrite %s: %s\n", outs->filename,
120 /* open file for writing */
121 file = fopen(outs->filename, "wb");
123 warnf(config, "Failed to create the file %s: %s\n", outs->filename,
127 outs->s_isreg = TRUE;
128 outs->fopened = TRUE;
134 rc = fwrite(buffer, sz, nmemb, outs->stream);
136 if((sz * nmemb) == rc)
137 /* we added this amount of data to the output */
138 outs->bytes += (sz * nmemb);
140 if(config->readbusy) {
141 config->readbusy = FALSE;
142 curl_easy_pause(config->easy, CURLPAUSE_CONT);
145 if(config->nobuffer) {
146 /* output buffering disabled */
147 int res = fflush(outs->stream);