• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #pragma once
18 
19 #include <android-base/function_ref.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/IDataLoader.h>
24 #include <android/content/pm/IDataLoaderStatusListener.h>
25 #include <android/os/incremental/PerUidReadTimeouts.h>
26 #include <binder/IAppOpsCallback.h>
27 #include <binder/IServiceManager.h>
28 #include <binder/Status.h>
29 #include <incfs.h>
30 #include <jni.h>
31 #include <utils/Looper.h>
32 
33 #include <memory>
34 #include <span>
35 #include <string>
36 #include <string_view>
37 
38 namespace android::incremental {
39 
40 using Clock = std::chrono::steady_clock;
41 using TimePoint = std::chrono::time_point<Clock>;
42 using Milliseconds = std::chrono::milliseconds;
43 using Job = std::function<void()>;
44 
45 // --- Wrapper interfaces ---
46 
47 using MountId = int32_t;
48 
49 class VoldServiceWrapper {
50 public:
51     virtual ~VoldServiceWrapper() = default;
52     virtual binder::Status mountIncFs(
53             const std::string& backingPath, const std::string& targetDir, int32_t flags,
54             const std::string& sysfsName,
55             os::incremental::IncrementalFileSystemControlParcel* result) const = 0;
56     virtual binder::Status unmountIncFs(const std::string& dir) const = 0;
57     virtual binder::Status bindMount(const std::string& sourceDir,
58                                      const std::string& targetDir) const = 0;
59     virtual binder::Status setIncFsMountOptions(
60             const os::incremental::IncrementalFileSystemControlParcel& control, bool enableReadLogs,
61             bool enableReadTimeouts, const std::string& sysfsName) const = 0;
62 };
63 
64 class DataLoaderManagerWrapper {
65 public:
66     virtual ~DataLoaderManagerWrapper() = default;
67     virtual binder::Status bindToDataLoader(
68             MountId mountId, const content::pm::DataLoaderParamsParcel& params, int bindDelayMs,
69             const sp<content::pm::IDataLoaderStatusListener>& listener, bool* result) const = 0;
70     virtual binder::Status getDataLoader(MountId mountId,
71                                          sp<content::pm::IDataLoader>* result) const = 0;
72     virtual binder::Status unbindFromDataLoader(MountId mountId) const = 0;
73 };
74 
75 class IncFsWrapper {
76 public:
77     using Control = incfs::Control;
78     using FileId = incfs::FileId;
79     using ErrorCode = incfs::ErrorCode;
80     using UniqueFd = incfs::UniqueFd;
81     using WaitResult = incfs::WaitResult;
82     using Features = incfs::Features;
83     using Metrics = incfs::Metrics;
84     using LastReadError = incfs::LastReadError;
85 
86     using ExistingMountCallback = android::base::function_ref<
87             void(std::string_view root, std::string_view backingDir,
88                  std::span<std::pair<std::string_view, std::string_view>> binds)>;
89 
90     using FileCallback = android::base::function_ref<bool(const Control& control, FileId fileId)>;
91 
92     static std::string toString(FileId fileId);
93 
94     virtual ~IncFsWrapper() = default;
95     virtual Features features() const = 0;
96     virtual void listExistingMounts(const ExistingMountCallback& cb) const = 0;
97     virtual Control openMount(std::string_view path) const = 0;
98     virtual Control createControl(IncFsFd cmd, IncFsFd pendingReads, IncFsFd logs,
99                                   IncFsFd blocksWritten) const = 0;
100     virtual ErrorCode makeFile(const Control& control, std::string_view path, int mode, FileId id,
101                                incfs::NewFileParams params) const = 0;
102     virtual ErrorCode makeMappedFile(const Control& control, std::string_view path, int mode,
103                                      incfs::NewMappedFileParams params) const = 0;
104     virtual ErrorCode makeDir(const Control& control, std::string_view path, int mode) const = 0;
105     virtual ErrorCode makeDirs(const Control& control, std::string_view path, int mode) const = 0;
106     virtual incfs::RawMetadata getMetadata(const Control& control, FileId fileid) const = 0;
107     virtual incfs::RawMetadata getMetadata(const Control& control, std::string_view path) const = 0;
108     virtual FileId getFileId(const Control& control, std::string_view path) const = 0;
109     virtual std::pair<IncFsBlockIndex, IncFsBlockIndex> countFilledBlocks(
110             const Control& control, std::string_view path) const = 0;
111     virtual incfs::LoadingState isFileFullyLoaded(const Control& control,
112                                                   std::string_view path) const = 0;
113     virtual incfs::LoadingState isFileFullyLoaded(const Control& control, FileId id) const = 0;
114     virtual incfs::LoadingState isEverythingFullyLoaded(const Control& control) const = 0;
115     virtual ErrorCode link(const Control& control, std::string_view from,
116                            std::string_view to) const = 0;
117     virtual ErrorCode unlink(const Control& control, std::string_view path) const = 0;
118     virtual UniqueFd openForSpecialOps(const Control& control, FileId id) const = 0;
119     virtual ErrorCode writeBlocks(std::span<const incfs::DataBlock> blocks) const = 0;
120     virtual ErrorCode reserveSpace(const Control& control, FileId id, IncFsSize size) const = 0;
121     virtual WaitResult waitForPendingReads(
122             const Control& control, std::chrono::milliseconds timeout,
123             std::vector<incfs::ReadInfoWithUid>* pendingReadsBuffer) const = 0;
124     virtual ErrorCode setUidReadTimeouts(
125             const Control& control,
126             const std::vector<::android::os::incremental::PerUidReadTimeouts>& perUidReadTimeouts)
127             const = 0;
128     virtual ErrorCode forEachFile(const Control& control, FileCallback cb) const = 0;
129     virtual ErrorCode forEachIncompleteFile(const Control& control, FileCallback cb) const = 0;
130     virtual std::optional<Metrics> getMetrics(std::string_view sysfsName) const = 0;
131     virtual std::optional<LastReadError> getLastReadError(const Control& control) const = 0;
132 };
133 
134 class AppOpsManagerWrapper {
135 public:
136     virtual ~AppOpsManagerWrapper() = default;
137     virtual binder::Status checkPermission(const char* permission, const char* operation,
138                                            const char* package) const = 0;
139     virtual void startWatchingMode(int32_t op, const String16& packageName,
140                                    const sp<IAppOpsCallback>& callback) = 0;
141     virtual void stopWatchingMode(const sp<IAppOpsCallback>& callback) = 0;
142 };
143 
144 class JniWrapper {
145 public:
146     virtual ~JniWrapper() = default;
147     virtual void initializeForCurrentThread() const = 0;
148 };
149 
150 class LooperWrapper {
151 public:
152     virtual ~LooperWrapper() = default;
153     virtual int addFd(int fd, int ident, int events, android::Looper_callbackFunc callback,
154                       void* data) = 0;
155     virtual int removeFd(int fd) = 0;
156     virtual void wake() = 0;
157     virtual int pollAll(int timeoutMillis) = 0;
158 };
159 
160 class TimedQueueWrapper {
161 public:
162     virtual ~TimedQueueWrapper() = default;
163     virtual void addJob(MountId id, Milliseconds after, Job what) = 0;
164     virtual void removeJobs(MountId id) = 0;
165     virtual void stop() = 0;
166 };
167 
168 class FsWrapper {
169 public:
170     virtual ~FsWrapper() = default;
171 
172     using FileCallback = android::base::function_ref<bool(std::string_view)>;
173     virtual void listFilesRecursive(std::string_view directoryPath, FileCallback onFile) const = 0;
174 };
175 
176 class ClockWrapper {
177 public:
178     virtual ~ClockWrapper() = default;
179     virtual TimePoint now() const = 0;
180 };
181 
182 class ServiceManagerWrapper {
183 public:
184     virtual ~ServiceManagerWrapper() = default;
185     virtual std::unique_ptr<VoldServiceWrapper> getVoldService() = 0;
186     virtual std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() = 0;
187     virtual std::unique_ptr<IncFsWrapper> getIncFs() = 0;
188     virtual std::unique_ptr<AppOpsManagerWrapper> getAppOpsManager() = 0;
189     virtual std::unique_ptr<JniWrapper> getJni() = 0;
190     virtual std::unique_ptr<LooperWrapper> getLooper() = 0;
191     virtual std::unique_ptr<TimedQueueWrapper> getTimedQueue() = 0;
192     virtual std::unique_ptr<TimedQueueWrapper> getProgressUpdateJobQueue() = 0;
193     virtual std::unique_ptr<FsWrapper> getFs() = 0;
194     virtual std::unique_ptr<ClockWrapper> getClock() = 0;
195 };
196 
197 // --- Real stuff ---
198 
199 class RealServiceManager : public ServiceManagerWrapper {
200 public:
201     RealServiceManager(sp<IServiceManager> serviceManager, JNIEnv* env);
202     ~RealServiceManager() = default;
203     std::unique_ptr<VoldServiceWrapper> getVoldService() final;
204     std::unique_ptr<DataLoaderManagerWrapper> getDataLoaderManager() final;
205     std::unique_ptr<IncFsWrapper> getIncFs() final;
206     std::unique_ptr<AppOpsManagerWrapper> getAppOpsManager() final;
207     std::unique_ptr<JniWrapper> getJni() final;
208     std::unique_ptr<LooperWrapper> getLooper() final;
209     std::unique_ptr<TimedQueueWrapper> getTimedQueue() final;
210     std::unique_ptr<TimedQueueWrapper> getProgressUpdateJobQueue() final;
211     std::unique_ptr<FsWrapper> getFs() final;
212     std::unique_ptr<ClockWrapper> getClock() final;
213 
214 private:
215     template <class INTERFACE>
216     sp<INTERFACE> getRealService(std::string_view serviceName) const;
217     sp<android::IServiceManager> mServiceManager;
218     JavaVM* const mJvm;
219 };
220 
221 } // namespace android::incremental
222