1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <[email protected]>, 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 https://curl.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 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24 /* <DESC>
25 * Upload to FTP, resuming failed transfers. Active mode.
26 * </DESC>
27 */
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <curl/curl.h>
32
33 /* parse headers for Content-Length */
getcontentlengthfunc(void * ptr,size_t size,size_t nmemb,void * stream)34 static size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb,
35 void *stream)
36 {
37 int r;
38 long len = 0;
39
40 r = sscanf(ptr, "Content-Length: %ld\n", &len);
41 if(r)
42 *((long *) stream) = len;
43
44 return size * nmemb;
45 }
46
47 /* discard downloaded data */
discardfunc(void * ptr,size_t size,size_t nmemb,void * stream)48 static size_t discardfunc(void *ptr, size_t size, size_t nmemb, void *stream)
49 {
50 (void)ptr;
51 (void)stream;
52 return size * nmemb;
53 }
54
55 /* read data to upload */
readfunc(char * ptr,size_t size,size_t nmemb,void * stream)56 static size_t readfunc(char *ptr, size_t size, size_t nmemb, void *stream)
57 {
58 FILE *f = stream;
59 size_t n;
60
61 if(ferror(f))
62 return CURL_READFUNC_ABORT;
63
64 n = fread(ptr, size, nmemb, f) * size;
65
66 return n;
67 }
68
69
upload(CURL * curlhandle,const char * remotepath,const char * localpath,long timeout,long tries)70 static int upload(CURL *curlhandle, const char *remotepath,
71 const char *localpath, long timeout, long tries)
72 {
73 FILE *f;
74 long uploaded_len = 0;
75 CURLcode r = CURLE_GOT_NOTHING;
76 int c;
77
78 f = fopen(localpath, "rb");
79 if(!f) {
80 perror(NULL);
81 return 0;
82 }
83
84 curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
85
86 curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
87
88 if(timeout)
89 curl_easy_setopt(curlhandle, CURLOPT_SERVER_RESPONSE_TIMEOUT, timeout);
90
91 curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
92 curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);
93
94 curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);
95
96 curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
97 curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
98
99 /* enable active mode */
100 curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-");
101
102 /* allow the server no more than 7 seconds to connect back */
103 curl_easy_setopt(curlhandle, CURLOPT_ACCEPTTIMEOUT_MS, 7000L);
104
105 curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
106
107 curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
108
109 for(c = 0; (r != CURLE_OK) && (c < tries); c++) {
110 /* are we resuming? */
111 if(c) { /* yes */
112 /* determine the length of the file already written */
113
114 /*
115 * With NOBODY and NOHEADER, libcurl issues a SIZE command, but the only
116 * way to retrieve the result is to parse the returned Content-Length
117 * header. Thus, getcontentlengthfunc(). We need discardfunc() above
118 * because HEADER dumps the headers to stdout without it.
119 */
120 curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);
121 curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);
122
123 r = curl_easy_perform(curlhandle);
124 if(r != CURLE_OK)
125 continue;
126
127 curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);
128 curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);
129
130 fseek(f, uploaded_len, SEEK_SET);
131
132 curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
133 }
134 else { /* no */
135 curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);
136 }
137
138 r = curl_easy_perform(curlhandle);
139 }
140
141 fclose(f);
142
143 if(r == CURLE_OK)
144 return 1;
145 else {
146 fprintf(stderr, "%s\n", curl_easy_strerror(r));
147 return 0;
148 }
149 }
150
main(void)151 int main(void)
152 {
153 CURL *curlhandle = NULL;
154
155 curl_global_init(CURL_GLOBAL_ALL);
156 curlhandle = curl_easy_init();
157
158 upload(curlhandle, "ftp://user:[email protected]/path/file", "C:\\file",
159 0, 3);
160
161 curl_easy_cleanup(curlhandle);
162 curl_global_cleanup();
163
164 return 0;
165 }
166