Hello Readers,
Recently on open source mailing lists, there was a big discussion on patch related issues. I just thought of writing something on Linux “Patch management”
· What is patch
· How patch works
· How to reverse the changes
Patch: - The commands diff and patch form a powerful combination. They are widely used to get differences between original files and updated files in such a way that other people who only have the original files can turn them into the updated files with just a single patch file that contains only the differences. The patch program applies diff files to originals. The diff command is used to compare an original to a changed file. Diff lists the changes made to the file. A person who has the original file can then use the patch command with the diff file to add the changes to their original file (patching the file). Patch should be installed because it is a common way of upgrading applications.
If the patch file has extension gz or bz2, it need to be uncompressed before applying patch:-
· gunzip patch-x.y.z.gz
· bunzip2 patch-x.y.z.bz2
To apply a patch:-
patch -p1 </patch/to/file
Patches can be undone, or reversed, with the '-R' option:
patch -R </path/to/file
patch -R </path/to/file
For complete details:-
man patch
man patch
Common errors and issues when applying patch:-
When a patch applies a patch file it verifies the sanity of the file in different ways
a. Checking that the file looks like a valid patch file
b. Checking the code around the bits being modified matches the context
provided in the patch
If patch encounters something that doesn't look quite right it has two options:-
· It can either refuse to apply the changes and abort
· It can try to find a way to make the patch apply with a few minor changes.
Scenario 1:-
One example of something that's not 'quite right' that patch will attempt to fix
if all the Context matches, the lines being changed match, but the
line numbers are different. This can happen, for example if the patch
makes a change in the middle of the file but for some reasons a few lines have
been added or removed near the beginning of the file. In that case everything
looks good it has just moved up or down a bit, and patch will usually adjust the
line numbers and apply the patch.
Scenario 2:-
When patch encounters a change that it can't fix up with fuzz it rejects it outright
and leaves a file with a .rej extension (a reject file). You can
read this file to see exactly what change couldn't be
applied, so you can go fix it up manually.
Scenario 3:-
If patch stops and presents a "File to patch:" prompt, then patch could not find
a file to be patched. Most likely you forgot to specify -p1 or you are in the wrong
directory. Less often, you'll find patches that need to be applied with -p0
instead of -p1 (reading the patch file should reveal if this is the case -- if
so, then this is an error by the person who created the patch
but it is not fatal).
Scenario 4 :-
If you get "Hunk #2 succeeded at 1887 with fuzz 2 (offset 7 lines)." or a message
similar to that, then it means that patch had to adjust the location
of the change (in this example it needed to move 7 lines from
where it expected to make the change to make it fit). The resulting file may or
may not be OK, depending on the reason the file was different
than expected. This often happens if you try to apply a patch that
was generated against a different kernel version than the one you are trying
to patch.
Scenario 5 :-
If you get a message like "Hunk #3 FAILED at 2387.", then it means that the patch
could not be applied correctly and the patch program was unable
to fuzz its way through. This will generate a .rej file with the
change that caused the patch to fail and also a .orig file showing you the original
content that couldn't be changed.
Scenario 6 :-
If you get "Reversed (or previously applied) patch detected! Assume -R? [n]" then
patch detected that the change contained in the patch seems to have already been
made. If you actually did apply this patch previously and you just
re-applied it in error, then just say [n]o and abort this patch.
If you applied this patch previously and actually intended to revert it, but forgot
to specify -R, then you can say [y]es here to make patch revert it for
you. This can also happen if the creator of the patch reversed
the source and destination directories when creating the patch, and in that case
reverting the patch will in fact apply it.
Patch Work project: - http://patchwork.ozlabs.org/
Hope you like this article; Feel free to share your feedback.