blob: 7b16093434fba476e087989cdb68e3f8f490aa4b [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <sys/stat.h> // umask
18#include <sys/types.h> // umask
19#include <unistd.h>
20
21#include <cerrno>
22#include <cstring>
23#include <fstream>
24#include <memory>
25#include <ostream>
26#include <string>
27
28#include "android-base/macros.h"
29#include "utils/String8.h"
30#include "utils/Trace.h"
31
32#include "idmap2/BinaryStreamVisitor.h"
33#include "idmap2/FileUtils.h"
34#include "idmap2/Idmap.h"
35
36#include "idmap2d/Idmap2Service.h"
37
38using android::binder::Status;
39using android::idmap2::BinaryStreamVisitor;
40using android::idmap2::Idmap;
41using android::idmap2::IdmapHeader;
Mårten Kongstadb8779022018-11-29 09:53:17 +010042using android::idmap2::utils::kIdmapFilePermissionMask;
Mårten Kongstad02751232018-04-27 13:16:32 +020043
44namespace {
45
Mårten Kongstadb8779022018-11-29 09:53:17 +010046constexpr const char* kIdmapCacheDir = "/data/resource-cache";
Mårten Kongstad02751232018-04-27 13:16:32 +020047
48Status ok() {
49 return Status::ok();
50}
51
52Status error(const std::string& msg) {
53 LOG(ERROR) << msg;
54 return Status::fromExceptionCode(Status::EX_NONE, msg.c_str());
55}
56
57} // namespace
58
59namespace android {
60namespace os {
61
62Status Idmap2Service::getIdmapPath(const std::string& overlay_apk_path,
63 int32_t user_id ATTRIBUTE_UNUSED, std::string* _aidl_return) {
64 assert(_aidl_return);
65 *_aidl_return = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
66 return ok();
67}
68
69Status Idmap2Service::removeIdmap(const std::string& overlay_apk_path,
70 int32_t user_id ATTRIBUTE_UNUSED, bool* _aidl_return) {
71 assert(_aidl_return);
72 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
Mårten Kongstadb8779022018-11-29 09:53:17 +010073 if (unlink(idmap_path.c_str()) != 0) {
Mårten Kongstad02751232018-04-27 13:16:32 +020074 *_aidl_return = false;
75 return error("failed to unlink " + idmap_path + ": " + strerror(errno));
76 }
Mårten Kongstadb8779022018-11-29 09:53:17 +010077 *_aidl_return = true;
78 return ok();
Mårten Kongstad02751232018-04-27 13:16:32 +020079}
80
Mårten Kongstadef0695d2018-12-04 14:36:48 +010081Status Idmap2Service::verifyIdmap(const std::string& overlay_apk_path,
82 int32_t user_id ATTRIBUTE_UNUSED, bool* _aidl_return) {
83 assert(_aidl_return);
84 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
85 std::ifstream fin(idmap_path);
86 const std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(fin);
87 fin.close();
88 std::stringstream dev_null;
89 *_aidl_return = header && header->IsUpToDate(dev_null);
90 return ok();
91}
92
Mårten Kongstad02751232018-04-27 13:16:32 +020093Status Idmap2Service::createIdmap(const std::string& target_apk_path,
94 const std::string& overlay_apk_path, int32_t user_id,
95 std::unique_ptr<std::string>* _aidl_return) {
96 assert(_aidl_return);
97 std::stringstream trace;
98 trace << __FUNCTION__ << " " << target_apk_path << " " << overlay_apk_path << " "
99 << std::to_string(user_id);
100 ATRACE_NAME(trace.str().c_str());
101 std::cout << trace.str() << std::endl;
102
103 _aidl_return->reset(nullptr);
104
Mårten Kongstad02751232018-04-27 13:16:32 +0200105 const std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
106 if (!target_apk) {
107 return error("failed to load apk " + target_apk_path);
108 }
109
110 const std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
111 if (!overlay_apk) {
112 return error("failed to load apk " + overlay_apk_path);
113 }
114
115 std::stringstream err;
116 const std::unique_ptr<const Idmap> idmap =
117 Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk, err);
118 if (!idmap) {
119 return error(err.str());
120 }
121
Mårten Kongstadb8779022018-11-29 09:53:17 +0100122 umask(kIdmapFilePermissionMask);
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100123 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200124 std::ofstream fout(idmap_path);
125 if (fout.fail()) {
126 return error("failed to open idmap path " + idmap_path);
127 }
128 BinaryStreamVisitor visitor(fout);
129 idmap->accept(&visitor);
130 fout.close();
131 if (fout.fail()) {
132 return error("failed to write to idmap path " + idmap_path);
133 }
134
135 _aidl_return->reset(new std::string(idmap_path));
136 return ok();
137}
138
139} // namespace os
140} // namespace android