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