Ejemplo n.º 1
0
// -------------------------------------------------------------------------------------------------
static int HandleImport
(
    void
)
// -------------------------------------------------------------------------------------------------
{
    le_cfg_IteratorRef_t iterRef = le_cfg_CreateWriteTxn(NodePath);
    le_result_t result;

    // Check requested format format.
    if (UseJson)
    {
        result = HandleImportJSON(iterRef, FilePath);
    }
    else
    {
        result = le_cfgAdmin_ImportTree(iterRef, FilePath, "");
    }

    if (result != LE_OK)
    {
        ReportImportExportFail(result, "Import", NodePath, FilePath);
        le_cfg_CancelTxn(iterRef);

        return EXIT_FAILURE;
    }

    le_cfg_CommitTxn(iterRef);
    return EXIT_SUCCESS;
}
Ejemplo n.º 2
0
//--------------------------------------------------------------------------------------------------
LE_SHARED void cfgInstall_Add
(
    const char* appName
)
//--------------------------------------------------------------------------------------------------
{
    le_result_t result;

    char filePath[256] = "/opt/legato/apps/";

    result = le_utf8_Append(filePath, appName, sizeof(filePath), NULL);
    LE_FATAL_IF(result != LE_OK, "App name '%s' is too long.", appName);

    result = le_utf8_Append(filePath, "/root.cfg", sizeof(filePath), NULL);
    LE_FATAL_IF(result != LE_OK, "App name '%s' is too long.", appName);

    LE_INFO("Importing configuration for application '%s' from '%s'.", appName, filePath);

    le_cfg_IteratorRef_t i = le_cfg_CreateWriteTxn("/apps");

    result = le_cfgAdmin_ImportTree(i, filePath, appName);

    LE_FATAL_IF(result != LE_OK,
                "Failed to import configuration from '%s' to 'root:/apps/%s' (%s)",
                filePath,
                appName,
                LE_RESULT_TXT(result));

    le_cfg_CommitTxn(i);
}
Ejemplo n.º 3
0
// -------------------------------------------------------------------------------------------------
static int HandleCopy
(
    void
)
// -------------------------------------------------------------------------------------------------
{
    // Create a temp file to export the tree to.
    char tempFilePath[] = "/tmp/configExport-XXXXXX";
    int tempFd;

    do
    {
        tempFd = mkstemp(tempFilePath);
    }
    while ((tempFd == -1) && (errno == EINTR));

    if (tempFd == -1)
    {
        fprintf(stderr, "Could not create temp file. Reason, %s (%d).", strerror(errno), errno);
        return 1;
    }

    // Unlink the file now so that we can make sure that it will end up being deleted, no matter how
    // we exit.
    if (unlink(tempFilePath) == -1)
    {
        printf("Could not unlink temporary file. Reason, %s (%d).", strerror(errno), errno);
    }

    // Create a transaction and export the data from the config tree.
    le_cfg_IteratorRef_t iterRef = le_cfg_CreateWriteTxn(NodePath);
    le_result_t result = le_cfgAdmin_ExportTree(iterRef, tempFilePath, "");

    if (result != LE_OK)
    {
        fprintf(stderr,
                "An I/O error occurred while updating the config tree.  "
                "Tree has been left untouched.\n");
        goto txnDone;
    }

    if (DeleteAfterCopy != false)
    {
        // Since this is a rename, then delete the node at the original location.
        le_cfg_DeleteNode(iterRef, "");
    }

    // Now, move the iterator to the node's new name, then attempt to reload the data.
    le_cfg_GoToNode(iterRef, "..");
    result = le_cfgAdmin_ImportTree(iterRef, tempFilePath, NodeDestPath);

    if (result != LE_OK)
    {
        switch (result)
        {
            case LE_FAULT:
                fprintf(stderr,
                        "An I/O error occurred while updating the config tree.  "
                        "Tree has been left untouched.\n");
                break;

            case LE_FORMAT_ERROR:
                fprintf(stderr,
                        "Import/export corruption detected.  Tree has been left untouched.\n");
                break;

            default:
                fprintf(stderr,
                        "An unexpected error has occurred: %s, (%d).\n",
                        LE_RESULT_TXT(result),
                        result);
                break;
        }
    }

 txnDone:
    // Make sure that the change was successful, and either commit or discard any changes that were
    // made.
    if (result == LE_OK)
    {
        le_cfg_CommitTxn(iterRef);
    }
    else
    {
        le_cfg_CancelTxn(iterRef);
    }

    // Was the operation successful?
    int exitResult = (result == LE_OK) ? EXIT_SUCCESS : EXIT_FAILURE;

    // Finally, clean up our temp file and report our results.
    int closeRetVal;

    do
    {
        closeRetVal = close(tempFd);
    }
    while ((closeRetVal == -1) && (errno == EINTR));

    if (closeRetVal == -1)
    {
        fprintf(stderr, "Could not close temp file. Reason, %s (%d).", strerror(errno), errno);
        exitResult = EXIT_FAILURE;
    }

    return exitResult;
}