Imported Upstream version 7.59.0
[platform/upstream/curl.git] / docs / examples / sftpuploadresume.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2018, 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 = 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     printf("filesize: %ld \n", remoteFileSizeByte);
69   }
70   curl_easy_cleanup(curlHandlePtr);
71
72   return remoteFileSizeByte;
73 }
74
75
76 static int sftpResumeUpload(CURL *curlhandle, const char *remotepath,
77                             const char *localpath)
78 {
79   FILE *f = NULL;
80   CURLcode result = CURLE_GOT_NOTHING;
81
82   curl_off_t remoteFileSizeByte = sftpGetRemoteFileSize(remotepath);
83   if(-1 == remoteFileSizeByte) {
84     printf("Error reading the remote file size: unable to resume upload\n");
85     return -1;
86   }
87
88   f = fopen(localpath, "rb");
89   if(!f) {
90     perror(NULL);
91     return 0;
92   }
93
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);
98
99   fseek(f, remoteFileSizeByte, SEEK_SET);
100   curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
101   result = curl_easy_perform(curlhandle);
102
103   fclose(f);
104
105   if(result == CURLE_OK)
106     return 1;
107   else {
108     fprintf(stderr, "%s\n", curl_easy_strerror(result));
109     return 0;
110   }
111 }
112
113 int main(void)
114 {
115   const char *remote = "sftp://user:pass@example.com/path/filename";
116   const char *filename = "filename";
117   CURL *curlhandle = NULL;
118
119   curl_global_init(CURL_GLOBAL_ALL);
120   curlhandle = curl_easy_init();
121
122   if(!sftpResumeUpload(curlhandle, remote, filename)) {
123     printf("resumed upload using curl %s failed\n", curl_version());
124   }
125
126   curl_easy_cleanup(curlhandle);
127   curl_global_cleanup();
128
129   return 0;
130 }