4 #include <androidfw/AssetManager.h>
5 #include <androidfw/ResourceTypes.h>
6 #include <androidfw/ZipFileRO.h>
7 #include <utils/String8.h>
12 using namespace android;
15 int get_zip_entry_crc(const char *zip_path, const char *entry_name, uint32_t *crc)
17 UniquePtr<ZipFileRO> zip(ZipFileRO::open(zip_path));
18 if (zip.get() == NULL) {
21 ZipEntryRO entry = zip->findEntryByName(entry_name);
25 if (!zip->getEntryInfo(entry, NULL, NULL, NULL, NULL, NULL, (long*)crc)) {
28 zip->releaseEntry(entry);
32 int open_idmap(const char *path)
34 int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644));
36 ALOGD("error: open %s: %s\n", path, strerror(errno));
39 if (fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
40 ALOGD("error: fchmod %s: %s\n", path, strerror(errno));
43 if (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX | LOCK_NB)) != 0) {
44 ALOGD("error: flock %s: %s\n", path, strerror(errno));
57 int write_idmap(int fd, const uint32_t *data, size_t size)
59 if (lseek(fd, SEEK_SET, 0) < 0) {
62 size_t bytesLeft = size;
63 while (bytesLeft > 0) {
64 ssize_t w = TEMP_FAILURE_RETRY(write(fd, data + size - bytesLeft, bytesLeft));
66 fprintf(stderr, "error: write: %s\n", strerror(errno));
74 bool is_idmap_stale_fd(const char *target_apk_path, const char *overlay_apk_path, int idmap_fd)
76 static const size_t N = ResTable::IDMAP_HEADER_SIZE_BYTES;
78 if (fstat(idmap_fd, &st) == -1) {
82 // file is empty or corrupt
87 ssize_t bytesLeft = N;
88 if (lseek(idmap_fd, SEEK_SET, 0) < 0) {
92 ssize_t r = TEMP_FAILURE_RETRY(read(idmap_fd, buf + N - bytesLeft, bytesLeft));
101 // "shouldn't happen"
106 uint32_t cached_target_crc, cached_overlay_crc;
107 String8 cached_target_path, cached_overlay_path;
108 if (!ResTable::getIdmapInfo(buf, N, NULL, &cached_target_crc, &cached_overlay_crc,
109 &cached_target_path, &cached_overlay_path)) {
113 if (cached_target_path != target_apk_path) {
116 if (cached_overlay_path != overlay_apk_path) {
120 uint32_t actual_target_crc, actual_overlay_crc;
121 if (get_zip_entry_crc(target_apk_path, AssetManager::RESOURCES_FILENAME,
122 &actual_target_crc) == -1) {
125 if (get_zip_entry_crc(overlay_apk_path, AssetManager::RESOURCES_FILENAME,
126 &actual_overlay_crc) == -1) {
130 return cached_target_crc != actual_target_crc || cached_overlay_crc != actual_overlay_crc;
133 bool is_idmap_stale_path(const char *target_apk_path, const char *overlay_apk_path,
134 const char *idmap_path)
137 if (stat(idmap_path, &st) == -1) {
138 // non-existing idmap is always stale; on other errors, abort idmap generation
139 return errno == ENOENT;
142 int idmap_fd = TEMP_FAILURE_RETRY(open(idmap_path, O_RDONLY));
143 if (idmap_fd == -1) {
146 bool is_stale = is_idmap_stale_fd(target_apk_path, overlay_apk_path, idmap_fd);
151 int create_idmap(const char *target_apk_path, const char *overlay_apk_path,
152 uint32_t **data, size_t *size)
154 uint32_t target_crc, overlay_crc;
155 if (get_zip_entry_crc(target_apk_path, AssetManager::RESOURCES_FILENAME,
156 &target_crc) == -1) {
159 if (get_zip_entry_crc(overlay_apk_path, AssetManager::RESOURCES_FILENAME,
160 &overlay_crc) == -1) {
165 bool b = am.createIdmap(target_apk_path, overlay_apk_path, target_crc, overlay_crc,
170 int create_and_write_idmap(const char *target_apk_path, const char *overlay_apk_path,
171 int fd, bool check_if_stale)
173 if (check_if_stale) {
174 if (!is_idmap_stale_fd(target_apk_path, overlay_apk_path, fd)) {
175 // already up to date -- nothing to do
180 uint32_t *data = NULL;
183 if (create_idmap(target_apk_path, overlay_apk_path, &data, &size) == -1) {
187 if (write_idmap(fd, data, size) == -1) {
197 int idmap_create_path(const char *target_apk_path, const char *overlay_apk_path,
198 const char *idmap_path)
200 if (!is_idmap_stale_path(target_apk_path, overlay_apk_path, idmap_path)) {
201 // already up to date -- nothing to do
205 int fd = open_idmap(idmap_path);
210 int r = create_and_write_idmap(target_apk_path, overlay_apk_path, fd, false);
215 return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
218 int idmap_create_fd(const char *target_apk_path, const char *overlay_apk_path, int fd)
220 return create_and_write_idmap(target_apk_path, overlay_apk_path, fd, true) == 0 ?
221 EXIT_SUCCESS : EXIT_FAILURE;