/* * Rename a file in storage. * * If not successful *ppszError will set to point to an error string, * on success it will be set to NULL. */ void storage_rename_file(char** ppszError, const pcsl_string* oldFilename_str, const pcsl_string* newFilename_str) { int status; *ppszError = NULL; DEBUGP2F("renaming %s", oldFilename_str); DEBUGP2F(" to %s\n", newFilename_str); if (!storage_file_exists(oldFilename_str)) { *ppszError = (char *)NOT_EXIST_RENAME_ERROR; } if (storage_file_exists(newFilename_str)) { storage_delete_file(ppszError, newFilename_str); if (*ppszError != NULL) { return; } } status = pcsl_file_rename(oldFilename_str, newFilename_str); if (status < 0) { *ppszError = storage_get_last_file_error("storageRename", newFilename_str); return; } *ppszError = NULL; }
/** * Looks to see if the storage file for record store * identified by <code>uidPath</code> exists * * @param suiteId ID of the MIDlet suite that owns the record store * @param name name of the record store * @param extension extension number to add to the end of the file name * * @return true if the file exists, false if it does not. */ int rmsdb_record_store_exists(SuiteIdType suiteId, const pcsl_string* name, int extension) { pcsl_string filename; int intStatus; StorageIdType storageId; MIDPError status; /* * IMPL Note: here is assumed that the record store is located in the same * storage as the midlet suite. This may not be true. */ status = midp_suite_get_suite_storage(suiteId, &storageId); if (status != ALL_OK) { return 0; } if (MIDP_ERROR_NONE != rmsdb_get_unique_id_path(suiteId, storageId, name, extension, &filename)) { return 0; } if (pcsl_string_is_null(&filename)) { return 0; } intStatus = storage_file_exists(&filename); pcsl_string_free(&filename); return 0 != intStatus; }
/** * Reads the given file into the given buffer. * File contents is read as one piece. * * @param ppszError pointer to character string pointer to accept an error * @param pFileName file to read * @param outBuffer buffer where the file contents should be stored * @param outBufferLen length of the outBuffer * * @return status code (ALL_OK if there was no errors) */ MIDPError read_file(char** ppszError, const pcsl_string* pFileName, char** outBuffer, long* outBufferLen) { int handle, status = ALL_OK; long fileSize, len; char* pszTemp; char* buffer = NULL; *ppszError = NULL; *outBuffer = NULL; *outBufferLen = 0; /* open the file */ handle = storage_open(ppszError, pFileName, OPEN_READ); if (*ppszError != NULL) { if (!storage_file_exists(pFileName)) { return NOT_FOUND; } return IO_ERROR; } do { /* get the size of file */ fileSize = storageSizeOf(ppszError, handle); if (*ppszError != NULL) { status = IO_ERROR; break; } if (fileSize > 0) { /* allocate a buffer to store the file contents */ buffer = (char*)pcsl_mem_malloc(fileSize); if (buffer == NULL) { status = OUT_OF_MEMORY; break; } /* read the whole file */ len = storageRead(ppszError, handle, buffer, fileSize); if (*ppszError != NULL || len != fileSize) { pcsl_mem_free(buffer); status = IO_ERROR; } } } while (0); /* close the file */ storageClose(&pszTemp, handle); storageFreeError(pszTemp); if (status == ALL_OK) { *outBuffer = buffer; *outBufferLen = fileSize; } return (MIDPError)status; }
/* * Return a 32 bit handle to an open a file in storage in different modes. * * See "I/O Modes" and "Filenames" above for move information. * * If not successful *ppszError will set to point to an error string, * on success it will be set to NULL. */ int storage_open(char** ppszError, const pcsl_string* filename_str, int ioMode) { int flags = 0; int openStatus; void *handle; *ppszError = NULL; if (OPEN_READ == ioMode) { DEBUGP2F("opening for read only %s\n", filename_str); flags |= PCSL_FILE_O_RDONLY; } else { if (!storage_file_exists(filename_str)) { flags |= PCSL_FILE_O_CREAT; } else if (OPEN_READ_WRITE_TRUNCATE == ioMode) { flags |= PCSL_FILE_O_TRUNC; } if (OPEN_WRITE == ioMode) { DEBUGP2F("opening write only %s\n", filename_str); flags |= PCSL_FILE_O_WRONLY; } else { DEBUGP2F("opening read write %s\n", filename_str); flags |= PCSL_FILE_O_RDWR; } } /** * Verify that the resource is available well within limit as per * the policy in ResourceLimiter */ if (midpCheckResourceLimit(RSC_TYPE_FILE, 1) == 0) { REPORT_INFO(LC_CORE, "Resource limit exceeded for file handles"); *ppszError = (char *)FILE_LIMIT_ERROR; return -1; } openStatus = pcsl_file_open(filename_str, flags, &handle); REPORT_INFO1(LC_CORE, "storage_open allocated file_desc %d\n", (int)handle); if (-1 == openStatus) { *ppszError = storage_get_last_file_error("storage_open()", filename_str); return -1; } /* Update the resource count */ if (midpIncResourceCount(RSC_TYPE_FILE, 1) == 0) { REPORT_INFO(LC_CORE, "FILE : resource limit update error"); } #if REPORT_LEVEL <= LOG_INFORMATION DEBUGP2F("created %s\n", filename_str); #endif return (int)handle; }
/** * Check if the suite is corrupted * @param suiteId ID of a suite * * @return ALL_OK if the suite is not corrupted, * SUITE_CORRUPTED_ERROR is suite is corrupted, * OUT_OF_MEMORY if out of memory, * IO_ERROR if I/O error */ MIDPError check_for_corrupted_suite(SuiteIdType suiteId) { pcsl_string filename[NUM_SUITE_FILES]; int arc[NUM_SUITE_FILES]; int i; StorageIdType storageId; MIDPError status = ALL_OK; /* Default to no error */ MidletSuiteData *pData = get_suite_data(suiteId); if (!pData) { return SUITE_CORRUPTED_ERROR; } /* if this suite was already checked, just return "OK" status */ if (pData->isChecked) { return status; } /* get an id of the storage where the suite is located */ status = midp_suite_get_suite_storage(suiteId, &storageId); if (status != ALL_OK) { return status; } arc[0] = get_suite_filename(suiteId, &INSTALL_INFO_FILENAME, &filename[0]); arc[1] = get_suite_filename(suiteId, &SETTINGS_FILENAME, &filename[1]); arc[2] = midp_suite_get_class_path(suiteId, storageId, KNI_FALSE, &filename[2]); arc[3] = get_property_file(suiteId, KNI_FALSE, &filename[3]); for (i = 0; i < NUM_SUITE_FILES; i++) { if (arc[i] != ALL_OK) { status = (MIDPError)arc[i]; break; } if (!storage_file_exists(&filename[i])) { /* File does not exist; suite must be corrupted */ status = SUITE_CORRUPTED_ERROR; break; } } if (status == ALL_OK) { /* if the suite is not currupted, mark it as "checked" */ pData->isChecked = 1; } pcsl_string_free(&filename[0]); pcsl_string_free(&filename[1]); pcsl_string_free(&filename[2]); pcsl_string_free(&filename[3]); return status; }
/** * Removes the files belonging to the specified component, or to all * components owned by the given suite. * * @param suiteId ID of the suite whose components are being removed * @param componentId ID of the component to remove or UNUSED_COMPONENT_ID * if all components of the suite are being removed * * @return status code, ALL_OK if no errors */ static MIDPError delete_components_files(SuiteIdType suiteId, ComponentIdType componentId) { pcsl_string componentFileName; char* pszError; int suiteFound = 0; MIDPError status = ALL_OK; MidletSuiteData* pData = g_pSuitesData; /* handle the list entries having the given suiteId */ while (pData != NULL) { if (pData->suiteId == suiteId) { suiteFound = 1; if (pData->type == COMPONENT_DYNAMIC && (componentId == UNUSED_COMPONENT_ID || pData->componentId == componentId)) { /* remove the file holding the component */ status = get_jar_path(COMPONENT_DYNAMIC, (jint)pData->componentId, &componentFileName); if (status != ALL_OK) { break; } storage_delete_file(&pszError, &componentFileName); if (pszError != NULL) { storageFreeError(pszError); /* it's an error only if the file exists */ if (storage_file_exists(&componentFileName)) { status = IO_ERROR; pcsl_string_free(&componentFileName); break; } } pcsl_string_free(&componentFileName); } } pData = pData->nextEntry; } if (status == ALL_OK && suiteFound == 0) { /* suite doesn't exist */ status = NOT_FOUND; } return status; }
/** * Checks if there is an unfinished transaction. * * @return 0 there is no unfinished transaction, != 0 otherwise */ int unfinished_transaction_exists() { pcsl_string_status rc; pcsl_string transDataFile; int res = 0; if (g_transactionStarted) { return 1; } /* get a full path to the transaction data file */ rc = pcsl_string_cat(storage_get_root(INTERNAL_STORAGE_ID), &TRANSACTION_DATA_FILENAME, &transDataFile); if (rc != PCSL_STRING_OK) { return 0; } res = storage_file_exists(&transDataFile); pcsl_string_free(&transDataFile); return res; }
/** * Looks to see if the storage file for record store * identified by <code>uidPath</code> exists * * @param filenameBase filenameBase of the MIDlet suite that owns the record * store * @param name name of the record store * @param extension extension number to add to the end of the file name * * @return true if the file exists, false if it does not. */ int rmsdb_record_store_exists(pcsl_string* filenameBase, const pcsl_string* name, int extension) { pcsl_string filename; int intStatus; /* * IMPL_NOTE: for security reasons the record store is always * located in the internal storage. */ if (MIDP_ERROR_NONE != rmsdb_get_unique_id_path(filenameBase, INTERNAL_STORAGE_ID, name, extension, &filename)) { return 0; } if (pcsl_string_is_null(&filename)) { return 0; } intStatus = storage_file_exists(&filename); pcsl_string_free(&filename); return 0 != intStatus; }
javacall_result bt_push_startup() { int i; char *error; pcsl_string full_name = PCSL_STRING_NULL; int storage; REPORT_INFO(LC_PUSH, "Bluetooth PushRegistry is now starting."); javacall_bt_sddb_initialize(); pcsl_string_cat(storage_get_root(INTERNAL_STORAGE_ID), &BT_PUSH_FILENAME, &full_name); if (!storage_file_exists(&full_name)) { pcsl_string_free(&full_name); return JAVACALL_OK; } storage = storage_open(&error, &full_name, OPEN_READ); pcsl_string_free(&full_name); if (error != NULL) { REPORT_ERROR1(LC_PUSH, "Failed to open `BtPush' file: %s", error); storageFreeError(error); return JAVACALL_FAIL; } storageRead(&error, storage, (char *)&g_count, sizeof(g_count)); for (i = 0; error == NULL && i < g_count; i++) { bt_push_t *push = (bt_push_t *)pcsl_mem_malloc(sizeof(bt_push_t)); if (push == NULL) { REPORT_ERROR(LC_PUSH, "Failed to allocate memory."); storageClose(&error, storage); storageFreeError(error); return JAVACALL_FAIL; } storageRead(&error, storage, (char *)&push->port, sizeof(push->port)); if (error != NULL) { pcsl_mem_free(push); break; } storageRead(&error, storage, (char *)&push->params, sizeof(push->params)); if (error != NULL) { pcsl_mem_free(push); break; } storageRead(&error, storage, (char *)&push->record.classes, sizeof(push->record.classes)); if (error != NULL) { pcsl_mem_free(push); break; } storageRead(&error, storage, (char *)&push->record.size, sizeof(push->record.size)); if (error != NULL) { pcsl_mem_free(push); break; } push->record.data = pcsl_mem_malloc(push->record.size); if (push->record.data == NULL) { pcsl_mem_free(push); REPORT_ERROR(LC_PUSH, "Failed to allocate memory."); storageClose(&error, storage); storageFreeError(error); return JAVACALL_FAIL; } storageRead(&error, storage, (char *)push->record.data, push->record.size); if (error != NULL) { pcsl_mem_free(push->record.data); pcsl_mem_free(push); break; } storageRead(&error, storage, (char *)&push->record.id, sizeof(push->record.id)); if (error != NULL) { pcsl_mem_free(push); break; } push->server = BT_INVALID_HANDLE; push->client = NULL; push->next = g_registry; g_registry = push; } if (error != NULL) { REPORT_ERROR1(LC_PUSH, "Error reading `BtPush' file: %s", error); storageFreeError(error); storageClose(&error, storage); storageFreeError(error); return JAVACALL_FAIL; } REPORT_INFO1(LC_PUSH, "%d record(s) read.", g_count); storageClose(&error, storage); storageFreeError(error); if (g_count > 0) { /* Attempt to enable Bluetooth radio, if it is not already on. */ javacall_bool enabled; javacall_bt_stack_initialize(); if (javacall_bt_stack_is_enabled(&enabled) == JAVACALL_OK && enabled == JAVACALL_FALSE) { javacall_bt_stack_enable(); } } return JAVACALL_OK; }