quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

ftpuploadresume.c (4511B)


      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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 */
     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 */
     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 */
     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 
     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 #ifndef UNDER_CE
     81     perror(NULL);
     82 #endif
     83     return 0;
     84   }
     85 
     86   curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
     87 
     88   curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
     89 
     90   if(timeout)
     91     curl_easy_setopt(curlhandle, CURLOPT_SERVER_RESPONSE_TIMEOUT, timeout);
     92 
     93   curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
     94   curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);
     95 
     96   curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);
     97 
     98   curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
     99   curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
    100 
    101   /* enable active mode */
    102   curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-");
    103 
    104   /* allow the server no more than 7 seconds to connect back */
    105   curl_easy_setopt(curlhandle, CURLOPT_ACCEPTTIMEOUT_MS, 7000L);
    106 
    107   curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
    108 
    109   curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
    110 
    111   for(c = 0; (r != CURLE_OK) && (c < tries); c++) {
    112     /* are we resuming? */
    113     if(c) { /* yes */
    114       /* determine the length of the file already written */
    115 
    116       /*
    117        * With NOBODY and NOHEADER, libcurl issues a SIZE command, but the only
    118        * way to retrieve the result is to parse the returned Content-Length
    119        * header. Thus, getcontentlengthfunc(). We need discardfunc() above
    120        * because HEADER dumps the headers to stdout without it.
    121        */
    122       curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);
    123       curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);
    124 
    125       r = curl_easy_perform(curlhandle);
    126       if(r != CURLE_OK)
    127         continue;
    128 
    129       curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);
    130       curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);
    131 
    132       fseek(f, uploaded_len, SEEK_SET);
    133 
    134       curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
    135     }
    136     else { /* no */
    137       curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);
    138     }
    139 
    140     r = curl_easy_perform(curlhandle);
    141   }
    142 
    143   fclose(f);
    144 
    145   if(r == CURLE_OK)
    146     return 1;
    147   else {
    148     fprintf(stderr, "%s\n", curl_easy_strerror(r));
    149     return 0;
    150   }
    151 }
    152 
    153 int main(void)
    154 {
    155   CURL *curlhandle = NULL;
    156 
    157   curl_global_init(CURL_GLOBAL_ALL);
    158   curlhandle = curl_easy_init();
    159 
    160   upload(curlhandle, "ftp://user:pass@example.com/path/file", "C:\\file",
    161          0, 3);
    162 
    163   curl_easy_cleanup(curlhandle);
    164   curl_global_cleanup();
    165 
    166   return 0;
    167 }