Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 60fa8b8a authored by Joshua Gilpatrick's avatar Joshua Gilpatrick Committed by Gerrit Code Review
Browse files

Merge "Fastdeploy converted to c++ and bin2c on the jar."

parents e1d4e390 af3ce087
Loading
Loading
Loading
Loading
+100 −5
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ cc_defaults {
        "-DADB_HOST=1",         // overridden by adbd_defaults
        "-DALLOW_ADBD_ROOT=0",  // overridden by adbd_defaults
        "-DANDROID_BASE_UNIQUE_FD_DISABLE_IMPLICIT_CONVERSION=1",
        "-DENABLE_FASTDEPLOY=1", // enable fast deploy
    ],
    cpp_std: "experimental",

@@ -270,22 +271,33 @@ cc_binary_host {
        "client/console.cpp",
        "client/adb_install.cpp",
        "client/line_printer.cpp",
        "client/fastdeploy.cpp",
        "client/fastdeploycallbacks.cpp",
        "shell_service_protocol.cpp",
    ],

    generated_headers: [
        "bin2c_fastdeployagent",
        "bin2c_fastdeployagentscript"
    ],

    static_libs: [
        "libadb_host",
        "libandroidfw",
        "libbase",
        "libcutils",
        "libcrypto_utils",
        "libcrypto",
        "libfastdeploy_host",
        "libdiagnose_usb",
        "liblog",
        "libmdnssd",
        "libprotobuf-cpp-lite",
        "libusb",
        "libutils",
        "liblog",
        "libcutils",
        "libziparchive",
        "libz",
    ],

    stl: "libc++_static",
