1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#ifndef SYS_INSTALLER_MODULE_LOOP_H 17#define SYS_INSTALLER_MODULE_LOOP_H 18 19#include <memory> 20#include <string> 21 22#include "unique_fd.h" 23 24namespace OHOS { 25namespace SysInstaller { 26namespace Loop { 27class LoopbackDeviceUniqueFd { 28public: 29 UniqueFd deviceFd; 30 std::string name; 31 32 LoopbackDeviceUniqueFd() {} 33 LoopbackDeviceUniqueFd(UniqueFd &&fd, const std::string &name) 34 : deviceFd(std::move(fd)), name(name) {} 35 36 LoopbackDeviceUniqueFd(LoopbackDeviceUniqueFd &&fd) noexcept 37 : deviceFd(std::move(fd.deviceFd)), name(std::move(fd.name)) {} 38 LoopbackDeviceUniqueFd &operator=(LoopbackDeviceUniqueFd &&other) noexcept 39 { 40 MaybeCloseBad(); 41 deviceFd = std::move(other.deviceFd); 42 name = std::move(other.name); 43 return *this; 44 } 45 46 ~LoopbackDeviceUniqueFd() 47 { 48 MaybeCloseBad(); 49 } 50 51 void MaybeCloseBad() const; 52 53 void CloseGood() 54 { 55 deviceFd.Release(); 56 } 57 58 int Get() const 59 { 60 return deviceFd.Get(); 61 } 62}; 63bool ConfigureReadAhead(const std::string &devicePath); 64bool PreAllocateLoopDevices(const size_t num); 65std::unique_ptr<LoopbackDeviceUniqueFd> CreateLoopDevice( 66 const std::string &target, const uint32_t imageOffset, const uint32_t imageSize); 67bool RemoveDmLoopDevice(const std::string &mountPoint, const std::string &imagePath); 68bool RemoveDmLoopDevice(const std::string &loopDevPath); 69bool IsLoopDevMatchedImg(const std::string &loopPath, const std::string &imgFilePath); 70bool CloseLoopDev(const std::string &loopPath); 71bool ClearDmLoopDevice(const std::string &loopDevPath, const bool clearDm); 72} // Loop 73} // SysInstaller 74} // namespace OHOS 75#endif // SYS_INSTALLER_MODULE_LOOP_H