1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * Upload to SFTP, resuming a previously aborted transfer.
29 #include <curl/curl.h>
31 /* read data to upload */
32 static size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)
34 FILE *f = (FILE *)stream;
38 return CURL_READFUNC_ABORT;
40 n = fread(ptr, size, nmemb, f) * size;
46 * sftpGetRemoteFileSize returns the remote file size in byte; -1 on error
48 static curl_off_t sftpGetRemoteFileSize(const char *i_remoteFile)
50 CURLcode result = CURLE_GOT_NOTHING;
51 curl_off_t remoteFileSizeByte = -1;
52 CURL *curlHandlePtr = NULL;
54 curlHandlePtr = curl_easy_init();
55 curl_easy_setopt(curlHandlePtr, CURLOPT_VERBOSE, 1L);
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);
63 result = curl_easy_perform(curlHandlePtr);
64 if(CURLE_OK == result) {
65 result = curl_easy_getinfo(curlHandlePtr,
66 CURLINFO_CONTENT_LENGTH_DOWNLOAD_T,
68 printf("filesize: %ld \n", remoteFileSizeByte);
70 curl_easy_cleanup(curlHandlePtr);
72 return remoteFileSizeByte;
76 static int sftpResumeUpload(CURL *curlhandle, const char *remotepath,
77 const char *localpath)
80 CURLcode result = CURLE_GOT_NOTHING;
82 curl_off_t remoteFileSizeByte = sftpGetRemoteFileSize(remotepath);
83 if(-1 == remoteFileSizeByte) {
84 printf("Error reading the remote file size: unable to resume upload\n");
88 f = fopen(localpath, "rb");
94 curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
95 curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
96 curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
97 curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
99 fseek(f, remoteFileSizeByte, SEEK_SET);
100 curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
101 result = curl_easy_perform(curlhandle);
105 if(result == CURLE_OK)
108 fprintf(stderr, "%s\n", curl_easy_strerror(result));
115 const char *remote = "sftp://user:pass@example.com/path/filename";
116 const char *filename = "filename";
117 CURL *curlhandle = NULL;
119 curl_global_init(CURL_GLOBAL_ALL);
120 curlhandle = curl_easy_init();
122 if(!sftpResumeUpload(curlhandle, remote, filename)) {
123 printf("resumed upload using curl %s failed\n", curl_version());
126 curl_easy_cleanup(curlhandle);
127 curl_global_cleanup();