2018-01-29 17:30:36 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 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 <libgen.h>
|
|
|
|
#include <algorithm>
|
2018-08-28 13:58:09 +02:00
|
|
|
#include <array>
|
2018-01-29 17:30:36 +01:00
|
|
|
|
2018-08-28 13:58:09 +02:00
|
|
|
#include "android-base/file.h"
|
|
|
|
#include "android-base/strings.h"
|
2018-01-29 17:30:36 +01:00
|
|
|
#include "client/file_sync_client.h"
|
|
|
|
#include "commandline.h"
|
|
|
|
#include "fastdeploy.h"
|
|
|
|
#include "fastdeploycallbacks.h"
|
|
|
|
#include "utils/String16.h"
|
|
|
|
|
2018-09-05 21:13:11 +02:00
|
|
|
static constexpr long kRequiredAgentVersion = 0x00000001;
|
2018-01-29 17:30:36 +01:00
|
|
|
|
2018-09-05 21:13:11 +02:00
|
|
|
static constexpr const char* kDeviceAgentPath = "/data/local/tmp/";
|
2018-01-29 17:30:36 +01:00
|
|
|
|
2018-09-06 19:42:39 +02:00
|
|
|
static bool g_use_localagent = false;
|
2018-08-28 13:58:09 +02:00
|
|
|
|
2018-01-29 17:30:36 +01:00
|
|
|
long get_agent_version() {
|
|
|
|
std::vector<char> versionOutputBuffer;
|
|
|
|
std::vector<char> versionErrorBuffer;
|
|
|
|
|
2018-08-24 12:46:45 +02:00
|
|
|
int statusCode = capture_shell_command("/data/local/tmp/deployagent version",
|
2018-01-29 17:30:36 +01:00
|
|
|
&versionOutputBuffer, &versionErrorBuffer);
|
|
|
|
long version = -1;
|
|
|
|
|
|
|
|
if (statusCode == 0 && versionOutputBuffer.size() > 0) {
|
|
|
|
version = strtol((char*)versionOutputBuffer.data(), NULL, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_device_api_level() {
|
|
|
|
std::vector<char> sdkVersionOutputBuffer;
|
|
|
|
std::vector<char> sdkVersionErrorBuffer;
|
|
|
|
int api_level = -1;
|
|
|
|
|
|
|
|
int statusCode = capture_shell_command("getprop ro.build.version.sdk", &sdkVersionOutputBuffer,
|
|
|
|
&sdkVersionErrorBuffer);
|
2018-08-23 18:22:21 +02:00
|
|
|
if (statusCode == 0 && sdkVersionOutputBuffer.size() > 0) {
|
2018-01-29 17:30:36 +01:00
|
|
|
api_level = strtol((char*)sdkVersionOutputBuffer.data(), NULL, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
return api_level;
|
|
|
|
}
|
|
|
|
|
2018-09-06 19:42:39 +02:00
|
|
|
void fastdeploy_set_local_agent(bool use_localagent) {
|
|
|
|
g_use_localagent = use_localagent;
|
2018-08-28 13:58:09 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 17:30:36 +01:00
|
|
|
// local_path - must start with a '/' and be relative to $ANDROID_PRODUCT_OUT
|
2018-09-06 19:42:39 +02:00
|
|
|
static std::string get_agent_component_host_path(const char* local_path, const char* sdk_path) {
|
2018-08-28 13:58:09 +02:00
|
|
|
std::string adb_dir = android::base::GetExecutableDirectory();
|
|
|
|
if (adb_dir.empty()) {
|
2018-09-06 19:42:39 +02:00
|
|
|
fatal("Could not determine location of adb!");
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
|
2018-09-06 19:42:39 +02:00
|
|
|
if (g_use_localagent) {
|
2018-01-29 17:30:36 +01:00
|
|
|
const char* product_out = getenv("ANDROID_PRODUCT_OUT");
|
|
|
|
if (product_out == nullptr) {
|
2018-09-06 19:42:39 +02:00
|
|
|
fatal("Could not locate %s because $ANDROID_PRODUCT_OUT is not defined", local_path);
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
2018-09-06 19:42:39 +02:00
|
|
|
return android::base::StringPrintf("%s%s", product_out, local_path);
|
2018-01-29 17:30:36 +01:00
|
|
|
} else {
|
2018-09-06 19:42:39 +02:00
|
|
|
return adb_dir + sdk_path;
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 13:58:09 +02:00
|
|
|
static bool deploy_agent(bool checkTimeStamps) {
|
2018-01-29 17:30:36 +01:00
|
|
|
std::vector<const char*> srcs;
|
2018-09-06 19:42:39 +02:00
|
|
|
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());
|
2018-01-29 17:30:36 +01:00
|
|
|
|
|
|
|
if (do_sync_push(srcs, kDeviceAgentPath, checkTimeStamps)) {
|
|
|
|
// on windows the shell script might have lost execute permission
|
|
|
|
// so need to set this explicitly
|
2018-08-24 12:46:45 +02:00
|
|
|
const char* kChmodCommandPattern = "chmod 777 %sdeployagent";
|
2018-01-29 17:30:36 +01:00
|
|
|
std::string chmodCommand =
|
|
|
|
android::base::StringPrintf(kChmodCommandPattern, kDeviceAgentPath);
|
2018-08-23 18:22:21 +02:00
|
|
|
int ret = send_shell_command(chmodCommand);
|
2018-09-06 19:42:39 +02:00
|
|
|
if (ret != 0) {
|
|
|
|
fatal("Error executing %s returncode: %d", chmodCommand.c_str(), ret);
|
|
|
|
}
|
2018-01-29 17:30:36 +01:00
|
|
|
} else {
|
2018-09-06 19:42:39 +02:00
|
|
|
fatal("Error pushing agent files to device");
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
2018-09-06 19:42:39 +02:00
|
|
|
|
|
|
|
return true;
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
|
2018-09-06 19:42:39 +02:00
|
|
|
void update_agent(FastDeploy_AgentUpdateStrategy agentUpdateStrategy) {
|
2018-01-29 17:30:36 +01:00
|
|
|
long agent_version = get_agent_version();
|
|
|
|
switch (agentUpdateStrategy) {
|
|
|
|
case FastDeploy_AgentUpdateAlways:
|
2018-09-06 19:42:39 +02:00
|
|
|
deploy_agent(false);
|
2018-01-29 17:30:36 +01:00
|
|
|
break;
|
|
|
|
case FastDeploy_AgentUpdateNewerTimeStamp:
|
2018-09-06 19:42:39 +02:00
|
|
|
deploy_agent(true);
|
2018-01-29 17:30:36 +01:00
|
|
|
break;
|
|
|
|
case FastDeploy_AgentUpdateDifferentVersion:
|
|
|
|
if (agent_version != kRequiredAgentVersion) {
|
|
|
|
if (agent_version < 0) {
|
|
|
|
printf("Could not detect agent on device, deploying\n");
|
|
|
|
} else {
|
|
|
|
printf("Device agent version is (%ld), (%ld) is required, re-deploying\n",
|
|
|
|
agent_version, kRequiredAgentVersion);
|
|
|
|
}
|
2018-09-06 19:42:39 +02:00
|
|
|
deploy_agent(false);
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
agent_version = get_agent_version();
|
2018-09-06 19:42:39 +02:00
|
|
|
if (agent_version != kRequiredAgentVersion) {
|
|
|
|
fatal("After update agent version remains incorrect! Expected %ld but version is %ld",
|
|
|
|
kRequiredAgentVersion, agent_version);
|
|
|
|
}
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
|
2018-08-28 13:58:09 +02:00
|
|
|
static std::string get_aapt2_path() {
|
2018-09-06 19:42:39 +02:00
|
|
|
if (g_use_localagent) {
|
2018-08-28 13:58:09 +02:00
|
|
|
// This should never happen on a Windows machine
|
|
|
|
const char* host_out = getenv("ANDROID_HOST_OUT");
|
|
|
|
if (host_out == nullptr) {
|
|
|
|
fatal("Could not locate aapt2 because $ANDROID_HOST_OUT is not defined");
|
|
|
|
}
|
|
|
|
return android::base::StringPrintf("%s/bin/aapt2", host_out);
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
|
2018-08-28 13:58:09 +02:00
|
|
|
std::string adb_dir = android::base::GetExecutableDirectory();
|
|
|
|
if (adb_dir.empty()) {
|
|
|
|
fatal("Could not locate aapt2");
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
2018-08-28 13:58:09 +02:00
|
|
|
return adb_dir + "/aapt2";
|
|
|
|
}
|
2018-01-29 17:30:36 +01:00
|
|
|
|
2018-08-28 13:58:09 +02:00
|
|
|
static int system_capture(const char* cmd, std::string& output) {
|
|
|
|
FILE* pipe = popen(cmd, "re");
|
|
|
|
int fd = -1;
|
2018-01-29 17:30:36 +01:00
|
|
|
|
2018-08-28 13:58:09 +02:00
|
|
|
if (pipe != nullptr) {
|
|
|
|
fd = fileno(pipe);
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
|
2018-08-28 13:58:09 +02:00
|
|
|
if (fd == -1) {
|
|
|
|
fatal_errno("Could not create pipe for process '%s'", cmd);
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
|
2018-08-28 13:58:09 +02:00
|
|
|
if (!android::base::ReadFdToString(fd, &output)) {
|
|
|
|
fatal_errno("Error reading from process '%s'", cmd);
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
|
2018-08-28 13:58:09 +02:00
|
|
|
return pclose(pipe);
|
|
|
|
}
|
2018-01-29 17:30:36 +01:00
|
|
|
|
2018-08-28 13:58:09 +02:00
|
|
|
// output is required to point to a valid output string (non-null)
|
2018-09-06 19:42:39 +02:00
|
|
|
static std::string get_packagename_from_apk(const char* apkPath) {
|
2018-08-28 13:58:09 +02:00
|
|
|
const char* kAapt2DumpNameCommandPattern = R"(%s dump packagename "%s")";
|
|
|
|
std::string aapt2_path_string = get_aapt2_path();
|
|
|
|
std::string getPackagenameCommand = android::base::StringPrintf(
|
|
|
|
kAapt2DumpNameCommandPattern, aapt2_path_string.c_str(), apkPath);
|
|
|
|
|
2018-09-06 19:42:39 +02:00
|
|
|
std::string package_name;
|
|
|
|
int exit_code = system_capture(getPackagenameCommand.c_str(), package_name);
|
|
|
|
if (exit_code != 0) {
|
|
|
|
fatal("Error executing '%s' exitcode: %d", getPackagenameCommand.c_str(), exit_code);
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
2018-09-06 19:42:39 +02:00
|
|
|
|
|
|
|
// strip any line end characters from the output
|
|
|
|
return android::base::Trim(package_name);
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int extract_metadata(const char* apkPath, FILE* outputFp) {
|
2018-09-06 19:42:39 +02:00
|
|
|
std::string packageName = get_packagename_from_apk(apkPath);
|
2018-08-24 12:46:45 +02:00
|
|
|
const char* kAgentExtractCommandPattern = "/data/local/tmp/deployagent extract %s";
|
2018-01-29 17:30:36 +01:00
|
|
|
std::string extractCommand =
|
|
|
|
android::base::StringPrintf(kAgentExtractCommandPattern, packageName.c_str());
|
|
|
|
|
|
|
|
std::vector<char> extractErrorBuffer;
|
|
|
|
int statusCode;
|
|
|
|
DeployAgentFileCallback cb(outputFp, &extractErrorBuffer, &statusCode);
|
2018-08-23 18:22:21 +02:00
|
|
|
int ret = send_shell_command(extractCommand, false, &cb);
|
2018-01-29 17:30:36 +01:00
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
return cb.getBytesWritten();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-08-28 13:58:09 +02:00
|
|
|
static std::string get_patch_generator_command() {
|
2018-09-06 19:42:39 +02:00
|
|
|
if (g_use_localagent) {
|
2018-01-29 17:30:36 +01:00
|
|
|
// This should never happen on a Windows machine
|
|
|
|
const char* host_out = getenv("ANDROID_HOST_OUT");
|
|
|
|
if (host_out == nullptr) {
|
2018-08-28 13:58:09 +02:00
|
|
|
fatal("Could not locate deploypatchgenerator.jar because $ANDROID_HOST_OUT is not "
|
|
|
|
"defined");
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
2018-08-28 13:58:09 +02:00
|
|
|
return android::base::StringPrintf("java -jar %s/framework/deploypatchgenerator.jar",
|
|
|
|
host_out);
|
|
|
|
}
|
2018-01-29 17:30:36 +01:00
|
|
|
|
2018-08-28 13:58:09 +02:00
|
|
|
std::string adb_dir = android::base::GetExecutableDirectory();
|
|
|
|
if (adb_dir.empty()) {
|
|
|
|
fatal("Could not locate deploypatchgenerator.jar");
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
2018-08-28 13:58:09 +02:00
|
|
|
return android::base::StringPrintf(R"(java -jar "%s/deploypatchgenerator.jar")",
|
|
|
|
adb_dir.c_str());
|
2018-01-29 17:30:36 +01:00
|
|
|
}
|
|
|
|
|
2018-08-28 13:58:09 +02:00
|
|
|
int create_patch(const char* apkPath, const char* metadataPath, const char* patchPath) {
|
2018-01-29 17:30:36 +01:00
|
|
|
std::string generatePatchCommand = android::base::StringPrintf(
|
2018-08-28 13:58:09 +02:00
|
|
|
R"(%s "%s" "%s" > "%s")", get_patch_generator_command().c_str(), apkPath, metadataPath,
|
|
|
|
patchPath);
|
2018-01-29 17:30:36 +01:00
|
|
|
return system(generatePatchCommand.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string get_patch_path(const char* apkPath) {
|
2018-09-06 19:42:39 +02:00
|
|
|
std::string packageName = get_packagename_from_apk(apkPath);
|
2018-01-29 17:30:36 +01:00
|
|
|
std::string patchDevicePath =
|
|
|
|
android::base::StringPrintf("%s%s.patch", kDeviceAgentPath, packageName.c_str());
|
|
|
|
return patchDevicePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
int apply_patch_on_device(const char* apkPath, const char* patchPath, const char* outputPath) {
|
2018-08-24 12:46:45 +02:00
|
|
|
const std::string kAgentApplyCommandPattern = "/data/local/tmp/deployagent apply %s %s -o %s";
|
2018-09-06 19:42:39 +02:00
|
|
|
std::string packageName = get_packagename_from_apk(apkPath);
|
2018-01-29 17:30:36 +01:00
|
|
|
std::string patchDevicePath = get_patch_path(apkPath);
|
|
|
|
|
|
|
|
std::vector<const char*> srcs = {patchPath};
|
|
|
|
bool push_ok = do_sync_push(srcs, patchDevicePath.c_str(), false);
|
|
|
|
|
|
|
|
if (!push_ok) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string applyPatchCommand =
|
|
|
|
android::base::StringPrintf(kAgentApplyCommandPattern.c_str(), packageName.c_str(),
|
|
|
|
patchDevicePath.c_str(), outputPath);
|
2018-08-28 13:58:09 +02:00
|
|
|
|
2018-01-29 17:30:36 +01:00
|
|
|
return send_shell_command(applyPatchCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
int install_patch(const char* apkPath, const char* patchPath, int argc, const char** argv) {
|
2018-08-24 12:46:45 +02:00
|
|
|
const std::string kAgentApplyCommandPattern = "/data/local/tmp/deployagent apply %s %s -pm %s";
|
2018-09-06 19:42:39 +02:00
|
|
|
std::string packageName = get_packagename_from_apk(apkPath);
|
2018-01-29 17:30:36 +01:00
|
|
|
std::vector<const char*> srcs;
|
|
|
|
std::string patchDevicePath =
|
|
|
|
android::base::StringPrintf("%s%s.patch", kDeviceAgentPath, packageName.c_str());
|
|
|
|
srcs.push_back(patchPath);
|
|
|
|
bool push_ok = do_sync_push(srcs, patchDevicePath.c_str(), false);
|
|
|
|
|
|
|
|
if (!push_ok) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<unsigned char> applyOutputBuffer;
|
|
|
|
std::vector<unsigned char> applyErrorBuffer;
|
|
|
|
std::string argsString;
|
|
|
|
|
|
|
|
for (int i = 0; i < argc; i++) {
|
|
|
|
argsString.append(argv[i]);
|
|
|
|
argsString.append(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string applyPatchCommand =
|
|
|
|
android::base::StringPrintf(kAgentApplyCommandPattern.c_str(), packageName.c_str(),
|
|
|
|
patchDevicePath.c_str(), argsString.c_str());
|
|
|
|
return send_shell_command(applyPatchCommand);
|
|
|
|
}
|