Annotation of embedaddon/curl/docs/examples/sftpuploadresume.c, revision 1.1.1.1

1.1       misho       1: /***************************************************************************
                      2:  *                                  _   _ ____  _
                      3:  *  Project                     ___| | | |  _ \| |
                      4:  *                             / __| | | | |_) | |
                      5:  *                            | (__| |_| |  _ <| |___
                      6:  *                             \___|\___/|_| \_\_____|
                      7:  *
                      8:  * Copyright (C) 1998 - 2019, 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.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 = curl_easy_init();
                     53: 
                     54:   curl_easy_setopt(curlHandlePtr, CURLOPT_VERBOSE, 1L);
                     55: 
                     56:   curl_easy_setopt(curlHandlePtr, CURLOPT_URL, i_remoteFile);
                     57:   curl_easy_setopt(curlHandlePtr, CURLOPT_NOPROGRESS, 1);
                     58:   curl_easy_setopt(curlHandlePtr, CURLOPT_NOBODY, 1);
                     59:   curl_easy_setopt(curlHandlePtr, CURLOPT_HEADER, 1);
                     60:   curl_easy_setopt(curlHandlePtr, CURLOPT_FILETIME, 1);
                     61: 
                     62:   result = curl_easy_perform(curlHandlePtr);
                     63:   if(CURLE_OK == result) {
                     64:     result = curl_easy_getinfo(curlHandlePtr,
                     65:                                CURLINFO_CONTENT_LENGTH_DOWNLOAD_T,
                     66:                                &remoteFileSizeByte);
                     67:     if(result)
                     68:       return -1;
                     69:     printf("filesize: %" CURL_FORMAT_CURL_OFF_T "\n", remoteFileSizeByte);
                     70:   }
                     71:   curl_easy_cleanup(curlHandlePtr);
                     72: 
                     73:   return remoteFileSizeByte;
                     74: }
                     75: 
                     76: 
                     77: static int sftpResumeUpload(CURL *curlhandle, const char *remotepath,
                     78:                             const char *localpath)
                     79: {
                     80:   FILE *f = NULL;
                     81:   CURLcode result = CURLE_GOT_NOTHING;
                     82: 
                     83:   curl_off_t remoteFileSizeByte = sftpGetRemoteFileSize(remotepath);
                     84:   if(-1 == remoteFileSizeByte) {
                     85:     printf("Error reading the remote file size: unable to resume upload\n");
                     86:     return -1;
                     87:   }
                     88: 
                     89:   f = fopen(localpath, "rb");
                     90:   if(!f) {
                     91:     perror(NULL);
                     92:     return 0;
                     93:   }
                     94: 
                     95:   curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
                     96:   curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
                     97:   curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
                     98:   curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
                     99: 
                    100: #ifdef _WIN32
                    101:   _fseeki64(f, remoteFileSizeByte, SEEK_SET);
                    102: #else
                    103:   fseek(f, (long)remoteFileSizeByte, SEEK_SET);
                    104: #endif
                    105:   curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
                    106:   result = curl_easy_perform(curlhandle);
                    107: 
                    108:   fclose(f);
                    109: 
                    110:   if(result == CURLE_OK)
                    111:     return 1;
                    112:   else {
                    113:     fprintf(stderr, "%s\n", curl_easy_strerror(result));
                    114:     return 0;
                    115:   }
                    116: }
                    117: 
                    118: int main(void)
                    119: {
                    120:   const char *remote = "sftp://user:pass@example.com/path/filename";
                    121:   const char *filename = "filename";
                    122:   CURL *curlhandle = NULL;
                    123: 
                    124:   curl_global_init(CURL_GLOBAL_ALL);
                    125:   curlhandle = curl_easy_init();
                    126: 
                    127:   if(!sftpResumeUpload(curlhandle, remote, filename)) {
                    128:     printf("resumed upload using curl %s failed\n", curl_version());
                    129:   }
                    130: 
                    131:   curl_easy_cleanup(curlhandle);
                    132:   curl_global_cleanup();
                    133: 
                    134:   return 0;
                    135: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>