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