1 karl 1.20 //%2006////////////////////////////////////////////////////////////////////////
|
2 mike 1.2 //
|
3 karl 1.17 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
|
6 karl 1.13 // IBM Corp.; EMC Corporation, The Open Group.
|
7 karl 1.17 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
|
9 karl 1.18 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
|
11 karl 1.20 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
|
13 mike 1.2 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
|
20 kumpf 1.6 //
|
21 mike 1.2 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 // Author: Nag Boranna (nagaraja_boranna@hp.com)
33 //
34 // Modified By: Yi Zhou (yi_zhou@hp.com)
|
35 kumpf 1.3 // : Sushma Fernandes (sushma_fernandes@hp.com)
|
36 kumpf 1.9 // Carol Ann Krug Graves, Hewlett-Packard Company
37 // (carolann_graves@hp.com)
|
38 vijay.eli 1.19 // Vijay Eli, IBM, (vijayeli@in.ibm.com), bug#3609.
|
39 mike 1.2 //
40 //%/////////////////////////////////////////////////////////////////////////////
41
42
43 #include <fstream>
|
44 kumpf 1.12 #include <errno.h>
|
45 mike 1.2 #include <Pegasus/Common/FileSystem.h>
46 #include <Pegasus/Common/HashTable.h>
|
47 kumpf 1.5 #include <Pegasus/Common/Tracer.h>
|
48 mike 1.2 #include "ConfigFileHandler.h"
|
49 kumpf 1.3 #include "ConfigManager.h"
|
50 david 1.10 #if defined(PEGASUS_OS_OS400)
51 #include "OS400ConvertChar.h"
52 #endif
|
53 mike 1.2
54
55 PEGASUS_USING_STD;
56
57 PEGASUS_NAMESPACE_BEGIN
58
59 ////////////////////////////////////////////////////////////////////////////////
60 //
61 // ConfigFileHandler Class
62 //
63 ////////////////////////////////////////////////////////////////////////////////
64
65
66 ////////////////////////////////////////////////////////////////////////////////
67 // ConfigTable
68 ////////////////////////////////////////////////////////////////////////////////
69
70 typedef HashTable<String, String, EqualFunc<String>, HashFunc<String> > Table;
71
72 struct ConfigTable
73 {
74 mike 1.2 Table table;
75 };
76
77
78 /**
79 Constructor.
80 */
81 ConfigFileHandler::ConfigFileHandler (
82 const String& currentFile,
83 const String& plannedFile,
84 const Boolean offLine)
85 : _offLine(offLine)
86 {
|
87 kumpf 1.3 String cFile;
88 String pFile;
|
89 mike 1.2
|
90 kumpf 1.3 //
91 // Set the current and planned config files
92 //
93 cFile = ConfigManager::getHomedPath(currentFile);
94
95 pFile = ConfigManager::getHomedPath(plannedFile);
|
96 mike 1.2
97 //
98 // Initialize instance variables.
99 //
|
100 a.arora 1.16
|
101 mike 1.2 _currentFileExist = true;
102 _plannedFileExist = true;
103
104
|
105 a.arora 1.16 _currentConfFile.reset(new ConfigFile(cFile));
106 _plannedConfFile.reset(new ConfigFile(pFile));
|
107 mike 1.2
108 _currentConfig = new ConfigTable;
109 _plannedConfig = new ConfigTable;
110
111 //
112 // check whether the planned file exists or not
113 //
114 if (!FileSystem::exists(pFile))
115 {
116 _plannedFileExist = false;
117 return;
118 }
119
120 //
121 // check whether the file is readable or not
122 //
123 if (!FileSystem::canRead(pFile))
124 {
125 throw FileNotReadable(pFile);
126 }
127
128 mike 1.2 //
129 // check whether the current file exists or not
130 //
131 if (!FileSystem::exists(cFile))
132 {
133 _currentFileExist = false;
134 //
135 // Current file need not exist.
136 // try creating one so that planned file contents
137 // can be copied over.
138 //
|
139 david 1.10 #if defined(PEGASUS_OS_OS400)
|
140 david 1.15 ofstream ofs(cFile.getCString(), PEGASUS_STD(_CCSID_T(1208)));
|
141 david 1.10 #else
|
142 david 1.15 ofstream ofs(cFile.getCString());
|
143 david 1.10 #endif
|
144 mike 1.2 if (!ofs)
145 {
|
146 kumpf 1.12 // unable to create file
147 PEG_TRACE_STRING(TRC_CONFIG, Tracer::LEVEL4,
148 "Failed to create config file: " + cFile + ", " + strerror(errno));
|
149 mike 1.2 throw NoSuchFile(cFile);
150 }
151 ofs.close();
152 }
153
154 //
155 // check whether the file is readable or not
156 //
157 if (!FileSystem::canRead(cFile))
158 {
159 throw FileNotReadable(cFile);
160 }
161
162 }
163
164 /**
165 Destructor.
166 */
167 ConfigFileHandler::~ConfigFileHandler ()
168 {
|
169 a.arora 1.16
|
170 mike 1.2 //
171 // delete tables
172 //
173 delete _currentConfig;
174 delete _plannedConfig;
175 }
176
177 /**
178 Overwrites config properties in the current config file with the
179 the config properties from the planned config file.
180
181 The content of the current config file will be copied in to a
182 backup (.bak) file before copying planned file contents over the
183 current file.
184 */
185 void ConfigFileHandler::copyPlannedFileOverCurrentFile()
186 {
187 if (_plannedFileExist)
188 {
189 _currentConfFile->replace(_plannedConfFile->getFileName());
|
190 kumpf 1.7 _currentFileExist = true;
|
191 mike 1.2 }
192 else if (_currentFileExist)
193 {
194 //
195 // Remove the current file
196 //
197 FileSystem::removeFileNoCase(_currentConfFile->getFileName());
198 }
199 }
200
201
202 /**
203 Load the config properties from the config files.
204 */
205 void ConfigFileHandler::loadAllConfigProperties ()
206 {
207 loadCurrentConfigProperties();
208
209 loadPlannedConfigProperties();
210 }
211
212 mike 1.2
213 /**
214 Load the config properties from the current config file.
215 */
216 void ConfigFileHandler::loadCurrentConfigProperties ()
217 {
218 if (_currentFileExist)
219 {
220 _currentConfFile->load(_currentConfig);
221 }
222 }
223
224
225 /**
226 Load the config properties from the planned config file.
227 */
228 void ConfigFileHandler::loadPlannedConfigProperties ()
229 {
230 if (_plannedFileExist)
231 {
232 _plannedConfFile->load(_plannedConfig);
233 mike 1.2 }
234 }
235
236
237 /**
238 Update the specified property name and value in the current
239 config file.
240 */
241 Boolean ConfigFileHandler::updateCurrentValue (
|
242 kumpf 1.9 const CIMName& name,
|
243 kumpf 1.4 const String& value,
244 Boolean unset)
|
245 mike 1.2 {
246 // Remove the old property name and value from the table
|
247 kumpf 1.9 if (_currentConfig->table.contains(name.getString()))
|
248 mike 1.2 {
|
249 kumpf 1.9 if (!_currentConfig->table.remove(name.getString()))
|
250 mike 1.2 {
251 return false;
252 }
253 }
254
|
255 kumpf 1.4 if (!unset)
|
256 mike 1.2 {
257 // Store the new property name and value in to the table
|
258 kumpf 1.9 if (!_currentConfig->table.insert(name.getString(), value))
|
259 mike 1.2 {
260 return false;
261 }
262 }
263
264 try
265 {
266 // Store the new property in current config file.
267 _currentConfFile->save(_currentConfig);
268 }
269 catch (CannotRenameFile& e)
270 {
271 //
272 // Back up creation failed
|
273 kumpf 1.5 // FUTURE: Log this message in a log file.
|
274 mike 1.2 //
|
275 kumpf 1.5 PEG_TRACE_STRING(TRC_CONFIG, Tracer::LEVEL3,
|
276 kumpf 1.14 "Backup configuration file creation failed: " +
277 e.getMessage() + ", " + strerror(errno));
|
278 mike 1.2
279 return false;
280 }
|
281 kumpf 1.14 catch (CannotOpenFile& cof)
282 {
283 PEG_TRACE_STRING(TRC_CONFIG, Tracer::LEVEL3,
284 "Setting permissions on current configuration file failed: " +
285 cof.getMessage() + ", " + strerror(errno));
286
287 return false;
288 }
289
|
290 mike 1.2 //
291 // The current config file would now been created,
292 // so set the flag to true.
293 //
294 _currentFileExist = true;
295
296 return true;
297 }
298
299
300 /**
301 Update the specified property name and value in the planned
302 config file.
303 */
304 Boolean ConfigFileHandler::updatePlannedValue (
|
305 kumpf 1.9 const CIMName& name,
|
306 kumpf 1.4 const String& value,
307 Boolean unset)
|
308 mike 1.2 {
309 //
310 // Remove the old property name and value from the table
311 //
|
312 kumpf 1.9 if (_plannedConfig->table.contains(name.getString()))
|
313 mike 1.2 {
|
314 kumpf 1.9 if (!_plannedConfig->table.remove(name.getString()))
|
315 mike 1.2 {
316 return false;
317 }
318 }
319
|
320 kumpf 1.4 if (!unset)
|
321 mike 1.2 {
322 //
323 // Store the new property name and value in to the table
324 //
|
325 kumpf 1.9 if (!_plannedConfig->table.insert(name.getString(), value))
|
326 mike 1.2 {
327 return false;
328 }
329 }
330
331 try
332 {
333 //
334 // Planned file need not exist for off line
335 // configuration setting update.
336 //
337 if (_offLine)
338 {
339 String pFile = _plannedConfFile->getFileName();
340
|
341 david 1.10 #if defined(PEGASUS_OS_OS400)
|
342 david 1.15 ofstream ofs(pFile.getCString(), PEGASUS_STD(_CCSID_T(1208)));
|
343 david 1.10 #else
|
344 david 1.15 ofstream ofs(pFile.getCString());
|
345 david 1.10 #endif
|
346 mike 1.2 if (!ofs)
347 {
|
348 kumpf 1.12 PEG_TRACE_STRING(TRC_CONFIG, Tracer::LEVEL4,
349 "Failed to create config file: " + pFile + ", " + strerror(errno));
|
350 mike 1.2 throw NoSuchFile(pFile);
351 }
352 ofs.close();
353 }
354
355 //
356 // Store the new property in planned config file.
357 //
358 _plannedConfFile->save(_plannedConfig);
359
360 }
361 catch (CannotRenameFile& e)
362 {
363 //
364 // Back up creation failed
|
365 kumpf 1.5 // FUTURE: Log this message in a log file.
|
366 mike 1.2 //
|
367 kumpf 1.5 PEG_TRACE_STRING(TRC_CONFIG, Tracer::LEVEL3,
|
368 kumpf 1.14 "Backup configuration file creation failed: " +
369 e.getMessage() + ", " + strerror(errno));
|
370 mike 1.2
371 return false;
372 }
|
373 kumpf 1.14 catch (CannotOpenFile& cof)
374 {
375 PEG_TRACE_STRING(TRC_CONFIG, Tracer::LEVEL3,
376 "Setting permissions on planned configuration file failed: " +
377 cof.getMessage() + ", " + strerror(errno));
378
379 return false;
380 }
381
|
382 mike 1.2 //
383 // The planned config file would now been created,
384 // so set the flag to true.
385 //
386 _plannedFileExist = true;
387
388 return true;
389 }
390
391
392 /**
393 Get the current property value for the specified property name.
394 */
|
395 kumpf 1.9 Boolean ConfigFileHandler::getCurrentValue (const CIMName& name, String& value)
|
396 vijay.eli 1.19 const
|
397 mike 1.2 {
398 if (_currentFileExist)
399 {
|
400 kumpf 1.9 return _currentConfig->table.lookup(name.getString(), value);
|
401 mike 1.2 }
402
|
403 kumpf 1.4 return false;
|
404 mike 1.2 }
405
406
407 /**
408 Get the planned property value for the specified property name.
409 */
|
410 kumpf 1.9 Boolean ConfigFileHandler::getPlannedValue (const CIMName& name, String& value)
|
411 vijay.eli 1.19 const
|
412 mike 1.2 {
413 if (_plannedFileExist)
414 {
|
415 kumpf 1.9 return _plannedConfig->table.lookup(name.getString(), value);
|
416 mike 1.2 }
417
|
418 kumpf 1.4 return false;
|
419 mike 1.2 }
420
421
422 /**
423 Get all current property names.
424 */
|
425 kumpf 1.9 void ConfigFileHandler::getAllCurrentPropertyNames (Array<CIMName>& propertyNames)
|
426 mike 1.2 {
427 propertyNames.clear();
428
429 if (_currentFileExist)
430 {
431 for (Table::Iterator i = _currentConfig->table.start(); i; i++)
432 {
433 propertyNames.append(i.key());
434 }
435 }
436 }
437
438
439 /**
440 Get all current property names and values.
441 */
442 void ConfigFileHandler::getAllCurrentProperties (
|
443 kumpf 1.9 Array<CIMName>& propertyNames,
|
444 mike 1.2 Array<String>& propertyValues)
445 {
446 propertyNames.clear();
447 propertyValues.clear();
448
449 if (_currentFileExist)
450 {
451 for (Table::Iterator i = _currentConfig->table.start(); i; i++)
452 {
453 propertyNames.append(i.key());
454 propertyValues.append(i.value());
455 }
456 }
457 }
458
459
460 /**
461 Get all planned property names and values.
462 */
463 void ConfigFileHandler::getAllPlannedPropertyNames (
|
464 kumpf 1.9 Array<CIMName>& propertyNames)
|
465 mike 1.2 {
466 propertyNames.clear();
467
468 if (_plannedFileExist)
469 {
470 for (Table::Iterator i = _plannedConfig->table.start(); i; i++)
471 {
472 propertyNames.append(i.key());
473 }
474 }
475 }
476
477
478 /**
479 Get all planned config property names and values.
480 */
481 void ConfigFileHandler::getAllPlannedProperties (
|
482 kumpf 1.9 Array<CIMName>& propertyNames,
|
483 mike 1.2 Array<String>& propertyValues)
484 {
485 propertyNames.clear();
486 propertyValues.clear();
487
488 if (_plannedFileExist)
489 {
490 for (Table::Iterator i = _plannedConfig->table.start(); i; i++)
491 {
492 propertyNames.append(i.key());
493 propertyValues.append(i.value());
494 }
495 }
496 }
497
498 PEGASUS_NAMESPACE_END
499
|