Adding Patch to an Application

In this section, you are going to learn

How to create patches ?

How to add patches to an application ?

How to add patches using diff and patch commands ?

How to add patches using quilt commands ?

Let us answer few basic questions

What is a patch ?

What are the ways to generate and apply patch ?

What is use of diff command ?

What is use of patch command ?

What is the command to generate patch file ?

What is the command to apply the patches to source code ?

What is the command to remove the patches from source code ?

What is quilt ?

What quilt init does ?

What quilt new does ?

What quilt add does ?

What quilt refresh does ?

What quilt series does ?

What quilt push does ?

What is quilt pop does ?

What quilt top does ?

Let us now explore it in depth !

  • Step 1: Edit the file which needs to be modified.

  • Step 2: Generate a patch file using the diff command.

diff -u <original_file_name> <modified_file_name> > <patch_file_name>
  • Step 3: Now the patches to the file can be applied using patch command.

patch <orginal_file_name> < <patch_file_name>
  • Now a patch is created and the changes is applied to the file.

1#include <stdio.h>
2
3void main()
4{
5	printf("Hello-World\n");
6
7	return;
8}
  • Copy the file which needs to be modified to a new file and make the changes required.

1#include <stdio.h>
2
3void main()
4{
5	printf("Hello-World\n");
6	printf("Patch applied\n");
7	return;
8}
$ diff -u helloworld_orginal.c helloworld_modified.c > add_patch_to_helloworld.diff
 1--- helloworld_orginal.c	2024-01-02 12:10:14.076379091 +0530
 2+++ helloworld_modified.c	2024-01-02 12:10:41.905334438 +0530
 3 -3,6 +3,6 
 4 void main()
 5 {
 6 	printf("Hello-World\n");
 7-
 8+	printf("Patch applied\n");
 9 	return;
10 }
$ patch helloworld_orginal.c < add_patch_to_helloworld.diff
patching file helloworld_orginal.c
$ gcc helloworld_orginal.c
$ ./a.out
Hello-World
Patch applied
  • Step 1: create a new quilt series using quilt init command.

$ quilt init
The quilt meta-data is now initialized.
  • Step 2: create a patch file using quilt new command.

$ quilt new add_hello_world_patch.patch
Patch patches/add_hello_world_patch.patch is now on top
  • Step 3: add the file which needs to be modified using quilt add command.

$ quilt add helloworld.c
File helloworld.c added to patch patches/add_hello_world_patch.patch
  • Step 4: edit the file and make the modifications needed.

$ vi helloworld.c
  • Step 5: once the changes is added run quilt refresh to update the series with latest modifications.

$ quilt refresh
Refreshed patch patches/add_hello_world_patch.patch
  • Now a patch is created and changes has been added to the series.

1#include <stdio.h>
2
3int main(void)
4{
5	printf("Hello World\n");
6	return 0;
7}
 1SOURCE_FILE = helloworld.c
 2
 3all: apply_patches build
 4
 5apply_patches:
 6	quilt push || true  
 7
 8build:
 9	gcc $(SOURCE_FILE) -o helloworld
10
11clean: unapply_patches
12	rm -f helloworld
13
14unapply_patches:
15	quilt pop -a || true 
16
17.PHONY: all apply_patches build clean unapply_patches
18
$ quilt init
The quilt meta-data is now initialized.
$ quilt new add_hello_world_patch.patch
Patch patches/add_hello_world_patch.patch is now on top
$ quilt add helloworld.c
File helloworld.c added to patch patches/add_hello_world_patch.patch
$ vi helloworld.c
$ quilt refresh
Refreshed patch patches/add_hello_world_patch.patch
 1Index: app/helloworld.c
 2===================================================================
 3--- app.orig/helloworld.c
 4+++ app/helloworld.c
 5 int main(void)
 6 {
 7 	printf("Hello World\n");
 8+	printf("Patches Applied\n");
 9 	return 0;
10 }
$ make
Applying patch patches/add_hello_world_patch.patch
patching file helloworld.c

Now at patch patches/add_hello_world_patch.patch
$ ./helloworld
Hello World
Patches Applied