Ticket #671: nALFS-libcurl-1.patch

File nALFS-libcurl-1.patch, 11.6 KB (added by kpfleming@…, 21 years ago)

patch to close bug

  • src/handlers/new-download.c

    diff -X /usr/src/dontdiff -urN nALFS/src/handlers/new-download.c nALFS-new/src/handlers/new-download.c
    old new  
    3737#include "nalfs.h"
    3838#include "backend.h"
    3939#include "config.h"
    40 #include "digest.h"
     40#include "lib.h"
    4141
    4242#define El_download_file(el) alloc_trimmed_param_value("file", el)
    4343#define El_download_destination(el) alloc_trimmed_param_value("destination", el)
    4444#define El_download_digest(el) alloc_trimmed_param_value("digest", el)
    4545
    46 #ifdef HAVE_LIBCURL
    47 
    48 typedef struct output_file_s {
    49         const char *filename;
    50         FILE *stream;
    51 } output_file_s;
    52 
    53 #include <curl/curl.h>
    54 
    55 int my_fwrite(void *buffer, size_t size, size_t nmemb, void *data)
    56 {
    57         output_file_s *output = (output_file_s *)data;
    58 
    59 
    60         if (output->stream == NULL) { /* Open the output file. */
    61                 if ((output->stream = fopen(output->filename, "wb")) == NULL) {
    62                         Nprint_err("Unable to open %s for writing.",
    63                                         output->filename);
    64                         return -1;
    65                 }
    66         }
    67 
    68         return fwrite(buffer, size, nmemb, output->stream);
    69 }
    70 
    71 int load_url(const char *archive, const char *url)
    72 {
    73         CURL *handle;
    74         CURLcode err;
    75         char error_buffer[CURL_ERROR_SIZE];
    76         output_file_s output = { archive, NULL };
    77         FILE *error_file = NULL;
    78 
    79 
    80         Nprint("Downloading with Curl:");
    81         Nprint("    %s", url);
    82 
    83         if ((err = curl_global_init(CURL_GLOBAL_ALL)) != 0) {
    84                 Nprint_err("Error initializing Curl (%d).", err);
    85                 return -1;
    86         }
    87 
    88         if ((handle = curl_easy_init()) == NULL) {
    89                 Nprint_err("Error initializing easy interface.");
    90                 curl_global_cleanup();
    91                 return -1;
    92         }
    93 
    94         /* Can't ignore CURLOPT_STDERR when CURLOPT_ERRORBUFFER is set? */
    95         error_file = fopen("/dev/null", "w");
    96         if (error_file) {
    97                 curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, error_buffer);
    98                 curl_easy_setopt(handle, CURLOPT_STDERR, error_file);
    99         }
    100         /* Set URL to download, callback function and the output file. */
    101         curl_easy_setopt(handle, CURLOPT_URL, url);
    102         curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, my_fwrite);
    103         curl_easy_setopt(handle, CURLOPT_FILE, &output);
    104         curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1);
    105 
    106         /* Start downloading. */
    107         err = curl_easy_perform(handle);
    108 
    109         /* Cleanup. */
    110         curl_easy_cleanup(handle);
    111         curl_global_cleanup();
    112 
    113         if (output.stream) { /* Close output file. */
    114                 fclose(output.stream);
    115         }
    116         if (error_file) { /* Close error file. */
    117                 fclose(error_file);
    118         }
    119 
    120         if (err != CURLE_OK) {
    121                 if (error_file) {
    122                         Nprint_err("Downloading with Curl failed:");
    123                         Nprint_err("    %s", error_buffer);
    124                 } else {
    125                         Nprint_err("Downloading with Curl failed (%d).", err);
    126                 }
    127 
    128                 return -1;
    129         }
    130 
    131         Nprint("Downloading with Curl completed successfully.");
    132 
    133         return 0;
    134 }
    135 
    136 #endif
    137 
    138 
    13946static INLINE int get_url(const char *urldir, const char *file)
    14047{
    14148        int status;
  • src/handlers/new-unpack.c

    diff -X /usr/src/dontdiff -urN nALFS/src/handlers/new-unpack.c nALFS-new/src/handlers/new-unpack.c
    old new  
    3636#include "nalfs.h"
    3737#include "backend.h"
    3838#include "config.h"
    39 #include "digest.h"
     39#include "lib.h"
    4040
    4141
    4242#define El_unpack_reference(el) alloc_trimmed_param_value("reference", el)
     
    4949        GZ, TAR_GZ, TGZ, BZ2, TAR_BZ2, TAR, ZIP, TAR_Z, Z, UNKNOWN
    5050} extension_e;
    5151
    52 #ifdef HAVE_LIBCURL
    53 
    54 typedef struct output_file_s {
    55         const char *filename;
    56         FILE *stream;
    57 } output_file_s;
    58 
    59 #include <curl/curl.h>
    60 
    61 int my_fwrite(void *buffer, size_t size, size_t nmemb, void *data)
    62 {
    63         output_file_s *output = (output_file_s *)data;
    64 
    65 
    66         if (output->stream == NULL) { /* Open the output file. */
    67                 if ((output->stream = fopen(output->filename, "wb")) == NULL) {
    68                         Nprint_err("Unable to open %s for writing.",
    69                                         output->filename);
    70                         return -1;
    71                 }
    72         }
    73 
    74         return fwrite(buffer, size, nmemb, output->stream);
    75 }
    76 
    77 int load_url(const char *archive, const char *url)
    78 {
    79         CURL *handle;
    80         CURLcode err;
    81         char error_buffer[CURL_ERROR_SIZE];
    82         output_file_s output = { archive, NULL };
    83         FILE *error_file = NULL;
    84 
    85 
    86         Nprint("Downloading with Curl:");
    87         Nprint("    %s", url);
    88 
    89         if ((err = curl_global_init(CURL_GLOBAL_ALL)) != 0) {
    90                 Nprint_err("Error initializing Curl (%d).", err);
    91                 return -1;
    92         }
    93 
    94         if ((handle = curl_easy_init()) == NULL) {
    95                 Nprint_err("Error initializing easy interface.");
    96                 curl_global_cleanup();
    97                 return -1;
    98         }
    99 
    100         /* Can't ignore CURLOPT_STDERR when CURLOPT_ERRORBUFFER is set? */
    101         error_file = fopen("/dev/null", "w");
    102         if (error_file) {
    103                 curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, error_buffer);
    104                 curl_easy_setopt(handle, CURLOPT_STDERR, error_file);
    105         }
    106         /* Set URL to download, callback function and the output file. */
    107         curl_easy_setopt(handle, CURLOPT_URL, url);
    108         curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, my_fwrite);
    109         curl_easy_setopt(handle, CURLOPT_FILE, &output);
    110 
    111         /* Start downloading. */
    112         err = curl_easy_perform(handle);
    113 
    114         /* Cleanup. */
    115         curl_easy_cleanup(handle);
    116         curl_global_cleanup();
    117 
    118         if (output.stream) { /* Close output file. */
    119                 fclose(output.stream);
    120         }
    121         if (error_file) { /* Close error file. */
    122                 fclose(error_file);
    123         }
    124 
    125         if (err != CURLE_OK) {
    126                 if (error_file) {
    127                         Nprint_err("Downloading with Curl failed:");
    128                         Nprint_err("    %s", error_buffer);
    129                 } else {
    130                         Nprint_err("Downloading with Curl failed (%d).", err);
    131                 }
    132 
    133                 return -1;
    134         }
    135 
    136         Nprint("Downloading with Curl completed successfully.");
    137 
    138         return 0;
    139 }
    140 
    141 #endif
    142 
    143 
    14452static INLINE int get_reference(const char *reference, const char *archive)
    14553{
    14654        int status;
  • src/lib/digest.h

    diff -X /usr/src/dontdiff -urN nALFS/src/lib/digest.h nALFS-new/src/lib/digest.h
    old new  
    1 /*
    2  *  digest.h - Verify a file's contents have not been changed.
    3  *
    4  *  Copyright (C) 2003-2003
    5  * 
    6  *  Neven Has <haski@sezampro.yu> and
    7  *  Kevin P. Fleming <kpfleming@backtobasicsmgmt.com>
    8  *
    9  *  This program is free software; you can redistribute it and/or modify
    10  *  it under the terms of the GNU General Public License as published by
    11  *  the Free Software Foundation; either version 2 of the License, or
    12  *  (at your option) any later version.
    13  *
    14  *  This program is distributed in the hope that it will be useful,
    15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  *  GNU General Public License for more details.
    18  *
    19  *  You should have received a copy of the GNU General Public License
    20  *  along with this program; if not, write to the Free Software
    21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    22  */
    23 
    24 #ifndef _DIGEST_H
    25 #define _DIGEST_H
    26 
    27 extern int verify_digest(const char *type, char *digest, const char *file);
    28 
    29 #endif /* _DIGEST_H */
  • src/lib/lib.h

    diff -X /usr/src/dontdiff -urN nALFS/src/lib/lib.h nALFS-new/src/lib/lib.h
    old new  
     1/*
     2 *  lib.h - Functions in the nALFS library.
     3 *
     4 *  Copyright (C) 2003-2003
     5 * 
     6 *  Neven Has <haski@sezampro.yu> and
     7 *  Kevin P. Fleming <kpfleming@backtobasicsmgmt.com>
     8 *
     9 *  This program is free software; you can redistribute it and/or modify
     10 *  it under the terms of the GNU General Public License as published by
     11 *  the Free Software Foundation; either version 2 of the License, or
     12 *  (at your option) any later version.
     13 *
     14 *  This program is distributed in the hope that it will be useful,
     15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 *  GNU General Public License for more details.
     18 *
     19 *  You should have received a copy of the GNU General Public License
     20 *  along with this program; if not, write to the Free Software
     21 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     22 */
     23
     24#ifndef _LIB_H
     25#define _LIB_H
     26
     27extern int verify_digest(const char *type, char *digest, const char *file);
     28extern int load_url(const char *output, const char *url);
     29
     30#endif /* _LIB_H */
  • src/lib/load_url.c

    diff -X /usr/src/dontdiff -urN nALFS/src/lib/load_url.c nALFS-new/src/lib/load_url.c
    old new  
     1/*
     2 *  load_url.c - Use libcurl to download from a URL.
     3 *
     4 *  Copyright (C) 2003
     5 * 
     6 *  Neven Has <haski@sezampro.yu>
     7 *  Vassili Dzuba <vdzuba@nerim.net>
     8 *
     9 *  This program is free software; you can redistribute it and/or modify
     10 *  it under the terms of the GNU General Public License as published by
     11 *  the Free Software Foundation; either version 2 of the License, or
     12 *  (at your option) any later version.
     13 *
     14 *  This program is distributed in the hope that it will be useful,
     15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 *  GNU General Public License for more details.
     18 *
     19 *  You should have received a copy of the GNU General Public License
     20 *  along with this program; if not, write to the Free Software
     21 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     22 */
     23
     24
     25#include <stdlib.h>
     26#include <string.h>
     27#include <unistd.h>
     28#include <errno.h>
     29#include <ctype.h>
     30#include <sys/types.h>
     31#include <sys/stat.h>
     32
     33#include "win.h"
     34
     35#ifdef HAVE_LIBCURL
     36
     37typedef struct output_file_s {
     38        const char *filename;
     39        FILE *stream;
     40} output_file_s;
     41
     42#include <curl/curl.h>
     43
     44static int my_fwrite(void *buffer, size_t size, size_t nmemb, void *data)
     45{
     46        output_file_s *output = (output_file_s *)data;
     47
     48
     49        if (output->stream == NULL) { /* Open the output file. */
     50                if ((output->stream = fopen(output->filename, "wb")) == NULL) {
     51                        Nprint_err("Unable to open %s for writing.",
     52                                        output->filename);
     53                        return -1;
     54                }
     55        }
     56
     57        return fwrite(buffer, size, nmemb, output->stream);
     58}
     59
     60int load_url(const char *archive, const char *url)
     61{
     62        CURL *handle;
     63        CURLcode err;
     64        char error_buffer[CURL_ERROR_SIZE];
     65        output_file_s output = { archive, NULL };
     66        FILE *error_file = NULL;
     67
     68
     69        Nprint("Downloading with Curl:");
     70        Nprint("    %s", url);
     71
     72        if ((err = curl_global_init(CURL_GLOBAL_ALL)) != 0) {
     73                Nprint_err("Error initializing Curl (%d).", err);
     74                return -1;
     75        }
     76
     77        if ((handle = curl_easy_init()) == NULL) {
     78                Nprint_err("Error initializing easy interface.");
     79                curl_global_cleanup();
     80                return -1;
     81        }
     82
     83        /* Can't ignore CURLOPT_STDERR when CURLOPT_ERRORBUFFER is set? */
     84        error_file = fopen("/dev/null", "w");
     85        if (error_file) {
     86                curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, error_buffer);
     87                curl_easy_setopt(handle, CURLOPT_STDERR, error_file);
     88        }
     89        /* Set URL to download, callback function and the output file. */
     90        curl_easy_setopt(handle, CURLOPT_URL, url);
     91        curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, my_fwrite);
     92        curl_easy_setopt(handle, CURLOPT_FILE, &output);
     93        curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1);
     94
     95        /* Start downloading. */
     96        err = curl_easy_perform(handle);
     97
     98        /* Cleanup. */
     99        curl_easy_cleanup(handle);
     100        curl_global_cleanup();
     101
     102        if (output.stream) { /* Close output file. */
     103                fclose(output.stream);
     104        }
     105        if (error_file) { /* Close error file. */
     106                fclose(error_file);
     107        }
     108
     109        if (err != CURLE_OK) {
     110                if (error_file) {
     111                        Nprint_err("Downloading with Curl failed:");
     112                        Nprint_err("    %s", error_buffer);
     113                } else {
     114                        Nprint_err("Downloading with Curl failed (%d).", err);
     115                }
     116
     117                return -1;
     118        }
     119
     120        Nprint("Downloading with Curl completed successfully.");
     121
     122        return 0;
     123}
     124
     125#endif