blob: 449b457045c60a773f8edb54f4b8ed43a57ab6db [file] [log] [blame]
Songchun Fan3c82a302019-11-29 14:23:45 -08001/*
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 Fan68645c42020-02-27 15:57:35 -080023#include <android/content/pm/IDataLoader.h>
24#include <android/content/pm/IDataLoaderManager.h>
Songchun Fan3c82a302019-11-29 14:23:45 -080025#include <android/content/pm/IDataLoaderStatusListener.h>
26#include <android/os/IVold.h>
Alex Buynytskyy96e350b2020-04-02 20:03:47 -070027#include <binder/AppOpsManager.h>
Songchun Fan3c82a302019-11-29 14:23:45 -080028#include <binder/IServiceManager.h>
29#include <incfs.h>
30
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080031#include <memory>
Songchun Fan3c82a302019-11-29 14:23:45 -080032#include <string>
33#include <string_view>
34
35using namespace android::incfs;
36using namespace android::content::pm;
37
38namespace android::os::incremental {
39
40// --- Wrapper interfaces ---
41
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080042using MountId = int32_t;
43
Songchun Fan3c82a302019-11-29 14:23:45 -080044class VoldServiceWrapper {
45public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080046 virtual ~VoldServiceWrapper() = default;
47 virtual binder::Status mountIncFs(const std::string& backingPath, const std::string& targetDir,
Songchun Fan3c82a302019-11-29 14:23:45 -080048 int32_t flags,
49 IncrementalFileSystemControlParcel* _aidl_return) const = 0;
50 virtual binder::Status unmountIncFs(const std::string& dir) const = 0;
51 virtual binder::Status bindMount(const std::string& sourceDir,
52 const std::string& targetDir) const = 0;
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -070053 virtual binder::Status setIncFsMountOptions(const ::android::os::incremental::IncrementalFileSystemControlParcel& control, bool enableReadLogs) const = 0;
Songchun Fan3c82a302019-11-29 14:23:45 -080054};
55
Songchun Fan68645c42020-02-27 15:57:35 -080056class DataLoaderManagerWrapper {
Songchun Fan3c82a302019-11-29 14:23:45 -080057public:
Songchun Fan68645c42020-02-27 15:57:35 -080058 virtual ~DataLoaderManagerWrapper() = default;
59 virtual binder::Status initializeDataLoader(MountId mountId,
60 const DataLoaderParamsParcel& params,
61 const FileSystemControlParcel& control,
62 const sp<IDataLoaderStatusListener>& listener,
63 bool* _aidl_return) const = 0;
64 virtual binder::Status getDataLoader(MountId mountId, sp<IDataLoader>* _aidl_return) const = 0;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080065 virtual binder::Status destroyDataLoader(MountId mountId) const = 0;
Songchun Fan3c82a302019-11-29 14:23:45 -080066};
67
68class IncFsWrapper {
69public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080070 virtual ~IncFsWrapper() = default;
Songchun Fan20d6ef22020-03-03 09:47:15 -080071 virtual Control createControl(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs) const = 0;
72 virtual ErrorCode makeFile(const Control& control, std::string_view path, int mode, FileId id,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080073 NewFileParams params) const = 0;
Songchun Fan20d6ef22020-03-03 09:47:15 -080074 virtual ErrorCode makeDir(const Control& control, std::string_view path, int mode) const = 0;
75 virtual RawMetadata getMetadata(const Control& control, FileId fileid) const = 0;
76 virtual RawMetadata getMetadata(const Control& control, std::string_view path) const = 0;
77 virtual FileId getFileId(const Control& control, std::string_view path) const = 0;
78 virtual ErrorCode link(const Control& control, std::string_view from,
79 std::string_view to) const = 0;
80 virtual ErrorCode unlink(const Control& control, std::string_view path) const = 0;
Yurii Zubrytskyie82cdd72020-04-01 12:19:26 -070081 virtual base::unique_fd openForSpecialOps(const Control& control, FileId id) const = 0;
Songchun Fan9b753082020-02-26 13:08:06 -080082 virtual ErrorCode writeBlocks(Span<const DataBlock> blocks) const = 0;
Songchun Fan3c82a302019-11-29 14:23:45 -080083};
84
Alex Buynytskyy96e350b2020-04-02 20:03:47 -070085class AppOpsManagerWrapper {
86public:
87 virtual ~AppOpsManagerWrapper() = default;
88 virtual void startWatchingMode(int32_t op, const String16& packageName, const sp<IAppOpsCallback>& callback) = 0;
89};
90
Songchun Fan3c82a302019-11-29 14:23:45 -080091class ServiceManagerWrapper {
92public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080093 virtual ~ServiceManagerWrapper() = default;
94 virtual std::unique_ptr<VoldServiceWrapper> getVoldService() = 0;
Songchun Fan68645c42020-02-27 15:57:35 -080095 virtual std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() = 0;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080096 virtual std::unique_ptr<IncFsWrapper> getIncFs() = 0;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -070097 virtual std::unique_ptr<AppOpsManagerWrapper> getAppOpsManager() = 0;
Songchun Fan3c82a302019-11-29 14:23:45 -080098};
99
100// --- Real stuff ---
101
102class RealVoldService : public VoldServiceWrapper {
103public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800104 RealVoldService(const sp<os::IVold> vold) : mInterface(std::move(vold)) {}
Songchun Fan3c82a302019-11-29 14:23:45 -0800105 ~RealVoldService() = default;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800106 binder::Status mountIncFs(const std::string& backingPath, const std::string& targetDir,
Songchun Fan3c82a302019-11-29 14:23:45 -0800107 int32_t flags,
108 IncrementalFileSystemControlParcel* _aidl_return) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800109 return mInterface->mountIncFs(backingPath, targetDir, flags, _aidl_return);
Songchun Fan3c82a302019-11-29 14:23:45 -0800110 }
111 binder::Status unmountIncFs(const std::string& dir) const override {
112 return mInterface->unmountIncFs(dir);
113 }
114 binder::Status bindMount(const std::string& sourceDir,
115 const std::string& targetDir) const override {
116 return mInterface->bindMount(sourceDir, targetDir);
117 }
Alex Buynytskyy5e860ba2020-03-31 15:30:21 -0700118 binder::Status setIncFsMountOptions(const ::android::os::incremental::IncrementalFileSystemControlParcel& control, bool enableReadLogs) const override {
119 return mInterface->setIncFsMountOptions(control, enableReadLogs);
120 }
Songchun Fan3c82a302019-11-29 14:23:45 -0800121
122private:
123 sp<os::IVold> mInterface;
124};
125
Songchun Fan68645c42020-02-27 15:57:35 -0800126class RealDataLoaderManager : public DataLoaderManagerWrapper {
Songchun Fan3c82a302019-11-29 14:23:45 -0800127public:
Songchun Fan68645c42020-02-27 15:57:35 -0800128 RealDataLoaderManager(const sp<content::pm::IDataLoaderManager> manager)
Songchun Fan3c82a302019-11-29 14:23:45 -0800129 : mInterface(manager) {}
Songchun Fan68645c42020-02-27 15:57:35 -0800130 ~RealDataLoaderManager() = default;
131 binder::Status initializeDataLoader(MountId mountId, const DataLoaderParamsParcel& params,
132 const FileSystemControlParcel& control,
133 const sp<IDataLoaderStatusListener>& listener,
134 bool* _aidl_return) const override {
135 return mInterface->initializeDataLoader(mountId, params, control, listener, _aidl_return);
Songchun Fan3c82a302019-11-29 14:23:45 -0800136 }
Songchun Fan68645c42020-02-27 15:57:35 -0800137 binder::Status getDataLoader(MountId mountId, sp<IDataLoader>* _aidl_return) const override {
138 return mInterface->getDataLoader(mountId, _aidl_return);
Songchun Fan3c82a302019-11-29 14:23:45 -0800139 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800140 binder::Status destroyDataLoader(MountId mountId) const override {
Songchun Fan3c82a302019-11-29 14:23:45 -0800141 return mInterface->destroyDataLoader(mountId);
142 }
Songchun Fan3c82a302019-11-29 14:23:45 -0800143
144private:
Songchun Fan68645c42020-02-27 15:57:35 -0800145 sp<content::pm::IDataLoaderManager> mInterface;
Songchun Fan3c82a302019-11-29 14:23:45 -0800146};
147
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700148class RealAppOpsManager : public AppOpsManagerWrapper {
149public:
150 ~RealAppOpsManager() = default;
151 void startWatchingMode(int32_t op, const String16& packageName, const sp<IAppOpsCallback>& callback) override {
152 mAppOpsManager.startWatchingMode(op, packageName, callback);
153 }
154private:
155 android::AppOpsManager mAppOpsManager;
156};
157
Songchun Fan3c82a302019-11-29 14:23:45 -0800158class RealServiceManager : public ServiceManagerWrapper {
159public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800160 RealServiceManager(sp<IServiceManager> serviceManager);
Songchun Fan3c82a302019-11-29 14:23:45 -0800161 ~RealServiceManager() = default;
Alex Buynytskyy96e350b2020-04-02 20:03:47 -0700162 std::unique_ptr<VoldServiceWrapper> getVoldService() final;
163 std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() final;
164 std::unique_ptr<IncFsWrapper> getIncFs() final;
165 std::unique_ptr<AppOpsManagerWrapper> getAppOpsManager() final;
Songchun Fan3c82a302019-11-29 14:23:45 -0800166
167private:
168 template <class INTERFACE>
169 sp<INTERFACE> getRealService(std::string_view serviceName) const;
170 sp<android::IServiceManager> mServiceManager;
171};
172
173class RealIncFs : public IncFsWrapper {
174public:
175 RealIncFs() = default;
176 ~RealIncFs() = default;
Songchun Fan20d6ef22020-03-03 09:47:15 -0800177 Control createControl(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs) const override {
178 return incfs::createControl(cmd, pendingReads, logs);
179 }
180 ErrorCode makeFile(const Control& control, std::string_view path, int mode, FileId id,
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800181 NewFileParams params) const override {
182 return incfs::makeFile(control, path, mode, id, params);
Songchun Fan3c82a302019-11-29 14:23:45 -0800183 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800184 ErrorCode makeDir(const Control& control, std::string_view path, int mode) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800185 return incfs::makeDir(control, path, mode);
Songchun Fan3c82a302019-11-29 14:23:45 -0800186 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800187 RawMetadata getMetadata(const Control& control, FileId fileid) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800188 return incfs::getMetadata(control, fileid);
Songchun Fan3c82a302019-11-29 14:23:45 -0800189 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800190 RawMetadata getMetadata(const Control& control, std::string_view path) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800191 return incfs::getMetadata(control, path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800192 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800193 FileId getFileId(const Control& control, std::string_view path) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800194 return incfs::getFileId(control, path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800195 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800196 ErrorCode link(const Control& control, std::string_view from,
197 std::string_view to) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800198 return incfs::link(control, from, to);
199 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800200 ErrorCode unlink(const Control& control, std::string_view path) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800201 return incfs::unlink(control, path);
202 }
Yurii Zubrytskyie82cdd72020-04-01 12:19:26 -0700203 base::unique_fd openForSpecialOps(const Control& control, FileId id) const override {
204 return base::unique_fd{incfs::openForSpecialOps(control, id).release()};
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800205 }
Songchun Fan9b753082020-02-26 13:08:06 -0800206 ErrorCode writeBlocks(Span<const DataBlock> blocks) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800207 return incfs::writeBlocks(blocks);
Songchun Fan3c82a302019-11-29 14:23:45 -0800208 }
209};
210
211} // namespace android::os::incremental