xref: /curl/docs/examples/sftpuploadresume.c (revision 2bc1d775)
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 */
readfunc(char * ptr,size_t size,size_t nmemb,void * stream)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  */
sftpGetRemoteFileSize(const char * i_remoteFile)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, 1);
60   curl_easy_setopt(curlHandlePtr, CURLOPT_NOBODY, 1);
61   curl_easy_setopt(curlHandlePtr, CURLOPT_HEADER, 1);
62   curl_easy_setopt(curlHandlePtr, CURLOPT_FILETIME, 1);
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 
sftpResumeUpload(CURL * curlhandle,const char * remotepath,const char * localpath)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     perror(NULL);
94     return 0;
95   }
96 
97   curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
98   curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
99   curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
100   curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
101 
102 #ifdef _WIN32
103   _fseeki64(f, remoteFileSizeByte, SEEK_SET);
104 #else
105   fseek(f, (long)remoteFileSizeByte, SEEK_SET);
106 #endif
107   curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
108   result = curl_easy_perform(curlhandle);
109 
110   fclose(f);
111 
112   if(result == CURLE_OK)
113     return 1;
114   else {
115     fprintf(stderr, "%s\n", curl_easy_strerror(result));
116     return 0;
117   }
118 }
119 
main(void)120 int main(void)
121 {
122   const char *remote = "sftp://user:pass@example.com/path/filename";
123   const char *filename = "filename";
124   CURL *curlhandle = NULL;
125 
126   curl_global_init(CURL_GLOBAL_ALL);
127   curlhandle = curl_easy_init();
128 
129   if(!sftpResumeUpload(curlhandle, remote, filename)) {
130     printf("resumed upload using curl %s failed\n", curl_version());
131   }
132 
133   curl_easy_cleanup(curlhandle);
134   curl_global_cleanup();
135 
136   return 0;
137 }
138