1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
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.
13 *
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.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22#include "tool_setup.h"
23
24#define ENABLE_CURLX_PRINTF
25/* use our own printf() functions */
26#include "curlx.h"
27
28#include "tool_cfgable.h"
29#include "tool_msgs.h"
30#include "tool_cb_wrt.h"
31
32#include "memdebug.h" /* keep this as LAST include */
33
34/*
35** callback for CURLOPT_WRITEFUNCTION
36*/
37
38size_t tool_write_cb(void *buffer, size_t sz, size_t nmemb, void *userdata)
39{
40  size_t rc;
41  struct OutStruct *outs = userdata;
42  struct Configurable *config = outs->config;
43
44  /*
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.
49   */
50  const size_t failure = (sz * nmemb) ? 0 : 1;
51
52  if(!config)
53    return failure;
54
55#ifdef DEBUGBUILD
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");
59      return failure;
60    }
61  }
62  else {
63    if(sz * nmemb > (size_t)CURL_MAX_WRITE_SIZE) {
64      warnf(config, "Data size exceeds single call write limit!\n");
65      return failure;
66    }
67  }
68
69  {
70    /* Some internal congruency checks on received OutStruct */
71    bool check_fails = FALSE;
72    if(outs->filename) {
73      /* regular file */
74      if(!*outs->filename)
75        check_fails = TRUE;
76      if(!outs->s_isreg)
77        check_fails = TRUE;
78      if(outs->fopened && !outs->stream)
79        check_fails = TRUE;
80      if(!outs->fopened && outs->stream)
81        check_fails = TRUE;
82      if(!outs->fopened && outs->bytes)
83        check_fails = TRUE;
84    }
85    else {
86      /* standard stream */
87      if(!outs->stream || outs->s_isreg || outs->fopened)
88        check_fails = TRUE;
89      if(outs->alloc_filename || outs->is_cd_filename || outs->init)
90        check_fails = TRUE;
91    }
92    if(check_fails) {
93      warnf(config, "Invalid output struct data for write callback\n");
94      return failure;
95    }
96  }
97#endif
98
99  if(!outs->stream) {
100    FILE *file;
101
102    if(!outs->filename || !*outs->filename) {
103      warnf(config, "Remote filename has no length!\n");
104      return failure;
105    }
106
107    if(outs->is_cd_filename) {
108      /* don't overwrite existing files */
109      file = fopen(outs->filename, "rb");
110      if(file) {
111        fclose(file);
112        warnf(config, "Refusing to overwrite %s: %s\n", outs->filename,
113              strerror(EEXIST));
114        return failure;
115      }
116    }
117
118    /* open file for writing */
119    file = fopen(outs->filename, "wb");
120    if(!file) {
121      warnf(config, "Failed to create the file %s: %s\n", outs->filename,
122            strerror(errno));
123      return failure;
124    }
125    outs->s_isreg = TRUE;
126    outs->fopened = TRUE;
127    outs->stream = file;
128    outs->bytes = 0;
129    outs->init = 0;
130  }
131
132  rc = fwrite(buffer, sz, nmemb, outs->stream);
133
134  if((sz * nmemb) == rc)
135    /* we added this amount of data to the output */
136    outs->bytes += (sz * nmemb);
137
138  if(config->readbusy) {
139    config->readbusy = FALSE;
140    curl_easy_pause(config->easy, CURLPAUSE_CONT);
141  }
142
143  if(config->nobuffer) {
144    /* output buffering disabled */
145    int res = fflush(outs->stream);
146    if(res)
147      return failure;
148  }
149
150  return rc;
151}
152
153