quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

sftpget.c (3361B)


      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  * Gets a file using an SFTP URL.
     26  * </DESC>
     27  */
     28 
     29 #include <stdio.h>
     30 
     31 #include <curl/curl.h>
     32 
     33 /* define this to switch off the use of ssh-agent in this program */
     34 #undef DISABLE_SSH_AGENT
     35 
     36 /*
     37  * This is an example showing how to get a single file from an SFTP server. It
     38  * delays the actual destination file creation until the first write callback
     39  * so that it does not create an empty file in case the remote file does not
     40  * exist or something else fails.
     41  */
     42 
     43 struct FtpFile {
     44   const char *filename;
     45   FILE *stream;
     46 };
     47 
     48 static size_t my_fwrite(void *buffer, size_t size, size_t nmemb,
     49                         void *stream)
     50 {
     51   struct FtpFile *out = (struct FtpFile *)stream;
     52   if(!out->stream) {
     53     /* open file for writing */
     54     out->stream = fopen(out->filename, "wb");
     55     if(!out->stream)
     56       return 0; /* failure, cannot open file to write */
     57   }
     58   return fwrite(buffer, size, nmemb, out->stream);
     59 }
     60 
     61 
     62 int main(void)
     63 {
     64   CURL *curl;
     65   CURLcode res;
     66   struct FtpFile ftpfile = {
     67     "yourfile.bin", /* name to store the file as if successful */
     68     NULL
     69   };
     70 
     71   curl_global_init(CURL_GLOBAL_DEFAULT);
     72 
     73   curl = curl_easy_init();
     74   if(curl) {
     75     /*
     76      * You better replace the URL with one that works!
     77      */
     78     curl_easy_setopt(curl, CURLOPT_URL,
     79                      "sftp://user@server/home/user/file.txt");
     80     /* Define our callback to get called when there is data to be written */
     81     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
     82     /* Set a pointer to our struct to pass to the callback */
     83     curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
     84 
     85 #ifndef DISABLE_SSH_AGENT
     86     /* We activate ssh agent. For this to work you need
     87        to have ssh-agent running (type set | grep SSH_AGENT to check) or
     88        pageant on Windows (there is an icon in systray if so) */
     89     curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES, (long)CURLSSH_AUTH_AGENT);
     90 #endif
     91 
     92     /* Switch on full protocol/debug output */
     93     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
     94 
     95     res = curl_easy_perform(curl);
     96 
     97     /* always cleanup */
     98     curl_easy_cleanup(curl);
     99 
    100     if(CURLE_OK != res) {
    101       /* we failed */
    102       fprintf(stderr, "curl told us %d\n", res);
    103     }
    104   }
    105 
    106   if(ftpfile.stream)
    107     fclose(ftpfile.stream); /* close the local file */
    108 
    109   curl_global_cleanup();
    110 
    111   return 0;
    112 }