首頁 > 軟體

Linux 修補程式生成與使用

2020-06-16 16:49:55

我們在升級Linux 核心的時候,難免會接觸到修補程式的知識。下面對如何生成修補程式和如何打修補程式作講解。

生成修補程式:

製作 hello.c 和 hello_new.c 兩個檔案如如下所示。

?  diff ls
hello.c  hello_new.c  hello_test.c  hi.patch
?  diff cat hello.c
#include "stdio.h"
int main(int argc ,char **argv)
{
    printf("Hello World");
}
?  diff cat hello_new.c
#include "stdio.h"
int main(int argc ,char **argv)
{
    printf("Hello Worldn");
    return 0;
}

使用 diff  -uN 命令 進行生成patch

?  diff diff -uN hello_new.c hello.c > hi.patch
?  diff cat hi.patch
--- hello_new.c    2018-07-17 16:58:23.679704122 +0800
+++ hello.c    2018-07-17 16:57:59.190677641 +0800
@@ -1,6 +1,5 @@
 #include "stdio.h"
 int main(int argc ,char **argv)
 {
-    printf("Hello Worldn");
-    return 0;
+    printf("Hello World");
 }

至此,patch 已經建立完畢。

 之後,我們進行使用 patch 命令 對 hello.c 檔案進行打修補程式。

?  diff patch -p0 <hi.patch
patching file hello.c
Reversed (or previously applied) patch detected!  Assume -R? [n] y
?  diff cat hello.c
#include "stdio.h"
int main(int argc ,char **argv)
{
    printf("Hello Worldn");
    return 0;
}
?  diff ls
hello.c  hello.c.orig  hello_new.c  hello_test.c  hi.patch
?  diff cat hello.c.orig
#include "stdio.h"
int main(int argc ,char **argv)
{
    printf("Hello World");
}
?  diff cat hello.c
#include "stdio.h"
int main(int argc ,char **argv)
{
    printf("Hello Worldn");
    return 0;
}
?  diff

可見,修補程式已經成功應用,並且生成了 .orig 原始檔。 --backup-if-mismatch  選項,可以不進行生成orig 檔案。

?  diff patch -p0 --no-backup-if-mismatch < hi.patch
patching file hello.c
Reversed (or previously applied) patch detected!  Assume -R? [n] y
?  diff ls
hello.c  hello_new.c  hello_test.c  hi.patch
?  diff cat hello.c
#include "stdio.h"
int main(int argc ,char **argv)
{
    printf("Hello Worldn");
    return 0;
}

diff 和 patch 命令介紹:

1、diff

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

NAME

      diff - find differences between two files

SYNOPSIS

      diff [options] from-file to-file

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

簡單的說,diff的功能就是用來比較兩個檔案的不同,然後記錄下來,也就是所謂的diff修補程式。語法格式:diff 【選項】 原始檔(夾) 目的檔案(夾),就是要給原始檔(夾)打個修補程式,使之變成目的檔案(夾),術語也就是“升級”。下面介紹三個最為常用選項:

-r 是一個遞迴選項,設定了這個選項,diff會將兩個不同版本原始碼目錄中的所有對應檔案全部都進行一次比較,包括子目錄檔案。

-N 選項確保修補程式檔案將正確地處理已經建立或刪除檔案的情況。

-u 選項以統一格式建立修補程式檔案,這種格式比預設格式更緊湊些。

2、patch

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

NAME

      patch - apply a diff file to an original

SYNOPSIS

      patch [options] [originalfile [patchfile]]

      but usually just

      patch -pnum <patchfile>

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

簡單的說,patch就是利用diff製作的修補程式來實現原始檔(夾)和目的檔案(夾)的轉換。這樣說就意味著你可以有原始檔(夾)――>目的檔案(夾),也可以目的檔案(夾)――>原始檔(夾)。下面介紹幾個最常用選項:

-pnum or --strip=num
 Strip the smallest prefix containing num leading slashes from each file name found in the patch file. A sequence of one or more adjacent slashes is counted as a
 single slash. This controls how file names found in the patch file are treated, in case you keep your files in a different directory than the person who sent out
 the patch. For example, supposing the file name in the patch file was

/u/howard/src/blurfl/blurfl.c

setting -p0 gives the entire file name unmodified, -p1 gives

u/howard/src/blurfl/blurfl.c

without the leading slash, -p4 gives

blurfl/blurfl.c

and not specifying -p at all just gives you blurfl.c. Whatever you end up with is looked for either in the current directory, or the directory specified by the -d
 option.

具體的使用說明,可以使用 man diff 和 man patch 命令來進行檢視。

Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx

本文永久更新連結地址https://www.linuxidc.com/Linux/2018-07/153240.htm


IT145.com E-mail:sddin#qq.com