blob: c70a47d40c4e3ecf24e57f971f3fecd6d47d34ef [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;
Songchun Fan20d6ef22020-03-03 09:47:15 -080069 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 Zubrytskyi4a25dfb2020-01-10 11:53:24 -080071 NewFileParams params) const = 0;
Songchun Fan20d6ef22020-03-03 09:47:15 -080072 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 Fan9b753082020-02-26 13:08:06 -080080 virtual ErrorCode writeBlocks(Span<const DataBlock> blocks) const = 0;
Songchun Fan3c82a302019-11-29 14:23:45 -080081};
82
83class ServiceManagerWrapper {
84public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080085 virtual ~ServiceManagerWrapper() = default;
86 virtual std::unique_ptr<VoldServiceWrapper> getVoldService() = 0;
Songchun Fan68645c42020-02-27 15:57:35 -080087 virtual std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() = 0;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080088 virtual std::unique_ptr<IncFsWrapper> getIncFs() = 0;
Songchun Fan3c82a302019-11-29 14:23:45 -080089};
90
91// --- Real stuff ---
92
93class RealVoldService : public VoldServiceWrapper {
94public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080095 RealVoldService(const sp<os::IVold> vold) : mInterface(std::move(vold)) {}
Songchun Fan3c82a302019-11-29 14:23:45 -080096 ~RealVoldService() = default;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080097 binder::Status mountIncFs(const std::string& backingPath, const std::string& targetDir,
Songchun Fan3c82a302019-11-29 14:23:45 -080098 int32_t flags,
99 IncrementalFileSystemControlParcel* _aidl_return) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800100 return mInterface->mountIncFs(backingPath, targetDir, flags, _aidl_return);
Songchun Fan3c82a302019-11-29 14:23:45 -0800101 }
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
110private:
111 sp<os::IVold> mInterface;
112};
113
Songchun Fan68645c42020-02-27 15:57:35 -0800114class RealDataLoaderManager : public DataLoaderManagerWrapper {
Songchun Fan3c82a302019-11-29 14:23:45 -0800115public:
Songchun Fan68645c42020-02-27 15:57:35 -0800116 RealDataLoaderManager(const sp<content::pm::IDataLoaderManager> manager)
Songchun Fan3c82a302019-11-29 14:23:45 -0800117 : mInterface(manager) {}
Songchun Fan68645c42020-02-27 15:57:35 -0800118 ~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 Fan3c82a302019-11-29 14:23:45 -0800124 }
Songchun Fan68645c42020-02-27 15:57:35 -0800125 binder::Status getDataLoader(MountId mountId, sp<IDataLoader>* _aidl_return) const override {
126 return mInterface->getDataLoader(mountId, _aidl_return);
Songchun Fan3c82a302019-11-29 14:23:45 -0800127 }
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800128 binder::Status destroyDataLoader(MountId mountId) const override {
Songchun Fan3c82a302019-11-29 14:23:45 -0800129 return mInterface->destroyDataLoader(mountId);
130 }
Songchun Fan3c82a302019-11-29 14:23:45 -0800131
132private:
Songchun Fan68645c42020-02-27 15:57:35 -0800133 sp<content::pm::IDataLoaderManager> mInterface;
Songchun Fan3c82a302019-11-29 14:23:45 -0800134};
135
136class RealServiceManager : public ServiceManagerWrapper {
137public:
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800138 RealServiceManager(sp<IServiceManager> serviceManager);
Songchun Fan3c82a302019-11-29 14:23:45 -0800139 ~RealServiceManager() = default;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800140 std::unique_ptr<VoldServiceWrapper> getVoldService() override;
Songchun Fan68645c42020-02-27 15:57:35 -0800141 std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() override;
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800142 std::unique_ptr<IncFsWrapper> getIncFs() override;
Songchun Fan3c82a302019-11-29 14:23:45 -0800143
144private:
145 template <class INTERFACE>
146 sp<INTERFACE> getRealService(std::string_view serviceName) const;
147 sp<android::IServiceManager> mServiceManager;
148};
149
150class RealIncFs : public IncFsWrapper {
151public:
152 RealIncFs() = default;
153 ~RealIncFs() = default;
Songchun Fan20d6ef22020-03-03 09:47:15 -0800154 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 Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800158 NewFileParams params) const override {
159 return incfs::makeFile(control, path, mode, id, params);
Songchun Fan3c82a302019-11-29 14:23:45 -0800160 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800161 ErrorCode makeDir(const Control& control, std::string_view path, int mode) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800162 return incfs::makeDir(control, path, mode);
Songchun Fan3c82a302019-11-29 14:23:45 -0800163 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800164 RawMetadata getMetadata(const Control& control, FileId fileid) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800165 return incfs::getMetadata(control, fileid);
Songchun Fan3c82a302019-11-29 14:23:45 -0800166 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800167 RawMetadata getMetadata(const Control& control, std::string_view path) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800168 return incfs::getMetadata(control, path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800169 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800170 FileId getFileId(const Control& control, std::string_view path) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800171 return incfs::getFileId(control, path);
Songchun Fan3c82a302019-11-29 14:23:45 -0800172 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800173 ErrorCode link(const Control& control, std::string_view from,
174 std::string_view to) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800175 return incfs::link(control, from, to);
176 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800177 ErrorCode unlink(const Control& control, std::string_view path) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800178 return incfs::unlink(control, path);
179 }
Songchun Fan20d6ef22020-03-03 09:47:15 -0800180 base::unique_fd openWrite(const Control& control, FileId id) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800181 return base::unique_fd{incfs::openWrite(control, id)};
182 }
Songchun Fan9b753082020-02-26 13:08:06 -0800183 ErrorCode writeBlocks(Span<const DataBlock> blocks) const override {
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800184 return incfs::writeBlocks(blocks);
Songchun Fan3c82a302019-11-29 14:23:45 -0800185 }
186};
187
188} // namespace android::os::incremental