Patch software

When packaging software, you might need to make certain changes to the original source code, such as fixing a bug or changing a configuration file. In RPM packaging, you can instead leave the original source code intact and apply patches on it.

A patch is a piece of text that updates a source code file. The patch has a diff format, because it represents the difference between two versions of the text. You can create a patch by using the diff utility, and then apply the patch to the source code by using the patch utility.

Note

Software developers often use version control systems such as Git to manage their code base. Such tools offer their own methods of creating diffs or patching software.

Create a patch file for a sample C program

Create a patch from the original source code of a sample Hello world program written in C (cello.c) by using the diff utility. You can then apply this patch to make changes to the packaged software without making changes to the original source code.

Before you begin

  • You installed the diff utility on your system:
    # dnf install diffutils

Procedure

  1. Back up the original source code:
    $ cp -p cello.c cello.c.orig

    The -p option preserves mode, ownership, and timestamps.

  2. Modify cello.c as needed:
    #include <stdio.h>
    
    int main(void) {
        printf("Hello World from my very first patch!\n");
        return 0;
    }
  3. Generate a patch:
    $ diff -Naur cello.c.orig cello.c
    --- cello.c.orig        2016-05-26 17:21:30.478523360 -0500
    +++ cello.c     2016-05-27 14:53:20.668588245 -0500
    @@ -1,6 +1,6 @@
     #include<stdio.h>
    
     int main(void){
    -    printf("Hello World!\n");
    +    printf("Hello World from my very first patch!\n");
         return 0;
     }
    \ No newline at end of file

    Lines that start with + replace the lines that start with -.

    Note

    Using the Naur options with the diff command is recommended because it fits the majority of use cases:

    • -N (--new-file)

      The -N option handles absent files as empty files.

    • -a (--text)

      The -a option treats all files as text. As a result, the diff utility does not ignore the files it classified as binaries.

    • -u (-U NUM or --unified[=NUM])

      The -u option returns output in the form of output NUM (default 3) lines of unified context. This is a compact and an easily readable format commonly used in patch files.

    • -r (--recursive)

      The -r option recursively compares any subdirectories that the diff utility found.

    However, note that in this particular case, only the -u option is necessary.

  4. Save the patch to a file:
    $ diff -Naur cello.c.orig cello.c > cello.patch
  5. Restore the original cello.c:
    $ mv cello.c.orig cello.c
    Important
    You must retain the original cello.c because the RPM package manager uses the original file, not the modified one, when building an RPM package. For more information, see Working with spec files.

Patch a sample C program

Apply code patches to a sample Hello world program written in C (cello.c) by using the patch utility.

Before you begin

Procedure

  1. Redirect the patch file to the patch command:
    $ patch < cello.patch
    patching file cello.c
  2. Check that the contents of cello.c now reflect the change:
    $ cat cello.c
    #include<stdio.h>
    
    int main(void){
        printf("Hello World from my very first patch!\n");
        return 1;
    }

Results

  1. Build the patched cello.c program:
    $ make
    gcc -g -o cello cello.c
  2. Run the built cello.c program:
    $ ./cello
    Hello World from my very first patch!