1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
5    Copyright (C) 2011-2012 Planets Communications B.V.
6    Copyright (C) 2013-2019 Bareos GmbH & Co. KG
7 
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12 
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    Affero General Public License for more details.
17 
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22 */
23 
24 #ifndef BAREOS_LIB_RESOURCE_ITEM_H_
25 #define BAREOS_LIB_RESOURCE_ITEM_H_
26 
27 struct s_password;
28 class alist;
29 class dlist;
30 
31 /*
32  * This is the structure that defines the record types (items) permitted within
33  * each resource. It is used to define the configuration tables.
34  */
35 struct ResourceItem {
36   const char* name; /* Resource name i.e. Director, ... */
37   const int type;
38   // union {
39   //  char** value; /* Where to store the item */
40   //  std::string* strValue;
41   //  uint16_t* ui16value;
42   //  uint32_t* ui32value;
43   //  int16_t* i16value;
44   //  int32_t* i32value;
45   //  uint64_t* ui64value;
46   //  int64_t* i64value;
47   //  bool* boolvalue;
48   //  utime_t* utimevalue;
49   //  s_password* pwdvalue;
50   //  BareosResource** resvalue;
51   //  alist** alistvalue;
52   //  dlist** dlistvalue;
53   //  char* bitvalue;
54   //  std::vector<std::string>* std_vector_of_strings;
55   //};
56   std::size_t offset;
57   BareosResource** allocated_resource;
58   int32_t code;              /* Item code/additional info */
59   uint32_t flags;            /* Flags: See CFG_ITEM_* */
60   const char* default_value; /* Default value */
61   /*
62    * version string in format: [start_version]-[end_version]
63    * start_version: directive has been introduced in this version
64    * end_version:   directive is deprecated since this version
65    */
66   const char* versions;
67   /*
68    * description of the directive, used for the documentation.
69    * Full sentence.
70    * Every new directive should have a description.
71    */
72   const char* description;
73 };
74 
CalculateAddressOfMemberVariable(const ResourceItem & item)75 static inline void* CalculateAddressOfMemberVariable(const ResourceItem& item)
76 {
77   char* base = reinterpret_cast<char*>(*item.allocated_resource);
78   return static_cast<void*>(base + item.offset);
79 }
80 
81 template <typename P>
GetItemVariable(const ResourceItem & item)82 P GetItemVariable(const ResourceItem& item)
83 {
84   void* p = CalculateAddressOfMemberVariable(item);
85   return *(static_cast<typename std::remove_reference<P>::type*>(p));
86 }
87 
88 template <typename P>
GetItemVariablePointer(const ResourceItem & item)89 P GetItemVariablePointer(const ResourceItem& item)
90 {
91   void* p = CalculateAddressOfMemberVariable(item);
92   return static_cast<P>(p);
93 }
94 
95 template <typename P, typename V>
SetItemVariable(const ResourceItem & item,const V & value)96 void SetItemVariable(const ResourceItem& item, const V& value)
97 {
98   P* p = GetItemVariablePointer<P*>(item);
99   *p = value;
100 }
101 
102 template <typename P, typename V>
SetItemVariableFreeMemory(const ResourceItem & item,const V & value)103 void SetItemVariableFreeMemory(const ResourceItem& item, const V& value)
104 {
105   void* p = GetItemVariablePointer<void*>(item);
106   P** q = (P**)p;
107   if (*q) free(*q);
108   (*(P**)p) = (P*)value;
109 }
110 
111 #endif /* BAREOS_LIB_RESOURCE_ITEM_H_ */
112