cvs commit: ALFS/nALFS/src/handlers patch.c

kpfleming at linuxfromscratch.org kpfleming at linuxfromscratch.org
Sun Nov 23 12:31:12 PST 2003


kpfleming    03/11/23 13:31:12

  Modified:    DTD      ALFS-CVS.dtd
               nALFS/src/handlers patch.c
  Log:
  implement <patch> syntax version 3.2
  
  Revision  Changes    Path
  1.3       +4 -2      ALFS/DTD/ALFS-CVS.dtd
  
  Index: ALFS-CVS.dtd
  ===================================================================
  RCS file: /home/cvsroot/ALFS/DTD/ALFS-CVS.dtd,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ALFS-CVS.dtd	23 Nov 2003 19:38:16 -0000	1.2
  +++ ALFS-CVS.dtd	23 Nov 2003 20:31:12 -0000	1.3
  @@ -108,9 +108,11 @@
   
   <!ELEMENT param          (#PCDATA)>
   
  -<!ELEMENT patch          ((param | prefix)*)>
  +<!ELEMENT patch          (digest?, reference*, file, (param | prefix)*)>
   <!ATTLIST patch
  -          base           CDATA #IMPLIED>
  +          base           CDATA #IMPLIED
  +          mode           CDATA #IMPLIED
  +          path_strip     CDATA #IMPLIED>
   
   <!ELEMENT permissions    (option?, name*)>
   <!ATTLIST permissions
  
  
  
  1.15      +134 -4    ALFS/nALFS/src/handlers/patch.c
  
  Index: patch.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/handlers/patch.c,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- patch.c	23 Nov 2003 19:38:16 -0000	1.14
  +++ patch.c	23 Nov 2003 20:31:12 -0000	1.15
  @@ -25,6 +25,8 @@
   #include <string.h>
   #include <unistd.h>
   #include <errno.h>
  +#include <sys/stat.h>
  +
   
   #ifdef HAVE_CONFIG_H
   #include <config.h>
  @@ -80,7 +82,7 @@
   #endif /* HANDLER_SYNTAX_2_0 */
   
   
  -#if HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1 || HANDLER_SYNTAX_3_2
  +#if HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1
   
   static const char *patch_parameters_ver3[] = { "param", "prefix", NULL };
   // char *HANDLER_SYMBOL(attributes)[] = { "base", NULL };
  @@ -134,8 +136,136 @@
   	return status;
   }
   
  -#endif /* HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1 || HANDLER_SYNTAX_3_2 */
  +#endif /* HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1 */
  +
  +
  +#if HANDLER_SYNTAX_3_2
  +
  +static const char *patch_parameters_ver3_2[] = { "digest", "reference", "file",
  +						 "param", "prefix", NULL };
  +// char *HANDLER_SYMBOL(attributes)[] = { "base", "mode", "path_strip", NULL };
  +
  +static int patch_main_ver3_2(element_s *el)
  +{
  +        int status = -1;
  +	char *base = NULL;
  +	char *file = NULL;
  +	char *command = NULL;
  +	char *digest = NULL;
  +	char *digest_type = NULL;
  +	char *decompressor = NULL;
  +	char *mode;
  +	char *path_strip;
  +	struct stat file_stat;
  +
  +
  +	if ((file = alloc_trimmed_param_value("file", el)) == NULL) {
  +		Nprint_h_err("File name is missing.");
  +		goto free_all_and_return;
  +	}
  +
  +	base = alloc_base_dir_new(el);
  +
  +	if (change_current_dir(base)) {
  +		goto free_all_and_return;
  +	}
  +	
  +	alloc_element_digest(el, &digest, &digest_type);
   
  +	/* Check if file exists and if not attempt to download it. */
  +	if ((stat(file, &file_stat))) {
  +		if (errno == ENOENT && first_param("reference", el) != NULL) {
  +			int found = 0;
  +			element_s *p;
  +
  +			Nprint_h_warn("File %s not found.", file);
  +			Nprint_h("Trying to fetch it from <reference>...");
  +
  +			for (p = first_param("reference", el); p; p = next_param(p)) {
  +				char *s;
  +
  +				if ((s = alloc_trimmed_str(p->content)) == NULL) {
  +					Nprint_h_warn("Source empty.");
  +					continue;
  +				}
  +
  +				if (! get_url(s, file, digest, digest_type)) {
  +					found = 1;
  +				}
  +				xfree(s);
  +				if (found)
  +					break;
  +			}
  +
  +			if (!found) {
  +				Nprint_h_err("Unable to download file %s.",
  +					     file);
  +				goto free_all_and_return;
  +			}
  +		} else {
  +			Nprint_h_err("Unable to download file %s.", file);
  +			goto free_all_and_return;
  +		}
  +	} else if (digest && verify_digest(digest_type, digest, file)) {
  +		Nprint_h_err("Wrong %s digest of file: %s", digest_type,
  +			     file);
  +		goto free_all_and_return;
  +	}
  +
  +	Nprint_h("Patching in %s", base);
  +
  +	decompressor = alloc_decompress_command(get_compression_type(file));
  +
  +	if (decompressor == NULL)
  +		decompressor = xstrdup("cat %s | %s");
  +	else
  +		append_str(&decompressor, " %s");
  +
  +	command = xstrdup("");
  +
  +	append_prefix_elements(&command, el);
  +
  +	append_str(&command, "patch -p");
  +
  +	path_strip = attr_value("path_strip", el);
  +	if (path_strip)
  +		append_str(&command, path_strip);
  +	else
  +		append_str(&command, "1");
  +	
  +	mode = attr_value("mode", el);
  +	if (mode) {
  +		if (strcmp(mode, "forward") == 0)
  +			append_str(&command, " -N");
  +		else if (strcmp(mode, "reverse") == 0)
  +			append_str(&command, " -R");
  +		else {
  +			Nprint_h_err("Unknown mode specified: %s", mode);
  +			goto free_all_and_return;
  +		}
  +	} else
  +		append_str(&command, " -N");
  +
  +	append_param_elements(&command, el);
  +
  +	Nprint_h("    %s", command);
  +
  +	if ((status = execute_command(decompressor, file, command))) {
  +		Nprint_h_err("Patching failed.");
  +	}
  +	
  +free_all_and_return:
  +	xfree(decompressor);
  +	xfree(digest_type);
  +	xfree(digest);
  +	xfree(file);
  +	xfree(command);
  +	xfree(base);
  +
  +	return status;
  +}
  +
  +#endif /* HANDLER_SYNTAX_3_2 */
   
   /*
    * Handlers' information.
  @@ -186,8 +316,8 @@
   		.name = "patch",
   		.description = "Patch",
   		.syntax_version = "3.2",
  -		.parameters = patch_parameters_ver3,
  -		.main = patch_main_ver3,
  +		.parameters = patch_parameters_ver3_2,
  +		.main = patch_main_ver3_2,
   		.type = 0,
   		.alloc_data = NULL,
   		.is_action = 1,
  
  
  



More information about the alfs-log mailing list