(file) Return to ConfigFile.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Config

  1 karl  1.23 //%2006////////////////////////////////////////////////////////////////////////
  2 mike  1.2  //
  3 karl  1.20 // 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.12 // IBM Corp.; EMC Corporation, The Open Group.
  7 karl  1.20 // 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.21 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10            // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11 karl  1.23 // 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.4  // 
 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 a.arora 1.19 // Modified By:  Amit K Arora, IBM (amita@in.ibm.com)
 35 vijay.eli 1.22 //               Vijay Eli, IBM (vijayeli@in.ibm.com), bug#3608.
 36 mike      1.2  //
 37                //%/////////////////////////////////////////////////////////////////////////////
 38                
 39                
 40                #include <cctype>
 41                #include <fstream>
 42                #include <Pegasus/Common/FileSystem.h>
 43                #include <Pegasus/Common/HashTable.h>
 44 kumpf     1.3  #include <Pegasus/Common/Tracer.h>
 45 mike      1.2  
 46                #include "ConfigExceptions.h"
 47                #include "ConfigFile.h"
 48 david     1.8  #if  defined(PEGASUS_OS_OS400)
 49                #include "OS400ConvertChar.h"
 50                #endif
 51 mike      1.2  
 52                
 53                PEGASUS_USING_STD;
 54                
 55                PEGASUS_NAMESPACE_BEGIN
 56                
 57                
 58                ////////////////////////////////////////////////////////////////////////////////
 59                //
 60                //  ConfigFile Class
 61                //
 62                ////////////////////////////////////////////////////////////////////////////////
 63                
 64                
 65                ////////////////////////////////////////////////////////////////////////////////
 66                // ConfigTable 
 67                ////////////////////////////////////////////////////////////////////////////////
 68                
 69                typedef HashTable<String, String, EqualFunc<String>, HashFunc<String> > Table;
 70                
 71                struct ConfigTable
 72 mike      1.2  {
 73                    Table table;
 74                };
 75                
 76                
 77                /*
 78                    Config file header information
 79                */
 80                static const char* ConfigHeader [] = {
 81                    "########################################################################",
 82                    "##                                                                    ##",
 83                    "##                  CIM Server configuration file                     ##",
 84                    "##                                                                    ##",
 85                    "########################################################################",
 86                    " ",    
 87                    "########################################################################",
 88                    "#                                                                      #",
 89                    "# This is the configuration file for the CIMOM. The configuration      #",
 90                    "# properties in this file are loaded in to CIMOM at startup.           #",
 91                    "# CIMOM updates this file with the changes in the configurations.      #",
 92                    "#                                                                      #",
 93 mike      1.2      "# It is recommended that user do not edit this file, instead use       #",
 94                    "# CIMConfigCommand to make any changes to the CIMOM configurations.    #",
 95                    "#                                                                      #",
 96                    "########################################################################",
 97                    " "
 98                };
 99                
