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