blob: 5349ebff50520d25ee46bb53749bdf699ddd53f2 [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>
Songchun Fan3c82a302019-11-29 14:23:45 -080027#include <binder/IServiceManager.h>
28#include <incfs.h>
29
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080030#include <memory>
Songchun Fan3c82a302019-11-29 14:23:45 -080031#include <string>
32#include <string_view>
33
34using namespace android::incfs;
35using namespace android::content::pm;
36
37namespace android::os::incremental {
38
39// --- Wrapper interfaces ---
40
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080041using MountId = int32_t;
42
Songchun Fan3c82a302019-11-29 14:23:45 -080043class VoldServiceWrapper {
44public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080045 virtual ~VoldServiceWrapper() = default;
46 virtual binder::Status mountIncFs(const std::string& backingPath, const std::string& targetDir,
Songchun Fan3c82a302019-11-29 14:23:45 -080047 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 Fan68645c42020-02-27 15:57:35 -080054class DataLoaderManagerWrapper {
Songchun Fan3c82a302019-11-29 14:23:45 -080055public:
Songchun Fan68645c42020-02-27 15:57:35 -080056 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 Zubrytskyi4a25dfb2020-01-10 11:53:24 -080063 virtual binder::Status destroyDataLoader(MountId mountId) const = 0;
Songchun Fan3c82a302019-11-29 14:23:45 -080064};
65
66class IncFsWrapper {
67public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080068 virtual ~IncFsWrapper() = default;
69 virtual ErrorCode makeFile(Control control, std::string_view path, int mode, FileId id,
70 NewFileParams params) const = 0;
Songchun Fan96100932020-02-03 19:20:58 -080071 virtual ErrorCode makeDir(Control control, std::string_view path, int mode) const = 0;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080072 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 Fan9b753082020-02-26 13:08:06 -080078 virtual ErrorCode writeBlocks(Span<const DataBlock> blocks) const = 0;
Songchun Fan3c82a302019-11-29 14:23:45 -080079};
80
81class ServiceManagerWrapper {
82public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080083 virtual ~ServiceManagerWrapper() = default;
84 virtual std::unique_ptr<VoldServiceWrapper> getVoldService() = 0;
Songchun Fan68645c42020-02-27 15:57:35 -080085 virtual std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() = 0;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080086 virtual std::unique_ptr<IncFsWrapper> getIncFs() = 0;
Songchun Fan3c82a302019-11-29 14:23:45 -080087};
88
89// --- Real stuff ---
90
91class RealVoldService : public VoldServiceWrapper {
92public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080093 RealVoldService(const sp<os::IVold> vold) : mInterface(std::move(vold)) {}
Songchun Fan3c82a302019-11-29 14:23:45 -080094 ~RealVoldService() = default;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080095 binder::Status mountIncFs(const std::string& backingPath, const std::string& targetDir,
Songchun Fan3c82a302019-11-29 14:23:45 -080096 int32_t flags,
97 IncrementalFileSystemControlParcel* _aidl_return) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080098 return mInterface->mountIncFs(backingPath, targetDir, flags, _aidl_return);
Songchun Fan3c82a302019-11-29 14:23:45 -080099 }
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
108private:
109 sp<os::IVold> mInterface;
110};
111
Songchun Fan68645c42020-02-27 15:57:35 -0800112class RealDataLoaderManager : public DataLoaderManagerWrapper {
Songchun Fan3c82a302019-11-29 14:23:45 -0800113public:
Songchun Fan68645c42020-02-27 15:57:35 -0800114 RealDataLoaderManager(const sp<content::pm::IDataLoaderManager> manager)
Songchun Fan3c82a302019-11-29 14:23:45 -0800115 : mInterface(manager) {}
Songchun Fan68645c42020-02-27 15:57:35 -0800116 ~RealDataLoaderManager() = default;
117 binder::Status initializeDataLoader(MountId mountId, const DataLoaderParamsParcel& params,
118 const FileSystemControlParcel& control,
119 const sp<IDataLoaderStatusListener>& listener,
120 bool* _aidl_return) const override {
121 return mInterface->initializeDataLoader(mountId, params, control, listener, _aidl_return);
Songchun Fan3c82a302019-11-29 14:23:45 -0800122 }
Songchun Fan68645c42020-02-27 15:57:35 -0800123 binder::Status getDataLoader(MountId mountId, sp<IDataLoader>* _aidl_return) const override {
124 return mInterface->getDataLoader(mountId, _aidl_return);
Songchun Fan3c82a302019-11-29 14:23:45 -0800125 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800126 binder::Status destroyDataLoader(MountId mountId) const override {
Songchun Fan3c82a302019-11-29 14:23:45 -0800127 return mInterface->destroyDataLoader(mountId);
128 }
Songchun Fan3c82a302019-11-29 14:23:45 -0800129
130private:
Songchun Fan68645c42020-02-27 15:57:35 -0800131 sp<content::pm::IDataLoaderManager> mInterface;
Songchun Fan3c82a302019-11-29 14:23:45 -0800132};
133
134class RealServiceManager : public ServiceManagerWrapper {
135public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800136 RealServiceManager(sp<IServiceManager> serviceManager);
Songchun Fan3c82a302019-11-29 14:23:45 -0800137 ~RealServiceManager() = default;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800138 std::unique_ptr<VoldServiceWrapper> getVoldService() override;
Songchun Fan68645c42020-02-27 15:57:35 -0800139 std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() override;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800140 std::unique_ptr<IncFsWrapper> getIncFs() override;
Songchun Fan3c82a302019-11-29 14:23:45 -0800141
142private:
143 template <class INTERFACE>
144 sp<INTERFACE> getRealService(std::string_view serviceName) const;
145 sp<android::IServiceManager> mServiceManager;
146};
147
148class RealIncFs : public IncFsWrapper {
149public:
150 RealIncFs() = default;
151 ~RealIncFs() = default;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800152 ErrorCode makeFile(Control control, std::string_view path, int mode, FileId id,
153 NewFileParams params) const override {
154 return incfs::makeFile(control, path, mode, id, params);
Songchun Fan3c82a302019-11-29 14:23:45 -0800155 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800156 ErrorCode makeDir(Control control, std::string_view path, int mode) const override {
157 return incfs::makeDir(control, path, mode);
Songchun Fan3c82a302019-11-29 14:23:45 -0800158 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800159 RawMetadata getMetadata(Control control, FileId fileid) const override {
160 return incfs::getMetadata(control, fileid);
Songchun Fan3c82a302019-11-29 14:23:45 -0800161 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800162 RawMetadata getMetadata(Control control, std::string_view path) const override {
163 return incfs::getMetadata(control, path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800164 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800165 FileId getFileId(Control control, std::string_view path) const override {
166 return incfs::getFileId(control, path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800167 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800168 ErrorCode link(Control control, std::string_view from, std::string_view to) const override {
169 return incfs::link(control, from, to);
170 }
171 ErrorCode unlink(Control control, std::string_view path) const override {
172 return incfs::unlink(control, path);
173 }
174 base::unique_fd openWrite(Control control, FileId id) const override {
175 return base::unique_fd{incfs::openWrite(control, id)};
176 }
Songchun Fan9b753082020-02-26 13:08:06 -0800177 ErrorCode writeBlocks(Span<const DataBlock> blocks) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800178 return incfs::writeBlocks(blocks);
Songchun Fan3c82a302019-11-29 14:23:45 -0800179 }
180};
181
182} // namespace android::os::incremental