예제 #1
0
/**
    Constructor.
*/
ConfigFileHandler::ConfigFileHandler(
    const String& currentFile,
    const String& plannedFile,
    const Boolean offLine)
    : _offLine(offLine)
{
    String cFile;
    String pFile;

    //
    // Set the current and planned config files
    //
    cFile = ConfigManager::getHomedPath(currentFile);

    pFile = ConfigManager::getHomedPath(plannedFile);

    //
    // Initialize instance variables.
    //

    _currentFileExist = true;
    _plannedFileExist = true;


    _currentConfFile.reset(new ConfigFile(cFile));
    _plannedConfFile.reset(new ConfigFile(pFile));

    _currentConfig = new ConfigTable;
    _plannedConfig = new ConfigTable;

    //
    // check whether the planned file exists or not
    //
    if (!FileSystem::exists(pFile))
    {
        _plannedFileExist = false;
        return;
    }

    //
    // check whether the file is readable or not
    //
    if (!FileSystem::canRead(pFile))
    {
        throw FileNotReadable(pFile);
    }

    //
    // check whether the current file exists or not
    //
    if (!FileSystem::exists(cFile))
    {
        _currentFileExist = false;
        //
        // Current file need not exist.
        // try creating one so that planned file contents
        // can be copied over.
        //

        FILE* is = Executor::openFile(cFile.getCString(), 'w');

        if (!is)
        {
            // unable to create file
            PEG_TRACE((TRC_CONFIG, Tracer::LEVEL1,
                "Failed to create config file: %s",
                 (const char*)cFile.getCString()));
            throw NoSuchFile(cFile);
        }

        fclose(is);
    }

    //
    // check whether the file is readable or not
    //
    if (!FileSystem::canRead(cFile))
    {
        throw FileNotReadable(cFile);
    }

}
예제 #2
0
/**
    Constructor.
*/
ConfigFileHandler::ConfigFileHandler (
    const String& currentFile,
    const String& plannedFile,
    const Boolean offLine)
    : _offLine(offLine)
{
    String cFile;
    String pFile;

    //
    // Set the current and planned config files
    //
    cFile = ConfigManager::getHomedPath(currentFile);

    pFile = ConfigManager::getHomedPath(plannedFile);

    //
    // Initialize instance variables.
    //

    _currentFileExist = true;
    _plannedFileExist = true;


    _currentConfFile.reset(new ConfigFile(cFile));
    _plannedConfFile.reset(new ConfigFile(pFile));

    _currentConfig = new ConfigTable;
    _plannedConfig = new ConfigTable;

    //
    // check whether the planned file exists or not
    //
    if (!FileSystem::exists(pFile))
    {
        _plannedFileExist = false;
        return;
    }

    //
    // check whether the file is readable or not
    //
    if (!FileSystem::canRead(pFile))
    {
        throw FileNotReadable(pFile);
    }

    //
    // check whether the current file exists or not
    //
    if (!FileSystem::exists(cFile))
    {
        _currentFileExist = false;
        //
        // Current file need not exist.
        // try creating one so that planned file contents
        // can be copied over.
        //
#if defined(PEGASUS_OS_OS400)
        ofstream ofs(cFile.getCString(), PEGASUS_STD(_CCSID_T(1208)));
#else
        ofstream ofs(cFile.getCString());
#endif
        if (!ofs)
        {
            // unable to create file
            PEG_TRACE_STRING(TRC_CONFIG, Tracer::LEVEL4,
                             "Failed to create config file: " + cFile + ", " + strerror(errno));
            throw NoSuchFile(cFile);
        }
        ofs.close();
    }

    //
    // check whether the file is readable or not
    //
    if (!FileSystem::canRead(cFile))
    {
        throw FileNotReadable(cFile);
    }

}
예제 #3
0
/**
    Update the specified property name and value in the planned
    config file.
*/
Boolean ConfigFileHandler::updatePlannedValue(
    const CIMName& name,
    const String& value,
    Boolean unset)
{
    //
    // Remove the old property name and value from the table
    //
    if (_plannedConfig->table.contains(name.getString()))
    {
        if (!_plannedConfig->table.remove(name.getString()))
        {
            return false;
        }
    }

    if (!unset)
    {
        //
        // Store the new property name and value in to the table
        //
        if (!_plannedConfig->table.insert(name.getString(), value))
        {
            return false;
        }
    }

    try
    {
        //
        // Planned file need not exist for off line
        // configuration setting update.
        //
        if (_offLine)
        {
            String pFile = _plannedConfFile->getFileName();

            FILE* fs = Executor::openFile(pFile.getCString(), 'w');

            if (!fs)
            {
                PEG_TRACE((TRC_CONFIG, Tracer::LEVEL1,
                    "Failed to create config file: %s",
                    (const char*)pFile.getCString()));
                throw NoSuchFile(pFile);
            }

            fclose(fs);
        }

        //
        // Store the new property in planned config file.
        //
        _plannedConfFile->save(_plannedConfig);

    }
    catch (Exception& e)
    {
        PEG_TRACE((TRC_CONFIG, Tracer::LEVEL1,
            "Can not save planned configuration: %s",
             (const char*)e.getMessage().getCString()));

        throw;
    }

    //
    // The planned config file would now been created,
    // so set the flag to true.
    //
    _plannedFileExist = true;

    return true;
}
예제 #4
0
/**
    Update the specified property name and value in the planned
    config file.
*/
Boolean ConfigFileHandler::updatePlannedValue (
    const CIMName& name,
    const String& value,
    Boolean unset)
{
    //
    // Remove the old property name and value from the table
    //
    if (_plannedConfig->table.contains(name.getString()))
    {
        if (!_plannedConfig->table.remove(name.getString()))
        {
            return false;
        }
    }

    if (!unset)
    {
        //
        // Store the new property name and value in to the table
        //
        if (!_plannedConfig->table.insert(name.getString(), value))
        {
            return false;
        }
    }

    try
    {
        //
        // Planned file need not exist for off line
        // configuration setting update.
        //
        if (_offLine)
        {
            String pFile = _plannedConfFile->getFileName();

#if defined(PEGASUS_OS_OS400)
            ofstream ofs(pFile.getCString(), PEGASUS_STD(_CCSID_T(1208)));
#else
            ofstream ofs(pFile.getCString());
#endif
            if (!ofs)
            {
                PEG_TRACE_STRING(TRC_CONFIG, Tracer::LEVEL4,
                                 "Failed to create config file: " + pFile + ", " + strerror(errno));
                throw NoSuchFile(pFile);
            }
            ofs.close();
        }

        //
        // Store the new property in planned config file.
        //
        _plannedConfFile->save(_plannedConfig);

    }
    catch (CannotRenameFile& e)
    {
        //
        // Back up creation failed
        // FUTURE: Log this message in a log file.
        //
        PEG_TRACE_STRING(TRC_CONFIG, Tracer::LEVEL3,
                         "Backup configuration file creation failed: " +
                         e.getMessage() + ", " + strerror(errno));

        return false;
    }
    catch (CannotOpenFile& cof)
    {
        PEG_TRACE_STRING(TRC_CONFIG, Tracer::LEVEL3,
                         "Setting permissions on planned configuration file failed: " +
                         cof.getMessage() + ", " + strerror(errno));

        return false;
    }

    //
    // The planned config file would now been created,
    // so set the flag to true.
    //
    _plannedFileExist = true;

    return true;
}