blob: 10eecc9b8239964b87d3451c217f29da1b103192 [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
20#include "status_or.h"
21
22#include <android-base/unique_fd.h>
23
24#include <functional>
25#include <string>
26
27namespace android {
28namespace apex {
29namespace loop {
30
31using android::base::unique_fd;
32
33struct 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
59Status configureReadAhead(const std::string& device_path);
60
Jooyung Haneeafec42019-03-21 01:54:16 +090061Status preAllocateLoopDevices(size_t num);
Jiyong Park4d0f8322019-02-02 19:45:57 +090062
Andreas Gampe225e1b02019-01-15 14:53:24 -080063StatusOr<LoopbackDeviceUniqueFd> createLoopDevice(const std::string& target,
64 const int32_t imageOffset,
65 const size_t imageSize);
66
67using DestroyLoopFn =
68 std::function<void(const std::string&, const std::string&)>;
Nikita Ioffe6bea4e52019-02-10 22:46:05 +000069void DestroyLoopDevice(const std::string& path, const DestroyLoopFn& extra);
Andreas Gampe225e1b02019-01-15 14:53:24 -080070
Andreas Gampe225e1b02019-01-15 14:53:24 -080071} // namespace loop
72} // namespace apex
73} // namespace android
74
75#endif // ANDROID_APEXD_APEXD_LOOP_H_