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