/** Replace the properties in the config file with the properties from the given file. */ void ConfigFile::replace (const String& fileName) { String line; // // Open the given config file for reading // #if defined(PEGASUS_OS_OS400) ifstream ifs(fileName.getCString(), PEGASUS_STD(_CCSID_T(1208))); #else ifstream ifs(fileName.getCString()); #endif // // Delete the backup configuration file // if (FileSystem::exists(_configBackupFile)) { FileSystem::removeFile(_configBackupFile); } // // Rename the existing config file as a backup file // if (FileSystem::exists(_configFile)) { if (!FileSystem::renameFile(_configFile, _configBackupFile)) { ifs.close(); throw CannotRenameFile(_configFile); } } // // Open the existing config file for writing // #if defined(PEGASUS_OS_OS400) ofstream ofs(_configFile.getCString(), PEGASUS_STD(_CCSID_T(1208))); #else ofstream ofs(_configFile.getCString()); #endif ofs.clear(); #if !defined(PEGASUS_OS_TYPE_WINDOWS) // // Set permissions on the config file to 0644 // if ( !FileSystem::changeFilePermissions(_configFile, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) ) // set 0644 { throw CannotOpenFile(_configFile); } #endif // // Read each line of the new file and write to the config file. // for (Uint32 lineNumber = 1; GetLine(ifs, line); lineNumber++) { ofs << line << endl; } // // Close the file handles // ifs.close(); ofs.close(); }
/** Replace the properties in the config file with the properties from the given file. */ void ConfigFile::replace (const String& fileName) { // // Open the given config file for reading // FILE* ifs = fopen(fileName.getCString(), "r"); if (!ifs) { throw CannotOpenFile(fileName); } // // Delete the backup configuration file // if (FileSystem::exists(_configBackupFile)) { Executor::removeFile(_configBackupFile.getCString()); } // // Rename the existing config file as a backup file // if (FileSystem::exists(_configFile)) { if (Executor::renameFile( _configFile.getCString(), _configBackupFile.getCString()) != 0) { fclose(ifs); throw CannotRenameFile(_configFile); } } // // Open the existing config file for writing // FILE* ofs = Executor::openFile(_configFile.getCString(), 'w'); if (!ofs) { fclose(ifs); throw CannotOpenFile(_configFile); } // // Read each line of the new file and write to the config file. // char buffer[4096]; while ((fgets(buffer, sizeof(buffer), ifs)) != NULL) fputs(buffer, ofs); // // Close the file handles // fclose(ifs); fclose(ofs); #if !defined(PEGASUS_ENABLE_PRIVILEGE_SEPARATION) # if !defined(PEGASUS_OS_TYPE_WINDOWS) // // Set permissions on the config file to 0644 // if (!FileSystem::changeFilePermissions(_configFile, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) // set 0644 { throw CannotChangeFilePerm(_configFile); } # endif #endif /* PEGASUS_ENABLE_PRIVILEGE_SEPARATION */ }
/** Save the properties to the config file. */ void ConfigFile::save (ConfigTable* confTable) { // // Delete the backup configuration file // if (FileSystem::exists(_configBackupFile)) { FileSystem::removeFile(_configBackupFile); } // // Rename the configuration file as a backup file // if (FileSystem::exists(_configFile)) { if (!FileSystem::renameFile(_configFile, _configBackupFile)) { throw CannotRenameFile(_configFile); } } // // Open the config file for writing // #if defined(PEGASUS_OS_OS400) ofstream ofs(_configFile.getCString(), PEGASUS_STD(_CCSID_T(1208))); #else ofstream ofs(_configFile.getCString()); #endif ofs.clear(); #if !defined(PEGASUS_OS_TYPE_WINDOWS) // // Set permissions on the config file to 0644 // if ( !FileSystem::changeFilePermissions(_configFile, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) ) // set 0644 { throw CannotOpenFile(_configFile); } #endif // // Write config file header information // for (int index = 0; index < HEADER_SIZE; index++) { ofs << ConfigHeader[index] << endl; } ofs << endl; // // Save config properties and values to the file // for (Table::Iterator i = confTable->table.start(); i; i++) { ofs << i.key() << "=" << i.value() << endl; } ofs.close(); }
/** Save the properties to the config file. */ void ConfigFile::save(ConfigTable* confTable) { // // Delete the backup configuration file // if (FileSystem::exists(_configBackupFile)) { Executor::removeFile(_configBackupFile.getCString()); } // // Rename the configuration file as a backup file // if (FileSystem::exists(_configFile)) { if (Executor::renameFile( _configFile.getCString(), _configBackupFile.getCString()) != 0) { throw CannotRenameFile(_configFile); } } // // Open the config file for writing // FILE* ofs = Executor::openFile(_configFile.getCString(), 'w'); if (!ofs) { throw CannotOpenFile(_configFile); } // // Write config file header information // for (int index = 0; index < HEADER_SIZE; index++) { fputs(ConfigHeader[index], ofs); fputc('\n', ofs); } // // Save config properties and values to the file // for (Table::Iterator i = confTable->table.start(); i; i++) { CString key = i.key().getCString(); CString value = i.value().getCString(); fprintf(ofs, "%s=%s\n", (const char*)key, (const char*)value); } fclose(ofs); #if !defined(PEGASUS_OS_TYPE_WINDOWS) // Note: The Executor process sets the permissions to 0644 when it // opens the config file for writing. if (Executor::detectExecutor() != 0) { // // Set permissions on the config file to 0644 // if (!FileSystem::changeFilePermissions( _configFile, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) // set 0644 { throw CannotChangeFilePerm(_configFile); } } #endif }