xref: /aosp_15_r20/external/curl/docs/examples/sftpget.c (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker /***************************************************************************
2*6236dae4SAndroid Build Coastguard Worker  *                                  _   _ ____  _
3*6236dae4SAndroid Build Coastguard Worker  *  Project                     ___| | | |  _ \| |
4*6236dae4SAndroid Build Coastguard Worker  *                             / __| | | | |_) | |
5*6236dae4SAndroid Build Coastguard Worker  *                            | (__| |_| |  _ <| |___
6*6236dae4SAndroid Build Coastguard Worker  *                             \___|\___/|_| \_\_____|
7*6236dae4SAndroid Build Coastguard Worker  *
8*6236dae4SAndroid Build Coastguard Worker  * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
9*6236dae4SAndroid Build Coastguard Worker  *
10*6236dae4SAndroid Build Coastguard Worker  * This software is licensed as described in the file COPYING, which
11*6236dae4SAndroid Build Coastguard Worker  * you should have received as part of this distribution. The terms
12*6236dae4SAndroid Build Coastguard Worker  * are also available at https://curl.se/docs/copyright.html.
13*6236dae4SAndroid Build Coastguard Worker  *
14*6236dae4SAndroid Build Coastguard Worker  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15*6236dae4SAndroid Build Coastguard Worker  * copies of the Software, and permit persons to whom the Software is
16*6236dae4SAndroid Build Coastguard Worker  * furnished to do so, under the terms of the COPYING file.
17*6236dae4SAndroid Build Coastguard Worker  *
18*6236dae4SAndroid Build Coastguard Worker  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19*6236dae4SAndroid Build Coastguard Worker  * KIND, either express or implied.
20*6236dae4SAndroid Build Coastguard Worker  *
21*6236dae4SAndroid Build Coastguard Worker  * SPDX-License-Identifier: curl
22*6236dae4SAndroid Build Coastguard Worker  *
23*6236dae4SAndroid Build Coastguard Worker  ***************************************************************************/
24*6236dae4SAndroid Build Coastguard Worker /* <DESC>
25*6236dae4SAndroid Build Coastguard Worker  * Gets a file using an SFTP URL.
26*6236dae4SAndroid Build Coastguard Worker  * </DESC>
27*6236dae4SAndroid Build Coastguard Worker  */
28*6236dae4SAndroid Build Coastguard Worker 
29*6236dae4SAndroid Build Coastguard Worker #include <stdio.h>
30*6236dae4SAndroid Build Coastguard Worker 
31*6236dae4SAndroid Build Coastguard Worker #include <curl/curl.h>
32*6236dae4SAndroid Build Coastguard Worker 
33*6236dae4SAndroid Build Coastguard Worker /* define this to switch off the use of ssh-agent in this program */
34*6236dae4SAndroid Build Coastguard Worker #undef DISABLE_SSH_AGENT
35*6236dae4SAndroid Build Coastguard Worker 
36*6236dae4SAndroid Build Coastguard Worker /*
37*6236dae4SAndroid Build Coastguard Worker  * This is an example showing how to get a single file from an SFTP server. It
38*6236dae4SAndroid Build Coastguard Worker  * delays the actual destination file creation until the first write callback
39*6236dae4SAndroid Build Coastguard Worker  * so that it does not create an empty file in case the remote file does not
40*6236dae4SAndroid Build Coastguard Worker  * exist or something else fails.
41*6236dae4SAndroid Build Coastguard Worker  */
42*6236dae4SAndroid Build Coastguard Worker 
43*6236dae4SAndroid Build Coastguard Worker struct FtpFile {
44*6236dae4SAndroid Build Coastguard Worker   const char *filename;
45*6236dae4SAndroid Build Coastguard Worker   FILE *stream;
46*6236dae4SAndroid Build Coastguard Worker };
47*6236dae4SAndroid Build Coastguard Worker 
my_fwrite(void * buffer,size_t size,size_t nmemb,void * stream)48*6236dae4SAndroid Build Coastguard Worker static size_t my_fwrite(void *buffer, size_t size, size_t nmemb,
49*6236dae4SAndroid Build Coastguard Worker                         void *stream)
50*6236dae4SAndroid Build Coastguard Worker {
51*6236dae4SAndroid Build Coastguard Worker   struct FtpFile *out = (struct FtpFile *)stream;
52*6236dae4SAndroid Build Coastguard Worker   if(!out->stream) {
53*6236dae4SAndroid Build Coastguard Worker     /* open file for writing */
54*6236dae4SAndroid Build Coastguard Worker     out->stream = fopen(out->filename, "wb");
55*6236dae4SAndroid Build Coastguard Worker     if(!out->stream)
56*6236dae4SAndroid Build Coastguard Worker       return 0; /* failure, cannot open file to write */
57*6236dae4SAndroid Build Coastguard Worker   }
58*6236dae4SAndroid Build Coastguard Worker   return fwrite(buffer, size, nmemb, out->stream);
59*6236dae4SAndroid Build Coastguard Worker }
60*6236dae4SAndroid Build Coastguard Worker 
61*6236dae4SAndroid Build Coastguard Worker 
main(void)62*6236dae4SAndroid Build Coastguard Worker int main(void)
63*6236dae4SAndroid Build Coastguard Worker {
64*6236dae4SAndroid Build Coastguard Worker   CURL *curl;
65*6236dae4SAndroid Build Coastguard Worker   CURLcode res;
66*6236dae4SAndroid Build Coastguard Worker   struct FtpFile ftpfile = {
67*6236dae4SAndroid Build Coastguard Worker     "yourfile.bin", /* name to store the file as if successful */
68*6236dae4SAndroid Build Coastguard Worker     NULL
69*6236dae4SAndroid Build Coastguard Worker   };
70*6236dae4SAndroid Build Coastguard Worker 
71*6236dae4SAndroid Build Coastguard Worker   curl_global_init(CURL_GLOBAL_DEFAULT);
72*6236dae4SAndroid Build Coastguard Worker 
73*6236dae4SAndroid Build Coastguard Worker   curl = curl_easy_init();
74*6236dae4SAndroid Build Coastguard Worker   if(curl) {
75*6236dae4SAndroid Build Coastguard Worker     /*
76*6236dae4SAndroid Build Coastguard Worker      * You better replace the URL with one that works!
77*6236dae4SAndroid Build Coastguard Worker      */
78*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_URL,
79*6236dae4SAndroid Build Coastguard Worker                      "sftp://user@server/home/user/file.txt");
80*6236dae4SAndroid Build Coastguard Worker     /* Define our callback to get called when there is data to be written */
81*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
82*6236dae4SAndroid Build Coastguard Worker     /* Set a pointer to our struct to pass to the callback */
83*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
84*6236dae4SAndroid Build Coastguard Worker 
85*6236dae4SAndroid Build Coastguard Worker #ifndef DISABLE_SSH_AGENT
86*6236dae4SAndroid Build Coastguard Worker     /* We activate ssh agent. For this to work you need
87*6236dae4SAndroid Build Coastguard Worker        to have ssh-agent running (type set | grep SSH_AGENT to check) or
88*6236dae4SAndroid Build Coastguard Worker        pageant on Windows (there is an icon in systray if so) */
89*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_AGENT);
90*6236dae4SAndroid Build Coastguard Worker #endif
91*6236dae4SAndroid Build Coastguard Worker 
92*6236dae4SAndroid Build Coastguard Worker     /* Switch on full protocol/debug output */
93*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
94*6236dae4SAndroid Build Coastguard Worker 
95*6236dae4SAndroid Build Coastguard Worker     res = curl_easy_perform(curl);
96*6236dae4SAndroid Build Coastguard Worker 
97*6236dae4SAndroid Build Coastguard Worker     /* always cleanup */
98*6236dae4SAndroid Build Coastguard Worker     curl_easy_cleanup(curl);
99*6236dae4SAndroid Build Coastguard Worker 
100*6236dae4SAndroid Build Coastguard Worker     if(CURLE_OK != res) {
101*6236dae4SAndroid Build Coastguard Worker       /* we failed */
102*6236dae4SAndroid Build Coastguard Worker       fprintf(stderr, "curl told us %d\n", res);
103*6236dae4SAndroid Build Coastguard Worker     }
104*6236dae4SAndroid Build Coastguard Worker   }
105*6236dae4SAndroid Build Coastguard Worker 
106*6236dae4SAndroid Build Coastguard Worker   if(ftpfile.stream)
107*6236dae4SAndroid Build Coastguard Worker     fclose(ftpfile.stream); /* close the local file */
108*6236dae4SAndroid Build Coastguard Worker 
109*6236dae4SAndroid Build Coastguard Worker   curl_global_cleanup();
110*6236dae4SAndroid Build Coastguard Worker 
111*6236dae4SAndroid Build Coastguard Worker   return 0;
112*6236dae4SAndroid Build Coastguard Worker }
113