1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
9 * Upload to FTP, resuming failed transfers
11 * Compile for MinGW like this:
12 * gcc -Wall -pedantic -std=c99 ftpuploadwithresume.c -o ftpuploadresume.exe
15 * Written by Philip Bock
21 #include <curl/curl.h>
23 #if defined(_MSC_VER) && (_MSC_VER < 1300)
24 # error _snscanf requires MSVC 7.0 or later.
27 /* The MinGW headers are missing a few Win32 function definitions,
28 you shouldn't need this if you use VC++ */
30 int __cdecl _snscanf(const char * input, size_t length, const char * format, ...);
34 /* parse headers for Content-Length */
35 size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb, void *stream)
40 /* _snscanf() is Win32 specific */
41 r = _snscanf(ptr, size * nmemb, "Content-Length: %ld\n", &len);
43 if (r) /* Microsoft: we don't read the specs */
44 *((long *) stream) = len;
49 /* discard downloaded data */
50 size_t discardfunc(void *ptr, size_t size, size_t nmemb, void *stream)
55 /* read data to upload */
56 size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)
62 return CURL_READFUNC_ABORT;
64 n = fread(ptr, size, nmemb, f) * size;
70 int upload(CURL *curlhandle, const char * remotepath, const char * localpath,
71 long timeout, long tries)
74 long uploaded_len = 0;
75 CURLcode r = CURLE_GOT_NOTHING;
78 f = fopen(localpath, "rb");
84 curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
86 curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
89 curl_easy_setopt(curlhandle, CURLOPT_FTP_RESPONSE_TIMEOUT, timeout);
91 curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
92 curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);
94 curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);
96 curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
97 curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
99 curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-"); /* disable passive mode */
100 curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
102 curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
104 for (c = 0; (r != CURLE_OK) && (c < tries); c++) {
105 /* are we resuming? */
107 /* determine the length of the file already written */
110 * With NOBODY and NOHEADER, libcurl will issue a SIZE
111 * command, but the only way to retrieve the result is
112 * to parse the returned Content-Length header. Thus,
113 * getcontentlengthfunc(). We need discardfunc() above
114 * because HEADER will dump the headers to stdout
117 curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);
118 curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);
120 r = curl_easy_perform(curlhandle);
124 curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);
125 curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);
127 fseek(f, uploaded_len, SEEK_SET);
129 curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
132 curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);
135 r = curl_easy_perform(curlhandle);
143 fprintf(stderr, "%s\n", curl_easy_strerror(r));
148 int main(int c, char **argv)
150 CURL *curlhandle = NULL;
152 curl_global_init(CURL_GLOBAL_ALL);
153 curlhandle = curl_easy_init();
155 upload(curlhandle, "ftp://user:pass@example.com/path/file", "C:\\file", 0, 3);
157 curl_easy_cleanup(curlhandle);
158 curl_global_cleanup();