100                static const int HEADER_SIZE = sizeof(ConfigHeader) / sizeof(ConfigHeader[0]);
101                
102                
103                /** 
104                    Constructor. 
105                */
106                ConfigFile::ConfigFile (const String& fileName)
107                {
108                    _configFile = fileName;
109                
110                    _configBackupFile = fileName + ".bak";
111                
112                }
113                
114 mike      1.2  /** 
115                    Destructor. 
116                */
117                ConfigFile::~ConfigFile ()
118                {
119                
120                }
121                
122                
123                /** 
124                    Get the name of the configuration file.
125                */
126 vijay.eli 1.22 String ConfigFile::getFileName () const
127 mike      1.2  {
128                    return ( _configFile );
129                }
130                
131                
132                /** 
133                    Load the properties from the config file.
134                */
135                void ConfigFile::load (ConfigTable* confTable)
136                {
137                    String line;
138                
139                    //
140                    // Delete the backup configuration file
141                    //
142                    if (FileSystem::exists(_configBackupFile))
143                    {
144                        FileSystem::removeFile(_configBackupFile);
145                    }
146                
147                    //
148 mike      1.2      // Open the config file
149                    //
150 david     1.8  #if defined(PEGASUS_OS_OS400)
151 david     1.18     ifstream ifs(_configFile.getCString(), PEGASUS_STD(_CCSID_T(1208)));
152 david     1.8  #else
153 david     1.18     ifstream ifs(_configFile.getCString());
154 david     1.8  #endif
155 mike      1.2      if (!ifs)
156                    {
157                        return;
158                    }
159                
160                    //
161                    // Read each line of the file
162                    //
163                    for (Uint32 lineNumber = 1; GetLine(ifs, line); lineNumber++)
164                    {
165                        // Get the property name and value
166                
167                        //
168                        // Skip leading whitespace
169                        //
170 kumpf     1.7          const Char16* p = line.getChar16Data();
171 mike      1.2  
172                        while (*p && isspace(*p))
173                        {
174                            p++;
175                        }
176                
177                        if (!*p)
178                        {
179                            continue;
180                        }
181                
182                        //
183                        // Skip comment lines
184                        //
185                        if (*p == '#')
186                        {
187                            continue;
188                        }
189                
190                        //
191                        // Get the property name
192 mike      1.2          //
193                        String name = String::EMPTY;
194                
195                        if (!(isalpha(*p) || *p == '_'))
196                        {
197                            ifs.close();
198                            throw ConfigFileSyntaxError(_configFile, lineNumber);
199                        }
200                
201 kumpf     1.5          name.append(*p++);
202 mike      1.2  
203                        while (isalnum(*p) || *p == '_')
204                        {
205 kumpf     1.5              name.append(*p++);
206 mike      1.2          }
207                
208                        //
209                        // Skip whitespace after property name
210                        //
211                        while (*p && isspace(*p))
212                        {
213                            p++;
214                        }
215                
216                        //
217                        // Expect an equal sign
218                        //
219                        if (*p != '=')
220                        {
221                            ifs.close();
222                            throw ConfigFileSyntaxError(_configFile, lineNumber);
223                        }
224                
225                        p++;
226                
227 mike      1.2          //
228                        // Skip whitespace after equal sign
229                        //
230                        while (*p && isspace(*p))
231                        {
232                            p++;
233                        }
234                
235                        //
236                        // Get the value
237                        //
238                        String value = String::EMPTY;
239                
240                        while (*p)
241                        {
242 kumpf     1.5              value.append(*p++);
243 mike      1.2          }
244                
245                        //
246                        // Store the property name and value in the table
247                        //
248                        if (!confTable->table.insert(name, value))
249                        {
250                            //
251                            // Duplicate property, ignore the new property value.
252 kumpf     1.3              // FUTURE: Log this message in a log file.
253 mike      1.2              //
254 kumpf     1.3              PEG_TRACE_STRING(TRC_CONFIG, Tracer::LEVEL3,
255                                "Duplicate property '" + name + "', value '" + value + "' is ignored.");
256 mike      1.2          }
257                    }
258                
259                    ifs.close();
260                }
261                
262                
263                /** 
264                    Save the properties to the config file.
265                */
266                void ConfigFile::save (ConfigTable* confTable)
267                {
268                    //
269                    // Delete the backup configuration file
270                    //
271                    if (FileSystem::exists(_configBackupFile))
272                    {
273                        FileSystem::removeFile(_configBackupFile);
274                    }
275                
276                    //
277 mike      1.2      // Rename the configuration file as a backup file
278                    //
279                    if (FileSystem::exists(_configFile))
280                    {
281                        if (!FileSystem::renameFile(_configFile, _configBackupFile))
282                        {
283                            throw CannotRenameFile(_configFile);
284                        }
285                    }
286                
287                    //
288                    // Open the config file for writing
289                    //
290 david     1.8  #if defined(PEGASUS_OS_OS400)
291 david     1.18     ofstream ofs(_configFile.getCString(), PEGASUS_STD(_CCSID_T(1208)));
292 david     1.8  #else
293 david     1.18     ofstream ofs(_configFile.getCString());
294 david     1.8  #endif
295 mike      1.2      ofs.clear();
296                
297 kumpf     1.17 #if !defined(PEGASUS_OS_TYPE_WINDOWS)
298 mike      1.2      //
299 kumpf     1.13     // Set permissions on the config file to 0644
300                    //
301 kumpf     1.15     if ( !FileSystem::changeFilePermissions(_configFile,
302                        (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) )    // set 0644 
303 kumpf     1.13     {
304                        throw CannotOpenFile(_configFile);
305                    }
306 kumpf     1.16 #endif
307 kumpf     1.13 
308                    //
309 mike      1.2      // Write config file header information
310                    //
311                    for (int index = 0; index < HEADER_SIZE; index++)
312                    {
313                        ofs << ConfigHeader[index] << endl;
314                    }
315                
316                    ofs << endl;
317                
318                    //
319                    // Save config properties and values to the file
320                    //
321                    for (Table::Iterator i = confTable->table.start(); i; i++)
322                    {
323                        ofs << i.key() << "=" << i.value() << endl;
324                    }
325                
326                    ofs.close();
327                }
328                
329                
330 mike      1.2  /** 
331                    Replace the properties in the config file with the properties from
332                    the given file.
333                */
334                void ConfigFile::replace (const String& fileName)
335                {
336                    String line;
337                
338                    //
339                    // Open the given config file for reading
340                    //
341 david     1.8  #if defined(PEGASUS_OS_OS400)
342 david     1.18     ifstream ifs(fileName.getCString(), PEGASUS_STD(_CCSID_T(1208)));
343 david     1.8  #else
344 david     1.18     ifstream ifs(fileName.getCString());
345 david     1.8  #endif
346 mike      1.2  
347                    //
348                    // Delete the backup configuration file
349                    //
350                    if (FileSystem::exists(_configBackupFile))
351                    {
352                        FileSystem::removeFile(_configBackupFile);
353                    }
354                
355                    //
356                    // Rename the existing config file as a backup file
357                    //
358                    if (FileSystem::exists(_configFile))
359                    {
360                        if (!FileSystem::renameFile(_configFile, _configBackupFile))
361                        {
362                            ifs.close();
363                            throw CannotRenameFile(_configFile);
364                        }
365                    }
366                
367 mike      1.2      //
368                    // Open the existing config file for writing
369                    //
370 david     1.8  #if defined(PEGASUS_OS_OS400)
371 david     1.18     ofstream ofs(_configFile.getCString(), PEGASUS_STD(_CCSID_T(1208)));
372 david     1.8  #else
373 david     1.18     ofstream ofs(_configFile.getCString());
374 david     1.8  #endif
375 mike      1.2      ofs.clear();
376 kumpf     1.13 
377 kumpf     1.17 #if !defined(PEGASUS_OS_TYPE_WINDOWS)
378 kumpf     1.13     //
379 kumpf     1.15     // Set permissions on the config file to 0644
380 kumpf     1.13     //
381 kumpf     1.15     if ( !FileSystem::changeFilePermissions(_configFile,
382                        (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) )    // set 0644 
383 kumpf     1.14     {
384                        throw CannotOpenFile(_configFile);
385                    }
386 kumpf     1.16 #endif
387 mike      1.2  
388                    //
389                    // Read each line of the new file and write to the config file.
390                    //
391                    for (Uint32 lineNumber = 1; GetLine(ifs, line); lineNumber++)
392                    {
393                        ofs << line << endl;
394                    }
395                
396                    // 
397                    // Close the file handles
398                    //
399                    ifs.close();
400                    ofs.close();
401                }
402                
403                
404                PEGASUS_NAMESPACE_END
405                

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2