Imported Upstream version 7.44.0
[platform/upstream/curl.git] / src / tool_cb_wrt.c
index dfbf95c..9be393f 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
 
 #include "memdebug.h" /* keep this as LAST include */
 
+/* create a local file for writing, return TRUE on success */
+bool tool_create_output_file(struct OutStruct *outs)
+{
+  struct GlobalConfig *global = outs->config->global;
+  FILE *file;
+
+  if(!outs->filename || !*outs->filename) {
+    warnf(global, "Remote filename has no length!\n");
+    return FALSE;
+  }
+
+  if(outs->is_cd_filename) {
+    /* don't overwrite existing files */
+    file = fopen(outs->filename, "rb");
+    if(file) {
+      fclose(file);
+      warnf(global, "Refusing to overwrite %s: %s\n", outs->filename,
+            strerror(EEXIST));
+      return FALSE;
+    }
+  }
+
+  /* open file for writing */
+  file = fopen(outs->filename, "wb");
+  if(!file) {
+    warnf(global, "Failed to create the file %s: %s\n", outs->filename,
+          strerror(errno));
+    return FALSE;
+  }
+  outs->s_isreg = TRUE;
+  outs->fopened = TRUE;
+  outs->stream = file;
+  outs->bytes = 0;
+  outs->init = 0;
+  return TRUE;
+}
+
 /*
 ** callback for CURLOPT_WRITEFUNCTION
 */
@@ -55,13 +92,14 @@ size_t tool_write_cb(void *buffer, size_t sz, size_t nmemb, void *userdata)
 #ifdef DEBUGBUILD
   if(config->include_headers) {
     if(sz * nmemb > (size_t)CURL_MAX_HTTP_HEADER) {
-      warnf(config, "Header data size exceeds single call write limit!\n");
+      warnf(config->global, "Header data size exceeds single call write "
+            "limit!\n");
       return failure;
     }
   }
   else {
     if(sz * nmemb > (size_t)CURL_MAX_WRITE_SIZE) {
-      warnf(config, "Data size exceeds single call write limit!\n");
+      warnf(config->global, "Data size exceeds single call write limit!\n");
       return failure;
     }
   }
@@ -90,44 +128,14 @@ size_t tool_write_cb(void *buffer, size_t sz, size_t nmemb, void *userdata)
         check_fails = TRUE;
     }
     if(check_fails) {
-      warnf(config, "Invalid output struct data for write callback\n");
+      warnf(config->global, "Invalid output struct data for write callback\n");
       return failure;
     }
   }
 #endif
 
-  if(!outs->stream) {
-    FILE *file;
-
-    if(!outs->filename || !*outs->filename) {
-      warnf(config, "Remote filename has no length!\n");
-      return failure;
-    }
-
-    if(outs->is_cd_filename) {
-      /* don't overwrite existing files */
-      file = fopen(outs->filename, "rb");
-      if(file) {
-        fclose(file);
-        warnf(config, "Refusing to overwrite %s: %s\n", outs->filename,
-              strerror(EEXIST));
-        return failure;
-      }
-    }
-
-    /* open file for writing */
-    file = fopen(outs->filename, "wb");
-    if(!file) {
-      warnf(config, "Failed to create the file %s: %s\n", outs->filename,
-            strerror(errno));
-      return failure;
-    }
-    outs->s_isreg = TRUE;
-    outs->fopened = TRUE;
-    outs->stream = file;
-    outs->bytes = 0;
-    outs->init = 0;
-  }
+  if(!outs->stream && !tool_create_output_file(outs))
+    return failure;
 
   rc = fwrite(buffer, sz, nmemb, outs->stream);
 
@@ -149,4 +157,3 @@ size_t tool_write_cb(void *buffer, size_t sz, size_t nmemb, void *userdata)
 
   return rc;
 }
-