@@ -295,10 +307,6 @@ cc_binary_host {
    // will violate ODR
    shared_libs: [],

    required: [
        "deploypatchgenerator",
    ],

    // Archive adb, adb.exe.
    dist: {
        targets: [
@@ -658,3 +666,90 @@ python_test_host {
        },
    },
}

// Note: using pipe for xxd to control the variable name generated
// the default name used by xxd is the path to the input file.
java_genrule {
    name: "bin2c_fastdeployagent",
    out: ["deployagent.inc"],
    srcs: [":deployagent"],
    cmd: "(echo 'unsigned char kDeployAgent[] = {' && xxd -i <$(in) && echo '};') > $(out)",
}

genrule {
    name: "bin2c_fastdeployagentscript",
    out: ["deployagentscript.inc"],
    srcs: ["fastdeploy/deployagent/deployagent.sh"],
    cmd: "(echo 'unsigned char kDeployAgentScript[] = {' && xxd -i <$(in) && echo '};') > $(out)",
}

cc_library_host_static {
    name: "libfastdeploy_host",
    defaults: ["adb_defaults"],
    srcs: [
        "fastdeploy/deploypatchgenerator/deploy_patch_generator.cpp",
        "fastdeploy/deploypatchgenerator/patch_utils.cpp",
        "fastdeploy/proto/ApkEntry.proto",
    ],
    static_libs: [
        "libadb_host",
        "libandroidfw",
        "libbase",
        "libcutils",
        "libcrypto_utils",
        "libcrypto",
        "libdiagnose_usb",
        "liblog",
        "libmdnssd",
        "libusb",
        "libutils",
        "libziparchive",
        "libz",
    ],
    stl: "libc++_static",
    proto: {
        type: "lite",
        export_proto_headers: true,
    },
    target: {
        windows: {
            enabled: true,
            shared_libs: ["AdbWinApi"],
        },
    },
}

cc_test_host {
    name: "fastdeploy_test",
    defaults: ["adb_defaults"],
    srcs: [
        "fastdeploy/deploypatchgenerator/deploy_patch_generator_test.cpp",
        "fastdeploy/deploypatchgenerator/patch_utils_test.cpp",
    ],
    static_libs: [
        "libadb_host",
        "libandroidfw",
        "libbase",
        "libcutils",
        "libcrypto_utils",
        "libcrypto",
        "libdiagnose_usb",
        "libfastdeploy_host",
        "liblog",
        "libmdnssd",
        "libprotobuf-cpp-lite",
        "libusb",
        "libutils",
        "libziparchive",
        "libz",
    ],
    target: {
        windows: {
            enabled: true,
            shared_libs: ["AdbWinApi"],
        },
    },
    data: [
        "fastdeploy/testdata/rotating_cube-release.apk",
    ],
}
+37 −65
Original line number Diff line number Diff line
@@ -27,6 +27,9 @@
#include "androidfw/ZipFileRO.h"
#include "client/file_sync_client.h"
#include "commandline.h"
#include "deployagent.inc"        // Generated include via build rule.
#include "deployagentscript.inc"  // Generated include via build rule.
#include "fastdeploy/deploypatchgenerator/deploy_patch_generator.h"
#include "fastdeploycallbacks.h"
#include "sysdeps.h"

@@ -35,6 +38,8 @@
static constexpr long kRequiredAgentVersion = 0x00000002;

static constexpr const char* kDeviceAgentPath = "/data/local/tmp/";
static constexpr const char* kDeviceAgentFile = "/data/local/tmp/deployagent.jar";
static constexpr const char* kDeviceAgentScript = "/data/local/tmp/deployagent";

static bool g_use_localagent = false;

@@ -71,47 +76,33 @@ void fastdeploy_set_local_agent(bool use_localagent) {
    g_use_localagent = use_localagent;
}

// local_path - must start with a '/' and be relative to $ANDROID_PRODUCT_OUT
static std::string get_agent_component_host_path(const char* local_path, const char* sdk_path) {
    std::string adb_dir = android::base::GetExecutableDirectory();
    if (adb_dir.empty()) {
        error_exit("Could not determine location of adb!");
    }

    if (g_use_localagent) {
        const char* product_out = getenv("ANDROID_PRODUCT_OUT");
        if (product_out == nullptr) {
            error_exit("Could not locate %s because $ANDROID_PRODUCT_OUT is not defined",
                       local_path);
        }
        return android::base::StringPrintf("%s%s", product_out, local_path);
    } else {
        return adb_dir + sdk_path;
    }
}

static bool deploy_agent(bool checkTimeStamps) {
    std::vector<const char*> srcs;
    std::string jar_path =
            get_agent_component_host_path("/system/framework/deployagent.jar", "/deployagent.jar");
    std::string script_path =
            get_agent_component_host_path("/system/bin/deployagent", "/deployagent");
    srcs.push_back(jar_path.c_str());
    srcs.push_back(script_path.c_str());

    if (do_sync_push(srcs, kDeviceAgentPath, checkTimeStamps)) {
    // TODO: Deploy agent from bin2c directly instead of writing to disk first.
    TemporaryFile tempAgent;
    android::base::WriteFully(tempAgent.fd, kDeployAgent, sizeof(kDeployAgent));
    srcs.push_back(tempAgent.path);
    if (!do_sync_push(srcs, kDeviceAgentFile, checkTimeStamps)) {
        error_exit("Failed to push fastdeploy agent to device.");
    }
    srcs.clear();
    // TODO: Deploy agent from bin2c directly instead of writing to disk first.
    TemporaryFile tempAgentScript;
    android::base::WriteFully(tempAgentScript.fd, kDeployAgentScript, sizeof(kDeployAgentScript));
    srcs.push_back(tempAgentScript.path);
    if (!do_sync_push(srcs, kDeviceAgentScript, checkTimeStamps)) {
        error_exit("Failed to push fastdeploy agent script to device.");
    }
    srcs.clear();
    // on windows the shell script might have lost execute permission
    // so need to set this explicitly
        const char* kChmodCommandPattern = "chmod 777 %sdeployagent";
    const char* kChmodCommandPattern = "chmod 777 %s";
    std::string chmodCommand =
                android::base::StringPrintf(kChmodCommandPattern, kDeviceAgentPath);
            android::base::StringPrintf(kChmodCommandPattern, kDeviceAgentScript);
    int ret = send_shell_command(chmodCommand);
    if (ret != 0) {
        error_exit("Error executing %s returncode: %d", chmodCommand.c_str(), ret);
    }
    } else {
        error_exit("Error pushing agent files to device");
    }

    return true;
}
@@ -238,34 +229,15 @@ void extract_metadata(const char* apkPath, FILE* outputFp) {
    }
}

