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; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 63 | virtual binder::Status showHealthBlockedUI(MountId mountId) const = 0; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | class IncFsWrapper { |
| 67 | public: |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 68 | virtual ~IncFsWrapper() = default; |
| 69 | virtual ErrorCode makeFile(Control control, std::string_view path, int mode, FileId id, |
| 70 | NewFileParams params) const = 0; |
Songchun Fan | 9610093 | 2020-02-03 19:20:58 -0800 | [diff] [blame] | 71 | virtual ErrorCode makeDir(Control control, std::string_view path, int mode) const = 0; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 72 | virtual RawMetadata getMetadata(Control control, FileId fileid) const = 0; |
| 73 | virtual RawMetadata getMetadata(Control control, std::string_view path) const = 0; |
| 74 | virtual FileId getFileId(Control control, std::string_view path) const = 0; |
| 75 | virtual ErrorCode link(Control control, std::string_view from, std::string_view to) const = 0; |
| 76 | virtual ErrorCode unlink(Control control, std::string_view path) const = 0; |
| 77 | virtual base::unique_fd openWrite(Control control, FileId id) const = 0; |
Songchun Fan | 9b75308 | 2020-02-26 13:08:06 -0800 | [diff] [blame] | 78 | virtual ErrorCode writeBlocks(Span<const DataBlock> blocks) const = 0; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | class ServiceManagerWrapper { |
| 82 | public: |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 83 | virtual ~ServiceManagerWrapper() = default; |
| 84 | virtual std::unique_ptr<VoldServiceWrapper> getVoldService() = 0; |
| 85 | virtual std::unique_ptr<IncrementalManagerWrapper> getIncrementalManager() = 0; |
| 86 | virtual std::unique_ptr<IncFsWrapper> getIncFs() = 0; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | // --- Real stuff --- |
| 90 | |
| 91 | class RealVoldService : public VoldServiceWrapper { |
| 92 | public: |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 93 | RealVoldService(const sp<os::IVold> vold) : mInterface(std::move(vold)) {} |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 94 | ~RealVoldService() = default; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 95 | binder::Status mountIncFs(const std::string& backingPath, const std::string& targetDir, |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 96 | int32_t flags, |
| 97 | IncrementalFileSystemControlParcel* _aidl_return) const override { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 98 | return mInterface->mountIncFs(backingPath, targetDir, flags, _aidl_return); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 99 | } |
| 100 | binder::Status unmountIncFs(const std::string& dir) const override { |
| 101 | return mInterface->unmountIncFs(dir); |
| 102 | } |
| 103 | binder::Status bindMount(const std::string& sourceDir, |
| 104 | const std::string& targetDir) const override { |
| 105 | return mInterface->bindMount(sourceDir, targetDir); |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | sp<os::IVold> mInterface; |
| 110 | }; |
| 111 | |
| 112 | class RealIncrementalManager : public IncrementalManagerWrapper { |
| 113 | public: |
| 114 | RealIncrementalManager(const sp<os::incremental::IIncrementalManager> manager) |
| 115 | : mInterface(manager) {} |
| 116 | ~RealIncrementalManager() = default; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 117 | binder::Status prepareDataLoader(MountId mountId, const FileSystemControlParcel& control, |
| 118 | const DataLoaderParamsParcel& params, |
| 119 | const sp<IDataLoaderStatusListener>& listener, |
| 120 | bool* _aidl_return) const override { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 121 | return mInterface->prepareDataLoader(mountId, control, params, listener, _aidl_return); |
| 122 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 123 | binder::Status startDataLoader(MountId mountId, bool* _aidl_return) const override { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 124 | return mInterface->startDataLoader(mountId, _aidl_return); |
| 125 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 126 | binder::Status destroyDataLoader(MountId mountId) const override { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 127 | return mInterface->destroyDataLoader(mountId); |
| 128 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 129 | binder::Status showHealthBlockedUI(MountId mountId) const override { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 130 | return mInterface->showHealthBlockedUI(mountId); |
| 131 | } |
| 132 | |
| 133 | private: |
| 134 | sp<os::incremental::IIncrementalManager> mInterface; |
| 135 | }; |
| 136 | |
| 137 | class RealServiceManager : public ServiceManagerWrapper { |
| 138 | public: |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 139 | RealServiceManager(sp<IServiceManager> serviceManager); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 140 | ~RealServiceManager() = default; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 141 | std::unique_ptr<VoldServiceWrapper> getVoldService() override; |
| 142 | std::unique_ptr<IncrementalManagerWrapper> getIncrementalManager() override; |
| 143 | std::unique_ptr<IncFsWrapper> getIncFs() override; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 144 | |
| 145 | private: |
| 146 | template <class INTERFACE> |
| 147 | sp<INTERFACE> getRealService(std::string_view serviceName) const; |
| 148 | sp<android::IServiceManager> mServiceManager; |
| 149 | }; |
| 150 | |
| 151 | class RealIncFs : public IncFsWrapper { |
| 152 | public: |
| 153 | RealIncFs() = default; |
| 154 | ~RealIncFs() = default; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 155 | ErrorCode makeFile(Control control, std::string_view path, int mode, FileId id, |
| 156 | NewFileParams params) const override { |
| 157 | return incfs::makeFile(control, path, mode, id, params); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 158 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 159 | ErrorCode makeDir(Control control, std::string_view path, int mode) const override { |
| 160 | return incfs::makeDir(control, path, mode); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 161 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 162 | RawMetadata getMetadata(Control control, FileId fileid) const override { |
| 163 | return incfs::getMetadata(control, fileid); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 164 | } |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 165 | RawMetadata getMetadata(Control control, std::string_view path) const override { |
| 166 | return incfs::getMetadata(control, path); |
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 | FileId getFileId(Control control, std::string_view path) const override { |
| 169 | return incfs::getFileId(control, path); |
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 | ErrorCode link(Control control, std::string_view from, std::string_view to) const override { |
| 172 | return incfs::link(control, from, to); |
| 173 | } |
| 174 | ErrorCode unlink(Control control, std::string_view path) const override { |
| 175 | return incfs::unlink(control, path); |
| 176 | } |
| 177 | base::unique_fd openWrite(Control control, FileId id) const override { |
| 178 | return base::unique_fd{incfs::openWrite(control, id)}; |
| 179 | } |
Songchun Fan | 9b75308 | 2020-02-26 13:08:06 -0800 | [diff] [blame] | 180 | ErrorCode writeBlocks(Span<const DataBlock> blocks) const override { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 181 | return incfs::writeBlocks(blocks); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 182 | } |
| 183 | }; |
| 184 | |
| 185 | } // namespace android::os::incremental |