通过程序补丁进行源代码的修订
2008-07-24 16:20
127 查看
目的
在项目的开发过程中,多名开发人员通过CVS等工具对源代码进行版本控制,软件发布以后,开发人员会根据程序运行过程中出现的BUG、或者新的需求对于源代码进行一定的修改,然后以补丁(patch)的方式发布程序补丁,其余开发人员或者使用人员对于原先的源代码进行打补丁,从而完成BUG或者新的功能的修订。本文档通过基本示例,介绍了程序补丁的生成和程序打补丁的基本方法和流程。
Sample程序结构
假设我们的程序结构为:---sample -------bin -------src -------include -------sample.h -------sample.c -------Makefile --------Makefile |
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 |
需要注意的是,第一个目录是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 |
Diff –aNur –exclude=bin sample/src/include/sample.h newsample/src/include/sample.h |
-pN 参数是可以去掉补丁文件中的目录层数。
-p0 则是从当前的工作目录中,寻找名称为smaple的子目录,然后在他下面是src目录,在src目录下下面有include目录,依次类推。
如果切换到sample目录下下面,则执行:
[root@moxuansheng sample] patch –p1 < ../sample-patch |
通过打补丁以后,原先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 |
相关文章推荐
- 从测试的角度来重新反思我们自己的程序以及我们的程序员之路——“通过追本溯源来进行前瞻性思考”
- flash将库中图片通过程序进行加载
- 通过InstrumentDriver用Eclipse的JUnit对IOS程序进行测试
- 程序使用 HTTP 协议和服务器交互主要是进行数据的提交,通常数据的提交是通过 GET 和 POST 两种方式来完成
- Java程序通过LDAP对用户进行登陆验证
- 通过javascript调用Applet程序对客户端文件系统进行操作
- 使用C++进行汉语编程(附源代码,VS2005编译通过)
- 在锐捷802.1x网络中使用xsupplicant的补丁进行认证的源代码和教程
- 如何通过程序对站点进行装配
- 通过java程序打开网页接受原码并进行数据爬取(链接方式)
- 编写程序通过栈的方式将任意输入的字符串内容进行逆序输出。
- 编写程序通过栈的方式将任意输入的字符串内容进行逆序输出。
- JDB调试Android程序(通过JDB进行代码注入)
- 用GCC输出带源代码的汇编程序进行链接的调试
- MicroSoft SQL Server Manager 通过ODBC与VS进行连接操作示例程序
- 通过位运算对程序进行加解密
- imx6 通过SD卡启动后,对emmc进行分区和程序更新
- 【static&extern】用GCC输出带源代码的汇编程序进行链接的调试
- SQL Server 2000升级sp4补丁的时候出现“以前进行的程序安装创建了挂起的文件操作”的错误!
- 编写程序通过栈的方式将任意输入的字符串内容进行逆序输出。