您的位置:首页 > 编程语言

通过程序补丁进行源代码的修订

2008-07-24 16:20 127 查看


目的

在项目的开发过程中,多名开发人员通过CVS等工具对源代码进行版本控制,软件发布以后,开发人员会根据程序运行过程中出现的BUG、或者新的需求对于源代码进行一定的修改,然后以补丁(patch)的方式发布程序补丁,其余开发人员或者使用人员对于原先的源代码进行打补丁,从而完成BUG或者新的功能的修订。

本文档通过基本示例,介绍了程序补丁的生成和程序打补丁的基本方法和流程。

Sample程序结构

假设我们的程序结构为:

---sample
-------bin
-------src
-------include
-------sample.h
-------sample.c
-------Makefile
--------Makefile
Sample目录中有两个子目录,bin目录和src目录,Makefile文件;src目录含有两个子目录include和test,其中,include包含头文件,test中包含源代码。

Sample.h 文件的内容为:
/*sample file made by mo xuansheng, this is demo will performance the usage of diff and patch.
July 22, 2008
*/
#ifndef _SAMPLE_H
#define _SAMPLE_H

#include

#endif


Sample.c文件的内容为:
include
#include "include/sample.h"

int
main()
{
char dst[100];
char src[] = "this is a sample";
strcpy(dst, src);
#ifdef DEBUG2
printf("%s/n", dst);
#endif
return 0;
}


Makefile文件的内容为:
CC=gcc
FLAGS=-g -Wall
LIBS=
PROG=sample
all:${PROG}
sample:sample.c
${CC} ${FLAGS} $< -o ../bin/sample
clean:
rm -f sample


其中,在sample.c文件中,调用strcpy函数没有检查dst的容量,会造成缓冲区溢出攻击。需要对于程序进行修订。现将sample目录拷贝为newsample。修改其中的文件。

Sample.h文件的内容为:
/*sample file made by mo xuansheng, this is demo will performance the usage of diff and patch.
July 22, 2008
*/
#ifndef _SAMPLE_H
#define _SAMPLE_H

#include
#include

#endif


Sample.c文件的内容为:
#include
#include "include/sample.h"

int
main()
{
char dst[100];
memset(dst, '/0', sizeof(dst));
char src[] = "this is a sample";
if (sizeof(dst) > strlen(src))
{
strcpy(dst, src);
}
#ifdef DEBUG2
printf("%s/n", dst);
#endif
return 0;
}


Makefile文件的内容为:
CC=gcc
FLAGS=-g -Wall -DDEBUG2
LIBS=
PROG=sample
all:${PROG}
sample:sample.c
${CC} ${FLAGS} $< -o ../bin/sample
clean:
rm -f sample


Diff生成补丁程序

Diff 程序主要是对比文件或者目录,生成文件或者目录不同的一个程序。

修订好程序以后,假设sample目录和newsample目录都在patch目录下面:

[root@moxuansheng
patch] ls
sample newsample
输入命令:

[root@moxuansheng patch] diff –aNur –exclude=bin sample newsample >
sample-patch
其中参数的参数可以通过man查找含义。

需要注意的是,第一个目录是old,第二个目录是new。

Diff通过比较两个目录,生成补丁文件。

Patch对程序打补丁

生成补丁文件以后,通过patch命令,对于旧的sample文件进行打补丁,修订相应文件。

在patch目录下下面,就是该目录下面含有sample和newsample目录。

[root@moxuansheng patch] patch –p0
< sample-patch
Patching file
sample/src/include/sample.h
Patching file
sample/src/Makefile
Patching File
sample/src/sample.c
需要注意的地方是:-pN参数的使用,查看sample-patch文件,其首部为:

Diff –aNur –exclude=bin sample/src/include/sample.h newsample/src/include/sample.h
其中的路径为:sample/src/include/sample.h

-pN 参数是可以去掉补丁文件中的目录层数。

-p0 则是从当前的工作目录中,寻找名称为smaple的子目录,然后在他下面是src目录,在src目录下下面有include目录,依次类推。

如果切换到sample目录下下面,则执行:

[root@moxuansheng sample] patch –p1 < ../sample-patch
则patch程序会消去第一层目录sample,会在工作目录下下面寻找src目录,在src目录下面寻找include目录,依次类推。

通过打补丁以后,原先sample目录下下面的文件都被修改了。

1
取消补丁

对程序打补丁以后,也许要取消这次的修订,需要取消这次补丁过程,则可以使用命令:

[root@moxuansheng sample] patch –p1 –R <
../sample/sample-patch
Patching file sample/src/include/sample.h
Patching file sample/src/Makefile
Patching file sample/src/sample.c
其中的-pN参数与上节所描述的相同。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