Use of patch files in Linux __linux

Source: Internet
Author: User
Tags diff diff patch

Because there are several common files to be modified during the u-boot transplant, it is too cumbersome to manually modify them every time. Making patches can solve this problem.

The collection of learning materials is relatively simple, method a similar to this primary problem online information is very rich, Google or Baidu Search, and then select valuable information, method two is to read the man online documents. Complete the collection work, of course, finally to do on their own Linux experiments, compare summary, digestion and absorption for their own things. To remove such a wrong thought: must learn the whole. To know, a learning is impossible, can only learn the most commonly used, in the process of continuous practice in the future gradually rich, and finally reached a relatively high level. The principle of grasping is: to learn, to apply, to promote learning.

First introduce the diff and patch. It is not necessary to introduce all the options in the man's online documentation. In 99% of the time, we will only use a few options. So you have to learn these few options.

1, diff

--------------------

NAME

Diff-find differences between two files

Synopsis

diff [Options] from-file To-file

--------------------

Simply put, the diff function is used to compare two files, and then record down, that is, the so-called diff patch. Syntax Format: diff "option" Source file (folder) purpose file (folder), is to give the source file (folder) to make a patch to become the destination file (folder), the term is "upgrade." Here are three of the most common options:

-R is a recursive option, set this option, diff will compare all the corresponding files in the two different versions of the source code directory, including subdirectory files.

The-n option ensures that the patch file will correctly handle situations where files have been created or deleted.

The-u option creates a patch file in a uniform format that is more compact than the default format.

2, Patch

------------------

NAME

Patch-apply a diff file to a original

Synopsis

patch [Options] [Originalfile [Patchfile]]

But usually just

Patch-pnum <patchfile>

------------------

Simply put, patch is the use of diff made patches to achieve the source file (folder) and the destination file (folder) conversion. This means that you can have the source file (folder) ――> destination file (folder), can also be the destination file (folder) ――> source files (folders). Here are some of the most common options:

-P0 option to find the destination file (folder) from the current directory

The-P1 option is to ignore the first level directory and start looking from the current directory.

************************************************************

Here is an example to illustrate:

---old/modules/pcitable Mon Sep 27 11:03:56 1999

+++ new/modules/pcitable Tue Dec 19 20:05:41 2000

If you use the parameter-p0, it means to look for a folder called old from the current directory and find the pcitable file under modules below it to perform the patch operation.

If you use parameter-p1, that means ignoring the first level directory (that is, regardless of old), looking for the Modules folder from the current directory, and looking for pcitable underneath it. This assumes that the current directory must be the directory where the modules resides. The diff patch file can be anywhere, as long as it indicates the path to the diff patch file. Of course, you can use a relative path, or you can use an absolute path. But I am generally accustomed to using a relative path.

************************************************************

The-e option indicates that if an empty file is found, delete it

The-r option indicates that the "new" file and the "old" file in the patch file are now being swapped (in effect, patching the new version to the old version)

The following combination of specific examples to analyze and solve, divided into two types: for a single file patch and a folder for a number of files to play patches.

Environment: Under the Redhat 9.0 to Armlinux user login.

The directory tree is as follows:

|--Bootloader

| |--DEBUG

|--Images

|--Kernel

|--Program

|--Rootfiles

|--Software

| |-source

|--Sysapps

| |-tmp

'--tools

Below the program folder to establish the patch folder as an experiment, and then enter the Patch folder.

One, patch for a single file

1, the establishment of test documents TEST0, Test1

[Armlinux@lqm patch]$ Cat >>test0<<eof

> 111111

> 111111

> 111111

> EOF

[ARMLINUX@LQM patch]$ more Test0

111111

111111

111111

[Armlinux@lqm patch]$ Cat >>test1<<eof

> 222222

> 111111

> 222222

> 111111

> EOF

[ARMLINUX@LQM patch]$ more Test1

222222

111111

111111

2, use diff to create a patch Test1.patch

[ARMLINUX@LQM patch]$ diff-un test0 test1 > Test1.patch

Note: the-r option is not required because of a single file. The order of options is not related, that is, it can be-un or-nu. 】

[ARMLINUX@LQM patch]$ ls

Test0 test1 Test1.patch

[ARMLINUX@LQM patch]$ more Test1.patch

************************************************************

Structure of the patch file

Patch Head

The patch header is two lines that begin with the---/+++ to indicate the file to be patched. The beginning of the---represents the old file, and +++ begins with the new file.

Multiple patches in a patch file

A patch file may contain many sections that begin with a---/+++, and each section is used to make a patch. So you can include multiple patches in a patch file.

Block

Block is the place to modify in the patch. It usually starts and ends with something that doesn't have to be modified. They are only used to indicate the location to be modified. They usually start with @@ 开始 ending with the start of another block or a new patch header.

Indentation of Blocks

Blocks are indented, and this column is used to indicate whether the line is to be added or deleted.

The first column of the Block

The + number indicates that the line is to be added.

-the number indicates that the line is to be deleted.

There is no plus sign and no minus sign indicates that it is only referenced and does not need to be modified.

************************************************************

The diff command records the first creation time of these two files in the patch file, as follows * *

---test0 2006-08-18 09:12:01.000000000 +0800

+++ Test1 2006-08-18 09:13:09.000000000 +0800

@@ -1,3 +1,4 @@

