Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 38 | using android::binder::Status; |
| 39 | using android::idmap2::BinaryStreamVisitor; |
| 40 | using android::idmap2::Idmap; |
| 41 | using android::idmap2::IdmapHeader; |
| 42 | |
| 43 | namespace { |
| 44 | |
| 45 | static constexpr const char* kIdmapCacheDir = "/data/resource-cache"; |
| 46 | |
| 47 | Status ok() { |
| 48 | return Status::ok(); |
| 49 | } |
| 50 | |
| 51 | Status error(const std::string& msg) { |
| 52 | LOG(ERROR) << msg; |
| 53 | return Status::fromExceptionCode(Status::EX_NONE, msg.c_str()); |
| 54 | } |
| 55 | |
| 56 | } // namespace |
| 57 | |
| 58 | namespace android { |
| 59 | namespace os { |
| 60 | |
| 61 | Status 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 | |
| 68 | Status 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 | |
Mårten Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame^] | 81 | Status 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 Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 93 | Status 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 Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 105 | 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 | |
| 122 | umask(0133); // u=rw,g=r,o=r |
Mårten Kongstad | ef0695d | 2018-12-04 14:36:48 +0100 | [diff] [blame^] | 123 | const std::string idmap_path = Idmap::CanonicalIdmapPathFor(kIdmapCacheDir, overlay_apk_path); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 124 | 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 |