2003-06-22 15:32:41 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* busybox patch applet to handle the unified diff format.
|
2004-04-25 05:11:19 +00:00
|
|
|
* Copyright (C) 2003 Glenn McGrath <bug1@iinet.net.au>
|
2003-06-22 15:32:41 +00:00
|
|
|
*
|
2005-10-27 06:59:05 +00:00
|
|
|
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
2003-06-22 15:32:41 +00:00
|
|
|
*
|
|
|
|
* 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 00:49:56 +00:00
|
|
|
* - Patches must apply cleanly or patch (not just one hunk) will fail.
|
2003-06-22 15:32:41 +00:00
|
|
|
* - Reject file isnt saved
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <getopt.h>
|
2007-05-26 19:00:18 +00:00
|
|
|
|
|
|
|
#include "libbb.h"
|
2003-06-22 15:32:41 +00:00
|
|
|
|
2006-01-31 09:44:04 +00:00
|
|
|
static unsigned int copy_lines(FILE *src_stream, FILE *dest_stream, const unsigned int lines_count)
|
2003-06-22 15:32:41 +00:00
|
|
|
{
|
2006-01-31 09:44:04 +00:00
|
|
|
unsigned int i = 0;
|
2003-06-22 15:32:41 +00:00
|
|
|
|
|
|
|
while (src_stream && (i < lines_count)) {
|
|
|
|
char *line;
|
2006-10-12 22:43:20 +00:00
|
|
|
line = xmalloc_fgets(src_stream);
|
2003-06-22 15:32:41 +00:00
|
|
|
if (line == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fputs(line, dest_stream) == EOF) {
|
2006-10-20 13:28:22 +00:00
|
|
|
bb_perror_msg_and_die("error writing to new file");
|
2003-06-22 15:32:41 +00:00
|
|
|
}
|
|
|
|
free(line);
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2003-06-22 15:32:41 +00:00
|
|
|
i++;
|
|
|
|
}
|
2006-11-27 16:49:31 +00:00
|
|
|
return i;
|
2003-06-22 15:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
|
2006-01-31 09:44:04 +00:00
|
|
|
static char *extract_filename(char *line, int patch_level)
|
2003-06-22 15:32:41 +00:00
|
|
|
{
|
2006-05-07 21:10:06 +00:00
|
|
|
char *temp, *filename_start_ptr = line + 4;
|
2003-06-22 15:32:41 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Terminate string at end of source filename */
|
2006-05-07 21:10:06 +00:00
|
|
|
temp = strchr(filename_start_ptr, '\t');
|
|
|
|
if (temp) *temp = 0;
|
2003-06-22 15:32:41 +00:00
|
|
|
|
2006-05-07 21:10:06 +00:00
|
|
|
/* skip over (patch_level) number of leading directories */
|
2003-06-22 15:32:41 +00:00
|
|
|
for (i = 0; i < patch_level; i++) {
|
2007-04-12 00:32:05 +00:00
|
|
|
temp = strchr(filename_start_ptr, '/');
|
|
|
|
if (!temp)
|
|
|
|
break;
|
2006-05-07 21:10:06 +00:00
|
|
|
filename_start_ptr = temp + 1;
|
2003-06-22 15:32:41 +00:00
|
|
|
}
|
|
|
|
|
2006-11-27 16:49:31 +00:00
|
|
|
return xstrdup(filename_start_ptr);
|
2003-06-22 15:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int file_doesnt_exist(const char *filename)
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
2006-11-27 16:49:31 +00:00
|
|
|
return stat(filename, &statbuf);
|
2003-06-22 15:32:41 +00:00
|
|
|
}
|
|
|
|
|
2007-02-03 17:28:39 +00:00
|
|
|
int patch_main(int argc, char **argv);
|
2006-03-06 20:47:33 +00:00
|
|
|
int patch_main(int argc, char **argv)
|
2003-06-22 15:32:41 +00:00
|
|
|
{
|
2006-01-31 09:44:04 +00:00
|
|
|
int patch_level = -1;
|
2003-06-22 15:32:41 +00:00
|
|
|
char *patch_line;
|
2005-09-01 03:02:23 +00:00
|
|
|
int ret;
|
|
|
|
FILE *patch_file = NULL;
|
2003-06-22 15:32:41 +00:00
|
|
|
|
2005-09-01 03:02:23 +00:00
|
|
|
{
|
|
|
|
char *p, *i;
|
2006-10-03 21:00:06 +00:00
|
|
|
ret = getopt32(argc, argv, "p:i:", &p, &i);
|
2005-09-01 03:02:23 +00:00
|
|
|
if (ret & 1)
|
2006-10-08 12:49:22 +00:00
|
|
|
patch_level = xatol_range(p, -1, USHRT_MAX);
|
2005-09-01 03:02:23 +00:00
|
|
|
if (ret & 2) {
|
2006-08-03 15:41:12 +00:00
|
|
|
patch_file = xfopen(i, "r");
|
2005-10-10 13:34:19 +00:00
|
|
|
} else {
|
|
|
|
patch_file = stdin;
|
2005-09-01 03:02:23 +00:00
|
|
|
}
|
|
|
|
ret = 0;
|
2003-06-22 15:32:41 +00:00
|
|
|
}
|
|
|
|
|
2006-10-12 22:43:20 +00:00
|
|
|
patch_line = xmalloc_fgets(patch_file);
|
2003-06-22 15:32:41 +00:00
|
|
|
while (patch_line) {
|
|
|
|
FILE *src_stream;
|
|
|
|
FILE *dst_stream;
|
|
|
|
char *original_filename;
|
|
|
|
char *new_filename;
|
|
|
|
char *backup_filename;
|
|
|
|
unsigned int src_cur_line = 1;
|
|
|
|
unsigned int dest_cur_line = 0;
|
|
|
|
unsigned int dest_beg_line;
|
|
|
|
unsigned int bad_hunk_count = 0;
|
|
|
|
unsigned int hunk_count = 0;
|
|
|
|
char copy_trailing_lines_flag = 0;
|
|
|
|
|
|
|
|
/* Skip everything upto the "---" marker
|
|
|
|
* No need to parse the lines "Only in <dir>", and "diff <args>"
|
|
|
|
*/
|
|
|
|
while (patch_line && strncmp(patch_line, "--- ", 4) != 0) {
|
|
|
|
free(patch_line);
|
2006-10-12 22:43:20 +00:00
|
|
|
patch_line = xmalloc_fgets(patch_file);
|
2003-06-22 15:32:41 +00:00
|
|
|
}
|
2006-11-30 16:41:15 +00:00
|
|
|
/* FIXME: patch_line NULL check?? */
|
2003-06-22 15:32:41 +00:00
|
|
|
|
|
|
|
/* Extract the filename used before the patch was generated */
|
|
|
|
original_filename = extract_filename(patch_line, patch_level);
|
|
|
|
free(patch_line);
|
|
|
|
|
2006-10-12 22:43:20 +00:00
|
|
|
patch_line = xmalloc_fgets(patch_file);
|
2006-11-30 16:41:15 +00:00
|
|
|
/* FIXME: NULL check?? */
|
2003-06-22 15:32:41 +00:00
|
|
|
if (strncmp(patch_line, "+++ ", 4) != 0) {
|
|
|
|
ret = 2;
|
2006-10-20 13:28:22 +00:00
|
|
|
bb_error_msg("invalid patch");
|
2003-06-22 15:32:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
new_filename = extract_filename(patch_line, patch_level);
|
|
|
|
free(patch_line);
|
|
|
|
|
|
|
|
if (file_doesnt_exist(new_filename)) {
|
|
|
|
char *line_ptr;
|
|
|
|
/* Create leading directories */
|
|
|
|
line_ptr = strrchr(new_filename, '/');
|
|
|
|
if (line_ptr) {
|
|
|
|
*line_ptr = '\0';
|
|
|
|
bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
|
|
|
|
*line_ptr = '/';
|
|
|
|
}
|
2006-08-03 15:41:12 +00:00
|
|
|
dst_stream = xfopen(new_filename, "w+");
|
2003-06-22 15:32:41 +00:00
|
|
|
backup_filename = NULL;
|
|
|
|
} else {
|
|
|
|
backup_filename = xmalloc(strlen(new_filename) + 6);
|
|
|
|
strcpy(backup_filename, new_filename);
|
|
|
|
strcat(backup_filename, ".orig");
|
|
|
|
if (rename(new_filename, backup_filename) == -1) {
|
2006-10-12 19:29:44 +00:00
|
|
|
bb_perror_msg_and_die("cannot create file %s",
|
2005-10-10 13:34:19 +00:00
|
|
|
backup_filename);
|
2003-06-22 15:32:41 +00:00
|
|
|
}
|
2006-08-03 15:41:12 +00:00
|
|
|
dst_stream = xfopen(new_filename, "w");
|
2003-06-22 15:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((backup_filename == NULL) || file_doesnt_exist(original_filename)) {
|
|
|
|
src_stream = NULL;
|
|
|
|
} else {
|
|
|
|
if (strcmp(original_filename, new_filename) == 0) {
|
2006-08-03 15:41:12 +00:00
|
|
|
src_stream = xfopen(backup_filename, "r");
|
2003-06-22 15:32:41 +00:00
|
|
|
} else {
|
2006-08-03 15:41:12 +00:00
|
|
|
src_stream = xfopen(original_filename, "r");
|
2003-06-22 15:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("patching file %s\n", new_filename);
|
|
|
|
|
|
|
|
/* Handle each hunk */
|
2006-10-12 22:43:20 +00:00
|
|
|
patch_line = xmalloc_fgets(patch_file);
|
2003-06-22 15:32:41 +00:00
|
|
|
while (patch_line) {
|
|
|
|
unsigned int count;
|
|
|
|
unsigned int src_beg_line;
|
|
|
|
unsigned int unused;
|
|
|
|
unsigned int hunk_offset_start = 0;
|
|
|
|
int hunk_error = 0;
|
|
|
|
|
|
|
|
/* This bit should be improved */
|
|
|
|
if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line, &unused, &dest_beg_line, &unused) != 4) &&
|
|
|
|
(sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line, &unused, &dest_beg_line) != 3) &&
|
|
|
|
(sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line, &dest_beg_line, &unused) != 3)) {
|
|
|
|
/* No more hunks for this file */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(patch_line);
|
|
|
|
hunk_count++;
|
|
|
|
|
|
|
|
if (src_beg_line && dest_beg_line) {
|
|
|
|
/* Copy unmodified lines upto start of hunk */
|
|
|
|
/* src_beg_line will be 0 if its a new file */
|
|
|
|
count = src_beg_line - src_cur_line;
|
|
|
|
if (copy_lines(src_stream, dst_stream, count) != count) {
|
2006-10-20 13:28:22 +00:00
|
|
|
bb_error_msg_and_die("bad src file");
|
2003-06-22 15:32:41 +00:00
|
|
|
}
|
|
|
|
src_cur_line += count;
|
|
|
|
dest_cur_line += count;
|
|
|
|
copy_trailing_lines_flag = 1;
|
|
|
|
}
|
|
|
|
hunk_offset_start = src_cur_line;
|
|
|
|
|
2006-10-12 22:43:20 +00:00
|
|
|
while ((patch_line = xmalloc_fgets(patch_file)) != NULL) {
|
2003-06-22 15:32:41 +00:00
|
|
|
if ((*patch_line == '-') || (*patch_line == ' ')) {
|
|
|
|
char *src_line = NULL;
|
|
|
|
if (src_stream) {
|
2006-10-12 22:43:20 +00:00
|
|
|
src_line = xmalloc_fgets(src_stream);
|
2003-06-22 15:32:41 +00:00
|
|
|
if (!src_line) {
|
|
|
|
hunk_error++;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
src_cur_line++;
|
|
|
|
}
|
|
|
|
if (strcmp(src_line, patch_line + 1) != 0) {
|
2006-10-20 13:28:22 +00:00
|
|
|
bb_error_msg("hunk #%d FAILED at %d", hunk_count, hunk_offset_start);
|
2003-06-22 15:32:41 +00:00
|
|
|
hunk_error++;
|
|
|
|
free(patch_line);
|
2006-12-17 00:49:56 +00:00
|
|
|
/* Probably need to find next hunk, etc... */
|
|
|
|
/* but for now we just bail out */
|
2006-12-17 00:33:29 +00:00
|
|
|
patch_line = NULL;
|
2003-06-22 15:32:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(src_line);
|
|
|
|
}
|
|
|
|
if (*patch_line == ' ') {
|
|
|
|
fputs(patch_line + 1, dst_stream);
|
|
|
|
dest_cur_line++;
|
|
|
|
}
|
|
|
|
} else if (*patch_line == '+') {
|
|
|
|
fputs(patch_line + 1, dst_stream);
|
|
|
|
dest_cur_line++;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(patch_line);
|
|
|
|
}
|
|
|
|
if (hunk_error) {
|
|
|
|
bad_hunk_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cleanup last patched file */
|
|
|
|
if (copy_trailing_lines_flag) {
|
|
|
|
copy_lines(src_stream, dst_stream, -1);
|
|
|
|
}
|
|
|
|
if (src_stream) {
|
|
|
|
fclose(src_stream);
|
|
|
|
}
|
|
|
|
if (dst_stream) {
|
|
|
|
fclose(dst_stream);
|
|
|
|
}
|
|
|
|
if (bad_hunk_count) {
|
|
|
|
if (!ret) {
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
bb_error_msg("%d out of %d hunk FAILED", bad_hunk_count, hunk_count);
|
|
|
|
} else {
|
|
|
|
/* It worked, we can remove the backup */
|
|
|
|
if (backup_filename) {
|
|
|
|
unlink(backup_filename);
|
|
|
|
}
|
|
|
|
if ((dest_cur_line == 0) || (dest_beg_line == 0)) {
|
|
|
|
/* The new patched file is empty, remove it */
|
2007-04-05 21:25:15 +00:00
|
|
|
xunlink(new_filename);
|
|
|
|
if (strcmp(new_filename, original_filename) != 0)
|
|
|
|
xunlink(original_filename);
|
2003-06-22 15:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-15 08:29:22 +00:00
|
|
|
/* 0 = SUCCESS
|
2003-06-22 15:32:41 +00:00
|
|
|
* 1 = Some hunks failed
|
|
|
|
* 2 = More serious problems
|
|
|
|
*/
|
2006-11-27 16:49:31 +00:00
|
|
|
return ret;
|
2003-06-22 15:32:41 +00:00
|
|
|
}
|