Andreas Gampe | 225e1b0 | 2019-01-15 14:53:24 -0800 | [diff] [blame] | 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 | |
| 37 | LoopbackDeviceUniqueFd() {} |
| 38 | LoopbackDeviceUniqueFd(unique_fd&& fd, const std::string& name) |
| 39 | : device_fd(std::move(fd)), name(name) {} |
| 40 | |
| 41 | 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 | |
| 50 | ~LoopbackDeviceUniqueFd() { MaybeCloseBad(); } |
| 51 | |
| 52 | void MaybeCloseBad(); |
| 53 | |
| 54 | void CloseGood() { device_fd.reset(-1); } |
| 55 | |
| 56 | int get() { return device_fd.get(); } |
| 57 | }; |
| 58 | |
| 59 | Status configureReadAhead(const std::string& device_path); |
| 60 | |
Jooyung Han | eeafec4 | 2019-03-21 01:54:16 +0900 | [diff] [blame] | 61 | Status preAllocateLoopDevices(size_t num); |
Jiyong Park | 4d0f832 | 2019-02-02 19:45:57 +0900 | [diff] [blame] | 62 | |
Andreas Gampe | 225e1b0 | 2019-01-15 14:53:24 -0800 | [diff] [blame] | 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&)>; |
Nikita Ioffe | 6bea4e5 | 2019-02-10 22:46:05 +0000 | [diff] [blame] | 69 | void DestroyLoopDevice(const std::string& path, const DestroyLoopFn& extra); |
Andreas Gampe | 225e1b0 | 2019-01-15 14:53:24 -0800 | [diff] [blame] | 70 | |
Andreas Gampe | 225e1b0 | 2019-01-15 14:53:24 -0800 | [diff] [blame] | 71 | } // namespace loop |
| 72 | } // namespace apex |
| 73 | } // namespace android |
| 74 | |
| 75 | #endif // ANDROID_APEXD_APEXD_LOOP_H_ |