Monday, March 24, 2008

How to create and apply a patch...

Warning: Before doing this, please backup your source code, patching wrongly can screwup your source code.

how to create patch file?
Patch file is a difference file that is created by diff with -c (context output format). To patch the entire folder of source codes(as usually people do) I usually follow this procedure...

In this case Original source code in in folder Tb01, and latest source code at folder Tb02. Also the folder might contain sub directories in it as well... So the command is.

diff -crB Tb01 Tb02 > Tb02.patch

-c context, -r recursive (multiple levels dir), -B is to ignore Blank Lines.
Using -B could help u a lot because blank lines is really useless for patching, sometimes I need to manually read the patch file to track the changes, without -B is really headache.

How to patch?
First of all, please do a dry-run before really patching it. keep in mind, patch will be working very specifically. Let say the version 3 Tb03.patch is use to patch from Tb02, if you apply patch on Tb01, sometimes it will corrupt your source code. So, to make sure it works, do a dry run. Dry-run means a fake-test, do it at the directory of the source code targeted to patch.

Doing dry-run like this:

patch --dry-run -p1 -i Tb02.patch

The success output looks like this:

patching file TbApi.cpp patching file TbApi.h patching file TbCard.cpp ...

The failure ouptut looks like this:

patching file TbCard.cpp Hunk #2 FAILED at 585. 1 out of 2 hunks FAILED -- saving rejects to file TbCard.cpp.rej patching file TbCard.h Hunk #1 FAILED at 57. Hunk #2 FAILED at 77. Hunk #3 succeeded at 83 with fuzz 1 (offset -21 lines). ....

At last, if the dry-run is giving good result, do this and enjoy the compilation.

patch -p1 -i Tb02.patch

Levels in the Patch Command (-p0 or -p1?):

The -p option will optionally strip off directory levels from the patchfile. For Ex: if you have a patchfile with a header as such:
--- old/modules/pcitable Mon Sep 27 11:03:56 1999
+++ new/modules/pcitable Tue Dec 19 20:05:41 2000
Using a -p0 will expect, from your current working directory, to find a subdirectory called "new", then "modules" below that, then the "pcitable" file below that.

Using a -p1 will strip off the 1st level from the path and will expect to find (from your current working directory) a directory called "modules", then a file called "pcitable". Patch will ignore the "new" directory mentioned in the header of the patchfile.

Using a -p2 will strip of the first two levels from the path. Patch will expect to find "pcitable" in the current working directory. Patch will ignore the "new" and "modules" directories mentioned in the header of the patchfile.

Using a -p3 in this example would not be a good thing. Patch probably wouldn't patch anything.

Compression and Uncompression of files in linux

Compress folder Test/ to Test.tar.gz

tar czfv Test.tar.gz Test/
czfv = ‘Compress Zip File Verbose’
If you want bzip files, use ‘j’ instead of ‘z’.


Uncompress Test.tar.gz to folder Test/

tar -xzf Test.tar.gz
x = ‘eXtract’
Again, if you want bzip files, use ‘j’ instead of ‘z’.


To compress a tar.bz2 file, use the command (note the j option)

Code:

tar -jcvf filename.tar.bz2

To extract a tar.bz2 file, use the command (note the j option)

Code:

tar -jxvf filename.tar.bz2

To extract a tar.gz file, the the command (note the z option)

Code:

tar -zxvf filename.tar.gz