Home | History | Annotate | Download | only in examples
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel (at) 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.haxx.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  ***************************************************************************/
     22 /* <DESC>
     23  * Upload to SFTP, resuming a previously aborted transfer.
     24  * </DESC>
     25  */
     26 
     27 #include <stdlib.h>
     28 #include <stdio.h>
     29 #include <curl/curl.h>
     30 
     31 /* read data to upload */
     32 static size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)
     33 {
     34   FILE *f = (FILE *)stream;
     35   size_t n;
     36 
     37   if(ferror(f))
     38     return CURL_READFUNC_ABORT;
     39 
     40   n = fread(ptr, size, nmemb, f) * size;
     41 
     42   return n;
     43 }
     44 
     45 /*
     46  * sftpGetRemoteFileSize returns the remote file size in byte; -1 on error
     47  */
     48 static curl_off_t sftpGetRemoteFileSize(const char *i_remoteFile)
     49 {
     50   CURLcode result = CURLE_GOT_NOTHING;
     51   curl_off_t remoteFileSizeByte = -1;
     52   CURL *curlHandlePtr = NULL;
     53 
     54   curlHandlePtr = curl_easy_init();
     55   curl_easy_setopt(curlHandlePtr, CURLOPT_VERBOSE, 1L);
     56 
     57   curl_easy_setopt(curlHandlePtr, CURLOPT_URL, i_remoteFile);
     58   curl_easy_setopt(curlHandlePtr, CURLOPT_NOPROGRESS, 1);
     59   curl_easy_setopt(curlHandlePtr, CURLOPT_NOBODY, 1);
     60   curl_easy_setopt(curlHandlePtr, CURLOPT_HEADER, 1);
     61   curl_easy_setopt(curlHandlePtr, CURLOPT_FILETIME, 1);
     62 
     63   result = curl_easy_perform(curlHandlePtr);
     64   if(CURLE_OK == result) {
     65     result = curl_easy_getinfo(curlHandlePtr,
     66                                CURLINFO_CONTENT_LENGTH_DOWNLOAD_T,
     67                                &remoteFileSizeByte);
     68     if(result)
     69       return -1;
     70     printf("filesize: %" CURL_FORMAT_CURL_OFF_T "\n", remoteFileSizeByte);
     71   }
     72   curl_easy_cleanup(curlHandlePtr);
     73 
     74   return remoteFileSizeByte;
     75 }
     76 
     77 
     78 static int sftpResumeUpload(CURL *curlhandle, const char *remotepath,
     79                             const char *localpath)
     80 {
     81   FILE *f = NULL;
     82   CURLcode result = CURLE_GOT_NOTHING;
     83 
     84   curl_off_t remoteFileSizeByte = sftpGetRemoteFileSize(remotepath);
     85   if(-1 == remoteFileSizeByte) {
     86     printf("Error reading the remote file size: unable to resume upload\n");
     87     return -1;
     88   }
     89 
     90   f = fopen(localpath, "rb");
     91   if(!f) {
     92     perror(NULL);
     93     return 0;
     94   }
     95 
     96   curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
     97   curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
     98   curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
     99   curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
    100 
    101 #ifdef _WIN32
    102   _fseeki64(f, remoteFileSizeByte, SEEK_SET);
    103 #else
    104   fseek(f, (long)remoteFileSizeByte, SEEK_SET);
    105 #endif
    106   curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
    107   result = curl_easy_perform(curlhandle);
    108 
    109   fclose(f);
    110 
    111   if(result == CURLE_OK)
    112     return 1;
    113   else {
    114     fprintf(stderr, "%s\n", curl_easy_strerror(result));
    115     return 0;
    116   }
    117 }
    118 
    119 int main(void)
    120 {
    121   const char *remote = "sftp://user:pass@example.com/path/filename";
    122   const char *filename = "filename";
    123   CURL *curlhandle = NULL;
    124 
    125   curl_global_init(CURL_GLOBAL_ALL);
    126   curlhandle = curl_easy_init();
    127 
    128   if(!sftpResumeUpload(curlhandle, remote, filename)) {
    129     printf("resumed upload using curl %s failed\n", curl_version());
    130   }
    131 
    132   curl_easy_cleanup(curlhandle);
    133   curl_global_cleanup();
    134 
    135   return 0;
    136 }
    137