summaryrefslogtreecommitdiff
path: root/src/patch.c
Commit message (Collapse)AuthorAgeFilesLines
* Move path traversal error reporting into main()Andreas Gruenbacher2015-03-051-4/+18
| | | | | | | * src/safe.c (traverse_another_path): Don't report errors here. * src/patch.c (main): Instead, recognize and report them here. Detect when an output file name is invalid; it doesn't make sense to try creating a reject file based on the same outbut file name in that case.
* Move error reporting out of make_tempfile()Andreas Gruenbacher2015-02-281-0/+4
| | | | | | | * src/util.c (make_tempfile): Remove error reporting here. * src/inp.c (plan_b): Readd error reporting here. * src/patch.c (main): Likewise. * src/pch.c (open_patch_file): Likewise.
* Fix handling of renamed filesAndreas Gruenbacher2015-02-221-5/+7
| | | | | | | | When a file has already been renamed, make sure it is not renamed back to its old name. Reported by Guido Draheim. * src/patch.c (main): Make sure we never rename a file back to its previous name. Report when a file was renamed already. * tests/copy-rename: Add "already renamed" test cases.
* patch: git-diff mode: do not change permissions if there isn't an explicit ↵Quentin Casasnovas2015-01-311-1/+1
| | | | | | mode change. Signed-off-by: Quentin Casasnovas <quentin.casasnovas@oracle.com>
* Use symlink-safe system call replacementsAndreas Gruenbacher2015-01-311-4/+5
| | | | | | | | | Use the symlink-safe replacements for system calls in many places throughout the code: In some places this makes patch safe against path traversal attacks; in other places, it saves the kernel from having to re-traverse the pathnames. * src/inp.c (plan_b): Use safe_open() + fdopen() instead of fopen(). * src/util.c (copy_attr): Document why we are safe here. (create_backup): Use safe_open() instead of creat().
* Avoid closing file descriptor twiceTim Waugh2015-01-311-0/+2
| | | | | * src/patch.c (main): Make sure we don't close() outfd after passing it on to fdopen(): the file descriptor might have been reused in the meantime.
* For renames and copies, make sure that both file names are validAndreas Gruenbacher2015-01-211-0/+3
| | | | | | | | | * src/patch.c (main): Allow there_is_another_patch() to set the skip_rest_of_patch flag. * src/pch.c (intuit_diff_type): For renames and copies, also check the "other" file name. (pch_copy, pch_rename): Now that both names are checked in intuit_diff_type(), we know they are defined here.
* Don't try applying hunks at offsets that can't workAndreas Gruenbacher2015-01-201-4/+8
| | | | | | * src/patch.c (locate_hunk): Start trying to apply the hunk at the minimum offset which puts the hunk in the valid range of lines. This will often still be offset 0.
* savebuf/savestr error handlingTobias Stoeckmann2014-11-301-9/+9
| | | | | | | | * src/patch.c (get_some_switches): The function savebuf (and therefore savestr) copies strings using malloc. If malloc fails, NULL is returned. This is intentional behavior so that in case of failure during "plan a" patching, "plan b" can step in. The return value has to be properly checked for NULL. If the return value must not be NULL, use xstrdup instead.
* Improve error message when refusing to delete fileAndreas Gruenbacher2014-08-131-1/+1
| | | | | * src/patch.c: Improve error message. * tests/create-delete: Update the test case.
* Correct the --help text of option --mergeAndreas Gruenbacher2013-12-091-1/+1
| | | | | * src/patch.c (option_help): The --merge option does not have a short form; update the help text.
* Preserve function names in reject filesSteven Rostedt2013-08-191-1/+2
| | | | | | | * src/patch.c (main): Preserve function names in reject files. * tests/reject-format: Update the test case. Signed-off-by: Steven Rostedt <rostedt@goodmis.org> Signed-off-by: Andreas Gruenbacher <agruen@linbit.com>
* Fix removing empty directoriesAndreas Gruenbacher2013-03-101-1/+1
| | | | | | | | | Reported by Thomas Moschny <thomas.moschny@gmx.de>: src/patch.c (main): Temporary output files are created in the same directory as the output file. Make sure to remove them before removing empty files and their empty ancestor directories; else the directories won't be empty. tests/remove-directories: Add directory removal test case. tests/Makefile.am (TESTS): Add new test case.
* Initialize data structures early enoughAndreas Gruenbacher2012-10-041-4/+4
| | | | | | | * src/patch.c (main): Initialize data structures early enough, before error paths can access them. * tests/bad-usage: Test bad command line usage. * tests/Makefile.am (TESTS): Add bad-usage here.
* Improve the previous commitAndreas Gruenbacher2012-09-261-1/+2
| | | | | | | | * src/patch.c: Only print the "file is not empty after patch" message when trying to delete the output file. Say that we were trying to delete the file. * tests/create-delete: Fix the expected messages. Add test cases for the --remove-empty-files and --posix options. * NEWS: Better describe this change.
* Only expect files to become empty if the patch says soAndreas Gruenbacher2012-09-251-1/+1
| | | | | | | Test cases based on patches from Dmitry V. Levin <ldv@altlinux.org>. * src/patch.c (main): Only expect files to become empty if the patch says so. * NEWS: Document this change. * tests/create-delete: Add (more) empty vs. non-empty test cases.
* Improve messages when in --dry-run modeAndreas Gruenbacher2012-09-221-2/+5
| | | | | | | | * src/patch.c (main): Say that we are checking a file and not that we are patching it in --dry-run mode. Don't say "saving rejects to file" when we don't create reject files. * tests/reject-format: Add rejects with --dry-run test case. * tests/bad-filenames, tests/fifo, tests/mixed-patch-types: Update.
* Improve handling of LF vs. CRLF line endingsAndreas Gruenbacher2012-09-221-2/+32
| | | | | | | | | * src/patch.c (check_line_endings): New function. (main): When a hunk fails, report when the line endings differ between the input file and the patch. * src/pch.c (there_is_another_patch): When saying that we strip trailing CRs, also say how to turn this off. * tests/crlf-handling: Update changed messages. Add test case that fails.
* Add --follow-symlinks option for backwards compatibilityAndreas Gruenbacher2012-09-191-0/+4
| | | | | | | | | * src/common.h (follow_symlinks): New variable. * src/patch.c (longopts): Add new --follow-symlinks option. (get_some_switches): Recognize the new option. * src/util.c (stat_file): Follow symlinks if requested. * patch.man: Document the new option. * tests/symlinks: Add test case.
* Introduce function to lstat all input filesAndreas Gruenbacher2012-09-191-5/+5
| | | | | | | | | | | * src/util.c (stat_file): New function. (move_file): Use here. * src/util.h (stat_file): Declare here. * src/inp.c (get_input_file): Use here. * src/patch.c (main): Use here. (delete_file_later): Use here. * src/pch.c (there_is_another_patch): Use here. (intuit_diff_type): Use here.
* Fix file truncation when switching from git diff to non-git diffAndreas Gruenbacher2012-09-181-1/+5
| | | | | | | | * src/patch.c (main): Output queued output files only when switching from a git diff to a non-git diff. This can modify the input file, so make sure to stat() it again. * tests/concat-git-diff: Add test case growing a file with a git diff and then with a non-git diff; without this fix; the result would be truncated.
* Change the type of *_needs_removal from int to boolAndreas Gruenbacher2012-09-131-12/+12
|
* In a git-style diff, make sure not to unlink the original by accidentAndreas Gruenbacher2012-09-131-1/+2
| | | | | | | | * src/patch.c (main): Fail if a file is not empty as expected. (output_files): In a git-style diff, make sure not to unlink the original when making a backup of an unmodified file. * tests/create-delete: Fix failed-file-deletion test and add successful-file-deletion test.
* Do not pass file type in mode of open(..., O_CREAT, mode)Andreas Gruenbacher2012-09-131-1/+2
| | | | | | * src/patch.c (main): Strip file type off of create mode for temporary output files: some systems don't ignore the file type; we want to create a regular file even when patching a symlink.
* Detect concatenated git-style patches by tracking what's in the output queueAndreas Gruenbacher2012-08-081-19/+29
| | | | | | | * src/patch.c (main): Instead of looking at the SHA hashes to detect concatenated git-style patches, detect when a file to write to is already in the output queue. * tests/concat-git-diff: Add create/delete tests.
* Change outst variable name to tmpoutst to be less misleadingAndreas Gruenbacher2012-08-071-7/+7
| | | | * src/patch.c (main): Rename outst to tmpoutst.
* In git-style diffs, create new files immediatetly and only remember files to ↵Andreas Gruenbacher2012-08-021-3/+25
| | | | | | | | modify * src/patch.c (output_file): Create new files immediately. Document why things are implemented that way. * tests/concat-git-diff: Fix glitch in test case.
* Try to recognize concatenated git diffs and handle them appropriatelyAndreas Gruenbacher2012-08-011-0/+25
| | | | | | | | * src/patch.c (main): Remember the "before" SHA1 hashes of git-style patches; the same patch will always use the same "before" SHA1 for a specific file. Try to recognize concatenated patches based on that. * tests/concat-git-diff: New test case. * tests/Makefile.am (TESTS): Add new test case.
* Allow to process only part of the deferred output file listAndreas Gruenbacher2012-08-011-5/+20
| | | | | * src/patch.c (output_files): Add parameter to specify which file to stop at. (main): Pass NULL to output_files() to process the entire list.
* Fix segfault in output_file_later()Andreas Gruenbacher2012-04-241-1/+1
| | | | | | Bug reported by Dmitry V. Levin <ldv@altlinux.org>. * src/patch.c (output_file_later): Fix case where the output file is identical with the input file (and to == NULL).
* Only warn when trying to modify read-only filesAndreas Gruenbacher2012-04-171-2/+17
| | | | | | | | | | | | Failing when trying to patch read-only files causes various users of patch to break. Instead, warn by default and introduce a command line option for choosing a different behavior. * patch.man: Describe the new behavior and command-line option. * src/patch.c (read_only_behavior): New variable. (main): Implement the new behavior. (longopts): Add the new --read-only option. (option_help): Describe the new behavior. (get_some_switches): Recognize the new --read-only option.
* For git-style patch files, do not output files immediatelyAndreas Gruenbacher2012-04-171-6/+117
| | | | | | | | | | | | | | | | | | | | | | | In git-style patch files, all patches refer to the initial state of the input files; files cannot be modified more than once. Implement these semantics by creating all output files once all patches in the patch file have been processed. * src/patch.c (init_files_to_output, output_files): Add prototypes. (main): Remember which type of patch file we are processing. Initialize the output files list. Output files of git-style patches once all patches have been read, or when from git-style to normal patches. (file_to_output): New struct. (files_to_output): List of the files to output. (output_file, output_file_now, output_file_later): Either queue a file for deletion, remember to output a file later (git-style), or output the file immediately (normal). (dispose_file_to_output, init_files_to_output, output_files, forget_output_files): New functions. (gl_list_clear): Should be provided by gnulib but isn't. (cleanup): Clean up any left-over temporary output files as well. * tests/Makefile-am (XFAIL_TESTS): Remove criss-cross; this test case works now. * tests/mixed-patch-types: Patch files that change from normal to git-style, or from git-style to normal.
* No longer remember backup filesAndreas Gruenbacher2012-04-171-1/+1
| | | | | | | | | | Remembering backup files was needed because we would have lost track of deleted files before -- but we don't delete files immediately anymore. * src/util.c (create_backup_copy): No longer remember backup files. (create_backup): Likewise; update comment. (move_file): Update create_backup() call. * src/util.h (create_backup): Update prototype. * src/patch.c (output_file): Update create_backup() call.
* Do not delete files immediatelyAndreas Gruenbacher2012-04-171-15/+83
| | | | | | | | | | | | | | | | | | | | | | | Fixes the bug that more than one numbered backup would be created when a patch file deletes and recreates a file. * bootstrap.conf (gnulib_modules): Add linked-list and xlist modules. * src/util.h (file_id_type): Add DELETE_LATER and OVERWRITTEN types. (create_backup, set_file_attributes): Update prototype. (insert_file_id): Add prototype. * src/util.c (insert_file_id): Export. (set_file_attributes, create_backup_copy): Make the st argument const. (create_backup): Pass in to_st instead of returning it from create_backup(). This obsoletes the to_errno argument. (move_file): Determine to_st here and pass it to create_backup(). Remember when a file is overwritten. * src/patch.c (output_file): Add to_st parameter. Remember files to delete instead of deleting them immediately. Pass from-st to create_backup(). (file_to_delete): New struct. (init_files_to_delete, delete_file_later, delete_files): New functions. (main): Use init_files_to_delete() and delete_files(). Pass to_st to output_file() where we already have it. * src/pch.c (intuit_diff_type): Assume that files which are marked for deletion don't exist.
* Create and delete output files in a single functionAndreas Gruenbacher2012-04-171-20/+37
| | | | | | | * src/patch.c (output_file): New function for creating or deleting an output file and backing the old file up as needed. (main): Use the new function. * src/util.c (move_file): Allow FROM_NEEDS_REMOVAL to be NULL.
* Add a type field to entries in the file id cacheAndreas Gruenbacher2012-04-171-1/+1
| | | | | | | | | | | * src/util.h (file_id_type): New enum. * src/util.c (file_id): Add a file_id_type field. (insert_file_id): Rename from insert_file(); specify a type when inserting a file id. (lookup_file_id): Rename from file_id_exists(); return a file id type. (create_backup_copy, create_backup, move_file): Use insert_file_id() instead of insert_file(), and lookup_file_id() instead of file_already_seen(). * src/patch.c (main): Use lookup_file_id() instead of file_already_seen().
* Switch from the `old' gnu quoting style to the 'new' oneAndreas Gruenbacher2012-04-171-6/+6
| | | | | * src/common.h, src/patch.c, src/pch.c, src/util.c, src/util.h: Switch from the `old' gnu quoting style to the 'new' one in messages and comments.
* maint: enable the useless_cpp_parens syntax-check ruleJim Meyering2012-01-011-1/+1
| | | | | | * cfg.mk (local-checks-to-skip): Remove sc_useless_cpp_parens, thus enabling this syntax-check rule. * src/patch.c: Remove unneeded parentheses.
* maint: update all copyright year number rangesJim Meyering2012-01-011-1/+1
| | | | Run "make update-copyright".
* Timestamp not set when creating files with --set-time or --set-utcAndreas Gruenbacher2011-12-091-3/+9
| | | | | | | | | | | * src/util.h (enum file_attributes): Add FA_XATTRS flag for extended attributes. * src/patch.c (main): Use set_file_attributes() even when the infile doesn't exist: it may still set the file time (FA_TIMES). Omit all other FA_ flags if infile doesn't exist. Otherwise, add FA_XATTRS as well. * src/util.c (set_file_attributes): Only copy extended attributes if FA_XATTRS is set. Avoid using st where it may be undefined. * tests/preserve-mode-and-timestamp: Add file create test.
* don't call fdopen with a negative FD upon dup failureJim Meyering2011-05-251-2/+5
| | | | | * src/patch.c (open_outfile): If dup fails, don't clobber its errno value by calling fdopen with -1.
* maint: use gnulib's progname moduleJim Meyering2011-03-271-1/+1
| | | | | | | | | | * src/patch.c (main): Call set_program_name rather than initializing program_name explicitly. * src/common.h: Include progname.h rather than declaring the extern, program_name. * bootstrap.conf (gnulib_modules): Add progname. * cfg.mk (local-checks-to-skip): Remove sc_program_name, thus enabling this test.
* maint: update copyright date year ranges to include 2011Jim Meyering2011-03-271-2/+2
| | | | | * bootstrap.conf (gnulib_modules): Add update-copyright. Run "make update-copyright".
* build: avoid three gcc warningsJim Meyering2011-03-271-1/+1
| | | | | | * src/patch.c (mangled_patch): Add "noreturn" attribute. * src/pch.h (pch_timestamp): Remove ignored "const" attribute. * src/version.c (XTERN): Remove unused #undef and #define.
* maint: remove unnecessary tests before freeJim Meyering2011-02-211-2/+1
| | | | | | | | There is no longer any need to avoid "free(NULL)" (since SunOS 4), so replace each "if (P) free (P);" with "free (P);". * src/inp.c (get_input_file): Remove unnecessary test-before-free. * src/patch.c (get_some_switches): Likewise. * src/pch.c (intuit_diff_type, pch_swap): Likewise.
* Fix use of uninitialized outstAndreas Gruenbacher2011-02-151-3/+4
| | | | | * src/patch.c (main): Fix use of initialized outst and add an additional assert. Reported by Jim Meyering.
* Preserve extended attributes of patched files when possibleAndreas Gruenbacher2010-10-271-4/+5
| | | | | | | | | | | | | | * src/patch.c (main): Set all file attributes of the temporary output file before renaming it over the final output file (possibly replacing the input file). Pass the input file name to set_file_attributes(). * src/util.c (set_file_attributes): When enabled (USE_XATTR), also copy extended attributes including attributes which define permissions. (copy_attr_error, copy_attr_quote, copy_attr_free, copy_attr_check, copy_attr): Helper functions for copying extended attributes. * m4/xattr.m4 (gl_FUNC_XATTR): Import from coreutils. * src/Makefile.am (patch_LDADD): Add $(LIB_XATTR) here. * bootstrap.conf: Use the gnulib verror module.
* Remove obsolete commentAndreas Gruenbacher2010-10-261-1/+0
| | | | * src/patch.c (print_header_line): Remove obsolete comment.
* Fix the "patching file" message for renames and copiesAndreas Gruenbacher2010-09-171-1/+18
| | | | | | | | | * src/patch.c (main): Generate the "patching file" message here. When the input and output file name is not he same, include both names in the message. * src/inp.c (scan_input): Previously the "patching file" message was generated here. * tests/unmodified-files, tests/copy-rename: Update.
* Stop using mktemp() for TMPOUTNAMEAndreas Gruenbacher2010-09-171-48/+20
| | | | | * src/patch.c (main): Compute TMPOUTNAME with make_tempfile(). (make_temp): Remove obsolete function.