+222222

111111

-111111

+222222

111111

[ARMLINUX@LQM patch]$ Patch-p0 < Test1.patch

Patching file Test0

[ARMLINUX@LQM patch]$ ls

Font face= "Times New Roman" >test0 test1 Test1.patch

[Armlinux@lqm patch]$ Cat Test0

222222

111111

222222

111111

3, can remove the patch, restore the old version

[ARMLINUX@LQM patch]$ Patch-re-p0 < Test1.patch

Patching file Test0

[ARMLINUX@LQM patch]$ ls

Test0 test1 Test1.patch

[Armlinux@lqm patch]$ Cat Test0

111111

111111

111111

Second, for multiple files to patch operations

1. Create a Test folder

[ARMLINUX@LQM patch]$ mkdir prj0

[ARMLINUX@LQM patch]$ CP test0 prj0

[ARMLINUX@LQM patch]$ ls

Prj0 test0 test1 Test1.patch

[ARMLINUX@LQM patch]$ CD prj0/

[ARMLINUX@LQM prj0]$ ls

Test0

[Armlinux@lqm prj0]$ Cat >>prj0name<<eof

>--------

> Prj0/prj0name

>--------

> EOF

[ARMLINUX@LQM prj0]$ ls

Prj0name test0

[Armlinux@lqm prj0]$ Cat Prj0name

--------

Prj0/prj0name

--------

[ARMLINUX@LQM prj0]$ CD ...

[ARMLINUX@LQM patch]$ mkdir prj1

[ARMLINUX@LQM patch]$ CP test1 PRJ1

[ARMLINUX@LQM patch]$ CD prj1

[Armlinux@lqm prj1]$ Cat >>prj1name<<eof

>---------

> Prj1/prj1name

>---------

> EOF

[Armlinux@lqm prj1]$ Cat Prj1name

---------

Prj1/prj1name

---------

[ARMLINUX@LQM prj1]$ CD ...

2, create a patch

[ARMLINUX@LQM patch]$ diff-unr prj0 prj1 > Prj1.patch

[ARMLINUX@LQM patch]$ more Prj1.patch

Diff-unr Prj0/prj0name Prj1/prj0name

---prj0/prj0name 2006-08-18 09:25:11.000000000 +0800

+++ prj1/

Prj0name 1970-01-01 08:00:00.000000000 +0800

@@ -1,3 +0,0 @@

---------

-prj0/prj0name

---------

Diff-unr Prj0/prj1name Prj1/prj1name

---prj0/prj1name 1970-01-01 08:00:00.000000000 +0800

+++ Prj1/prj1name 2006-08-18 09:26:36.000000000 +0800

@@ -0,0 +1,3 @@

+---------

+prj1/prj1name

+---------

Diff-unr prj0/test0 Prj1/test0

---prj0/test0 2006-08-18 09:23:53.000000000 +0800

+++ Prj1/test0 1970-01-01 08:00:00.000000000 +0800

@@ -1,3 +0,0 @@

-111111

-111111

-111111

Diff-unr Prj0/test1 Prj1/test1

---prj0/test1 1970-01-01 08:00:00.000000000 +0800

+++ Prj1/test1 2006-08-18 09:26:00.000000000 +0800

@@ -0,0 +1,4 @@

+222222

+111111

+222222

+111111

[ARMLINUX@LQM patch]$ ls

Prj0 prj1 prj1.patch test0 test1 Test1.patch

[ARMLINUX@LQM patch]$ cp Prj1.patch./prj0

[ARMLINUX@LQM patch]$ CD Prj0

[ARMLINUX@LQM prj0]$ Patch-p1 < Prj1.patch

Patching file Prj0name

Patching file Prj1name

Patching file Test0

Patching file Test1

[ARMLINUX@LQM prj0]$ ls

Prj1name Prj1.patch Test1

[ARMLINUX@LQM prj0]$ patch-r-p1 < Prj1.patch

Patching file Prj0name

Patching file Prj1name

Patching file Test0

Patching file Test1

[ARMLINUX@LQM prj0]$ ls

Prj0name Prj1.patch test0

-------------------

To sum up:

Single File

Diff–un from-file To-file >to-file.patch

Patch–p0 < TO-FILE.P

Atch

Patch–re–p0 < To-file.patch

Multiple files

Diff–unr FROM-DOCU TO-DOCU >to-docu.patch

Patch–p1 < To-docu.patch

PATCH–R–P1 <to-docu.patch

-------------------

Third, the application

Patch the kernel. One of the steps in creating the cross-compilation tool chain earlier is to patch the kernel. It was not particularly understood at the time, and it is now clear. Refer to the previous article, "based on the development of Arm+linux embedded development tool chain building."

1, first is decompression, because the release of the patch files are using gzip compression.

$gunzip. /setup-dir/patch-2.4.21-rmk1.gz

2, then enter your kernel source code directory

$CD linux-2.4.21

3, play the patch

$patch –p1 < ... /.. /setup-dir/patch-2.4.21-rmk1

After the patch, you need to check to see if there are any rejected files, that is, check the existence of the. rej file. To use the command:

$find. -name *.rej

If found, it is output to the standard output terminal, the default screen. Of course, you can also use redirects to output to the specified file, such as reject.

$fine. -name *.rej >reject

You can then view the contents of the Reject.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.