static std::string get_patch_generator_command() {
    if (g_use_localagent) {
        // This should never happen on a Windows machine
        const char* host_out = getenv("ANDROID_HOST_OUT");
        if (host_out == nullptr) {
            error_exit(
                    "Could not locate deploypatchgenerator.jar because $ANDROID_HOST_OUT "
                    "is not defined");
        }
        return android::base::StringPrintf("java -jar %s/framework/deploypatchgenerator.jar",
                                           host_out);
    }

    std::string adb_dir = android::base::GetExecutableDirectory();
    if (adb_dir.empty()) {
        error_exit("Could not locate deploypatchgenerator.jar");
    }
    return android::base::StringPrintf(R"(java -jar "%s/deploypatchgenerator.jar")",
                                       adb_dir.c_str());
}

void create_patch(const char* apkPath, const char* metadataPath, const char* patchPath) {
    std::string generatePatchCommand = android::base::StringPrintf(
            R"(%s "%s" "%s" > "%s")", get_patch_generator_command().c_str(), apkPath, metadataPath,
            patchPath);
    int returnCode = system(generatePatchCommand.c_str());
    if (returnCode != 0) {
        error_exit("Executing %s returned %d", generatePatchCommand.c_str(), returnCode);
    DeployPatchGenerator generator(false);
    unique_fd patchFd(adb_open(patchPath, O_WRONLY | O_CREAT | O_CLOEXEC));
    if (patchFd < 0) {
        perror_exit("adb: failed to create %s", patchPath);
    }
    bool success = generator.CreatePatch(apkPath, metadataPath, patchFd);
    if (!success) {
        error_exit("Failed to create patch for %s", apkPath);
    }
}

+1 −13
Original line number Diff line number Diff line
@@ -13,13 +13,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//

java_binary {
    name: "deployagent",
    sdk_version: "24",
    srcs: ["deployagent/src/**/*.java", "deploylib/src/**/*.java", "proto/**/*.proto"],
    static_libs: ["apkzlib_zip"],
    wrapper: "deployagent/deployagent.sh",
    proto: {
        type: "lite",
    },
@@ -27,13 +25,3 @@ java_binary {
        enabled: false,
    }
}
 No newline at end of file

java_binary_host {
    name: "deploypatchgenerator",
    srcs: ["deploypatchgenerator/src/**/*.java", "deploylib/src/**/*.java", "proto/**/*.proto"],
    static_libs: ["apkzlib"],
    manifest: "deploypatchgenerator/manifest.txt",
    proto: {
        type: "full",
    }
}
+1 −3
Original line number Diff line number Diff line
@@ -181,7 +181,7 @@ public final class DeployAgent {
    private static void extractMetaData(String packageName) throws IOException {
        File apkFile = getFileFromPackageName(packageName);
        APKMetaData apkMetaData = PatchUtils.getAPKMetaData(apkFile);
        apkMetaData.writeDelimitedTo(System.out);
        apkMetaData.writeTo(System.out);
    }

    private static int createInstallSession(String[] args) throws IOException {
@@ -223,7 +223,6 @@ public final class DeployAgent {
        }

        int writeExitCode = writePatchedDataToSession(new RandomAccessFile(deviceFile, "r"), deltaStream, sessionId);

        if (writeExitCode == 0) {
            return commitInstallSession(sessionId);
        } else {
@@ -285,7 +284,6 @@ public final class DeployAgent {
            if (oldDataLen > 0) {
                PatchUtils.pipe(oldData, outputStream, buffer, (int) oldDataLen);
            }

            newDataBytesWritten += copyLen + oldDataLen;
        }

+1 −1
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ import com.android.fastdeploy.APKEntry;
class PatchUtils {
    private static final long NEGATIVE_MASK = 1L << 63;
    private static final long NEGATIVE_LONG_SIGN_MASK = 1L << 63;
    public static final String SIGNATURE = "HAMADI/IHD";
    public static final String SIGNATURE = "FASTDEPLOY";

    private static long getOffsetFromEntry(StoredEntry entry) {
        return entry.getCentralDirectoryHeader().getOffset() + entry.getLocalHeaderSize();
Loading