1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2000-2011 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 #include "stored/device_resource.h"
25 #include "stored/stored_globals.h"
26 
27 namespace storagedaemon {
28 
DeviceResource()29 DeviceResource::DeviceResource()
30     : BareosResource()
31     , media_type(nullptr)
32     , device_name(nullptr)
33     , device_options(nullptr)
34     , diag_device_name(nullptr)
35     , changer_name(nullptr)
36     , changer_command(nullptr)
37     , alert_command(nullptr)
38     , spool_directory(nullptr)
39     , dev_type(B_UNKNOWN_DEV)
40     , label_type(B_BAREOS_LABEL)
41     , autoselect(true)
42     , norewindonclose(true)
43     , drive_tapealert_enabled(false)
44     , drive_crypto_enabled(false)
45     , query_crypto_status(false)
46     , collectstats(false)
47     , eof_on_error_is_eot(false)
48     , drive(0)
49     , drive_index(0)
50     , cap_bits{0}
51     , max_changer_wait(300)
52     , max_rewind_wait(300)
53     , max_open_wait(300)
54     , max_open_vols(1)
55     , label_block_size(64512)
56     , min_block_size(0)
57     , max_block_size(0)
58     , max_network_buffer_size(0)
59     , max_concurrent_jobs(0)
60     , autodeflate_algorithm(0)
61     , autodeflate_level(6)
62     , autodeflate(0)
63     , autoinflate(0)
64     , vol_poll_interval(300)
65     , max_volume_size(0)
66     , max_file_size(1000000000)
67     , volume_capacity(0)
68     , max_spool_size(0)
69     , max_job_spool_size(0)
70 
71     , max_part_size(0)
72     , mount_point(nullptr)
73     , mount_command(nullptr)
74     , unmount_command(nullptr)
75     , write_part_command(nullptr)
76     , free_space_command(nullptr)
77     , count(1)
78     , multiplied_device_resource(nullptr)
79 
80     , dev(nullptr)
81     , changer_res(nullptr)
82 
83     /* private: */
84     , temporarily_swapped_numbered_name(nullptr)
85 {
86   return;
87 }
88 
DeviceResource(const DeviceResource & other)89 DeviceResource::DeviceResource(const DeviceResource& other)
90     : BareosResource(other)
91     , media_type(nullptr)
92     , device_name(nullptr)
93     , device_options(nullptr)
94     , diag_device_name(nullptr)
95     , changer_name(nullptr)
96     , changer_command(nullptr)
97     , alert_command(nullptr)
98     , spool_directory(nullptr)
99     , mount_point(nullptr)
100     , mount_command(nullptr)
101     , unmount_command(nullptr)
102     , write_part_command(nullptr)
103     , free_space_command(nullptr)
104     , temporarily_swapped_numbered_name(nullptr) /* should not copy */
105 {
106   if (other.media_type) { media_type = strdup(other.media_type); }
107   if (other.device_name) { device_name = strdup(other.device_name); }
108   if (other.device_options) { device_options = strdup(other.device_options); }
109   if (other.diag_device_name) {
110     diag_device_name = strdup(other.diag_device_name);
111   }
112   if (other.changer_name) { changer_name = strdup(other.changer_name); }
113   if (other.changer_command) {
114     changer_command = strdup(other.changer_command);
115   }
116   if (other.alert_command) { alert_command = strdup(other.alert_command); }
117   if (other.spool_directory) {
118     spool_directory = strdup(other.spool_directory);
119   }
120   dev_type = other.dev_type;
121   label_type = other.label_type;
122   autoselect = other.autoselect;
123   norewindonclose = other.norewindonclose;
124   drive_tapealert_enabled = other.drive_tapealert_enabled;
125   drive_crypto_enabled = other.drive_crypto_enabled;
126   query_crypto_status = other.query_crypto_status;
127   collectstats = other.collectstats;
128   eof_on_error_is_eot = other.eof_on_error_is_eot;
129   drive = other.drive;
130   drive_index = other.drive_index;
131   memcpy(cap_bits, other.cap_bits, CAP_BYTES);
132   max_changer_wait = other.max_changer_wait;
133   max_rewind_wait = other.max_rewind_wait;
134   max_open_wait = other.max_open_wait;
135   max_open_vols = other.max_open_vols;
136   label_block_size = other.label_block_size;
137   min_block_size = other.min_block_size;
138   max_block_size = other.max_block_size;
139   max_network_buffer_size = other.max_network_buffer_size;
140   max_concurrent_jobs = other.max_concurrent_jobs;
141   autodeflate_algorithm = other.autodeflate_algorithm;
142   autodeflate_level = other.autodeflate_level;
143   autodeflate = other.autodeflate;
144   autoinflate = other.autoinflate;
145   vol_poll_interval = other.vol_poll_interval;
146   max_volume_size = other.max_volume_size;
147   max_file_size = other.max_file_size;
148   volume_capacity = other.volume_capacity;
149   max_spool_size = other.max_spool_size;
150   max_job_spool_size = other.max_job_spool_size;
151 
152   max_part_size = other.max_part_size;
153   if (other.mount_point) { mount_point = strdup(other.mount_point); }
154   if (other.mount_command) { mount_command = strdup(other.mount_command); }
155   if (other.unmount_command) {
156     unmount_command = strdup(other.unmount_command);
157   }
158   if (other.write_part_command) {
159     write_part_command = strdup(other.write_part_command);
160   }
161   if (other.free_space_command) {
162     free_space_command = strdup(other.free_space_command);
163   }
164   count = other.count;
165   multiplied_device_resource = other.multiplied_device_resource;
166   multiplied_device_resource_base_name =
167       other.multiplied_device_resource_base_name;
168   dev = other.dev;
169   changer_res = other.changer_res;
170 }
171 
operator =(const DeviceResource & rhs)172 DeviceResource& DeviceResource::operator=(const DeviceResource& rhs)
173 {
174   BareosResource::operator=(rhs);
175   media_type = rhs.media_type;
176   device_name = rhs.device_name;
177   device_options = rhs.device_options;
178   diag_device_name = rhs.diag_device_name;
179   changer_name = rhs.changer_name;
180   changer_command = rhs.changer_command;
181   alert_command = rhs.alert_command;
182   spool_directory = rhs.spool_directory;
183   dev_type = rhs.dev_type;
184   label_type = rhs.label_type;
185   autoselect = rhs.autoselect;
186   norewindonclose = rhs.norewindonclose;
187   drive_tapealert_enabled = rhs.drive_tapealert_enabled;
188   drive_crypto_enabled = rhs.drive_crypto_enabled;
189   query_crypto_status = rhs.query_crypto_status;
190   collectstats = rhs.collectstats;
191   eof_on_error_is_eot = rhs.eof_on_error_is_eot;
192   drive = rhs.drive;
193   drive_index = rhs.drive_index;
194   memcpy(cap_bits, rhs.cap_bits, CAP_BYTES);
195   max_changer_wait = rhs.max_changer_wait;
196   max_rewind_wait = rhs.max_rewind_wait;
197   max_open_wait = rhs.max_open_wait;
198   max_open_vols = rhs.max_open_vols;
199   label_block_size = rhs.label_block_size;
200   min_block_size = rhs.min_block_size;
201   max_block_size = rhs.max_block_size;
202   max_network_buffer_size = rhs.max_network_buffer_size;
203   max_concurrent_jobs = rhs.max_concurrent_jobs;
204   autodeflate_algorithm = rhs.autodeflate_algorithm;
205   autodeflate_level = rhs.autodeflate_level;
206   autodeflate = rhs.autodeflate;
207   autoinflate = rhs.autoinflate;
208   vol_poll_interval = rhs.vol_poll_interval;
209   max_volume_size = rhs.max_volume_size;
210   max_file_size = rhs.max_file_size;
211   volume_capacity = rhs.volume_capacity;
212   max_spool_size = rhs.max_spool_size;
213   max_job_spool_size = rhs.max_job_spool_size;
214 
215   max_part_size = rhs.max_part_size;
216   mount_point = rhs.mount_point;
217   mount_command = rhs.mount_command;
218   unmount_command = rhs.unmount_command;
219   write_part_command = rhs.write_part_command;
220   free_space_command = rhs.free_space_command;
221   count = rhs.count;
222   multiplied_device_resource = rhs.multiplied_device_resource;
223   multiplied_device_resource_base_name =
224       rhs.multiplied_device_resource_base_name;
225   dev = rhs.dev;
226   changer_res = rhs.changer_res;
227   temporarily_swapped_numbered_name = rhs.temporarily_swapped_numbered_name;
228 
229   return *this;
230 }
231 
PrintConfig(PoolMem & buf,const ConfigurationParser &,bool hide_sensitive_data,bool verbose)232 bool DeviceResource::PrintConfig(PoolMem& buf,
233                                  const ConfigurationParser& /* unused */,
234                                  bool hide_sensitive_data,
235                                  bool verbose)
236 {
237   if (multiplied_device_resource) {
238     if (multiplied_device_resource == this) {
239       MultipliedDeviceRestoreBaseName();
240       BareosResource::PrintConfig(buf, *my_config);
241       MultipliedDeviceRestoreNumberedName();
242     } else {
243       /* do not print the multiplied devices */
244       return false;
245     }
246   } else {
247     BareosResource::PrintConfig(buf, *my_config);
248   }
249   return true;
250 }
251 
MultipliedDeviceRestoreBaseName()252 void DeviceResource::MultipliedDeviceRestoreBaseName()
253 {
254   temporarily_swapped_numbered_name = resource_name_;
255   resource_name_ =
256       const_cast<char*>(multiplied_device_resource_base_name.c_str());
257 }
258 
MultipliedDeviceRestoreNumberedName()259 void DeviceResource::MultipliedDeviceRestoreNumberedName()
260 {
261   /* call MultipliedDeviceRestoreBaseName() before */
262   ASSERT(temporarily_swapped_numbered_name);
263 
264   resource_name_ = temporarily_swapped_numbered_name;
265   temporarily_swapped_numbered_name = nullptr;
266 }
267 
CreateAndAssignSerialNumber(uint16_t number)268 void DeviceResource::CreateAndAssignSerialNumber(uint16_t number)
269 {
270   if (multiplied_device_resource_base_name.empty()) {
271     /* save the original name which is
272      * the base name for multiplied devices */
273     multiplied_device_resource_base_name = resource_name_;
274   }
275 
276   std::string tmp_name = multiplied_device_resource_base_name;
277 
278   char b[4 + 1];
279   ::sprintf(b, "%04d", number < 10000 ? number : 9999);
280   tmp_name += b;
281 
282   free(resource_name_);
283   resource_name_ = strdup(tmp_name.c_str());
284 }
285 
286 
287 } /* namespace storagedaemon  */
288