summaryrefslogtreecommitdiff
path: root/src/tool_cb_wrt.c
diff options
context:
space:
mode:
authorKamil Dudka <kdudka@redhat.com>2015-03-30 13:45:22 +0200
committerKamil Dudka <kdudka@redhat.com>2015-04-08 09:36:56 +0200
commitf251417d85d232605ca86e9562a64500c67ccdee (patch)
tree85031ca818b7e326ece175bbed6de0ec71a1a18e /src/tool_cb_wrt.c
parenta9e46749b2a23257674974ac2a88738905bac5e1 (diff)
downloadcurl-f251417d85d232605ca86e9562a64500c67ccdee.tar.gz
src/tool_cb_wrt: separate fnc for output file creation
Diffstat (limited to 'src/tool_cb_wrt.c')
-rw-r--r--src/tool_cb_wrt.c71
1 files changed, 39 insertions, 32 deletions
diff --git a/src/tool_cb_wrt.c b/src/tool_cb_wrt.c
index 17d0409f2..9be393ff2 100644
--- a/src/tool_cb_wrt.c
+++ b/src/tool_cb_wrt.c
@@ -31,6 +31,43 @@
#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
*/
@@ -97,38 +134,8 @@ size_t tool_write_cb(void *buffer, size_t sz, size_t nmemb, void *userdata)
}
#endif
- if(!outs->stream) {
- FILE *file;
-
- if(!outs->filename || !*outs->filename) {
- warnf(config->global, "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->global, "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->global, "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);