blob: cf72cb94da2c4eb38763e7743e07e63517901544 [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;
42
43namespace {
44
45static constexpr const char* kIdmapCacheDir = "/data/resource-cache";
46
47Status ok() {
48 return Status::ok();
49}
50
51Status error(const std::string& msg) {
52 LOG(ERROR) << msg;
53 return Status::fromExceptionCode(Status::EX_NONE, msg.c_str());
54}
55
56} // namespace
57
58namespace android {
59namespace os {
60
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);
72 if (unlink(idmap_path.c_str()) == 0) {
73 *_aidl_return = true;
74 return ok();
75 } else {
76 *_aidl_return = false;
77 return error("failed to unlink " + idmap_path + ": " + strerror(errno));
78 }
79}
80
81Status Idmap2Service::createIdmap(const std::string& target_apk_path,
82 const std::string& overlay_apk_path, int32_t user_id,
83 std::unique_ptr<std::string>* _aidl_return) {
84 assert(_aidl_return);
85 std::stringstream trace;
86 trace << __FUNCTION__ << " " << target_apk_path << " " << overlay_apk_path << " "
87 << std::to_string(user_id);
88 ATRACE_NAME(trace.str().c_str());
89 std::cout << trace.str() << std::endl;
90
91 _aidl_return->reset(nullptr);
92
93 const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path);
94 std::ifstream fin(idmap_path);
95 const std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(fin);
96 fin.close();
97 // do not reuse error stream from IsUpToDate below, or error messages will be
98 // polluted with irrelevant data
99 std::stringstream dev_null;
100 if (header && header->IsUpToDate(dev_null)) {
101 return ok();
102 }
103
104 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
121 umask(0133); // u=rw,g=r,o=r
122 std::ofstream fout(idmap_path);
123 if (fout.fail()) {
124 return error("failed to open idmap path " + idmap_path);
125 }
126 BinaryStreamVisitor visitor(fout);
127 idmap->accept(&visitor);
128 fout.close();
129 if (fout.fail()) {
130 return error("failed to write to idmap path " + idmap_path);
131 }
132
133 _aidl_return->reset(new std::string(idmap_path));
134 return ok();
135}
136
137} // namespace os
138} // namespace android