• dushu.tw 读书网、小说免费阅读网站

Linux 补丁生成与使用

软件 ksdroberta 56次浏览 已收录 0个评论 扫描二维码

我们在升级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 命令来进行查看。


举个栗子 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Linux 补丁生成与使用
喜欢 (0)
关于作者:
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到