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 24 namespace OHOS { 25 namespace SysInstaller { 26 namespace Loop { 27 class LoopbackDeviceUniqueFd { 28 public: 29 UniqueFd deviceFd; 30 std::string name; 31 LoopbackDeviceUniqueFd()32 LoopbackDeviceUniqueFd() {} LoopbackDeviceUniqueFd(UniqueFd && fd,const std::string & name)33 LoopbackDeviceUniqueFd(UniqueFd &&fd, const std::string &name) 34 : deviceFd(std::move(fd)), name(name) {} 35 LoopbackDeviceUniqueFd(LoopbackDeviceUniqueFd && fd)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 ~LoopbackDeviceUniqueFd()46 ~LoopbackDeviceUniqueFd() 47 { 48 MaybeCloseBad(); 49 } 50 51 void MaybeCloseBad() const; 52 CloseGood()53 void CloseGood() 54 { 55 deviceFd.Release(); 56 } 57 Get()58 int Get() const 59 { 60 return deviceFd.Get(); 61 } 62 }; 63 bool ConfigureReadAhead(const std::string &devicePath); 64 bool PreAllocateLoopDevices(const size_t num); 65 std::unique_ptr<LoopbackDeviceUniqueFd> CreateLoopDevice( 66 const std::string &target, const uint32_t imageOffset, const uint32_t imageSize); 67 bool RemoveDmLoopDevice(const std::string &mountPoint, const std::string &imagePath); 68 bool RemoveDmLoopDevice(const std::string &loopDevPath); 69 bool IsLoopDevMatchedImg(const std::string &loopPath, const std::string &imgFilePath); 70 bool CloseLoopDev(const std::string &loopPath); 71 bool ClearDmLoopDevice(const std::string &loopDevPath, const bool clearDm); 72 } // Loop 73 } // SysInstaller 74 } // namespace OHOS 75 #endif // SYS_INSTALLER_MODULE_LOOP_H