blob: 8341675d015a60dabd452bf156ec167352b1afc7 [file] [log] [blame]
Andreas Gampe225e1b02019-01-15 14:53:24 -08001/*
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
Mohammad Samiul Islam159ea8e2019-06-20 15:55:27 +010020#include <android-base/result.h>
Andreas Gampe225e1b02019-01-15 14:53:24 -080021#include <android-base/unique_fd.h>
22
23#include <functional>
24#include <string>
25
26namespace android {
27namespace apex {
28namespace loop {
29
30using android::base::unique_fd;
31
32struct LoopbackDeviceUniqueFd {
33 unique_fd device_fd;
34 std::string name;
35
36 LoopbackDeviceUniqueFd() {}
37 LoopbackDeviceUniqueFd(unique_fd&& fd, const std::string& name)
38 : device_fd(std::move(fd)), name(name) {}
39
40 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
49 ~LoopbackDeviceUniqueFd() { MaybeCloseBad(); }
50
51 void MaybeCloseBad();
52
53 void CloseGood() { device_fd.reset(-1); }
54
55 int get() { return device_fd.get(); }
56};
57
Mohammad Samiul Islam159ea8e2019-06-20 15:55:27 +010058android::base::Result<void> configureReadAhead(const std::string& device_path);
Andreas Gampe225e1b02019-01-15 14:53:24 -080059
Mohammad Samiul Islam159ea8e2019-06-20 15:55:27 +010060android::base::Result<void> preAllocateLoopDevices(size_t num);
Jiyong Park4d0f8322019-02-02 19:45:57 +090061
Mohammad Samiul Islam159ea8e2019-06-20 15:55:27 +010062android::base::Result<LoopbackDeviceUniqueFd> createLoopDevice(
63 const std::string& target, const int32_t imageOffset,
64 const size_t imageSize);
Andreas Gampe225e1b02019-01-15 14:53:24 -080065
66using DestroyLoopFn =
67 std::function<void(const std::string&, const std::string&)>;
Nikita Ioffe6bea4e52019-02-10 22:46:05 +000068void DestroyLoopDevice(const std::string& path, const DestroyLoopFn& extra);
Andreas Gampe225e1b02019-01-15 14:53:24 -080069
Andreas Gampe225e1b02019-01-15 14:53:24 -080070} // namespace loop
71} // namespace apex
72} // namespace android
73
74#endif // ANDROID_APEXD_APEXD_LOOP_H_