blob: 197e36bd97afd21943dc2d7cd6759cbdb667a9e3 [file] [log] [blame]
Colin Cross13221c92014-02-11 18:04:44 -08001#include "idmap.h"
2
3#include <UniquePtr.h>
4#include <androidfw/ResourceTypes.h>
5#include <androidfw/StreamingZipInflater.h>
6#include <androidfw/ZipFileRO.h>
7#include <private/android_filesystem_config.h> // for AID_SYSTEM
8#include <utils/SortedVector.h>
9#include <utils/String16.h>
10#include <utils/String8.h>
11
12#include <dirent.h>
13
14#define NO_OVERLAY_TAG (-1000)
15
16using namespace android;
17
18namespace {
19 struct Overlay {
20 Overlay() {}
21 Overlay(const String8& a, const String8& i, int p) :
22 apk_path(a), idmap_path(i), priority(p) {}
23
24 bool operator<(Overlay const& rhs) const
25 {
26 // Note: order is reversed by design
27 return rhs.priority < priority;
28 }
29
30 String8 apk_path;
31 String8 idmap_path;
32 int priority;
33 };
34
35 bool writePackagesList(const char *filename, const SortedVector<Overlay>& overlayVector)
36 {
37 FILE* fout = fopen(filename, "w");
38 if (fout == NULL) {
39 return false;
40 }
41
42 for (size_t i = 0; i < overlayVector.size(); ++i) {
43 const Overlay& overlay = overlayVector[i];
44 fprintf(fout, "%s %s\n", overlay.apk_path.string(), overlay.idmap_path.string());
45 }
46
47 fclose(fout);
48
49 // Make file world readable since Zygote (running as root) will read
50 // it when creating the initial AssetManger object
51 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // 0644
52 if (chmod(filename, mode) == -1) {
53 unlink(filename);
54 return false;
55 }
56
57 return true;
58 }
59
60 String8 flatten_path(const char *path)
61 {
62 String16 tmp(path);
63 tmp.replaceAll('/', '@');
64 return String8(tmp);
65 }
66
Colin Cross13221c92014-02-11 18:04:44 -080067 int parse_overlay_tag(const ResXMLTree& parser, const char *target_package_name)
68 {
69 const size_t N = parser.getAttributeCount();
70 String16 target;
71 int priority = -1;
72 for (size_t i = 0; i < N; ++i) {
73 size_t len;
74 String16 key(parser.getAttributeName(i, &len));
75 if (key == String16("targetPackage")) {
Dan Albert66987492014-11-20 11:41:21 -080076 const char16_t *p = parser.getAttributeStringValue(i, &len);
Andreas Gampecfedceb2014-09-30 21:48:18 -070077 if (p != NULL) {
Colin Cross13221c92014-02-11 18:04:44 -080078 target = String16(p, len);
79 }
80 } else if (key == String16("priority")) {
81 Res_value v;
82 if (parser.getAttributeValue(i, &v) == sizeof(Res_value)) {
83 priority = v.data;
84 if (priority < 0 || priority > 9999) {
85 return -1;
86 }
87 }
88 }
89 }
90 if (target == String16(target_package_name)) {
91 return priority;
92 }
93 return NO_OVERLAY_TAG;
94 }
95
96 int parse_manifest(const void *data, size_t size, const char *target_package_name)
97 {
Adam Lesinskide898ff2014-01-29 18:20:45 -080098 ResXMLTree parser;
99 parser.setTo(data, size);
Colin Cross13221c92014-02-11 18:04:44 -0800100 if (parser.getError() != NO_ERROR) {
101 ALOGD("%s failed to init xml parser, error=0x%08x\n", __FUNCTION__, parser.getError());
102 return -1;
103 }
104
105 ResXMLParser::event_code_t type;
106 do {
107 type = parser.next();
108 if (type == ResXMLParser::START_TAG) {
109 size_t len;
110 String16 tag(parser.getElementName(&len));
111 if (tag == String16("overlay")) {
112 return parse_overlay_tag(parser, target_package_name);
113 }
114 }
115 } while (type != ResXMLParser::BAD_DOCUMENT && type != ResXMLParser::END_DOCUMENT);
116
117 return NO_OVERLAY_TAG;
118 }
119
120 int parse_apk(const char *path, const char *target_package_name)
121 {
122 UniquePtr<ZipFileRO> zip(ZipFileRO::open(path));
123 if (zip.get() == NULL) {
124 ALOGW("%s: failed to open zip %s\n", __FUNCTION__, path);
125 return -1;
126 }
127 ZipEntryRO entry;
128 if ((entry = zip->findEntryByName("AndroidManifest.xml")) == NULL) {
129 ALOGW("%s: failed to find entry AndroidManifest.xml\n", __FUNCTION__);
130 return -1;
131 }
132 size_t uncompLen = 0;
133 int method;
134 if (!zip->getEntryInfo(entry, &method, &uncompLen, NULL, NULL, NULL, NULL)) {
135 ALOGW("%s: failed to read entry info\n", __FUNCTION__);
136 return -1;
137 }
138 if (method != ZipFileRO::kCompressDeflated) {
139 ALOGW("%s: cannot handle zip compression method %d\n", __FUNCTION__, method);
140 return -1;
141 }
142 FileMap *dataMap = zip->createEntryFileMap(entry);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700143 if (dataMap == NULL) {
Colin Cross13221c92014-02-11 18:04:44 -0800144 ALOGW("%s: failed to create FileMap\n", __FUNCTION__);
145 return -1;
146 }
147 char *buf = new char[uncompLen];
148 if (NULL == buf) {
Bernhard Rosenkränzer46c82b42014-11-30 11:04:10 +0100149 ALOGW("%s: failed to allocate %zd byte\n", __FUNCTION__, uncompLen);
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000150 delete dataMap;
Colin Cross13221c92014-02-11 18:04:44 -0800151 return -1;
152 }
153 StreamingZipInflater inflater(dataMap, uncompLen);
154 if (inflater.read(buf, uncompLen) < 0) {
Bernhard Rosenkränzer46c82b42014-11-30 11:04:10 +0100155 ALOGW("%s: failed to inflate %zd byte\n", __FUNCTION__, uncompLen);
Colin Cross13221c92014-02-11 18:04:44 -0800156 delete[] buf;
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000157 delete dataMap;
Colin Cross13221c92014-02-11 18:04:44 -0800158 return -1;
159 }
160
161 int priority = parse_manifest(buf, uncompLen, target_package_name);
162 delete[] buf;
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000163 delete dataMap;
Colin Cross13221c92014-02-11 18:04:44 -0800164 return priority;
165 }
166}
167
168int idmap_scan(const char *overlay_dir, const char *target_package_name,
169 const char *target_apk_path, const char *idmap_dir)
170{
171 String8 filename = String8(idmap_dir);
172 filename.appendPath("overlays.list");
173 if (unlink(filename.string()) != 0 && errno != ENOENT) {
174 return EXIT_FAILURE;
175 }
176
177 DIR *dir = opendir(overlay_dir);
178 if (dir == NULL) {
179 return EXIT_FAILURE;
180 }
181
182 SortedVector<Overlay> overlayVector;
183 struct dirent *dirent;
184 while ((dirent = readdir(dir)) != NULL) {
185 struct stat st;
186 char overlay_apk_path[PATH_MAX + 1];
187 snprintf(overlay_apk_path, PATH_MAX, "%s/%s", overlay_dir, dirent->d_name);
188 if (stat(overlay_apk_path, &st) < 0) {
189 continue;
190 }
191 if (!S_ISREG(st.st_mode)) {
192 continue;
193 }
194
195 int priority = parse_apk(overlay_apk_path, target_package_name);
196 if (priority < 0) {
197 continue;
198 }
199
200 String8 idmap_path(idmap_dir);
201 idmap_path.appendPath(flatten_path(overlay_apk_path + 1));
202 idmap_path.append("@idmap");
203
204 if (idmap_create_path(target_apk_path, overlay_apk_path, idmap_path.string()) != 0) {
205 ALOGE("error: failed to create idmap for target=%s overlay=%s idmap=%s\n",
206 target_apk_path, overlay_apk_path, idmap_path.string());
207 continue;
208 }
209
210 Overlay overlay(String8(overlay_apk_path), idmap_path, priority);
211 overlayVector.add(overlay);
212 }
213
214 closedir(dir);
215
216 if (!writePackagesList(filename.string(), overlayVector)) {
217 return EXIT_FAILURE;
218 }
219
220 return EXIT_SUCCESS;
221}