• 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 "status_or.h"
21 
22 #include <android-base/unique_fd.h>
23 
24 #include <functional>
25 #include <string>
26 
27 namespace android {
28 namespace apex {
29 namespace loop {
30 
31 using android::base::unique_fd;
32 
33 struct LoopbackDeviceUniqueFd {
34   unique_fd device_fd;
35   std::string name;
36 
LoopbackDeviceUniqueFdLoopbackDeviceUniqueFd37   LoopbackDeviceUniqueFd() {}
LoopbackDeviceUniqueFdLoopbackDeviceUniqueFd38   LoopbackDeviceUniqueFd(unique_fd&& fd, const std::string& name)
39       : device_fd(std::move(fd)), name(name) {}
40 
LoopbackDeviceUniqueFdLoopbackDeviceUniqueFd41   LoopbackDeviceUniqueFd(LoopbackDeviceUniqueFd&& fd) noexcept
42       : device_fd(std::move(fd.device_fd)), name(fd.name) {}
43   LoopbackDeviceUniqueFd& operator=(LoopbackDeviceUniqueFd&& other) noexcept {
44     MaybeCloseBad();
45     device_fd = std::move(other.device_fd);
46     name = std::move(other.name);
47     return *this;
48   }
49 
~LoopbackDeviceUniqueFdLoopbackDeviceUniqueFd50   ~LoopbackDeviceUniqueFd() { MaybeCloseBad(); }
51 
52   void MaybeCloseBad();
53 
CloseGoodLoopbackDeviceUniqueFd54   void CloseGood() { device_fd.reset(-1); }
55 
getLoopbackDeviceUniqueFd56   int get() { return device_fd.get(); }
57 };
58 
59 Status configureReadAhead(const std::string& device_path);
60 
61 Status preAllocateLoopDevices(size_t num);
62 
63 StatusOr<LoopbackDeviceUniqueFd> createLoopDevice(const std::string& target,
64                                                   const int32_t imageOffset,
65                                                   const size_t imageSize);
66 
67 using DestroyLoopFn =
68     std::function<void(const std::string&, const std::string&)>;
69 void DestroyLoopDevice(const std::string& path, const DestroyLoopFn& extra);
70 
71 }  // namespace loop
72 }  // namespace apex
73 }  // namespace android
74 
75 #endif  // ANDROID_APEXD_APEXD_LOOP_H_
76