• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #ifndef ANDROID_APEXD_APEXD_LOOP_H_
18 #define ANDROID_APEXD_APEXD_LOOP_H_
19 
20 #include <android-base/result.h>
21 #include <android-base/unique_fd.h>
22 
23 #include <functional>
24 #include <string>
25 
26 namespace android {
27 namespace apex {
28 namespace loop {
29 
30 using android::base::unique_fd;
31 
32 struct LoopbackDeviceUniqueFd {
33   unique_fd device_fd;
34   std::string name;
35 
LoopbackDeviceUniqueFdLoopbackDeviceUniqueFd36   LoopbackDeviceUniqueFd() {}
LoopbackDeviceUniqueFdLoopbackDeviceUniqueFd37   LoopbackDeviceUniqueFd(unique_fd&& fd, const std::string& name)
38       : device_fd(std::move(fd)), name(name) {}
39 
LoopbackDeviceUniqueFdLoopbackDeviceUniqueFd40   LoopbackDeviceUniqueFd(LoopbackDeviceUniqueFd&& fd) noexcept
41       : device_fd(std::move(fd.device_fd)), name(fd.name) {}
42   LoopbackDeviceUniqueFd& operator=(LoopbackDeviceUniqueFd&& other) noexcept {
43     MaybeCloseBad();
44     device_fd = std::move(other.device_fd);
45     name = std::move(other.name);
46     return *this;
47   }
48 
~LoopbackDeviceUniqueFdLoopbackDeviceUniqueFd49   ~LoopbackDeviceUniqueFd() { MaybeCloseBad(); }
50 
51   void MaybeCloseBad();
52 
CloseGoodLoopbackDeviceUniqueFd53   void CloseGood() { device_fd.reset(-1); }
54 
getLoopbackDeviceUniqueFd55   int get() { return device_fd.get(); }
56 };
57 
58 android::base::Result<void> configureReadAhead(const std::string& device_path);
59 
60 android::base::Result<void> preAllocateLoopDevices(size_t num);
61 
62 android::base::Result<LoopbackDeviceUniqueFd> createLoopDevice(
63     const std::string& target, const int32_t imageOffset,
64     const size_t imageSize);
65 
66 using DestroyLoopFn =
67     std::function<void(const std::string&, const std::string&)>;
68 void DestroyLoopDevice(const std::string& path, const DestroyLoopFn& extra);
69 
70 }  // namespace loop
71 }  // namespace apex
72 }  // namespace android
73 
74 #endif  // ANDROID_APEXD_APEXD_LOOP_H_
75