From 5a7908763888909f200fa68cf4b8fbdeac388751 Mon Sep 17 00:00:00 2001 From: Doug Zongker Date: Fri, 12 Jun 2009 09:42:43 -0700 Subject: [PATCH] make applypatch into a static library Turn the bulk of applypatch into a static library so it can be used from the updater. Also build it as a standalone executable for use by the existing OTA mechanism. --- tools/applypatch/Android.mk | 15 +++++++-- tools/applypatch/applypatch.c | 57 +++++++++++++++++++++++---------- tools/applypatch/applypatch.h | 2 ++ tools/applypatch/main.c | 60 +++++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 18 deletions(-) create mode 100644 tools/applypatch/main.c diff --git a/tools/applypatch/Android.mk b/tools/applypatch/Android.mk index c25e0d639..fe317ff04 100644 --- a/tools/applypatch/Android.mk +++ b/tools/applypatch/Android.mk @@ -18,11 +18,22 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES := applypatch.c bsdiff.c freecache.c imgpatch.c +LOCAL_MODULE := libapplypatch +LOCAL_MODULE_TAGS := eng +LOCAL_C_INCLUDES += external/bzip2 external/zlib bootable/recovery +LOCAL_STATIC_LIBRARIES += libmtdutils libmincrypt libbz libz + +include $(BUILD_STATIC_LIBRARY) + +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := main.c LOCAL_MODULE := applypatch LOCAL_FORCE_STATIC_EXECUTABLE := true LOCAL_MODULE_TAGS := eng -LOCAL_C_INCLUDES += external/bzip2 external/zlib bootable/recovery -LOCAL_STATIC_LIBRARIES += libmtdutils libmincrypt libbz libz libc +LOCAL_STATIC_LIBRARIES += libapplypatch +LOCAL_STATIC_LIBRARIES += libmtdutils libmincrypt libbz libz +LOCAL_STATIC_LIBRARIES += libcutils libstdc++ libc include $(BUILD_EXECUTABLE) diff --git a/tools/applypatch/applypatch.c b/tools/applypatch/applypatch.c index db4c30b82..49ec97ff6 100644 --- a/tools/applypatch/applypatch.c +++ b/tools/applypatch/applypatch.c @@ -509,23 +509,48 @@ size_t FreeSpaceForFile(const char* filename) { // (or in check mode) may refer to an MTD partition // to read the source data. See the comments for the // LoadMTDContents() function above for the format of such a filename. +// +// +// As you might guess from the arguments, this function used to be +// main(); it was split out this way so applypatch could be built as a +// static library and linked into other executables as well. In the +// future only the library form will exist; we will not need to build +// this as a standalone executable. +// +// The arguments to this function are just the command-line of the +// standalone executable: +// +// [: ...] +// to apply a patch. Returns 0 on success, 1 on failure. +// +// "-c" [ ...] +// to check a file's contents against zero or more sha1s. Returns +// 0 if it matches any of them, 1 if it doesn't. +// +// "-s" +// returns 0 if enough free space is available on /cache; 1 if it +// does not. +// +// "-l" +// shows open-source license information and returns 0. +// +// This function returns 2 if the arguments are not understood (in the +// standalone executable, this causes the usage message to be +// printed). +// +// TODO: make the interface more sensible for use as a library. + +int applypatch(int argc, char** argv) { + + printf("applypatch argc %d\n", argc); + int xx; + for (xx = 0; xx < argc; ++xx) { + printf("%d %p %s\n", xx, argv[xx], argv[xx]); + fflush(stdout); + } -int main(int argc, char** argv) { if (argc < 2) { - usage: - fprintf(stderr, - "usage: %s " - "[: ...]\n" - " or %s -c [ ...]\n" - " or %s -s \n" - " or %s -l\n" - "\n" - "Filenames may be of the form\n" - " MTD:::::" - ":...:\n" - "to specify reading from or writing to an MTD partition.\n\n", - argv[0], argv[0], argv[0], argv[0]); - return 1; + return 2; } if (strncmp(argv[1], "-l", 3) == 0) { @@ -538,7 +563,7 @@ int main(int argc, char** argv) { if (strncmp(argv[1], "-s", 3) == 0) { if (argc != 3) { - goto usage; + return 2; } size_t bytes = strtol(argv[2], NULL, 10); if (MakeFreeSpaceOnCache(bytes) < 0) { diff --git a/tools/applypatch/applypatch.h b/tools/applypatch/applypatch.h index e0320fb7d..ccd8424ab 100644 --- a/tools/applypatch/applypatch.h +++ b/tools/applypatch/applypatch.h @@ -17,6 +17,7 @@ #ifndef _APPLYPATCH_H #define _APPLYPATCH_H +#include #include "mincrypt/sha.h" typedef struct _Patch { @@ -45,6 +46,7 @@ typedef struct _FileContents { // applypatch.c size_t FreeSpaceForFile(const char* filename); +int applypatch(int argc, char** argv); // bsdiff.c void ShowBSDiffLicense(); diff --git a/tools/applypatch/main.c b/tools/applypatch/main.c new file mode 100644 index 000000000..e25c73045 --- /dev/null +++ b/tools/applypatch/main.c @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +extern int applypatch(int argc, char** argv); + +// This program applies binary patches to files in a way that is safe +// (the original file is not touched until we have the desired +// replacement for it) and idempotent (it's okay to run this program +// multiple times). +// +// - if the sha1 hash of is , does nothing and exits +// successfully. +// +// - otherwise, if the sha1 hash of is , applies the +// bsdiff to to produce a new file (the type of patch +// is automatically detected from the file header). If that new +// file has sha1 hash , moves it to replace , and +// exits successfully. Note that if and are +// not the same, is NOT deleted on success. +// may be the string "-" to mean "the same as src-file". +// +// - otherwise, or if any error is encountered, exits with non-zero +// status. +// +// (or in check mode) may refer to an MTD partition +// to read the source data. See the comments for the +// LoadMTDContents() function above for the format of such a filename. + +int main(int argc, char** argv) { + int result = applypatch(argc, argv); + if (result == 2) { + fprintf(stderr, + "usage: %s " + "[: ...]\n" + " or %s -c [ ...]\n" + " or %s -s \n" + " or %s -l\n" + "\n" + "Filenames may be of the form\n" + " MTD::::::...\n" + "to specify reading from or writing to an MTD partition.\n\n", + argv[0], argv[0], argv[0], argv[0]); + } + return result; +}