1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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 ***************************************************************************/
22 #include "tool_setup.h"
24 #define ENABLE_CURLX_PRINTF
25 /* use our own printf() functions */
28 #include "tool_cfgable.h"
29 #include "tool_msgs.h"
30 #include "tool_cb_wrt.h"
32 #include "memdebug.h" /* keep this as LAST include */
35 ** callback for CURLOPT_WRITEFUNCTION
38 size_t tool_write_cb(void *buffer, size_t sz, size_t nmemb, void *userdata)
41 struct OutStruct *outs = userdata;
42 struct OperationConfig *config = outs->config;
45 * Once that libcurl has called back tool_write_cb() the returned value
46 * is checked against the amount that was intended to be written, if
47 * it does not match then it fails with CURLE_WRITE_ERROR. So at this
48 * point returning a value different from sz*nmemb indicates failure.
50 const size_t failure = (sz * nmemb) ? 0 : 1;
56 if(config->include_headers) {
57 if(sz * nmemb > (size_t)CURL_MAX_HTTP_HEADER) {
58 warnf(config, "Header data size exceeds single call write limit!\n");
63 if(sz * nmemb > (size_t)CURL_MAX_WRITE_SIZE) {
64 warnf(config, "Data size exceeds single call write limit!\n");
70 /* Some internal congruency checks on received OutStruct */
71 bool check_fails = FALSE;
78 if(outs->fopened && !outs->stream)
80 if(!outs->fopened && outs->stream)
82 if(!outs->fopened && outs->bytes)
87 if(!outs->stream || outs->s_isreg || outs->fopened)
89 if(outs->alloc_filename || outs->is_cd_filename || outs->init)
93 warnf(config, "Invalid output struct data for write callback\n");
102 if(!outs->filename || !*outs->filename) {
103 warnf(config, "Remote filename has no length!\n");
107 if(outs->is_cd_filename) {
108 /* don't overwrite existing files */
109 file = fopen(outs->filename, "rb");
112 warnf(config, "Refusing to overwrite %s: %s\n", outs->filename,
118 /* open file for writing */
119 file = fopen(outs->filename, "wb");
121 warnf(config, "Failed to create the file %s: %s\n", outs->filename,
125 outs->s_isreg = TRUE;
126 outs->fopened = TRUE;
132 rc = fwrite(buffer, sz, nmemb, outs->stream);
134 if((sz * nmemb) == rc)
135 /* we added this amount of data to the output */
136 outs->bytes += (sz * nmemb);
138 if(config->readbusy) {
139 config->readbusy = FALSE;
140 curl_easy_pause(config->easy, CURLPAUSE_CONT);
143 if(config->nobuffer) {
144 /* output buffering disabled */
145 int res = fflush(outs->stream);