前言:本文是对之前apk增量更新文章的补充内容,单独拎出来梳理一下bsdiff在mac上的编译和使用。下面的介绍流程就是我在自己电脑的操作实现步骤。
第一步:下载
下载请点击:bsdiff/bspatch压缩包
第二步:编译
先解压下载的bsdiff-4.3.tar.gz压缩包;
再进入bsdiff-4.3目录,在终端下执行构建用 make 命令;
此时应该会报错: Makefile:13: *** missing separator. Stop.
这是makefile文件的内容的格式不对,修改Makefile文件,修改后的文件内容如下:
CFLAGS += -O3 -lbz2
PREFIX ?= /usr/local
INSTALL_PROGRAM ?= ${INSTALL} -c -s -m 555
INSTALL_MAN ?= ${INSTALL} -c -m 444
all: bsdiff bspatch
bsdiff: bsdiff.c
bspatch: bspatch.c
install:
${INSTALL_PROGRAM} bsdiff bspatch ${PREFIX}/bin
.ifndef WITHOUT_MAN
${INSTALL_MAN} bsdiff.1 bspatch.1 ${PREFIX}/man/man1
.endif
即在 .ifndef WITHOUT_MAN、 .endi语句前面加TAB键进行缩进。
然后再执行make,此时又报错:
bspatch.c:39:21: error: unknown type name 'u_char'; did you mean 'char'?
static off_t offtin(u_char *buf)
^~~~~~
char
bspatch.c:65:8: error: expected ';' after expression
u_char header[32],buf[8];
^
;
bspatch.c:65:2: error: use of undeclared identifier 'u_char'; did you mean 'putchar'?
u_char header[32],buf[8];
^~~~~~
putchar
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/stdio.h:261:6: note:
'putchar' declared here
int putchar(int);
^
bspatch.c:65:9: error: use of undeclared identifier 'header'
u_char header[32],buf[8];
^
bspatch.c:65:20: error: use of undeclared identifier 'buf'
u_char header[32],buf[8];
^
bspatch.c:66:2: error: use of undeclared identifier 'u_char'; did you mean 'putchar'?
u_char *old, *new;
^~~~~~
putchar
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/stdio.h:261:6: note:
'putchar' declared here
int putchar(int);
^
bspatch.c:66:10: error: use of undeclared identifier 'old'
u_char *old, *new;
^
bspatch.c:66:16: error: use of undeclared identifier 'new'
u_char *old, *new;
^
bspatch.c:93:12: error: use of undeclared identifier 'header'
if (fread(header, 1, 32, f) < 32) {
^
bspatch.c:100:13: error: use of undeclared identifier 'header'
if (memcmp(header, "BSDIFF40", 8) != 0)
^
bspatch.c:104:19: error: use of undeclared identifier 'header'
bzctrllen=offtin(header+8);
^
bspatch.c:105:19: error: use of undeclared identifier 'header'
bzdatalen=offtin(header+16);
^
bspatch.c:106:17: error: use of undeclared identifier 'header'
newsize=offtin(header+24);
^
bspatch.c:137:5: error: use of undeclared identifier 'old'
((old=malloc(oldsize+1))==NULL) ||
^
bspatch.c:139:12: error: use of undeclared identifier 'old'
(read(fd,old,oldsize)!=oldsize) ||
^
bspatch.c:141:6: error: use of undeclared identifier 'new'
if((new=malloc(newsize+1))==NULL) err(1,NULL);
^
bspatch.c:147:43: error: use of undeclared identifier 'buf'
lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8);
^
bspatch.c:151:19: error: use of undeclared identifier 'buf'
ctrl[i]=offtin(buf);
^
bspatch.c:159:42: error: use of undeclared identifier 'new'
lenread = BZ2_bzRead(&dbz2err, dpfbz2, new + newpos, ctrl[0]);
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
errors generated.
make: *** [bspatch] Error 1
解决办法是在 bspatch.c 文件中加入 #include
然后再执行make命令,此时命令执行成功,会在当前目录下生成bsdiff,bspatch文件。
接下来简单介绍一下bsdiff、bspatch命令的使用。
bsdiff:生成差异文件,常用于服务端生成差异包。
命令:bsdiff oldfile newfile patchfile
oldfile: 旧的文件;
newfile:新的文件;
patchfile:生成的差异文件。
bspatch:合并文件,常用于客户端根据差异包合并生成新的文件。
命令:bspatch oldfile newfile2 patchfile
oldfile:旧的文件;
newfile2:生成的新的文件;
patchfile:差异文件;
最后举个例子,测试一下使用bsdiff进行增量更新。
命令 :./bsdiff old.apk new.apk old-new.patch
命令 :./bspatch old.apk new2.apk old-new.patch
命令:md5 new.apk
得到:MD5 (new.apk) = fc8bdb15f80abef22eb25f9601063cf0
命令:md5 new2.apk
得到:MD5 (new2.apk) = fc8bdb15f80abef22eb25f9601063cf0
会发现此时new.apk和new2.apk的md5值是一致的,证明差分包的生成和合并过程是没问题的。