/**
 * Checks the integrity of the suite storage database and of the
 * installed suites.
 *
 * @param fullCheck 0 to check just an integrity of the database,
 *                    other value for full check
 * @param delCorruptedSuites != 0 to delete the corrupted suites,
 *                           0 - to keep them (for re-installation).
 *
 * @return ALL_OK if no errors,
 *         SUITE_CORRUPTED_ERROR if the suite database was corrupted
 *                               but has been successfully repaired,
 *         another error code if the database is corrupted and
 *         could not be repaired
 */
MIDPError
midp_check_suites_integrity(int fullCheck, int delCorruptedSuites) {
    MIDPError status;
    char *pszError = NULL;
    int dbWasCorrupted = 0;

    /* Check if there is a previously started transaction exists. */
    if (unfinished_transaction_exists()) {
        (void)rollback_transaction();
    }

    /* Check if the suite database is corrupted and repair it if needed. */
    status = read_suites_data(&pszError);
    if (status == SUITE_CORRUPTED_ERROR) {
        dbWasCorrupted = 1;
        status = repair_suite_db();
    }
    if (status != ALL_OK) {
        /* give up, user interaction is needed */
        return status;
    }

    /* if fullCheck is true, check all installed suites */
    if (fullCheck) {
        int i, numOfSuites;
        SuiteIdType suiteId, *pSuiteIds = NULL;

        status = midp_get_suite_ids(&pSuiteIds, &numOfSuites);

        if (status == ALL_OK) {
            for (i = 0; i < numOfSuites; i++) {
                suiteId = pSuiteIds[i];

                if (check_for_corrupted_suite(suiteId) ==
                        SUITE_CORRUPTED_ERROR) {
                    dbWasCorrupted = 1;
                    if (delCorruptedSuites) {
                        midp_remove_suite(suiteId);
                    }
                }
            }

            if (pSuiteIds != NULL) {
                midp_free_suite_ids(pSuiteIds, numOfSuites);
            }
        }
    }

    return dbWasCorrupted ? SUITE_CORRUPTED_ERROR : ALL_OK;
}
Пример #2
0
/**
 * Deletes an installed MIDlet suite. This is an example of how to use
 * the public MIDP API.
 *
 * @param argc The total number of arguments
 * @param argv An array of 'C' strings containing the arguments
 *
 * @return <tt>0</tt> for success, otherwise <tt>-1</tt>
 *
 * IMPL_NOTE: determine if it is desirable for user targeted output
 *       messages to be sent via the log/trace service, or if
 *       they should remain as printf calls
 */
int removeMidlet(int argc, char* argv[]) {
    int   status = -1;
    char* appDir = NULL;

    if (argc == 1) {
        fprintf(stderr, removeUsageText);
        return -1;
    }

    if (argc > 2) {
        REPORT_ERROR1(LC_AMS, "Too many arguments given\n%s", removeUsageText);
        fprintf(stderr, "Too many arguments given\n%s", removeUsageText);
        return -1;
    }

    /* get midp home directory, set it */
    appDir = getApplicationDir(argv[0]);
    if (appDir == NULL) {
        return -1;
    }
    /* set up appDir before calling initialize */
    midpSetAppDir(appDir);

    if (midpInitialize() != 0) {
        REPORT_ERROR(LC_AMS, "Not enough memory");
        fprintf(stderr, "Not enough memory\n");
        return -1;
    }

    do {
        int onlyDigits;
        int len;
        int i;
        SuiteIdType* pSuites = NULL;
        int numberOfSuites = 0;
        MIDPError err;

        /* if the storage name only digits, convert it */
        onlyDigits = 1;
        len = strlen(argv[1]);
        for (i = 0; i < len; i++) {
            if (!isdigit((argv[1])[i])) {
                onlyDigits = 0;
                break;
            }
        }

        if (onlyDigits) {
            /* Remove by number */
            int suiteNumber;

            /* the format of the string is "number:" */
            if (sscanf(argv[1], "%d", &suiteNumber) != 1) {
                REPORT_ERROR(LC_AMS, "Invalid suite number format");
                fprintf(stderr, "Invalid suite number format\n");
                break;
            }

            err = midp_get_suite_ids(&pSuites, &numberOfSuites);
            if (err != ALL_OK) {
                REPORT_ERROR1(LC_AMS, "Error in midp_get_suite_ids(), code %d",
		              err);
                fprintf(stderr, "Error in midp_get_suite_ids(), code %d.\n",
		        err);
                break;
            }

            if (suiteNumber > numberOfSuites || suiteNumber < 1) {
                REPORT_ERROR(LC_AMS, "Suite number out of range");
                fprintf(stderr, "Suite number out of range\n");
                midp_free_suite_ids(pSuites, numberOfSuites);
                break;
            }

            /* The suite number for the first suite is 1. */
            midp_remove_suite(pSuites[suiteNumber - 1]);

            midp_free_suite_ids(pSuites, numberOfSuites);
        } else if (strcmp(argv[1], "all") == 0) {
            /* Remove all */
            err = midp_get_suite_ids(&pSuites, &numberOfSuites);

            if (err != ALL_OK) {
                REPORT_ERROR1(LC_AMS, "Error in midp_get_suite_ids(), code %d",
                              err);
                fprintf(stderr, "Error in midp_get_suite_ids(), code %d.\n",
                        err);
                break;
            }

            for (i = 0; i < numberOfSuites; i++) {
                midp_remove_suite(pSuites[i]);
            }

            midp_free_suite_ids(pSuites, numberOfSuites);
        } else {
            REPORT_ERROR2(LC_AMS, "Invalid argument: '%s'.\n%s",
                          argv[1], removeUsageText);
            fprintf(stderr, "Invalid argument: '%s'.\n%s",
                    argv[1], removeUsageText);
        }

        status = 0;
    } while (0);

    midpFinalize();

    return status;
}