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; |
| 52 | }; |
| 53 | |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 54 | class DataLoaderManagerWrapper { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 55 | public: |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 56 | virtual ~DataLoaderManagerWrapper() = default; |
| 57 | virtual binder::Status initializeDataLoader(MountId mountId, |
| 58 | const DataLoaderParamsParcel& params, |
| 59 | const FileSystemControlParcel& control, |
| 60 | const sp<IDataLoaderStatusListener>& listener, |
| 61 | bool* _aidl_return) const = 0; |
| 62 | virtual binder::Status getDataLoader(MountId mountId, sp<IDataLoader>* _aidl_return) const = 0; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 63 | virtual binder::Status destroyDataLoader(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; |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame^] | 69 | virtual Control createControl(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs) const = 0; |
| 70 | 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] | 71 | NewFileParams params) const = 0; |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame^] | 72 | virtual ErrorCode makeDir(const Control& control, std::string_view path, int mode) const = 0; |
| 73 | virtual RawMetadata getMetadata(const Control& control, FileId fileid) const = 0; |
| 74 | virtual RawMetadata getMetadata(const Control& control, std::string_view path) const = 0; |
| 75 | virtual FileId getFileId(const Control& control, std::string_view path) const = 0; |
| 76 | virtual ErrorCode link(const Control& control, std::string_view from, |
| 77 | std::string_view to) const = 0; |
| 78 | virtual ErrorCode unlink(const Control& control, std::string_view path) const = 0; |
| 79 | virtual base::unique_fd openWrite(const Control& control, FileId id) const = 0; |
Songchun Fan | 9b75308 | 2020-02-26 13:08:06 -0800 | [diff] [blame] | 80 | virtual ErrorCode writeBlocks(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; |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 87 | virtual std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() = 0; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 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 | |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 114 | class RealDataLoaderManager : public DataLoaderManagerWrapper { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 115 | public: |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 116 | RealDataLoaderManager(const sp<content::pm::IDataLoaderManager> manager) |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 117 | : mInterface(manager) {} |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 118 | ~RealDataLoaderManager() = default; |
| 119 | binder::Status initializeDataLoader(MountId mountId, const DataLoaderParamsParcel& params, |
| 120 | const FileSystemControlParcel& control, |
| 121 | const sp<IDataLoaderStatusListener>& listener, |
| 122 | bool* _aidl_return) const override { |
| 123 | return mInterface->initializeDataLoader(mountId, params, control, listener, _aidl_return); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 124 | } |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 125 | binder::Status getDataLoader(MountId mountId, sp<IDataLoader>* _aidl_return) const override { |
| 126 | return mInterface->getDataLoader(mountId, _aidl_return); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 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 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 131 | |
| 132 | private: |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 133 | sp<content::pm::IDataLoaderManager> mInterface; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | class RealServiceManager : public ServiceManagerWrapper { |
| 137 | public: |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 138 | RealServiceManager(sp<IServiceManager> serviceManager); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 139 | ~RealServiceManager() = default; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 140 | std::unique_ptr<VoldServiceWrapper> getVoldService() override; |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 141 | std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() override; |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 142 | std::unique_ptr<IncFsWrapper> getIncFs() override; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 143 | |
| 144 | private: |
| 145 | template <class INTERFACE> |
| 146 | sp<INTERFACE> getRealService(std::string_view serviceName) const; |
| 147 | sp<android::IServiceManager> mServiceManager; |
| 148 | }; |
| 149 | |
| 150 | class RealIncFs : public IncFsWrapper { |
| 151 | public: |
| 152 | RealIncFs() = default; |
| 153 | ~RealIncFs() = default; |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame^] | 154 | Control createControl(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs) const override { |
| 155 | return incfs::createControl(cmd, pendingReads, logs); |
| 156 | } |
| 157 | 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] | 158 | NewFileParams params) const override { |
| 159 | return incfs::makeFile(control, path, mode, id, params); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 160 | } |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame^] | 161 | 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] | 162 | return incfs::makeDir(control, path, mode); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 163 | } |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame^] | 164 | RawMetadata getMetadata(const Control& control, FileId fileid) const override { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 165 | return incfs::getMetadata(control, fileid); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 166 | } |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame^] | 167 | RawMetadata getMetadata(const Control& control, std::string_view path) const override { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 168 | return incfs::getMetadata(control, path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 169 | } |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame^] | 170 | FileId getFileId(const Control& control, std::string_view path) const override { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 171 | return incfs::getFileId(control, path); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 172 | } |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame^] | 173 | ErrorCode link(const Control& control, std::string_view from, |
| 174 | std::string_view to) const override { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 175 | return incfs::link(control, from, to); |
| 176 | } |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame^] | 177 | ErrorCode unlink(const Control& control, std::string_view path) const override { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 178 | return incfs::unlink(control, path); |
| 179 | } |
Songchun Fan | 20d6ef2 | 2020-03-03 09:47:15 -0800 | [diff] [blame^] | 180 | base::unique_fd openWrite(const Control& control, FileId id) const override { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 181 | return base::unique_fd{incfs::openWrite(control, id)}; |
| 182 | } |
Songchun Fan | 9b75308 | 2020-02-26 13:08:06 -0800 | [diff] [blame] | 183 | ErrorCode writeBlocks(Span<const DataBlock> blocks) const override { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 184 | return incfs::writeBlocks(blocks); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 185 | } |
| 186 | }; |
| 187 | |
| 188 | } // namespace android::os::incremental |