blob: d2e46e15fc59aaf4520c2914e762678514e8fc30 [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
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010059namespace android::os {
Mårten Kongstad02751232018-04-27 13:16:32 +020060
61Status Idmap2Service::getIdmapPath(const std::string& overlay_apk_path,
62 int32_t user_id ATTRIBUTE_UNUSED, std::string* _aidl_return) {
63 assert(_aidl_return);
64 *_aidl_return = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
65 return ok();
66}
67
68Status Idmap2Service::removeIdmap(const std::string& overlay_apk_path,
69 int32_t user_id ATTRIBUTE_UNUSED, bool* _aidl_return) {
70 assert(_aidl_return);
71 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
Mårten Kongstadb8779022018-11-29 09:53:17 +010072 if (unlink(idmap_path.c_str()) != 0) {
Mårten Kongstad02751232018-04-27 13:16:32 +020073 *_aidl_return = false;
74 return error("failed to unlink " + idmap_path + ": " + strerror(errno));
75 }
Mårten Kongstadb8779022018-11-29 09:53:17 +010076 *_aidl_return = true;
77 return ok();
Mårten Kongstad02751232018-04-27 13:16:32 +020078}
79
Mårten Kongstadef0695d2018-12-04 14:36:48 +010080Status Idmap2Service::verifyIdmap(const std::string& overlay_apk_path,
81 int32_t user_id ATTRIBUTE_UNUSED, bool* _aidl_return) {
82 assert(_aidl_return);
83 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
84 std::ifstream fin(idmap_path);
85 const std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(fin);
86 fin.close();
87 std::stringstream dev_null;
88 *_aidl_return = header && header->IsUpToDate(dev_null);
89 return ok();
90}
91
Mårten Kongstad02751232018-04-27 13:16:32 +020092Status Idmap2Service::createIdmap(const std::string& target_apk_path,
93 const std::string& overlay_apk_path, int32_t user_id,
94 std::unique_ptr<std::string>* _aidl_return) {
95 assert(_aidl_return);
96 std::stringstream trace;
97 trace << __FUNCTION__ << " " << target_apk_path << " " << overlay_apk_path << " "
98 << std::to_string(user_id);
99 ATRACE_NAME(trace.str().c_str());
100 std::cout << trace.str() << std::endl;
101
102 _aidl_return->reset(nullptr);
103
Mårten Kongstad02751232018-04-27 13:16:32 +0200104 const std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
105 if (!target_apk) {
106 return error("failed to load apk " + target_apk_path);
107 }
108
109 const std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
110 if (!overlay_apk) {
111 return error("failed to load apk " + overlay_apk_path);
112 }
113
114 std::stringstream err;
115 const std::unique_ptr<const Idmap> idmap =
116 Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk, err);
117 if (!idmap) {
118 return error(err.str());
119 }
120
Mårten Kongstadb8779022018-11-29 09:53:17 +0100121 umask(kIdmapFilePermissionMask);
Mårten Kongstadef0695d2018-12-04 14:36:48 +0100122 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200123 std::ofstream fout(idmap_path);
124 if (fout.fail()) {
125 return error("failed to open idmap path " + idmap_path);
126 }
127 BinaryStreamVisitor visitor(fout);
128 idmap->accept(&visitor);
129 fout.close();
130 if (fout.fail()) {
131 return error("failed to write to idmap path " + idmap_path);
132 }
133
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100134 *_aidl_return = std::make_unique<std::string>(idmap_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200135 return ok();
136}
137
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100138} // namespace android::os