libstorage-ng
Device.h
1 /*
2  * Copyright (c) [2014-2015] Novell, Inc.
3  * Copyright (c) [2016-2023] SUSE LLC
4  *
5  * All Rights Reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of version 2 of the GNU General Public License as published
9  * by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, contact Novell, Inc.
18  *
19  * To contact Novell about this file by physical or electronic mail, you may
20  * find current contact information at www.novell.com.
21  */
22 
23 
24 #ifndef STORAGE_DEVICE_H
25 #define STORAGE_DEVICE_H
26 
27 
28 #include <libxml/tree.h>
29 #include <string>
30 #include <vector>
31 #include <map>
32 #include <memory>
33 #include <boost/noncopyable.hpp>
34 
35 #include "storage/Utils/Exception.h"
36 #include "storage/Utils/Swig.h"
37 #include "storage/View.h"
38 
39 
40 namespace storage
41 {
42  class Devicegraph;
43  class Holder;
44  class ResizeInfo;
45 
46 
48  {
49  public:
50 
51  DeviceHasWrongType(const char* seen, const char* expected);
52  };
53 
54 
67  typedef unsigned int sid_t;
68 
69 
81  class Device : private boost::noncopyable
82  {
83 
84  public:
85 
86  virtual ~Device();
87 
93  sid_t get_sid() const;
94 
95  bool operator==(const Device& rhs) const;
96  bool operator!=(const Device& rhs) const;
97 
110  Device* copy_to_devicegraph(Devicegraph* devicegraph) const;
111 
115  bool exists_in_devicegraph(const Devicegraph* devicegraph) const;
116 
120  bool exists_in_probed() const;
121 
125  bool exists_in_staging() const;
126 
130  bool exists_in_system() const;
131 
132  std::string get_displayname() const;
133 
144 
145  bool has_children() const;
146  size_t num_children() const;
147 
148  bool has_parents() const;
149  size_t num_parents() const;
150 
154  static std::vector<Device*> get_all(Devicegraph* devicegraph);
155 
159  static std::vector<const Device*> get_all(const Devicegraph* devicegraph);
160 
161  // TODO check if we can somehow return a iterator. getting rid of the
162  // ptr would also allow to use references instead of pointer in the
163  // interface.
164 
165  // all function without a view are obsolete (to be replaced by functions with
166  // default view)
167 
168  std::vector<Device*> get_children();
169  std::vector<const Device*> get_children() const;
170 
174  std::vector<Device*> get_children(View view);
175 
179  std::vector<const Device*> get_children(View view) const;
180 
181  std::vector<Device*> get_parents();
182  std::vector<const Device*> get_parents() const;
183 
187  std::vector<Device*> get_parents(View view);
188 
192  std::vector<const Device*> get_parents(View view) const;
193 
194  std::vector<Device*> get_siblings(bool itself);
195  std::vector<const Device*> get_siblings(bool itself) const;
196 
197  std::vector<Device*> get_descendants(bool itself);
198  std::vector<const Device*> get_descendants(bool itself) const;
199 
200  std::vector<Device*> get_descendants(bool itself, View view);
201  std::vector<const Device*> get_descendants(bool itself, View view) const;
202 
203  std::vector<Device*> get_ancestors(bool itself);
204  std::vector<const Device*> get_ancestors(bool itself) const;
205 
206  std::vector<Device*> get_leaves(bool itself);
207  std::vector<const Device*> get_leaves(bool itself) const;
208 
209  std::vector<Device*> get_roots(bool itself);
210  std::vector<const Device*> get_roots(bool itself) const;
211 
212  std::vector<Holder*> get_in_holders();
213  std::vector<const Holder*> get_in_holders() const;
214 
215  std::vector<Holder*> get_out_holders();
216  std::vector<const Holder*> get_out_holders() const;
217 
218  void remove_descendants() ST_DEPRECATED;
219 
224  void remove_descendants(View view);
225 
229  const std::map<std::string, std::string>& get_userdata() const;
230 
234  void set_userdata(const std::map<std::string, std::string>& userdata);
235 
236  friend std::ostream& operator<<(std::ostream& out, const Device& device);
237 
243  static bool compare_by_sid(const Device* lhs, const Device* rhs);
244 
262  static bool compare_by_name(const Device* lhs, const Device* rhs);
263 
282  std::string get_name_sort_key() const;
283 
288 
292  const Devicegraph* get_devicegraph() const;
293 
294  public:
295 
296  class Impl;
297 
298  Impl& get_impl() { return *impl; }
299  const Impl& get_impl() const { return *impl; }
300 
301  virtual Device* clone() const = 0;
302 
303  void save(xmlNode* node) const ST_DEPRECATED;
304 
305  protected:
306 
307  Device(Impl* impl);
308 
314  void create(Devicegraph* devicegraph) ST_DEPRECATED;
315 
316  void load(Devicegraph* devicegraph) ST_DEPRECATED;
317 
318  private:
319 
320  void add_to_devicegraph(Devicegraph* devicegraph) ST_DEPRECATED;
321 
322  const std::unique_ptr<Impl> impl;
323 
324  };
325 
326 }
327 
328 #endif
static std::vector< Device * > get_all(Devicegraph *devicegraph)
Get all Devices.
ResizeInfo detect_resize_info() const
Detect the resize info of the device.
void create(Devicegraph *devicegraph) ST_DEPRECATED
Create a device in the devicegraph.
bool exists_in_probed() const
Checks if the device exists in the probed devicegraph.
std::string get_name_sort_key() const
Get a sort-key based on the device name.
The main container of the libstorage-ng.
Definition: Devicegraph.h:169
static bool compare_by_sid(const Device *lhs, const Device *rhs)
Compare (less than) two Devices by sid.
bool exists_in_staging() const
Checks if the device exists in the staging devicegraph.
Device * copy_to_devicegraph(Devicegraph *devicegraph) const
Copies the device to the devicegraph.
An abstract base class for storage devices.
Definition: Device.h:81
bool exists_in_system() const
Checks if the device exists in the system devicegraph.
const std::map< std::string, std::string > & get_userdata() const
Return the userdata of the device.
Definition: FreeInfo.h:198
Devicegraph * get_devicegraph()
Return the devicegraph the device belongs to.
sid_t get_sid() const
Return the storage id (sid) of the device.
static bool compare_by_name(const Device *lhs, const Device *rhs)
Compare (less than) two Devices by name.
Base class for storage exceptions.
Definition: Exception.h:113
The storage namespace.
Definition: Actiongraph.h:39
unsigned int sid_t
An integer storage ID.
Definition: Device.h:67
bool exists_in_devicegraph(const Devicegraph *devicegraph) const
Checks if the device exists in the devicegraph.
void set_userdata(const std::map< std::string, std::string > &userdata)
Set the userdata of the device.
Definition: Device.h:47
View
Enum with possible views on the devicegraph.
Definition: View.h:33