Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #pragma once |
| 18 | |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 19 | #include "IncrementalServiceValidation.h" |
| 20 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 21 | #include <android-base/strings.h> |
| 22 | #include <android-base/unique_fd.h> |
| 23 | #include <android/content/pm/DataLoaderParamsParcel.h> |
| 24 | #include <android/content/pm/FileSystemControlParcel.h> |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 25 | #include <android/content/pm/IDataLoader.h> |
| 26 | #include <android/content/pm/IDataLoaderManager.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 27 | #include <android/content/pm/IDataLoaderStatusListener.h> |
| 28 | #include <android/os/IVold.h> |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 29 | #include <binder/AppOpsManager.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 30 | #include <binder/IServiceManager.h> |
| 31 | #include <incfs.h> |
| 32 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 33 | #include <memory> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 34 | #include <string> |
| 35 | #include <string_view> |
| 36 | |
| 37 | using namespace android::incfs; |
| 38 | using namespace android::content::pm; |
| 39 | |
| 40 | namespace android::os::incremental { |
| 41 | |
| 42 | // --- Wrapper interfaces --- |
| 43 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 44 | using MountId = int32_t; |
| 45 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 46 | class VoldServiceWrapper { |
| 47 | public: |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 48 | virtual ~VoldServiceWrapper() = default; |
| 49 | virtual binder::Status mountIncFs(const std::string& backingPath, const std::string& targetDir, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 50 | int32_t flags, |
| 51 | IncrementalFileSystemControlParcel* _aidl_return) const = 0; |
| 52 | virtual binder::Status unmountIncFs(const std::string& dir) const = 0; |
| 53 | virtual binder::Status bindMount(const std::string& sourceDir, |
| 54 | const std::string& targetDir) const = 0; |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 55 | virtual binder::Status setIncFsMountOptions(const ::android::os::incremental::IncrementalFileSystemControlParcel& control, bool enableReadLogs) const = 0; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 56 | }; |
| 57 | |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 58 | class DataLoaderManagerWrapper { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 59 | public: |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 60 | virtual ~DataLoaderManagerWrapper() = default; |
| 61 | virtual binder::Status initializeDataLoader(MountId mountId, |
| 62 | const DataLoaderParamsParcel& params, |
| 63 | const FileSystemControlParcel& control, |
| 64 | const sp<IDataLoaderStatusListener>& listener, |
| 65 | bool* _aidl_return) const = 0; |
| 66 | virtual binder::Status getDataLoader(MountId mountId, sp<IDataLoader>* _aidl_return) const = 0; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 67 | virtual binder::Status destroyDataLoader(MountId mountId) const = 0; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | class IncFsWrapper { |
| 71 | public: |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 72 | virtual ~IncFsWrapper() = default; |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame] | 73 | virtual Control createControl(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs) const = 0; |
| 74 | virtual ErrorCode makeFile(const Control& control, std::string_view path, int mode, FileId id, |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 75 | NewFileParams params) const = 0; |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame] | 76 | virtual ErrorCode makeDir(const Control& control, std::string_view path, int mode) const = 0; |
| 77 | virtual RawMetadata getMetadata(const Control& control, FileId fileid) const = 0; |
| 78 | virtual RawMetadata getMetadata(const Control& control, std::string_view path) const = 0; |
| 79 | virtual FileId getFileId(const Control& control, std::string_view path) const = 0; |
| 80 | virtual ErrorCode link(const Control& control, std::string_view from, |
| 81 | std::string_view to) const = 0; |
| 82 | virtual ErrorCode unlink(const Control& control, std::string_view path) const = 0; |
Yurii Zubrytskyi | e82cdd7 | 2020-04-01 12:19:26 -0700 | [diff] [blame] | 83 | virtual base::unique_fd openForSpecialOps(const Control& control, FileId id) const = 0; |
Songchun Fan | 9b75308 | 2020-02-26 13:08:06 -0800 | [diff] [blame] | 84 | virtual ErrorCode writeBlocks(Span<const DataBlock> blocks) const = 0; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 87 | class AppOpsManagerWrapper { |
| 88 | public: |
| 89 | virtual ~AppOpsManagerWrapper() = default; |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 90 | virtual binder::Status checkPermission(const char* permission, const char* operation, |
| 91 | const char* package) const = 0; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 92 | virtual void startWatchingMode(int32_t op, const String16& packageName, const sp<IAppOpsCallback>& callback) = 0; |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 93 | virtual void stopWatchingMode(const sp<IAppOpsCallback>& callback) = 0; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 96 | class ServiceManagerWrapper { |
| 97 | public: |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 98 | virtual ~ServiceManagerWrapper() = default; |
| 99 | virtual std::unique_ptr<VoldServiceWrapper> getVoldService() = 0; |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 100 | virtual std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() = 0; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 101 | virtual std::unique_ptr<IncFsWrapper> getIncFs() = 0; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 102 | virtual std::unique_ptr<AppOpsManagerWrapper> getAppOpsManager() = 0; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | // --- Real stuff --- |
| 106 | |
| 107 | class RealVoldService : public VoldServiceWrapper { |
| 108 | public: |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 109 | RealVoldService(const sp<os::IVold> vold) : mInterface(std::move(vold)) {} |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 110 | ~RealVoldService() = default; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 111 | binder::Status mountIncFs(const std::string& backingPath, const std::string& targetDir, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 112 | int32_t flags, |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 113 | IncrementalFileSystemControlParcel* _aidl_return) const final { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 114 | return mInterface->mountIncFs(backingPath, targetDir, flags, _aidl_return); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 115 | } |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 116 | binder::Status unmountIncFs(const std::string& dir) const final { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 117 | return mInterface->unmountIncFs(dir); |
| 118 | } |
| 119 | binder::Status bindMount(const std::string& sourceDir, |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 120 | const std::string& targetDir) const final { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 121 | return mInterface->bindMount(sourceDir, targetDir); |
| 122 | } |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 123 | binder::Status setIncFsMountOptions( |
| 124 | const ::android::os::incremental::IncrementalFileSystemControlParcel& control, |
| 125 | bool enableReadLogs) const final { |
Alex Buynytskyy | 5e860ba | 2020-03-31 15:30:21 -0700 | [diff] [blame] | 126 | return mInterface->setIncFsMountOptions(control, enableReadLogs); |
| 127 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 128 | |
| 129 | private: |
| 130 | sp<os::IVold> mInterface; |
| 131 | }; |
| 132 | |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 133 | class RealDataLoaderManager : public DataLoaderManagerWrapper { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 134 | public: |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 135 | RealDataLoaderManager(const sp<content::pm::IDataLoaderManager> manager) |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 136 | : mInterface(manager) {} |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 137 | ~RealDataLoaderManager() = default; |
| 138 | binder::Status initializeDataLoader(MountId mountId, const DataLoaderParamsParcel& params, |
| 139 | const FileSystemControlParcel& control, |
| 140 | const sp<IDataLoaderStatusListener>& listener, |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 141 | bool* _aidl_return) const final { |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 142 | return mInterface->initializeDataLoader(mountId, params, control, listener, _aidl_return); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 143 | } |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 144 | binder::Status getDataLoader(MountId mountId, sp<IDataLoader>* _aidl_return) const final { |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 145 | return mInterface->getDataLoader(mountId, _aidl_return); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 146 | } |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 147 | binder::Status destroyDataLoader(MountId mountId) const final { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 148 | return mInterface->destroyDataLoader(mountId); |
| 149 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 150 | |
| 151 | private: |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 152 | sp<content::pm::IDataLoaderManager> mInterface; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 153 | }; |
| 154 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 155 | class RealAppOpsManager : public AppOpsManagerWrapper { |
| 156 | public: |
| 157 | ~RealAppOpsManager() = default; |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 158 | binder::Status checkPermission(const char* permission, const char* operation, |
| 159 | const char* package) const final { |
| 160 | return android::incremental::CheckPermissionForDataDelivery(permission, operation, package); |
| 161 | } |
| 162 | void startWatchingMode(int32_t op, const String16& packageName, |
| 163 | const sp<IAppOpsCallback>& callback) final { |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 164 | mAppOpsManager.startWatchingMode(op, packageName, callback); |
| 165 | } |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 166 | void stopWatchingMode(const sp<IAppOpsCallback>& callback) final { |
| 167 | mAppOpsManager.stopWatchingMode(callback); |
| 168 | } |
| 169 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 170 | private: |
| 171 | android::AppOpsManager mAppOpsManager; |
| 172 | }; |
| 173 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 174 | class RealServiceManager : public ServiceManagerWrapper { |
| 175 | public: |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 176 | RealServiceManager(sp<IServiceManager> serviceManager); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 177 | ~RealServiceManager() = default; |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 178 | std::unique_ptr<VoldServiceWrapper> getVoldService() final; |
| 179 | std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() final; |
| 180 | std::unique_ptr<IncFsWrapper> getIncFs() final; |
| 181 | std::unique_ptr<AppOpsManagerWrapper> getAppOpsManager() final; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 182 | |
| 183 | private: |
| 184 | template <class INTERFACE> |
| 185 | sp<INTERFACE> getRealService(std::string_view serviceName) const; |
| 186 | sp<android::IServiceManager> mServiceManager; |
| 187 | }; |
| 188 | |
| 189 | class RealIncFs : public IncFsWrapper { |
| 190 | public: |
| 191 | RealIncFs() = default; |
| 192 | ~RealIncFs() = default; |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 193 | Control createControl(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs) const final { |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame] | 194 | return incfs::createControl(cmd, pendingReads, logs); |
| 195 | } |
| 196 | ErrorCode makeFile(const Control& control, std::string_view path, int mode, FileId id, |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 197 | NewFileParams params) const final { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 198 | return incfs::makeFile(control, path, mode, id, params); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 199 | } |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 200 | ErrorCode makeDir(const Control& control, std::string_view path, int mode) const final { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 201 | return incfs::makeDir(control, path, mode); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 202 | } |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 203 | RawMetadata getMetadata(const Control& control, FileId fileid) const final { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 204 | return incfs::getMetadata(control, fileid); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 205 | } |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 206 | RawMetadata getMetadata(const Control& control, std::string_view path) const final { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 207 | return incfs::getMetadata(control, path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 208 | } |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 209 | FileId getFileId(const Control& control, std::string_view path) const final { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 210 | return incfs::getFileId(control, path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 211 | } |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 212 | ErrorCode link(const Control& control, std::string_view from, std::string_view to) const final { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 213 | return incfs::link(control, from, to); |
| 214 | } |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 215 | ErrorCode unlink(const Control& control, std::string_view path) const final { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 216 | return incfs::unlink(control, path); |
| 217 | } |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 218 | base::unique_fd openForSpecialOps(const Control& control, FileId id) const final { |
Yurii Zubrytskyi | e82cdd7 | 2020-04-01 12:19:26 -0700 | [diff] [blame] | 219 | return base::unique_fd{incfs::openForSpecialOps(control, id).release()}; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 220 | } |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 221 | ErrorCode writeBlocks(Span<const DataBlock> blocks) const final { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 222 | return incfs::writeBlocks(blocks); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 223 | } |
| 224 | }; |
| 225 | |
| 226 | } // namespace android::os::incremental |