2003-06-22 21:02:41 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* busybox patch applet to handle the unified diff format.
|
2007-09-21 18:46:32 +05:30
|
|
|
* Copyright (C) 2003 Glenn McGrath
|
2003-06-22 21:02:41 +05:30
|
|
|
*
|
2005-10-27 12:29:05 +05:30
|
|
|
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
2003-06-22 21:02:41 +05:30
|
|
|
*
|
|
|
|
* This applet is written to work with patches generated by GNU diff,
|
|
|
|
* where there is equivalent functionality busybox patch shall behave
|
|
|
|
* as per GNU patch.
|
|
|
|
*
|
|
|
|
* There is a SUSv3 specification for patch, however it looks to be
|
|
|
|
* incomplete, it doesnt even mention unified diff format.
|
|
|
|
* http://www.opengroup.org/onlinepubs/007904975/utilities/patch.html
|
|
|
|
*
|
|
|
|
* Issues
|
|
|
|
* - Non-interactive
|
2006-12-17 06:19:56 +05:30
|
|
|
* - Patches must apply cleanly or patch (not just one hunk) will fail.
|
2003-06-22 21:02:41 +05:30
|
|
|
* - Reject file isnt saved
|
|
|
|
*/
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2003-06-22 21:02:41 +05:30
|
|
|
|
2008-03-26 19:02:30 +05:30
|
|
|
static unsigned copy_lines(FILE *src_stream, FILE *dst_stream, unsigned lines_count)
|
2003-06-22 21:02:41 +05:30
|
|
|
{
|
2008-03-24 04:25:25 +05:30
|
|
|
while (src_stream && lines_count) {
|
2003-06-22 21:02:41 +05:30
|
|
|
char *line;
|
2006-10-13 04:13:20 +05:30
|
|
|
line = xmalloc_fgets(src_stream);
|
2003-06-22 21:02:41 +05:30
|
|
|
if (line == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
2008-03-26 19:02:30 +05:30
|
|
|
if (fputs(line, dst_stream) == EOF) {
|
2006-10-20 18:58:22 +05:30
|
|
|
bb_perror_msg_and_die("error writing to new file");
|
2003-06-22 21:02:41 +05:30
|
|
|
}
|
|
|
|
free(line);
|
2008-03-24 04:25:25 +05:30
|
|
|
lines_count--;
|
2003-06-22 21:02:41 +05:30
|
|
|
}
|
2008-03-24 04:25:25 +05:30
|
|
|
return lines_count;
|
2003-06-22 21:02:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* If patch_level is -1 it will remove all directory names
|
|
|
|
* char *line must be greater than 4 chars
|
|
|
|
* returns NULL if the file doesnt exist or error
|
|
|
|
* returns malloc'ed filename
|
2008-03-24 04:25:25 +05:30
|
|
|
* NB: frees 1st argument!
|
2003-06-22 21:02:41 +05:30
|
|
|
*/
|
2008-03-24 23:48:03 +05:30
|
|
|
static char *extract_filename(char *line, int patch_level, const char *pat)
|
2003-06-22 21:02:41 +05:30
|
|
|
{
|
2008-03-24 23:48:03 +05:30
|
|
|
char *temp = NULL, *filename_start_ptr = line + 4;
|
2003-06-22 21:02:41 +05:30
|
|
|
|
2008-03-24 23:48:03 +05:30
|
|
|
if (strncmp(line, pat, 4) == 0) {
|
|
|
|
/* Terminate string at end of source filename */
|
2008-06-26 20:02:57 +05:30
|
|
|
line[strcspn(line, "\t\n\r")] = '\0';
|
2003-06-22 21:02:41 +05:30
|
|
|
|
2008-03-24 23:48:03 +05:30
|
|
|
/* Skip over (patch_level) number of leading directories */
|
|
|
|
while (patch_level--) {
|
|
|
|
temp = strchr(filename_start_ptr, '/');
|
|
|
|
if (!temp)
|
|
|
|
break;
|
|
|
|
filename_start_ptr = temp + 1;
|
|
|
|
}
|
|
|
|
temp = xstrdup(filename_start_ptr);
|
2003-06-22 21:02:41 +05:30
|
|
|
}
|
2008-03-24 04:25:25 +05:30
|
|
|
free(line);
|
|
|
|
return temp;
|
2003-06-22 21:02:41 +05:30
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int patch_main(int argc UNUSED_PARAM, char **argv)
|
2003-06-22 21:02:41 +05:30
|
|
|
{
|
2008-03-18 07:08:04 +05:30
|
|
|
struct stat saved_stat;
|
2008-03-24 04:25:25 +05:30
|
|
|
char *patch_line;
|
|
|
|
FILE *patch_file;
|
|
|
|
int patch_level;
|
|
|
|
int ret = 0;
|
2008-03-26 19:02:30 +05:30
|
|
|
char plus = '+';
|
2008-03-24 04:25:25 +05:30
|
|
|
|
2008-03-24 07:48:03 +05:30
|
|
|
xfunc_error_retval = 2;
|
2005-09-01 08:32:23 +05:30
|
|
|
{
|
2008-03-24 04:25:25 +05:30
|
|
|
const char *p = "-1";
|
|
|
|
const char *i = "-"; /* compat */
|
2008-03-26 19:02:30 +05:30
|
|
|
if (getopt32(argv, "p:i:R", &p, &i) & 4)
|
|
|
|
plus = '-';
|
2008-03-24 04:25:25 +05:30
|
|
|
patch_level = xatoi(p); /* can be negative! */
|
|
|
|
patch_file = xfopen_stdin(i);
|
2003-06-22 21:02:41 +05:30
|
|
|
}
|
|
|
|
|
2008-03-27 01:34:27 +05:30
|
|
|
patch_line = xmalloc_fgetline(patch_file);
|
2003-06-22 21:02:41 +05:30
|
|
|
while (patch_line) {
|
|
|
|
FILE *src_stream;
|
|
|
|
FILE *dst_stream;
|
2008-03-25 19:45:39 +05:30
|
|
|
//char *old_filename;
|
2003-06-22 21:02:41 +05:30
|
|
|
char *new_filename;
|
|
|
|
char *backup_filename;
|
2008-03-24 04:25:25 +05:30
|
|
|
unsigned src_cur_line = 1;
|
2008-03-26 19:02:30 +05:30
|
|
|
unsigned dst_cur_line = 0;
|
|
|
|
unsigned dst_beg_line;
|
2008-03-24 04:25:25 +05:30
|
|
|
unsigned bad_hunk_count = 0;
|
|
|
|
unsigned hunk_count = 0;
|
|
|
|
smallint copy_trailing_lines_flag = 0;
|
2003-06-22 21:02:41 +05:30
|
|
|
|
|
|
|
/* Skip everything upto the "---" marker
|
|
|
|
* No need to parse the lines "Only in <dir>", and "diff <args>"
|
|
|
|
*/
|
2008-03-24 23:48:03 +05:30
|
|
|
do {
|
|
|
|
/* Extract the filename used before the patch was generated */
|
2008-03-25 19:45:39 +05:30
|
|
|
new_filename = extract_filename(patch_line, patch_level, "--- ");
|
|
|
|
// was old_filename above
|
2008-03-27 01:34:27 +05:30
|
|
|
patch_line = xmalloc_fgetline(patch_file);
|
2008-03-24 23:48:03 +05:30
|
|
|
if (!patch_line) goto quit;
|
2008-03-25 19:45:39 +05:30
|
|
|
} while (!new_filename);
|
|
|
|
free(new_filename); // "source" filename is irrelevant
|
2003-06-22 21:02:41 +05:30
|
|
|
|
2008-03-24 23:48:03 +05:30
|
|
|
new_filename = extract_filename(patch_line, patch_level, "+++ ");
|
|
|
|
if (!new_filename) {
|
2008-03-24 04:25:25 +05:30
|
|
|
bb_error_msg_and_die("invalid patch");
|
2008-03-24 23:48:03 +05:30
|
|
|
}
|
2008-03-24 07:48:03 +05:30
|
|
|
|
2008-03-24 23:48:03 +05:30
|
|
|
/* Get access rights from the file to be patched */
|
2008-03-24 04:25:25 +05:30
|
|
|
if (stat(new_filename, &saved_stat) != 0) {
|
2008-03-24 23:48:03 +05:30
|
|
|
char *slash = strrchr(new_filename, '/');
|
|
|
|
if (slash) {
|
|
|
|
/* Create leading directories */
|
|
|
|
*slash = '\0';
|
2003-06-22 21:02:41 +05:30
|
|
|
bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
|
2008-03-24 23:48:03 +05:30
|
|
|
*slash = '/';
|
2003-06-22 21:02:41 +05:30
|
|
|
}
|
|
|
|
backup_filename = NULL;
|
2008-03-25 19:45:39 +05:30
|
|
|
src_stream = NULL;
|
2008-03-24 04:25:25 +05:30
|
|
|
saved_stat.st_mode = 0644;
|
2003-06-22 21:02:41 +05:30
|
|
|
} else {
|
2008-03-24 04:25:25 +05:30
|
|
|
backup_filename = xasprintf("%s.orig", new_filename);
|
2008-02-17 19:58:53 +05:30
|
|
|
xrename(new_filename, backup_filename);
|
2008-07-22 04:35:26 +05:30
|
|
|
src_stream = xfopen_for_read(backup_filename);
|
2003-06-22 21:02:41 +05:30
|
|
|
}
|
2008-07-22 04:35:26 +05:30
|
|
|
dst_stream = xfopen_for_write(new_filename);
|
2008-03-24 04:25:25 +05:30
|
|
|
fchmod(fileno(dst_stream), saved_stat.st_mode);
|
2003-06-22 21:02:41 +05:30
|
|
|
|
|
|
|
printf("patching file %s\n", new_filename);
|
|
|
|
|
2008-03-24 04:25:25 +05:30
|
|
|
/* Handle all hunks for this file */
|
2006-10-13 04:13:20 +05:30
|
|
|
patch_line = xmalloc_fgets(patch_file);
|
2003-06-22 21:02:41 +05:30
|
|
|
while (patch_line) {
|
2008-03-24 04:25:25 +05:30
|
|
|
unsigned count;
|
|
|
|
unsigned src_beg_line;
|
2008-03-24 23:48:03 +05:30
|
|
|
unsigned hunk_offset_start;
|
|
|
|
unsigned src_last_line = 1;
|
2008-03-26 19:02:30 +05:30
|
|
|
unsigned dst_last_line = 1;
|
2008-03-24 23:48:03 +05:30
|
|
|
|
2008-03-26 19:02:30 +05:30
|
|
|
if ((sscanf(patch_line, "@@ -%d,%d +%d,%d", &src_beg_line, &src_last_line, &dst_beg_line, &dst_last_line) < 3)
|
|
|
|
&& (sscanf(patch_line, "@@ -%d +%d,%d", &src_beg_line, &dst_beg_line, &dst_last_line) < 2)
|
|
|
|
) {
|
|
|
|
/* No more hunks for this file */
|
2003-06-22 21:02:41 +05:30
|
|
|
break;
|
|
|
|
}
|
2008-03-26 19:02:30 +05:30
|
|
|
if (plus != '+') {
|
|
|
|
/* reverse patch */
|
|
|
|
unsigned tmp = src_last_line;
|
|
|
|
src_last_line = dst_last_line;
|
|
|
|
dst_last_line = tmp;
|
|
|
|
tmp = src_beg_line;
|
|
|
|
src_beg_line = dst_beg_line;
|
|
|
|
dst_beg_line = tmp;
|
|
|
|
}
|
2003-06-22 21:02:41 +05:30
|
|
|
hunk_count++;
|
|
|
|
|
2008-03-26 19:02:30 +05:30
|
|
|
if (src_beg_line && dst_beg_line) {
|
2003-06-22 21:02:41 +05:30
|
|
|
/* Copy unmodified lines upto start of hunk */
|
2008-03-24 04:25:25 +05:30
|
|
|
/* src_beg_line will be 0 if it's a new file */
|
2003-06-22 21:02:41 +05:30
|
|
|
count = src_beg_line - src_cur_line;
|
2008-03-24 04:25:25 +05:30
|
|
|
if (copy_lines(src_stream, dst_stream, count)) {
|
2006-10-20 18:58:22 +05:30
|
|
|
bb_error_msg_and_die("bad src file");
|
2003-06-22 21:02:41 +05:30
|
|
|
}
|
|
|
|
src_cur_line += count;
|
2008-03-26 19:02:30 +05:30
|
|
|
dst_cur_line += count;
|
2003-06-22 21:02:41 +05:30
|
|
|
copy_trailing_lines_flag = 1;
|
|
|
|
}
|
2008-03-24 23:48:03 +05:30
|
|
|
src_last_line += hunk_offset_start = src_cur_line;
|
2008-03-26 19:02:30 +05:30
|
|
|
dst_last_line += dst_cur_line;
|
2003-06-22 21:02:41 +05:30
|
|
|
|
2008-03-24 04:25:25 +05:30
|
|
|
while (1) {
|
|
|
|
free(patch_line);
|
2008-03-26 19:02:30 +05:30
|
|
|
patch_line = xmalloc_fgets(patch_file);
|
2008-03-24 23:48:03 +05:30
|
|
|
if (patch_line == NULL)
|
|
|
|
break; /* EOF */
|
|
|
|
if ((*patch_line != '-') && (*patch_line != '+')
|
|
|
|
&& (*patch_line != ' ')
|
|
|
|
) {
|
|
|
|
break; /* End of hunk */
|
|
|
|
}
|
2008-03-26 19:02:30 +05:30
|
|
|
if (*patch_line != plus) { /* '-' or ' ' */
|
2003-06-22 21:02:41 +05:30
|
|
|
char *src_line = NULL;
|
2008-03-24 23:48:03 +05:30
|
|
|
if (src_cur_line == src_last_line)
|
|
|
|
break;
|
2003-06-22 21:02:41 +05:30
|
|
|
if (src_stream) {
|
2006-10-13 04:13:20 +05:30
|
|
|
src_line = xmalloc_fgets(src_stream);
|
2008-03-24 04:25:25 +05:30
|
|
|
if (src_line) {
|
|
|
|
int diff = strcmp(src_line, patch_line + 1);
|
2003-06-22 21:02:41 +05:30
|
|
|
src_cur_line++;
|
2008-03-24 04:25:25 +05:30
|
|
|
free(src_line);
|
2008-03-26 19:02:30 +05:30
|
|
|
if (diff)
|
|
|
|
src_line = NULL;
|
2003-06-22 21:02:41 +05:30
|
|
|
}
|
|
|
|
}
|
2008-03-24 23:48:03 +05:30
|
|
|
if (!src_line) {
|
|
|
|
bb_error_msg("hunk #%u FAILED at %u", hunk_count, hunk_offset_start);
|
|
|
|
bad_hunk_count++;
|
|
|
|
break;
|
|
|
|
}
|
2008-03-26 19:02:30 +05:30
|
|
|
if (*patch_line != ' ') { /* '-' */
|
2008-03-24 23:48:03 +05:30
|
|
|
continue;
|
2003-06-22 21:02:41 +05:30
|
|
|
}
|
|
|
|
}
|
2008-03-26 19:02:30 +05:30
|
|
|
if (dst_cur_line == dst_last_line)
|
|
|
|
break;
|
2008-03-24 23:48:03 +05:30
|
|
|
fputs(patch_line + 1, dst_stream);
|
2008-03-26 19:02:30 +05:30
|
|
|
dst_cur_line++;
|
2008-03-24 04:25:25 +05:30
|
|
|
} /* end of while loop handling one hunk */
|
|
|
|
} /* end of while loop handling one file */
|
2003-06-22 21:02:41 +05:30
|
|
|
|
|
|
|
/* Cleanup last patched file */
|
|
|
|
if (copy_trailing_lines_flag) {
|
2008-03-24 04:25:25 +05:30
|
|
|
copy_lines(src_stream, dst_stream, (unsigned)(-1));
|
2003-06-22 21:02:41 +05:30
|
|
|
}
|
|
|
|
if (src_stream) {
|
|
|
|
fclose(src_stream);
|
|
|
|
}
|
2008-03-24 23:48:03 +05:30
|
|
|
fclose(dst_stream);
|
2003-06-22 21:02:41 +05:30
|
|
|
if (bad_hunk_count) {
|
2008-03-24 04:25:25 +05:30
|
|
|
ret = 1;
|
|
|
|
bb_error_msg("%u out of %u hunk FAILED", bad_hunk_count, hunk_count);
|
2003-06-22 21:02:41 +05:30
|
|
|
} else {
|
|
|
|
/* It worked, we can remove the backup */
|
|
|
|
if (backup_filename) {
|
|
|
|
unlink(backup_filename);
|
|
|
|
}
|
2008-03-26 19:02:30 +05:30
|
|
|
if ((dst_cur_line == 0) || (dst_beg_line == 0)) {
|
2003-06-22 21:02:41 +05:30
|
|
|
/* The new patched file is empty, remove it */
|
2007-04-06 02:55:15 +05:30
|
|
|
xunlink(new_filename);
|
2008-03-25 19:45:39 +05:30
|
|
|
// /* old_filename and new_filename may be the same file */
|
|
|
|
// unlink(old_filename);
|
2003-06-22 21:02:41 +05:30
|
|
|
}
|
|
|
|
}
|
2008-03-25 19:45:39 +05:30
|
|
|
free(backup_filename);
|
|
|
|
//free(old_filename);
|
|
|
|
free(new_filename);
|
2008-03-24 04:25:25 +05:30
|
|
|
} /* end of "while there are patch lines" */
|
2008-03-26 19:02:30 +05:30
|
|
|
quit:
|
2004-03-15 13:59:22 +05:30
|
|
|
/* 0 = SUCCESS
|
2003-06-22 21:02:41 +05:30
|
|
|
* 1 = Some hunks failed
|
2008-03-24 04:25:25 +05:30
|
|
|
* 2 = More serious problems (exited earlier)
|
2003-06-22 21:02:41 +05:30
|
|
|
*/
|
2006-11-27 22:19:31 +05:30
|
|
|
return ret;
|
2003-06-22 21:02:41 +05:30
|
|
|
}
|