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.
diff --git a/tools/applypatch/Android.mk b/tools/applypatch/Android.mk
index c25e0d6..fe317ff 100644
--- a/tools/applypatch/Android.mk
+++ b/tools/applypatch/Android.mk
@@ -18,11 +18,22 @@
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 db4c30b..49ec97f 100644
--- a/tools/applypatch/applypatch.c
+++ b/tools/applypatch/applypatch.c
@@ -509,23 +509,48 @@
// <src-file> (or <file> 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:
+//
+// <src-file> <tgt-file> <tgt-sha1> <tgt-size> [<src-sha1>:<patch> ...]
+// to apply a patch. Returns 0 on success, 1 on failure.
+//
+// "-c" <file> [<sha1> ...]
+// 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" <bytes>
+// 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 main(int argc, char** argv) {
+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);
+ }
+
if (argc < 2) {
- usage:
- fprintf(stderr,
- "usage: %s <src-file> <tgt-file> <tgt-sha1> <tgt-size> "
- "[<src-sha1>:<patch> ...]\n"
- " or %s -c <file> [<sha1> ...]\n"
- " or %s -s <bytes>\n"
- " or %s -l\n"
- "\n"
- "Filenames may be of the form\n"
- " MTD:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>"
- ":...:<backup-file>\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 @@
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 e0320fb..ccd8424 100644
--- a/tools/applypatch/applypatch.h
+++ b/tools/applypatch/applypatch.h
@@ -17,6 +17,7 @@
#ifndef _APPLYPATCH_H
#define _APPLYPATCH_H
+#include <sys/stat.h>
#include "mincrypt/sha.h"
typedef struct _Patch {
@@ -45,6 +46,7 @@
// 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 0000000..e25c730
--- /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 <stdio.h>
+
+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 <tgt-file> is <tgt-sha1>, does nothing and exits
+// successfully.
+//
+// - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the
+// bsdiff <patch> to <src-file> to produce a new file (the type of patch
+// is automatically detected from the file header). If that new
+// file has sha1 hash <tgt-sha1>, moves it to replace <tgt-file>, and
+// exits successfully. Note that if <src-file> and <tgt-file> are
+// not the same, <src-file> is NOT deleted on success. <tgt-file>
+// may be the string "-" to mean "the same as src-file".
+//
+// - otherwise, or if any error is encountered, exits with non-zero
+// status.
+//
+// <src-file> (or <file> 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 <src-file> <tgt-file> <tgt-sha1> <tgt-size> "
+ "[<src-sha1>:<patch> ...]\n"
+ " or %s -c <file> [<sha1> ...]\n"
+ " or %s -s <bytes>\n"
+ " or %s -l\n"
+ "\n"
+ "Filenames may be of the form\n"
+ " MTD:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n"
+ "to specify reading from or writing to an MTD partition.\n\n",
+ argv[0], argv[0], argv[0], argv[0]);
+ }
+ return result;
+}