cvs commit: ALFS/nALFS/src/lib lib.h load_url.c digest.h

neven at linuxfromscratch.org neven at linuxfromscratch.org
Sun Sep 21 13:25:14 PDT 2003


neven       03/09/21 14:25:14

  Modified:    nALFS/src/handlers new-download.c new-unpack.c
  Added:       nALFS/src/lib lib.h load_url.c
  Removed:     nALFS/src/lib digest.h
  Log:
  Duplicated curl code moved from handlers to src/lib/.  src/lib/digest.h
  removed and common src/lib/lib.h created.  Patch by Kevin P. Fleming.
  
  Revision  Changes    Path
  1.2       +1 -94     ALFS/nALFS/src/handlers/new-download.c
  
  Index: new-download.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/handlers/new-download.c,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- new-download.c	19 Sep 2003 11:09:29 -0000	1.1
  +++ new-download.c	21 Sep 2003 20:25:13 -0000	1.2
  @@ -37,104 +37,11 @@
   #include "nalfs.h"
   #include "backend.h"
   #include "config.h"
  -#include "digest.h"
  +#include "lib.h"
   
   #define El_download_file(el) alloc_trimmed_param_value("file", el)
   #define El_download_destination(el) alloc_trimmed_param_value("destination", el)
   #define El_download_digest(el) alloc_trimmed_param_value("digest", el)
  -
  -#ifdef HAVE_LIBCURL
  -
  -typedef struct output_file_s {
  -	const char *filename;
  -	FILE *stream;
  -} output_file_s;
  -
  -#include <curl/curl.h>
  -
  -int my_fwrite(void *buffer, size_t size, size_t nmemb, void *data)
  -{
  -	output_file_s *output = (output_file_s *)data;
  -
  -
  -	if (output->stream == NULL) { /* Open the output file. */
  -		if ((output->stream = fopen(output->filename, "wb")) == NULL) {
  -			Nprint_err("Unable to open %s for writing.",
  -					output->filename);
  -			return -1;
  -		}
  -	}
  -
  -	return fwrite(buffer, size, nmemb, output->stream);
  -}
  -
  -int load_url(const char *archive, const char *url)
  -{
  -	CURL *handle;
  -	CURLcode err;
  -	char error_buffer[CURL_ERROR_SIZE];
  -	output_file_s output = { archive, NULL };
  -	FILE *error_file = NULL;
  -
  -
  -	Nprint("Downloading with Curl:");
  -	Nprint("    %s", url);
  -
  -	if ((err = curl_global_init(CURL_GLOBAL_ALL)) != 0) {
  -		Nprint_err("Error initializing Curl (%d).", err);
  -		return -1;
  -	}
  -
  -	if ((handle = curl_easy_init()) == NULL) {
  -		Nprint_err("Error initializing easy interface.");
  -		curl_global_cleanup();
  -		return -1;
  -	}
  -
  -	/* Can't ignore CURLOPT_STDERR when CURLOPT_ERRORBUFFER is set? */
  -	error_file = fopen("/dev/null", "w");
  -	if (error_file) {
  -		curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, error_buffer);
  -		curl_easy_setopt(handle, CURLOPT_STDERR, error_file);
  -	}
  -	/* Set URL to download, callback function and the output file. */
  -	curl_easy_setopt(handle, CURLOPT_URL, url);
  -	curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, my_fwrite);
  -	curl_easy_setopt(handle, CURLOPT_FILE, &output);
  -	curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1);
  -
  -	/* Start downloading. */
  -	err = curl_easy_perform(handle);
  -
  -	/* Cleanup. */
  -	curl_easy_cleanup(handle);
  -	curl_global_cleanup();
  -
  -	if (output.stream) { /* Close output file. */
  -		fclose(output.stream);
  -	}
  -	if (error_file) { /* Close error file. */
  -		fclose(error_file);
  -	}
  -
  -	if (err != CURLE_OK) {
  -		if (error_file) {
  -			Nprint_err("Downloading with Curl failed:");
  -			Nprint_err("    %s", error_buffer);
  -		} else {
  -			Nprint_err("Downloading with Curl failed (%d).", err);
  -		}
  -
  -		return -1;
  -	}
  -
  -	Nprint("Downloading with Curl completed successfully.");
  -
  -	return 0;
  -}
  -
  -#endif
  -
   
   static INLINE int get_url(const char *urldir, const char *file)
   {
  
  
  
  1.3       +1 -93     ALFS/nALFS/src/handlers/new-unpack.c
  
  Index: new-unpack.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/handlers/new-unpack.c,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- new-unpack.c	19 Sep 2003 10:49:01 -0000	1.2
  +++ new-unpack.c	21 Sep 2003 20:25:13 -0000	1.3
  @@ -36,7 +36,7 @@
   #include "nalfs.h"
   #include "backend.h"
   #include "config.h"
  -#include "digest.h"
  +#include "lib.h"
   
   
   #define El_unpack_reference(el) alloc_trimmed_param_value("reference", el)
  @@ -48,98 +48,6 @@
   typedef enum extension_e {
   	GZ, TAR_GZ, TGZ, BZ2, TAR_BZ2, TAR, ZIP, TAR_Z, Z, UNKNOWN
   } extension_e;
  -
  -#ifdef HAVE_LIBCURL
  -
  -typedef struct output_file_s {
  -	const char *filename;
  -	FILE *stream;
  -} output_file_s;
  -
  -#include <curl/curl.h>
  -
  -int my_fwrite(void *buffer, size_t size, size_t nmemb, void *data)
  -{
  -	output_file_s *output = (output_file_s *)data;
  -
  -
  -	if (output->stream == NULL) { /* Open the output file. */
  -		if ((output->stream = fopen(output->filename, "wb")) == NULL) {
  -			Nprint_err("Unable to open %s for writing.",
  -					output->filename);
  -			return -1;
  -		}
  -	}
  -
  -	return fwrite(buffer, size, nmemb, output->stream);
  -}
  -
  -int load_url(const char *archive, const char *url)
  -{
  -	CURL *handle;
  -	CURLcode err;
  -	char error_buffer[CURL_ERROR_SIZE];
  -	output_file_s output = { archive, NULL };
  -	FILE *error_file = NULL;
  -
  -
  -	Nprint("Downloading with Curl:");
  -	Nprint("    %s", url);
  -
  -	if ((err = curl_global_init(CURL_GLOBAL_ALL)) != 0) {
  -		Nprint_err("Error initializing Curl (%d).", err);
  -		return -1;
  -	}
  -
  -	if ((handle = curl_easy_init()) == NULL) {
  -		Nprint_err("Error initializing easy interface.");
  -		curl_global_cleanup();
  -		return -1;
  -	}
  -
  -	/* Can't ignore CURLOPT_STDERR when CURLOPT_ERRORBUFFER is set? */
  -	error_file = fopen("/dev/null", "w");
  -	if (error_file) {
  -		curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, error_buffer);
  -		curl_easy_setopt(handle, CURLOPT_STDERR, error_file);
  -	}
  -	/* Set URL to download, callback function and the output file. */
  -	curl_easy_setopt(handle, CURLOPT_URL, url);
  -	curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, my_fwrite);
  -	curl_easy_setopt(handle, CURLOPT_FILE, &output);
  -
  -	/* Start downloading. */
  -	err = curl_easy_perform(handle);
  -
  -	/* Cleanup. */
  -	curl_easy_cleanup(handle);
  -	curl_global_cleanup();
  -
  -	if (output.stream) { /* Close output file. */
  -		fclose(output.stream);
  -	}
  -	if (error_file) { /* Close error file. */
  -		fclose(error_file);
  -	}
  -
  -	if (err != CURLE_OK) {
  -		if (error_file) {
  -			Nprint_err("Downloading with Curl failed:");
  -			Nprint_err("    %s", error_buffer);
  -		} else {
  -			Nprint_err("Downloading with Curl failed (%d).", err);
  -		}
  -
  -		return -1;
  -	}
  -
  -	Nprint("Downloading with Curl completed successfully.");
  -
  -	return 0;
  -}
  -
  -#endif
  -
   
   static INLINE int get_reference(const char *reference, const char *archive)
   {
  
  
  
  1.1                  ALFS/nALFS/src/lib/lib.h
  
  Index: lib.h
  ===================================================================
  /*
   *  lib.h - Functions in the nALFS library.
   * 
   *  Copyright (C) 2003-2003
   *  
   *  Neven Has <haski at sezampro.yu> and
   *  Kevin P. Fleming <kpfleming at backtobasicsmgmt.com>
   *
   *  This program is free software; you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation; either version 2 of the License, or
   *  (at your option) any later version.
   *
   *  This program is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *  GNU General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License
   *  along with this program; if not, write to the Free Software
   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   */
  
  #ifndef _LIB_H
  #define _LIB_H
  
  extern int verify_digest(const char *type, char *digest, const char *file);
  extern int load_url(const char *output, const char *url);
  
  #endif /* _LIB_H */
  
  
  
  1.1                  ALFS/nALFS/src/lib/load_url.c
  
  Index: load_url.c
  ===================================================================
  /*
   *  load_url.c - Use libcurl to download from a URL.
   * 
   *  Copyright (C) 2003
   *  
   *  Neven Has <haski at sezampro.yu>
   *  Vassili Dzuba <vdzuba at nerim.net>
   *
   *  This program is free software; you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation; either version 2 of the License, or
   *  (at your option) any later version.
   *
   *  This program is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *  GNU General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License
   *  along with this program; if not, write to the Free Software
   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   */
  
  
  #include <stdlib.h>
  #include <string.h>
  #include <unistd.h>
  #include <errno.h>
  #include <ctype.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  
  #include "win.h"
  
  #ifdef HAVE_LIBCURL
  
  typedef struct output_file_s {
  	const char *filename;
  	FILE *stream;
  } output_file_s;
  
  #include <curl/curl.h>
  
  static int my_fwrite(void *buffer, size_t size, size_t nmemb, void *data)
  {
  	output_file_s *output = (output_file_s *)data;
  
  
  	if (output->stream == NULL) { /* Open the output file. */
  		if ((output->stream = fopen(output->filename, "wb")) == NULL) {
  			Nprint_err("Unable to open %s for writing.",
  					output->filename);
  			return -1;
  		}
  	}
  
  	return fwrite(buffer, size, nmemb, output->stream);
  }
  
  int load_url(const char *archive, const char *url)
  {
  	CURL *handle;
  	CURLcode err;
  	char error_buffer[CURL_ERROR_SIZE];
  	output_file_s output = { archive, NULL };
  	FILE *error_file = NULL;
  
  
  	Nprint("Downloading with Curl:");
  	Nprint("    %s", url);
  
  	if ((err = curl_global_init(CURL_GLOBAL_ALL)) != 0) {
  		Nprint_err("Error initializing Curl (%d).", err);
  		return -1;
  	}
  
  	if ((handle = curl_easy_init()) == NULL) {
  		Nprint_err("Error initializing easy interface.");
  		curl_global_cleanup();
  		return -1;
  	}
  
  	/* Can't ignore CURLOPT_STDERR when CURLOPT_ERRORBUFFER is set? */
  	error_file = fopen("/dev/null", "w");
  	if (error_file) {
  		curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, error_buffer);
  		curl_easy_setopt(handle, CURLOPT_STDERR, error_file);
  	}
  	/* Set URL to download, callback function and the output file. */
  	curl_easy_setopt(handle, CURLOPT_URL, url);
  	curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, my_fwrite);
  	curl_easy_setopt(handle, CURLOPT_FILE, &output);
  	curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1);
  
  	/* Start downloading. */
  	err = curl_easy_perform(handle);
  
  	/* Cleanup. */
  	curl_easy_cleanup(handle);
  	curl_global_cleanup();
  
  	if (output.stream) { /* Close output file. */
  		fclose(output.stream);
  	}
  	if (error_file) { /* Close error file. */
  		fclose(error_file);
  	}
  
  	if (err != CURLE_OK) {
  		if (error_file) {
  			Nprint_err("Downloading with Curl failed:");
  			Nprint_err("    %s", error_buffer);
  		} else {
  			Nprint_err("Downloading with Curl failed (%d).", err);
  		}
  
  		return -1;
  	}
  
  	Nprint("Downloading with Curl completed successfully.");
  
  	return 0;
  }
  
  #endif
  
  
  



More information about the alfs-log mailing list