xref: /aosp_15_r20/system/apex/apexd/apexd_dm.h (revision 33f3758387333dbd2962d7edbd98681940d895da)
1 /*
2  * Copyright (C) 2024 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 #pragma once
18 
19 #include <android-base/result.h>
20 #include <libdm/dm.h>
21 
22 #include <string>
23 
24 namespace android::apex {
25 
26 class DmDevice {
27  public:
DmDevice()28   DmDevice() : cleared_(true) {}
DmDevice(const std::string & name)29   explicit DmDevice(const std::string& name) : name_(name), cleared_(false) {}
DmDevice(const std::string & name,const std::string & dev_path)30   DmDevice(const std::string& name, const std::string& dev_path)
31       : name_(name), dev_path_(dev_path), cleared_(false) {}
32 
DmDevice(DmDevice && other)33   DmDevice(DmDevice&& other) noexcept
34       : name_(std::move(other.name_)),
35         dev_path_(std::move(other.dev_path_)),
36         cleared_(other.cleared_) {
37     other.cleared_ = true;
38   }
39 
40   DmDevice& operator=(DmDevice&& other) noexcept {
41     name_ = other.name_;
42     dev_path_ = other.dev_path_;
43     cleared_ = other.cleared_;
44     other.cleared_ = true;
45     return *this;
46   }
47 
48   ~DmDevice();
49 
GetName()50   const std::string& GetName() const { return name_; }
GetDevPath()51   const std::string& GetDevPath() const { return dev_path_; }
52 
Release()53   void Release() { cleared_ = true; }
54 
55  private:
56   std::string name_;
57   std::string dev_path_;
58   bool cleared_;
59 };
60 
61 base::Result<DmDevice> CreateDmDevice(const std::string& name,
62                                       const dm::DmTable& table,
63                                       bool reuse_device);
64 
65 base::Result<void> DeleteDmDevice(const std::string& name, bool deferred);
66 
67 }  // namespace android::apex
68