Merge "Make 'idmap --scan' accept more than one input directory"

This commit is contained in:
Adam Lesinski
2015-10-12 18:53:25 +00:00
committed by Gerrit Code Review
4 changed files with 92 additions and 55 deletions

View File

@@ -13,7 +13,8 @@ SYNOPSIS \n\
idmap --help \n\ idmap --help \n\
idmap --fd target overlay fd \n\ idmap --fd target overlay fd \n\
idmap --path target overlay idmap \n\ idmap --path target overlay idmap \n\
idmap --scan dir-to-scan target-to-look-for target dir-to-hold-idmaps \n\ idmap --scan target-package-name-to-look-for path-to-target-apk dir-to-hold-idmaps \\\
dir-to-scan [additional-dir-to-scan [additional-dir-to-scan [...]]]\n\
idmap --inspect idmap \n\ idmap --inspect idmap \n\
\n\ \n\
DESCRIPTION \n\ DESCRIPTION \n\
@@ -49,9 +50,9 @@ OPTIONS \n\
'overlay' (path to apk); write results to 'idmap' (path). \n\ 'overlay' (path to apk); write results to 'idmap' (path). \n\
\n\ \n\
--scan: non-recursively search directory 'dir-to-scan' (path) for overlay packages with \n\ --scan: non-recursively search directory 'dir-to-scan' (path) for overlay packages with \n\
target package 'target-to-look-for' (package name) present at 'target' (path to \n\ target package 'target-package-name-to-look-for' (package name) present at\n\
apk). For each overlay package found, create an idmap file in 'dir-to-hold-idmaps' \n\ 'path-to-target-apk' (path to apk). For each overlay package found, create an\n\
(path). \n\ idmap file in 'dir-to-hold-idmaps' (path). \n\
\n\ \n\
--inspect: decode the binary format of 'idmap' (path) and display the contents in a \n\ --inspect: decode the binary format of 'idmap' (path) and display the contents in a \n\
debug-friendly format. \n\ debug-friendly format. \n\
@@ -166,19 +167,14 @@ NOTES \n\
return idmap_create_path(target_apk_path, overlay_apk_path, idmap_path); return idmap_create_path(target_apk_path, overlay_apk_path, idmap_path);
} }
int maybe_scan(const char *overlay_dir, const char *target_package_name, int maybe_scan(const char *target_package_name, const char *target_apk_path,
const char *target_apk_path, const char *idmap_dir) const char *idmap_dir, const android::Vector<const char *> *overlay_dirs)
{ {
if (!verify_root_or_system()) { if (!verify_root_or_system()) {
fprintf(stderr, "error: permission denied: not user root or user system\n"); fprintf(stderr, "error: permission denied: not user root or user system\n");
return -1; return -1;
} }
if (!verify_directory_readable(overlay_dir)) {
ALOGD("error: no read access to %s: %s\n", overlay_dir, strerror(errno));
return -1;
}
if (!verify_file_readable(target_apk_path)) { if (!verify_file_readable(target_apk_path)) {
ALOGD("error: failed to read apk %s: %s\n", target_apk_path, strerror(errno)); ALOGD("error: failed to read apk %s: %s\n", target_apk_path, strerror(errno));
return -1; return -1;
@@ -189,7 +185,16 @@ NOTES \n\
return -1; return -1;
} }
return idmap_scan(overlay_dir, target_package_name, target_apk_path, idmap_dir); const size_t N = overlay_dirs->size();
for (size_t i = 0; i < N; i++) {
const char *dir = overlay_dirs->itemAt(i);
if (!verify_directory_readable(dir)) {
ALOGD("error: no read access to %s: %s\n", dir, strerror(errno));
return -1;
}
}
return idmap_scan(target_package_name, target_apk_path, idmap_dir, overlay_dirs);
} }
int maybe_inspect(const char *idmap_path) int maybe_inspect(const char *idmap_path)
@@ -230,8 +235,12 @@ int main(int argc, char **argv)
return maybe_create_path(argv[2], argv[3], argv[4]); return maybe_create_path(argv[2], argv[3], argv[4]);
} }
if (argc == 6 && !strcmp(argv[1], "--scan")) { if (argc >= 6 && !strcmp(argv[1], "--scan")) {
return maybe_scan(argv[2], argv[3], argv[4], argv[5]); android::Vector<const char *> v;
for (int i = 5; i < argc; i++) {
v.push(argv[i]);
}
return maybe_scan(argv[2], argv[3], argv[4], &v);
} }
if (argc == 3 && !strcmp(argv[1], "--inspect")) { if (argc == 3 && !strcmp(argv[1], "--inspect")) {

View File

@@ -1,9 +1,11 @@
#ifndef _IDMAP_H_ #ifndef _IDMAP_H_
#define _IDMAP_H_ #define _IDMAP_H_
#define LOG_TAG "idmap" #define LOG_TAG "idmap"
#include <utils/Log.h> #include <utils/Log.h>
#include <utils/Vector.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
@@ -26,8 +28,8 @@ int idmap_create_fd(const char *target_apk_path, const char *overlay_apk_path, i
// Regarding target_package_name: the idmap_scan implementation should // Regarding target_package_name: the idmap_scan implementation should
// be able to extract this from the manifest in target_apk_path, // be able to extract this from the manifest in target_apk_path,
// simplifying the external API. // simplifying the external API.
int idmap_scan(const char *overlay_dir, const char *target_package_name, int idmap_scan(const char *target_package_name, const char *target_apk_path,
const char *target_apk_path, const char *idmap_dir); const char *idmap_dir, const android::Vector<const char *> *overlay_dirs);
int idmap_inspect(const char *idmap_path); int idmap_inspect(const char *idmap_path);

View File

@@ -167,8 +167,8 @@ namespace {
} }
} }
int idmap_scan(const char *overlay_dir, const char *target_package_name, int idmap_scan(const char *target_package_name, const char *target_apk_path,
const char *target_apk_path, const char *idmap_dir) const char *idmap_dir, const android::Vector<const char *> *overlay_dirs)
{ {
String8 filename = String8(idmap_dir); String8 filename = String8(idmap_dir);
filename.appendPath("overlays.list"); filename.appendPath("overlays.list");
@@ -176,45 +176,49 @@ int idmap_scan(const char *overlay_dir, const char *target_package_name,
return EXIT_FAILURE; return EXIT_FAILURE;
} }
DIR *dir = opendir(overlay_dir);
if (dir == NULL) {
return EXIT_FAILURE;
}
SortedVector<Overlay> overlayVector; SortedVector<Overlay> overlayVector;
struct dirent *dirent; const size_t N = overlay_dirs->size();
while ((dirent = readdir(dir)) != NULL) { for (size_t i = 0; i < N; ++i) {
struct stat st; const char *overlay_dir = overlay_dirs->itemAt(i);
char overlay_apk_path[PATH_MAX + 1]; DIR *dir = opendir(overlay_dir);
snprintf(overlay_apk_path, PATH_MAX, "%s/%s", overlay_dir, dirent->d_name); if (dir == NULL) {
if (stat(overlay_apk_path, &st) < 0) { return EXIT_FAILURE;
continue;
}
if (!S_ISREG(st.st_mode)) {
continue;
} }
int priority = parse_apk(overlay_apk_path, target_package_name); struct dirent *dirent;
if (priority < 0) { while ((dirent = readdir(dir)) != NULL) {
continue; struct stat st;
char overlay_apk_path[PATH_MAX + 1];
snprintf(overlay_apk_path, PATH_MAX, "%s/%s", overlay_dir, dirent->d_name);
if (stat(overlay_apk_path, &st) < 0) {
continue;
}
if (!S_ISREG(st.st_mode)) {
continue;
}
int priority = parse_apk(overlay_apk_path, target_package_name);
if (priority < 0) {
continue;
}
String8 idmap_path(idmap_dir);
idmap_path.appendPath(flatten_path(overlay_apk_path + 1));
idmap_path.append("@idmap");
if (idmap_create_path(target_apk_path, overlay_apk_path, idmap_path.string()) != 0) {
ALOGE("error: failed to create idmap for target=%s overlay=%s idmap=%s\n",
target_apk_path, overlay_apk_path, idmap_path.string());
continue;
}
Overlay overlay(String8(overlay_apk_path), idmap_path, priority);
overlayVector.add(overlay);
} }
String8 idmap_path(idmap_dir); closedir(dir);
idmap_path.appendPath(flatten_path(overlay_apk_path + 1));
idmap_path.append("@idmap");
if (idmap_create_path(target_apk_path, overlay_apk_path, idmap_path.string()) != 0) {
ALOGE("error: failed to create idmap for target=%s overlay=%s idmap=%s\n",
target_apk_path, overlay_apk_path, idmap_path.string());
continue;
}
Overlay overlay(String8(overlay_apk_path), idmap_path, priority);
overlayVector.add(overlay);
} }
closedir(dir);
if (!writePackagesList(filename.string(), overlayVector)) { if (!writePackagesList(filename.string(), overlayVector)) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }

View File

@@ -24,6 +24,7 @@
#include <stdio.h> #include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/stat.h>
#include <private/android_filesystem_config.h> // for AID_SYSTEM #include <private/android_filesystem_config.h> // for AID_SYSTEM
@@ -162,11 +163,32 @@ static void verifySystemIdmaps()
exit(1); exit(1);
} }
execl(AssetManager::IDMAP_BIN, AssetManager::IDMAP_BIN, "--scan", // Generic idmap parameters
AssetManager::OVERLAY_DIR, AssetManager::TARGET_PACKAGE_NAME, const char* argv[7];
AssetManager::TARGET_APK_PATH, AssetManager::IDMAP_DIR, (char*)NULL); int argc = 0;
ALOGE("failed to execl for idmap: %s", strerror(errno)); struct stat st;
exit(1); // should never get here
memset(argv, NULL, sizeof(argv));
argv[argc++] = AssetManager::IDMAP_BIN;
argv[argc++] = "--scan";
argv[argc++] = AssetManager::TARGET_PACKAGE_NAME;
argv[argc++] = AssetManager::TARGET_APK_PATH;
argv[argc++] = AssetManager::IDMAP_DIR;
// Directories to scan for overlays
// /vendor/overlay
if (stat(AssetManager::OVERLAY_DIR, &st) == 0) {
argv[argc++] = AssetManager::OVERLAY_DIR;
}
// Finally, invoke idmap (if any overlay directory exists)
if (argc > 5) {
execv(AssetManager::IDMAP_BIN, (char* const*)argv);
ALOGE("failed to execl for idmap: %s", strerror(errno));
exit(1); // should never get here
} else {
exit(0);
}
} }
break; break;
default: // parent default: // parent