ftpuploadresume.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * Upload to FTP, resuming failed transfers.
00024  * </DESC>
00025  */
00026 
00027 #include <stdlib.h>
00028 #include <stdio.h>
00029 
00030 #include <curl/curl.h>
00031 
00032 #if defined(_MSC_VER) && (_MSC_VER < 1300)
00033 #  error _snscanf requires MSVC 7.0 or later.
00034 #endif
00035 
00036 /* The MinGW headers are missing a few Win32 function definitions,
00037    you shouldn't need this if you use VC++ */
00038 #if defined(__MINGW32__) && !defined(__MINGW64__)
00039 int __cdecl _snscanf(const char *input, size_t length,
00040                      const char *format, ...);
00041 #endif
00042 
00043 
00044 /* parse headers for Content-Length */
00045 size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb, void *stream)
00046 {
00047   int r;
00048   long len = 0;
00049 
00050   /* _snscanf() is Win32 specific */
00051   r = _snscanf(ptr, size * nmemb, "Content-Length: %ld\n", &len);
00052 
00053   if(r) /* Microsoft: we don't read the specs */
00054     *((long *) stream) = len;
00055 
00056   return size * nmemb;
00057 }
00058 
00059 /* discard downloaded data */
00060 size_t discardfunc(void *ptr, size_t size, size_t nmemb, void *stream)
00061 {
00062   return size * nmemb;
00063 }
00064 
00065 /* read data to upload */
00066 size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)
00067 {
00068   FILE *f = stream;
00069   size_t n;
00070 
00071   if(ferror(f))
00072     return CURL_READFUNC_ABORT;
00073 
00074   n = fread(ptr, size, nmemb, f) * size;
00075 
00076   return n;
00077 }
00078 
00079 
00080 int upload(CURL *curlhandle, const char *remotepath, const char *localpath,
00081            long timeout, long tries)
00082 {
00083   FILE *f;
00084   long uploaded_len = 0;
00085   CURLcode r = CURLE_GOT_NOTHING;
00086   int c;
00087 
00088   f = fopen(localpath, "rb");
00089   if(!f) {
00090     perror(NULL);
00091     return 0;
00092   }
00093 
00094   curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
00095 
00096   curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
00097 
00098   if(timeout)
00099     curl_easy_setopt(curlhandle, CURLOPT_FTP_RESPONSE_TIMEOUT, timeout);
00100 
00101   curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
00102   curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);
00103 
00104   curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);
00105 
00106   curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
00107   curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
00108 
00109   /* disable passive mode */
00110   curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-");
00111   curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
00112 
00113   curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
00114 
00115   for(c = 0; (r != CURLE_OK) && (c < tries); c++) {
00116     /* are we resuming? */
00117     if(c) { /* yes */
00118       /* determine the length of the file already written */
00119 
00120       /*
00121        * With NOBODY and NOHEADER, libcurl will issue a SIZE
00122        * command, but the only way to retrieve the result is
00123        * to parse the returned Content-Length header. Thus,
00124        * getcontentlengthfunc(). We need discardfunc() above
00125        * because HEADER will dump the headers to stdout
00126        * without it.
00127        */
00128       curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);
00129       curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);
00130 
00131       r = curl_easy_perform(curlhandle);
00132       if(r != CURLE_OK)
00133         continue;
00134 
00135       curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);
00136       curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);
00137 
00138       fseek(f, uploaded_len, SEEK_SET);
00139 
00140       curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
00141     }
00142     else { /* no */
00143       curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);
00144     }
00145 
00146     r = curl_easy_perform(curlhandle);
00147   }
00148 
00149   fclose(f);
00150 
00151   if(r == CURLE_OK)
00152     return 1;
00153   else {
00154     fprintf(stderr, "%s\n", curl_easy_strerror(r));
00155     return 0;
00156   }
00157 }
00158 
00159 int main(int c, char **argv)
00160 {
00161   CURL *curlhandle = NULL;
00162 
00163   curl_global_init(CURL_GLOBAL_ALL);
00164   curlhandle = curl_easy_init();
00165 
00166   upload(curlhandle, "ftp://user:pass@example.com/path/file", "C:\\file",
00167          0, 3);
00168 
00169   curl_easy_cleanup(curlhandle);
00170   curl_global_cleanup();
00171 
00172   return 0;
00173 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02