blob: 570458283ac99cad6fcdbe74b2e863d4f1b11d31 [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>
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
29#include <string>
30#include <string_view>
31
32using namespace android::incfs;
33using namespace android::content::pm;
34
35namespace android::os::incremental {
36
37// --- Wrapper interfaces ---
38
39class VoldServiceWrapper {
40public:
41 virtual ~VoldServiceWrapper(){};
42 virtual binder::Status mountIncFs(const std::string& imagePath, const std::string& targetDir,
43 int32_t flags,
44 IncrementalFileSystemControlParcel* _aidl_return) const = 0;
45 virtual binder::Status unmountIncFs(const std::string& dir) const = 0;
46 virtual binder::Status bindMount(const std::string& sourceDir,
47 const std::string& targetDir) const = 0;
48};
49
50class IncrementalManagerWrapper {
51public:
52 virtual ~IncrementalManagerWrapper() {}
53 virtual binder::Status prepareDataLoader(
54 int32_t mountId, const FileSystemControlParcel& control,
55 const DataLoaderParamsParcel& params,
56 const sp<IDataLoaderStatusListener>& listener,
57 bool* _aidl_return) const = 0;
58 virtual binder::Status startDataLoader(int32_t mountId, bool* _aidl_return) const = 0;
59 virtual binder::Status destroyDataLoader(int32_t mountId) const = 0;
60 virtual binder::Status newFileForDataLoader(int32_t mountId, int64_t inode,
61 const ::std::vector<uint8_t>& metadata) const = 0;
62 virtual binder::Status showHealthBlockedUI(int32_t mountId) const = 0;
63};
64
65class IncFsWrapper {
66public:
67 virtual ~IncFsWrapper() {}
68 virtual Inode makeFile(Control control, std::string_view name, Inode parent, Size size,
69 std::string_view metadata) const = 0;
70 virtual Inode makeDir(Control control, std::string_view name, Inode parent,
71 std::string_view metadata, int mode = 0555) const = 0;
72 virtual RawMetadata getMetadata(Control control, Inode inode) const = 0;
73 virtual ErrorCode link(Control control, Inode item, Inode targetParent,
74 std::string_view name) const = 0;
75 virtual ErrorCode unlink(Control control, Inode parent, std::string_view name) const = 0;
76 virtual ErrorCode writeBlocks(Control control, const incfs_new_data_block blocks[],
77 int blocksCount) const = 0;
78};
79
80class ServiceManagerWrapper {
81public:
82 virtual ~ServiceManagerWrapper() {}
83 virtual std::shared_ptr<VoldServiceWrapper> getVoldService() const = 0;
84 virtual std::shared_ptr<IncrementalManagerWrapper> getIncrementalManager() const = 0;
85 virtual std::shared_ptr<IncFsWrapper> getIncFs() const = 0;
86};
87
88// --- Real stuff ---
89
90class RealVoldService : public VoldServiceWrapper {
91public:
92 RealVoldService(const sp<os::IVold> vold) : mInterface(vold) {}
93 ~RealVoldService() = default;
94 binder::Status mountIncFs(const std::string& imagePath, const std::string& targetDir,
95 int32_t flags,
96 IncrementalFileSystemControlParcel* _aidl_return) const override {
97 return mInterface->mountIncFs(imagePath, targetDir, flags, _aidl_return);
98 }
99 binder::Status unmountIncFs(const std::string& dir) const override {
100 return mInterface->unmountIncFs(dir);
101 }
102 binder::Status bindMount(const std::string& sourceDir,
103 const std::string& targetDir) const override {
104 return mInterface->bindMount(sourceDir, targetDir);
105 }
106
107private:
108 sp<os::IVold> mInterface;
109};
110
111class RealIncrementalManager : public IncrementalManagerWrapper {
112public:
113 RealIncrementalManager(const sp<os::incremental::IIncrementalManager> manager)
114 : mInterface(manager) {}
115 ~RealIncrementalManager() = default;
116 binder::Status prepareDataLoader(
117 int32_t mountId, const FileSystemControlParcel& control,
118 const DataLoaderParamsParcel& params,
119 const sp<IDataLoaderStatusListener>& listener,
120 bool* _aidl_return) const override {
121 return mInterface->prepareDataLoader(mountId, control, params, listener, _aidl_return);
122 }
123 binder::Status startDataLoader(int32_t mountId, bool* _aidl_return) const override {
124 return mInterface->startDataLoader(mountId, _aidl_return);
125 }
126 binder::Status destroyDataLoader(int32_t mountId) const override {
127 return mInterface->destroyDataLoader(mountId);
128 }
129 binder::Status newFileForDataLoader(int32_t mountId, int64_t inode,
130 const ::std::vector<uint8_t>& metadata) const override {
131 return mInterface->newFileForDataLoader(mountId, inode, metadata);
132 }
133 binder::Status showHealthBlockedUI(int32_t mountId) const override {
134 return mInterface->showHealthBlockedUI(mountId);
135 }
136
137private:
138 sp<os::incremental::IIncrementalManager> mInterface;
139};
140
141class RealServiceManager : public ServiceManagerWrapper {
142public:
143 RealServiceManager(const sp<IServiceManager>& serviceManager);
144 ~RealServiceManager() = default;
145 std::shared_ptr<VoldServiceWrapper> getVoldService() const override;
146 std::shared_ptr<IncrementalManagerWrapper> getIncrementalManager() const override;
147 std::shared_ptr<IncFsWrapper> getIncFs() const override;
148
149private:
150 template <class INTERFACE>
151 sp<INTERFACE> getRealService(std::string_view serviceName) const;
152 sp<android::IServiceManager> mServiceManager;
153};
154
155class RealIncFs : public IncFsWrapper {
156public:
157 RealIncFs() = default;
158 ~RealIncFs() = default;
159 Inode makeFile(Control control, std::string_view name, Inode parent, Size size,
160 std::string_view metadata) const override {
161 return incfs::makeFile(control, name, parent, size, metadata);
162 }
163 Inode makeDir(Control control, std::string_view name, Inode parent, std::string_view metadata,
164 int mode) const override {
165 return incfs::makeDir(control, name, parent, metadata, mode);
166 }
167 RawMetadata getMetadata(Control control, Inode inode) const override {
168 return incfs::getMetadata(control, inode);
169 }
170 ErrorCode link(Control control, Inode item, Inode targetParent,
171 std::string_view name) const override {
172 return incfs::link(control, item, targetParent, name);
173 }
174 ErrorCode unlink(Control control, Inode parent, std::string_view name) const override {
175 return incfs::unlink(control, parent, name);
176 }
177 ErrorCode writeBlocks(Control control, const incfs_new_data_block blocks[],
178 int blocksCount) const override {
179 return incfs::writeBlocks(control, blocks, blocksCount);
180 }
181};
182
183} // namespace android::os::incremental