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 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 17 | #define LOG_TAG "IncrementalService" |
| 18 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 19 | #include "ServiceWrappers.h" |
| 20 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 21 | #include <MountRegistry.h> |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 22 | #include <android-base/logging.h> |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 23 | #include <android/content/pm/IDataLoaderManager.h> |
| 24 | #include <android/os/IVold.h> |
| 25 | #include <binder/AppOpsManager.h> |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 26 | #include <utils/String16.h> |
| 27 | |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 28 | #include <filesystem> |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 29 | #include <thread> |
| 30 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 31 | #include "IncrementalServiceValidation.h" |
| 32 | |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 33 | using namespace std::literals; |
| 34 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 35 | namespace android::incremental { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 36 | |
| 37 | static constexpr auto kVoldServiceName = "vold"sv; |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 38 | static constexpr auto kDataLoaderManagerName = "dataloader_manager"sv; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 39 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 40 | class RealVoldService : public VoldServiceWrapper { |
| 41 | public: |
Yurii Zubrytskyi | 510037b | 2020-04-22 15:46:21 -0700 | [diff] [blame] | 42 | RealVoldService(sp<os::IVold> vold) : mInterface(std::move(vold)) {} |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 43 | ~RealVoldService() = default; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 44 | binder::Status mountIncFs( |
| 45 | const std::string& backingPath, const std::string& targetDir, int32_t flags, |
| 46 | os::incremental::IncrementalFileSystemControlParcel* _aidl_return) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 47 | return mInterface->mountIncFs(backingPath, targetDir, flags, _aidl_return); |
| 48 | } |
| 49 | binder::Status unmountIncFs(const std::string& dir) const final { |
| 50 | return mInterface->unmountIncFs(dir); |
| 51 | } |
| 52 | binder::Status bindMount(const std::string& sourceDir, |
| 53 | const std::string& targetDir) const final { |
| 54 | return mInterface->bindMount(sourceDir, targetDir); |
| 55 | } |
| 56 | binder::Status setIncFsMountOptions( |
| 57 | const ::android::os::incremental::IncrementalFileSystemControlParcel& control, |
| 58 | bool enableReadLogs) const final { |
| 59 | return mInterface->setIncFsMountOptions(control, enableReadLogs); |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | sp<os::IVold> mInterface; |
| 64 | }; |
| 65 | |
| 66 | class RealDataLoaderManager : public DataLoaderManagerWrapper { |
| 67 | public: |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 68 | RealDataLoaderManager(sp<content::pm::IDataLoaderManager> manager) |
| 69 | : mInterface(std::move(manager)) {} |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 70 | ~RealDataLoaderManager() = default; |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 71 | binder::Status bindToDataLoader(MountId mountId, |
| 72 | const content::pm::DataLoaderParamsParcel& params, |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 73 | int bindDelayMs, |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 74 | const sp<content::pm::IDataLoaderStatusListener>& listener, |
| 75 | bool* _aidl_return) const final { |
Alex Buynytskyy | b19ee3e | 2021-02-06 20:31:43 -0800 | [diff] [blame] | 76 | return mInterface->bindToDataLoader(mountId, params, bindDelayMs, listener, _aidl_return); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 77 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 78 | binder::Status getDataLoader(MountId mountId, |
| 79 | sp<content::pm::IDataLoader>* _aidl_return) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 80 | return mInterface->getDataLoader(mountId, _aidl_return); |
| 81 | } |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 82 | binder::Status unbindFromDataLoader(MountId mountId) const final { |
| 83 | return mInterface->unbindFromDataLoader(mountId); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | private: |
| 87 | sp<content::pm::IDataLoaderManager> mInterface; |
| 88 | }; |
| 89 | |
| 90 | class RealAppOpsManager : public AppOpsManagerWrapper { |
| 91 | public: |
| 92 | ~RealAppOpsManager() = default; |
| 93 | binder::Status checkPermission(const char* permission, const char* operation, |
| 94 | const char* package) const final { |
| 95 | return android::incremental::CheckPermissionForDataDelivery(permission, operation, package); |
| 96 | } |
| 97 | void startWatchingMode(int32_t op, const String16& packageName, |
| 98 | const sp<IAppOpsCallback>& callback) final { |
| 99 | mAppOpsManager.startWatchingMode(op, packageName, callback); |
| 100 | } |
| 101 | void stopWatchingMode(const sp<IAppOpsCallback>& callback) final { |
| 102 | mAppOpsManager.stopWatchingMode(callback); |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | android::AppOpsManager mAppOpsManager; |
| 107 | }; |
| 108 | |
| 109 | class RealJniWrapper final : public JniWrapper { |
| 110 | public: |
| 111 | RealJniWrapper(JavaVM* jvm); |
| 112 | void initializeForCurrentThread() const final; |
| 113 | |
| 114 | static JavaVM* getJvm(JNIEnv* env); |
| 115 | |
| 116 | private: |
| 117 | JavaVM* const mJvm; |
| 118 | }; |
| 119 | |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 120 | class RealLooperWrapper final : public LooperWrapper { |
| 121 | public: |
| 122 | int addFd(int fd, int ident, int events, android::Looper_callbackFunc callback, |
| 123 | void* data) final { |
| 124 | return mLooper.addFd(fd, ident, events, callback, data); |
| 125 | } |
| 126 | int removeFd(int fd) final { return mLooper.removeFd(fd); } |
| 127 | void wake() final { return mLooper.wake(); } |
| 128 | int pollAll(int timeoutMillis) final { return mLooper.pollAll(timeoutMillis); } |
| 129 | |
| 130 | private: |
| 131 | struct Looper : public android::Looper { |
| 132 | Looper() : android::Looper(/*allowNonCallbacks=*/false) {} |
| 133 | ~Looper() {} |
| 134 | } mLooper; |
| 135 | }; |
| 136 | |
Yurii Zubrytskyi | 4375a74 | 2021-03-18 16:59:47 -0700 | [diff] [blame] | 137 | std::string IncFsWrapper::toString(FileId fileId) { |
| 138 | return incfs::toString(fileId); |
| 139 | } |
| 140 | |
Yurii Zubrytskyi | a5946f7 | 2021-02-17 14:24:14 -0800 | [diff] [blame] | 141 | class RealIncFs final : public IncFsWrapper { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 142 | public: |
| 143 | RealIncFs() = default; |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 144 | ~RealIncFs() final = default; |
Yurii Zubrytskyi | a5946f7 | 2021-02-17 14:24:14 -0800 | [diff] [blame] | 145 | Features features() const final { return incfs::features(); } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 146 | void listExistingMounts(const ExistingMountCallback& cb) const final { |
| 147 | for (auto mount : incfs::defaultMountRegistry().copyMounts()) { |
| 148 | auto binds = mount.binds(); // span() doesn't like rvalue containers, needs to save it. |
| 149 | cb(mount.root(), mount.backingDir(), binds); |
| 150 | } |
| 151 | } |
| 152 | Control openMount(std::string_view path) const final { return incfs::open(path); } |
Yurii Zubrytskyi | 5f69292 | 2020-12-08 07:35:24 -0800 | [diff] [blame] | 153 | Control createControl(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs, |
| 154 | IncFsFd blocksWritten) const final { |
| 155 | return incfs::createControl(cmd, pendingReads, logs, blocksWritten); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 156 | } |
| 157 | ErrorCode makeFile(const Control& control, std::string_view path, int mode, FileId id, |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 158 | incfs::NewFileParams params) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 159 | return incfs::makeFile(control, path, mode, id, params); |
| 160 | } |
Yurii Zubrytskyi | a5946f7 | 2021-02-17 14:24:14 -0800 | [diff] [blame] | 161 | ErrorCode makeMappedFile(const Control& control, std::string_view path, int mode, |
| 162 | incfs::NewMappedFileParams params) const final { |
| 163 | return incfs::makeMappedFile(control, path, mode, params); |
| 164 | } |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 165 | ErrorCode makeDir(const Control& control, std::string_view path, int mode) const final { |
| 166 | return incfs::makeDir(control, path, mode); |
| 167 | } |
Yurii Zubrytskyi | efebb45 | 2020-04-22 13:59:06 -0700 | [diff] [blame] | 168 | ErrorCode makeDirs(const Control& control, std::string_view path, int mode) const final { |
| 169 | return incfs::makeDirs(control, path, mode); |
| 170 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 171 | incfs::RawMetadata getMetadata(const Control& control, FileId fileid) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 172 | return incfs::getMetadata(control, fileid); |
| 173 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 174 | incfs::RawMetadata getMetadata(const Control& control, std::string_view path) const final { |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 175 | return incfs::getMetadata(control, path); |
| 176 | } |
| 177 | FileId getFileId(const Control& control, std::string_view path) const final { |
| 178 | return incfs::getFileId(control, path); |
| 179 | } |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 180 | std::pair<IncFsBlockIndex, IncFsBlockIndex> countFilledBlocks( |
| 181 | const Control& control, std::string_view path) const final { |
Yurii Zubrytskyi | 4375a74 | 2021-03-18 16:59:47 -0700 | [diff] [blame] | 182 | if (incfs::features() & Features::v2) { |
| 183 | const auto counts = incfs::getBlockCount(control, path); |
| 184 | if (!counts) { |
| 185 | return {-errno, -errno}; |
| 186 | } |
| 187 | return {counts->filledDataBlocks + counts->filledHashBlocks, |
| 188 | counts->totalDataBlocks + counts->totalHashBlocks}; |
| 189 | } |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 190 | const auto fileId = incfs::getFileId(control, path); |
| 191 | const auto fd = incfs::openForSpecialOps(control, fileId); |
| 192 | int res = fd.get(); |
| 193 | if (!fd.ok()) { |
| 194 | return {res, res}; |
| 195 | } |
| 196 | const auto ranges = incfs::getFilledRanges(res); |
| 197 | res = ranges.first; |
| 198 | if (res) { |
| 199 | return {res, res}; |
| 200 | } |
| 201 | const auto totalBlocksCount = ranges.second.internalRawRanges().endIndex; |
| 202 | int filledBlockCount = 0; |
| 203 | for (const auto& dataRange : ranges.second.dataRanges()) { |
| 204 | filledBlockCount += dataRange.size(); |
| 205 | } |
| 206 | for (const auto& hashRange : ranges.second.hashRanges()) { |
| 207 | filledBlockCount += hashRange.size(); |
| 208 | } |
| 209 | return {filledBlockCount, totalBlocksCount}; |
| 210 | } |
Yurii Zubrytskyi | 256a1a4 | 2021-03-18 14:21:54 -0700 | [diff] [blame] | 211 | incfs::LoadingState isFileFullyLoaded(const Control& control, |
| 212 | std::string_view path) const final { |
| 213 | return incfs::isFullyLoaded(control, path); |
| 214 | } |
| 215 | incfs::LoadingState isEverythingFullyLoaded(const Control& control) const final { |
| 216 | return incfs::isEverythingFullyLoaded(control); |
| 217 | } |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 218 | ErrorCode link(const Control& control, std::string_view from, std::string_view to) const final { |
| 219 | return incfs::link(control, from, to); |
| 220 | } |
| 221 | ErrorCode unlink(const Control& control, std::string_view path) const final { |
| 222 | return incfs::unlink(control, path); |
| 223 | } |
Alex Buynytskyy | bc0a7e6 | 2020-08-25 12:45:22 -0700 | [diff] [blame] | 224 | incfs::UniqueFd openForSpecialOps(const Control& control, FileId id) const final { |
| 225 | return incfs::openForSpecialOps(control, id); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 226 | } |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 227 | ErrorCode writeBlocks(std::span<const incfs::DataBlock> blocks) const final { |
| 228 | return incfs::writeBlocks({blocks.data(), size_t(blocks.size())}); |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 229 | } |
Yurii Zubrytskyi | 65fc38a | 2021-03-17 13:18:30 -0700 | [diff] [blame] | 230 | ErrorCode reserveSpace(const Control& control, std::string_view path, |
| 231 | IncFsSize size) const final { |
| 232 | return incfs::reserveSpace(control, path, size); |
| 233 | } |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 234 | WaitResult waitForPendingReads(const Control& control, std::chrono::milliseconds timeout, |
| 235 | std::vector<incfs::ReadInfo>* pendingReadsBuffer) const final { |
| 236 | return incfs::waitForPendingReads(control, timeout, pendingReadsBuffer); |
| 237 | } |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 238 | ErrorCode setUidReadTimeouts(const Control& control, |
| 239 | const std::vector<android::os::incremental::PerUidReadTimeouts>& |
| 240 | perUidReadTimeouts) const final { |
Alex Buynytskyy | fe6b4c0 | 2021-01-26 13:29:24 -0800 | [diff] [blame] | 241 | std::vector<incfs::UidReadTimeouts> timeouts; |
| 242 | timeouts.resize(perUidReadTimeouts.size()); |
| 243 | for (int i = 0, size = perUidReadTimeouts.size(); i < size; ++i) { |
| 244 | auto&& timeout = timeouts[i]; |
| 245 | const auto& perUidTimeout = perUidReadTimeouts[i]; |
| 246 | timeout.uid = perUidTimeout.uid; |
| 247 | timeout.minTimeUs = perUidTimeout.minTimeUs; |
| 248 | timeout.minPendingTimeUs = perUidTimeout.minPendingTimeUs; |
| 249 | timeout.maxPendingTimeUs = perUidTimeout.maxPendingTimeUs; |
| 250 | } |
Alex Buynytskyy | 07694ed | 2021-01-27 06:58:55 -0800 | [diff] [blame] | 251 | |
Alex Buynytskyy | fe6b4c0 | 2021-01-26 13:29:24 -0800 | [diff] [blame] | 252 | return incfs::setUidReadTimeouts(control, timeouts); |
Alex Buynytskyy | aa8e95e | 2020-12-14 21:50:04 -0800 | [diff] [blame] | 253 | } |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 254 | }; |
| 255 | |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 256 | static JNIEnv* getOrAttachJniEnv(JavaVM* jvm); |
| 257 | |
Yurii Zubrytskyi | 0583e7f | 2021-03-19 17:00:12 -0700 | [diff] [blame^] | 258 | class RealTimedQueueWrapper final : public TimedQueueWrapper { |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 259 | public: |
| 260 | RealTimedQueueWrapper(JavaVM* jvm) { |
| 261 | mThread = std::thread([this, jvm]() { |
| 262 | (void)getOrAttachJniEnv(jvm); |
| 263 | runTimers(); |
| 264 | }); |
| 265 | } |
| 266 | ~RealTimedQueueWrapper() final { |
| 267 | CHECK(!mRunning) << "call stop first"; |
| 268 | CHECK(!mThread.joinable()) << "call stop first"; |
| 269 | } |
| 270 | |
Yurii Zubrytskyi | 0583e7f | 2021-03-19 17:00:12 -0700 | [diff] [blame^] | 271 | void addJob(MountId id, Milliseconds timeout, Job what) final { |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 272 | const auto now = Clock::now(); |
| 273 | { |
| 274 | std::unique_lock lock(mMutex); |
Yurii Zubrytskyi | 0583e7f | 2021-03-19 17:00:12 -0700 | [diff] [blame^] | 275 | mJobs.insert(TimedJob{id, now + timeout, std::move(what)}); |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 276 | } |
| 277 | mCondition.notify_all(); |
| 278 | } |
| 279 | void removeJobs(MountId id) final { |
| 280 | std::unique_lock lock(mMutex); |
| 281 | std::erase_if(mJobs, [id](auto&& item) { return item.id == id; }); |
| 282 | } |
| 283 | void stop() final { |
| 284 | { |
| 285 | std::unique_lock lock(mMutex); |
| 286 | mRunning = false; |
| 287 | } |
| 288 | mCondition.notify_all(); |
| 289 | mThread.join(); |
| 290 | mJobs.clear(); |
| 291 | } |
| 292 | |
| 293 | private: |
| 294 | void runTimers() { |
| 295 | static constexpr TimePoint kInfinityTs{Clock::duration::max()}; |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 296 | std::unique_lock lock(mMutex); |
| 297 | for (;;) { |
Yurii Zubrytskyi | 0583e7f | 2021-03-19 17:00:12 -0700 | [diff] [blame^] | 298 | const TimePoint nextJobTs = mJobs.empty() ? kInfinityTs : mJobs.begin()->when; |
| 299 | mCondition.wait_until(lock, nextJobTs, [this, oldNextJobTs = nextJobTs]() { |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 300 | const auto now = Clock::now(); |
Yurii Zubrytskyi | 0583e7f | 2021-03-19 17:00:12 -0700 | [diff] [blame^] | 301 | const auto newFirstJobTs = !mJobs.empty() ? mJobs.begin()->when : kInfinityTs; |
| 302 | return newFirstJobTs <= now || newFirstJobTs < oldNextJobTs || !mRunning; |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 303 | }); |
| 304 | if (!mRunning) { |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | const auto now = Clock::now(); |
Yurii Zubrytskyi | 0583e7f | 2021-03-19 17:00:12 -0700 | [diff] [blame^] | 309 | // Always re-acquire begin(). We can't use it after unlock as mTimedJobs can change. |
| 310 | for (auto it = mJobs.begin(); it != mJobs.end() && it->when <= now; |
| 311 | it = mJobs.begin()) { |
Yurii Zubrytskyi | 3fde572 | 2021-02-19 00:08:36 -0800 | [diff] [blame] | 312 | auto jobNode = mJobs.extract(it); |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 313 | |
| 314 | lock.unlock(); |
Yurii Zubrytskyi | 3fde572 | 2021-02-19 00:08:36 -0800 | [diff] [blame] | 315 | jobNode.value().what(); |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 316 | lock.lock(); |
| 317 | } |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | struct TimedJob { |
| 322 | MountId id; |
| 323 | TimePoint when; |
| 324 | Job what; |
| 325 | friend bool operator<(const TimedJob& lhs, const TimedJob& rhs) { |
| 326 | return lhs.when < rhs.when; |
| 327 | } |
| 328 | }; |
| 329 | bool mRunning = true; |
Yurii Zubrytskyi | 0583e7f | 2021-03-19 17:00:12 -0700 | [diff] [blame^] | 330 | std::multiset<TimedJob> mJobs; |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 331 | std::condition_variable mCondition; |
| 332 | std::mutex mMutex; |
| 333 | std::thread mThread; |
| 334 | }; |
| 335 | |
Yurii Zubrytskyi | 3fde572 | 2021-02-19 00:08:36 -0800 | [diff] [blame] | 336 | class RealFsWrapper final : public FsWrapper { |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 337 | public: |
| 338 | RealFsWrapper() = default; |
| 339 | ~RealFsWrapper() = default; |
| 340 | |
Yurii Zubrytskyi | 3fde572 | 2021-02-19 00:08:36 -0800 | [diff] [blame] | 341 | void listFilesRecursive(std::string_view directoryPath, FileCallback onFile) const final { |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 342 | for (const auto& entry : std::filesystem::recursive_directory_iterator(directoryPath)) { |
| 343 | if (!entry.is_regular_file()) { |
| 344 | continue; |
| 345 | } |
Yurii Zubrytskyi | 3fde572 | 2021-02-19 00:08:36 -0800 | [diff] [blame] | 346 | if (!onFile(entry.path().native())) { |
| 347 | break; |
| 348 | } |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 349 | } |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 350 | } |
| 351 | }; |
| 352 | |
Alex Buynytskyy | 7e06d71 | 2021-03-09 19:24:23 -0800 | [diff] [blame] | 353 | class RealClockWrapper final : public ClockWrapper { |
| 354 | public: |
| 355 | RealClockWrapper() = default; |
| 356 | ~RealClockWrapper() = default; |
| 357 | |
| 358 | TimePoint now() const final { return Clock::now(); } |
| 359 | }; |
| 360 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 361 | RealServiceManager::RealServiceManager(sp<IServiceManager> serviceManager, JNIEnv* env) |
| 362 | : mServiceManager(std::move(serviceManager)), mJvm(RealJniWrapper::getJvm(env)) {} |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 363 | |
| 364 | template <class INTERFACE> |
| 365 | sp<INTERFACE> RealServiceManager::getRealService(std::string_view serviceName) const { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 366 | sp<IBinder> binder = |
| 367 | mServiceManager->getService(String16(serviceName.data(), serviceName.size())); |
| 368 | if (!binder) { |
| 369 | return nullptr; |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 370 | } |
| 371 | return interface_cast<INTERFACE>(binder); |
| 372 | } |
| 373 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 374 | std::unique_ptr<VoldServiceWrapper> RealServiceManager::getVoldService() { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 375 | sp<os::IVold> vold = RealServiceManager::getRealService<os::IVold>(kVoldServiceName); |
| 376 | if (vold != 0) { |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 377 | return std::make_unique<RealVoldService>(vold); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 378 | } |
| 379 | return nullptr; |
| 380 | } |
| 381 | |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 382 | std::unique_ptr<DataLoaderManagerWrapper> RealServiceManager::getDataLoaderManager() { |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 383 | sp<content::pm::IDataLoaderManager> manager = |
| 384 | RealServiceManager::getRealService<content::pm::IDataLoaderManager>( |
| 385 | kDataLoaderManagerName); |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 386 | if (manager) { |
Songchun Fan | 68645c4 | 2020-02-27 15:57:35 -0800 | [diff] [blame] | 387 | return std::make_unique<RealDataLoaderManager>(manager); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 388 | } |
| 389 | return nullptr; |
| 390 | } |
| 391 | |
Yurii Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 392 | std::unique_ptr<IncFsWrapper> RealServiceManager::getIncFs() { |
| 393 | return std::make_unique<RealIncFs>(); |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 396 | std::unique_ptr<AppOpsManagerWrapper> RealServiceManager::getAppOpsManager() { |
| 397 | return std::make_unique<RealAppOpsManager>(); |
| 398 | } |
| 399 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 400 | std::unique_ptr<JniWrapper> RealServiceManager::getJni() { |
| 401 | return std::make_unique<RealJniWrapper>(mJvm); |
| 402 | } |
| 403 | |
Alex Buynytskyy | cca2c11 | 2020-05-05 12:48:41 -0700 | [diff] [blame] | 404 | std::unique_ptr<LooperWrapper> RealServiceManager::getLooper() { |
| 405 | return std::make_unique<RealLooperWrapper>(); |
| 406 | } |
| 407 | |
Alex Buynytskyy | 46d3ddb | 2020-05-29 12:05:05 -0700 | [diff] [blame] | 408 | std::unique_ptr<TimedQueueWrapper> RealServiceManager::getTimedQueue() { |
| 409 | return std::make_unique<RealTimedQueueWrapper>(mJvm); |
| 410 | } |
| 411 | |
Songchun Fan | a709859 | 2020-09-03 11:45:53 -0700 | [diff] [blame] | 412 | std::unique_ptr<TimedQueueWrapper> RealServiceManager::getProgressUpdateJobQueue() { |
| 413 | return std::make_unique<RealTimedQueueWrapper>(mJvm); |
| 414 | } |
| 415 | |
Songchun Fan | 374f765 | 2020-08-20 08:40:29 -0700 | [diff] [blame] | 416 | std::unique_ptr<FsWrapper> RealServiceManager::getFs() { |
| 417 | return std::make_unique<RealFsWrapper>(); |
| 418 | } |
| 419 | |
Alex Buynytskyy | 7e06d71 | 2021-03-09 19:24:23 -0800 | [diff] [blame] | 420 | std::unique_ptr<ClockWrapper> RealServiceManager::getClock() { |
| 421 | return std::make_unique<RealClockWrapper>(); |
| 422 | } |
| 423 | |
Yurii Zubrytskyi | 8632140 | 2020-04-09 19:22:30 -0700 | [diff] [blame] | 424 | static JavaVM* getJavaVm(JNIEnv* env) { |
| 425 | CHECK(env); |
| 426 | JavaVM* jvm = nullptr; |
| 427 | env->GetJavaVM(&jvm); |
| 428 | CHECK(jvm); |
| 429 | return jvm; |
| 430 | } |
| 431 | |
| 432 | static JNIEnv* getJniEnv(JavaVM* vm) { |
| 433 | JNIEnv* env; |
| 434 | if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { |
| 435 | return nullptr; |
| 436 | } |
| 437 | return env; |
| 438 | } |
| 439 | |
| 440 | static JNIEnv* getOrAttachJniEnv(JavaVM* jvm) { |
| 441 | if (!jvm) { |
| 442 | LOG(ERROR) << "No JVM instance"; |
| 443 | return nullptr; |
| 444 | } |
| 445 | |
| 446 | JNIEnv* env = getJniEnv(jvm); |
| 447 | if (!env) { |
| 448 | int result = jvm->AttachCurrentThread(&env, nullptr); |
| 449 | if (result != JNI_OK) { |
| 450 | LOG(ERROR) << "JVM thread attach failed: " << result; |
| 451 | return nullptr; |
| 452 | } |
| 453 | struct VmDetacher { |
| 454 | VmDetacher(JavaVM* vm) : mVm(vm) {} |
| 455 | ~VmDetacher() { mVm->DetachCurrentThread(); } |
| 456 | |
| 457 | private: |
| 458 | JavaVM* const mVm; |
| 459 | }; |
| 460 | static thread_local VmDetacher detacher(jvm); |
| 461 | } |
| 462 | |
| 463 | return env; |
| 464 | } |
| 465 | |
| 466 | RealJniWrapper::RealJniWrapper(JavaVM* jvm) : mJvm(jvm) { |
| 467 | CHECK(!!mJvm) << "JVM is unavailable"; |
| 468 | } |
| 469 | |
| 470 | void RealJniWrapper::initializeForCurrentThread() const { |
| 471 | (void)getOrAttachJniEnv(mJvm); |
| 472 | } |
| 473 | |
| 474 | JavaVM* RealJniWrapper::getJvm(JNIEnv* env) { |
| 475 | return getJavaVm(env); |
| 476 | } |
| 477 | |
Yurii Zubrytskyi | 629051fd | 2020-04-17 23:13:47 -0700 | [diff] [blame] | 478 | } // namespace android::incremental |