/** * The recursive helper function. It deletes all files inside specified * directory and calls itself to delete subdirectories. * * @param pDirName the directory to delete * @param pSep system-dependent file separator */ void do_cleanup(const pcsl_string* pDirName, const pcsl_string* pSep) { void* fileList = NULL; pcsl_string fileName = PCSL_STRING_NULL; pcsl_string dirName = PCSL_STRING_NULL; // add tailing file separator to directory name if (pcsl_string_cat(pDirName, pSep, &dirName) != PCSL_STRING_OK) { return; } fileList = pcsl_file_openfilelist(&dirName); if (fileList == NULL) { pcsl_string_free(&dirName); return; } // iterate over the directory's content while (!pcsl_file_getnextentry(fileList, &dirName, &fileName)) { int isDir = pcsl_file_is_directory(&fileName); if (isDir == 1) { // make recursion do_cleanup(&fileName, pSep); } else { // remove file pcsl_file_unlink(&fileName); } pcsl_string_free(&fileName); }; pcsl_string_free(&dirName); pcsl_file_closefilelist(fileList); // remove empty directory pcsl_file_rmdir(pDirName); }
/** * Initializes a property set with the contents of a property file. * * @param props The property set to initialize * @param name The name of the property file to load. It is relative * to the <tt>configRoot</tt> path. * @param configRoot The fully qualified pathname to the root * configuration directory. * * @return <tt>0</tt> for success, otherwise <tt>-1</tt> */ static int initProps(Property** props, const pcsl_string * name, const pcsl_string * configRoot) { pcsl_string pathname; int fd = -1 ; char * errStr; /* Property file can be relative or at midp_home variable */ pcsl_string_cat(configRoot, name, &pathname); fd = storage_open(&errStr, &pathname, OPEN_READ); pcsl_string_free(&pathname); if (errStr != NULL) { REPORT_WARN2(LC_CORE, "Warning: could not open config file(%s): %s\n", pathname, errStr); storageFreeError(errStr); return 0; } /* Read through the file one line at a time */ if (parseConfig(fd, props) != 0) { return -1; } /* Close the storage handle */ storageClose(&errStr, fd); return 0; }
/** * Finishes the previously started transaction. * * @return ALL_OK if the transaction was successfully finished, * NOT_FOUND if the transaction has not been started, * IO_ERROR if I/O error */ MIDPError finish_transaction() { pcsl_string_status rc; pcsl_string transDataFile; char* pszTemp = NULL; MIDPError status = ALL_OK; if (!g_transactionStarted) { return NOT_FOUND; } /* transaction is finished, removed the transaction file */ /* 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 OUT_OF_MEMORY; } storage_delete_file(&pszTemp, &transDataFile); pcsl_string_free(&transDataFile); if (pszTemp != NULL) { storageFreeError(pszTemp); status = IO_ERROR; } g_transactionStarted = 0; return status; }
/** * Tries to repair the suite database. * * @return ALL_OK if succeeded, other value if failed */ MIDPError repair_suite_db() { /* IMPL_NOTE: should be replaced with more sophisticated routine. */ MIDPError status = ALL_OK; pcsl_string_status rc; pcsl_string suitesDataFile; char* pszTemp = NULL; /* get a full path to the _suites.dat */ rc = pcsl_string_cat(storage_get_root(INTERNAL_STORAGE_ID), &SUITE_DATA_FILENAME, &suitesDataFile); if (rc != PCSL_STRING_OK) { return OUT_OF_MEMORY; } storage_delete_file(&pszTemp, &suitesDataFile); pcsl_string_free(&suitesDataFile); if (pszTemp != NULL) { storageFreeError(pszTemp); status = IO_ERROR; } if (g_isSuitesDataLoaded) { free_suites_data(); g_isSuitesDataLoaded = 0; } return status; }
static void push_save() { char *error; bt_push_t *push = g_registry; pcsl_string full_name = PCSL_STRING_NULL; int storage; pcsl_string_cat(storage_get_root(INTERNAL_STORAGE_ID), &BT_PUSH_FILENAME, &full_name); storage = storage_open(&error, &full_name, OPEN_READ_WRITE_TRUNCATE); pcsl_string_free(&full_name); if (error != NULL) { REPORT_ERROR1(LC_PUSH, "Error opening `BtPush' file: %s", error); storageFreeError(error); return; } storageWrite(&error, storage, (char *)&g_count, sizeof(g_count)); while (push != NULL && error == NULL) { bt_push_t *next = push->next; storageWrite(&error, storage, (char *)&push->port, sizeof(bt_port_t)); if (error != NULL) { break; } storageWrite(&error, storage, (char *)&push->params, sizeof(bt_params_t)); if (error != NULL) { break; } storageWrite(&error, storage, (char *)&push->record.classes, sizeof(push->record.classes)); if (error != NULL) { break; } storageWrite(&error, storage, (char *)&push->record.size, sizeof(push->record.size)); if (error != NULL) { break; } storageWrite(&error, storage, (char *)push->record.data, push->record.size); storageWrite(&error, storage, (char *)&push->record.id, sizeof(push->record.id)); if (error != NULL) { break; } push = next; } if (error != NULL) { REPORT_ERROR1(LC_PUSH, "Error writing `BtPush' file: %s", error); storageFreeError(error); } storageClose(&error, storage); storageFreeError(error); }
/** * Writes the contents of the given buffer into the given file. * * Note that if the length of the input buffer is zero or less, * the file will be truncated. * * @param ppszError pointer to character string pointer to accept an error * @param pFileName file to write * @param inBuffer buffer with data that will be stored * @param inBufferLen length of the inBuffer * * @return status code (ALL_OK if there was no errors) */ MIDPError write_file(char** ppszError, const pcsl_string* pFileName, char* inBuffer, long inBufferLen) { int handle, status = ALL_OK; char* pszTemp; pcsl_string tmpFileName; pcsl_string_status rc; *ppszError = NULL; /* get the name of the temporary file */ rc = pcsl_string_cat(pFileName, &TMP_FILE_EXTENSION, &tmpFileName); if (rc != PCSL_STRING_OK) { return OUT_OF_MEMORY; } /* open the file */ handle = storage_open(ppszError, &tmpFileName, OPEN_READ_WRITE_TRUNCATE); if (*ppszError != NULL) { pcsl_string_free(&tmpFileName); return IO_ERROR; } /* write the whole buffer */ if (inBufferLen > 0) { storageWrite(ppszError, handle, inBuffer, inBufferLen); } if (*ppszError != NULL) { status = IO_ERROR; } /* close the file */ storageClose(&pszTemp, handle); storageFreeError(pszTemp); if (status == ALL_OK) { /* rename the temporary file */ storage_rename_file(ppszError, &tmpFileName, pFileName); if (*ppszError != NULL) { status = IO_ERROR; storage_delete_file(&pszTemp, &tmpFileName); storageFreeError(pszTemp); } } else { storage_delete_file(&pszTemp, &tmpFileName); storageFreeError(pszTemp); } pcsl_string_free(&tmpFileName); return (MIDPError)status; }
/** * Returns a storage system unique string for this record store file * based on the current vendor and suite of the running MIDlet. * <ul> * <li>The native storage path for the desired MIDlet suite * is acquired from the Scheduler. * * <li>The <code>filename</code> arg is converted into an ascii * equivalent safe to use directly in the underlying * file system and appended to the native storage path. See the * com.sun.midp.io.j2me.storage.File.unicodeToAsciiFilename() * method for conversion details. * * <li>Finally the extension number given by the extension parameter * is appended to the file name. * <ul> * @param suiteId ID of the MIDlet suite that owns the record store * @param storageId ID of the storage where the RMS will be located * @param name name of the record store * @param extension extension number to add to the end of the file name * * @return a unique identifier for this record store file */ static MIDP_ERROR rmsdb_get_unique_id_path(SuiteIdType suiteId, StorageIdType storageId, const pcsl_string* name, int extension, pcsl_string * res_path) { pcsl_string temp = PCSL_STRING_NULL; MIDP_ERROR midpErr; pcsl_string_status pcslErr; *res_path = PCSL_STRING_NULL; // null in case of any error if (pcsl_string_is_null(name)) { return MIDP_ERROR_ILLEGAL_ARGUMENT; } midpErr = midp_suite_get_rms_filename(suiteId, storageId, (extension == IDX_EXTENSION_INDEX ? MIDP_RMS_IDX_EXT : MIDP_RMS_DB_EXT), name, res_path); if (midpErr != MIDP_ERROR_NONE) { return midpErr; } if (pcsl_string_is_null(res_path)) { /* Assume this is special case where the suite was not installed and create a filename from the ID. */ pcslErr = pcsl_string_cat(storage_get_root(storageId), midp_suiteid2pcsl_string(suiteId), &temp); if (pcslErr != PCSL_STRING_OK || pcsl_string_is_null(&temp) ) { return MIDP_ERROR_FOREIGN; } pcslErr = pcsl_string_cat(&temp, name, res_path); pcsl_string_free(&temp); if (PCSL_STRING_OK != pcslErr) { return MIDP_ERROR_FOREIGN; } } return MIDP_ERROR_NONE; }
/** * The function is called upon MIDlet suite removal. * It deletes suites's private directory and all it's content. * * @param suiteId the ID of MIDlet suite */ void jsr75_suite_remove_cleanup(SuiteIdType suiteId) { const pcsl_string* pStg = storage_get_root(INTERNAL_STORAGE_ID); const jchar jsep = pcsl_file_getfileseparator(); pcsl_string dirName1 = PCSL_STRING_NULL; pcsl_string dirName2 = PCSL_STRING_NULL; pcsl_string dirName = PCSL_STRING_NULL; pcsl_string sep = PCSL_STRING_NULL; if (pcsl_string_convert_from_utf16(&jsep, 1, &sep) != PCSL_STRING_OK) { return; } if (pcsl_string_cat(pStg, &privateDir, &dirName1) != PCSL_STRING_OK) { pcsl_string_free(&sep); return; } if (pcsl_string_cat(&dirName1, &sep, &dirName2) != PCSL_STRING_OK) { pcsl_string_free(&sep); pcsl_string_free(&dirName1); return; } if (pcsl_string_cat(&dirName2, midp_suiteid2pcsl_string(suiteId), &dirName) != PCSL_STRING_OK) { pcsl_string_free(&sep); pcsl_string_free(&dirName1); pcsl_string_free(&dirName2); return; } pcsl_string_free(&dirName1); pcsl_string_free(&dirName2); do_cleanup(&dirName, &sep); pcsl_string_free(&sep); pcsl_string_free(&dirName); }
/** * Opens registry file * * @param write write access flag * @return operation result */ static jsr211_result open_table_file(int write) { pcsl_string fileName = PCSL_STRING_NULL_INITIALIZER; int io_mode = OPEN_READ | (write ? OPEN_WRITE : 0); if (PCSL_STRING_OK != pcsl_string_cat(storage_get_root(INTERNAL_STORAGE_ID), &JSR211_TABLE_NAME, &fileName)) { return JSR211_FAILED; } table_file = storage_open(&io_error_message, &fileName, io_mode); pcsl_string_free(&fileName); return io_error_message? JSR211_FAILED: JSR211_OK; }
/** * Starts a new transaction of the given type. * * @param transactionType type of the new transaction * @param suiteId ID of the suite, may be UNUSED_SUITE_ID * @param pFilename name of the midlet suite's file, may be NULL * * @return ALL_OK if no errors, * IO_ERROR if I/O error */ MIDPError begin_transaction(MIDPTransactionType transactionType, SuiteIdType suiteId, const pcsl_string *pFilename) { pcsl_string_status rc; pcsl_string transDataFile; char *pszError = NULL; MIDPError status = ALL_OK; char pBuffer[MAX_FILENAME_LENGTH + sizeof(int) /* file name length */ + sizeof(suiteId) + sizeof(transactionType)]; char *p = pBuffer; int len = sizeof(suiteId) + sizeof(transactionType); *(MIDPTransactionType*)p = transactionType; p += sizeof(MIDPTransactionType); *(SuiteIdType*)p = suiteId; p += sizeof(SuiteIdType); if (pFilename != NULL) { int strLen; rc = pcsl_string_convert_to_utf16(pFilename, (jchar*)(p + sizeof(int)), MAX_FILENAME_LENGTH / sizeof(jchar), &strLen); if (rc != PCSL_STRING_OK) { return OUT_OF_MEMORY; } *(int*)p = strLen; len += strLen; } /* 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 OUT_OF_MEMORY; } status = write_file(&pszError, &transDataFile, pBuffer, len); storageFreeError(pszError); pcsl_string_free(&transDataFile); g_transactionStarted = 1; return status; }
static int findNextMatch(PCSLFileIterator* pIterator, const pcsl_string * match, pcsl_string * result) { WIN32_FIND_DATAW findData; pcsl_string root = PCSL_STRING_NULL; pcsl_string foundName = PCSL_STRING_NULL; jsize rootLen = 0; if (result == NULL) { return -1; } * result = PCSL_STRING_NULL; if (!FindNextFileW(pIterator->iteratorHandle, &findData)) { return -1; } rootLen = pIterator->savedRootLength; if (pcsl_string_substring(match, 0, rootLen, &root) != PCSL_STRING_OK) { return -1; } if (pcsl_string_convert_from_utf16(findData.cFileName, wcslen(findData.cFileName), &foundName) != PCSL_STRING_OK) { pcsl_string_free(&root); return -1; } if (pcsl_string_cat(&root, &foundName, result) != PCSL_STRING_OK) { pcsl_string_free(&foundName); pcsl_string_free(&root); return -1; } pcsl_string_free(&foundName); pcsl_string_free(&root); return 0; }
/** * Gets filename of the secure suite resource by suiteId and resource name * * Note that memory for the in/out parameter filename * MUST be allocated using pcsl_mem_malloc(). * The caller is responsible for freeing it. * * @param suiteId The suite id used to identify the MIDlet suite * @param pResourceName The name of secure resource to read from storage * @param pFilename The in/out parameter that will return filename * of the specified secure resource * @return one of the error codes: * <pre> * ALL_OK, OUT_OF_MEMORY, NOT_FOUND, * SUITE_CORRUPTED_ERROR, BAD_PARAMS * </pre> */ MIDPError get_secure_resource_file(SuiteIdType suiteId, const pcsl_string* pResourceName, pcsl_string *pFilename) { pcsl_string resourceID = PCSL_STRING_NULL; MIDPError errorCode; do { if (pcsl_string_cat(pResourceName, &SECURE_EXTENSION, &resourceID) != PCSL_STRING_OK) { errorCode = OUT_OF_MEMORY; break; } errorCode = get_suite_resource_file(suiteId, &resourceID, KNI_FALSE, pFilename); } while (0); pcsl_string_free(&resourceID); return errorCode; }
/** * 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; }
/** * Rolls back the transaction being in progress. * * @return ALL_OK if the transaction was rolled back, * NOT_FOUND if the transaction has not been started, * IO_ERROR if I/O error */ MIDPError rollback_transaction() { MIDPError status = ALL_OK; pcsl_string_status rc; pcsl_string transDataFile; char *pszTemp = NULL; /* 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 OUT_OF_MEMORY; } storage_delete_file(&pszTemp, &transDataFile); pcsl_string_free(&transDataFile); if (pszTemp != NULL) { storageFreeError(pszTemp); status = IO_ERROR; } else { g_transactionStarted = 0; } return ALL_OK; }
/** * Writes the file with information about the installed suites. * * Note that if the value of the global variable g_numberOfSuites * is zero, the file will be truncated. * * @param ppszError pointer to character string pointer to accept an error * * @return status code: ALL_OK if no errors, * OUT_OF_MEMORY if malloc failed * IO_ERROR if an IO_ERROR */ MIDPError write_suites_data(char** ppszError) { MIDPError status = ALL_OK; long bufferLen, pos; char* buffer = NULL; pcsl_string_status rc; pcsl_string suitesDataFile; MidletSuiteData* pData; *ppszError = NULL; /* get a full path to the _suites.dat */ rc = pcsl_string_cat(storage_get_root(INTERNAL_STORAGE_ID), &SUITE_DATA_FILENAME, &suitesDataFile); if (rc != PCSL_STRING_OK) { return OUT_OF_MEMORY; } if (!g_numberOfSuites) { /* truncate the file with the list of the installed suites */ status = write_file(ppszError, &suitesDataFile, buffer, 0); pcsl_string_free(&suitesDataFile); return status; } /* allocate a buffer where the information about all suites will be saved */ bufferLen = g_numberOfSuites * (sizeof(MidletSuiteData) + MAX_VAR_SUITE_DATA_LEN); /* space to store the number of suites */ bufferLen += sizeof(int); buffer = (char*)pcsl_mem_malloc(bufferLen); if (buffer == NULL) { pcsl_string_free(&suitesDataFile); return OUT_OF_MEMORY; } /* assemble the information about all suites into the allocated buffer */ pos = 0; pData = g_pSuitesData; *(int*)&buffer[pos] = g_numberOfSuites; ADJUST_POS_IN_BUF(pos, bufferLen, sizeof(int)); while (pData != NULL) { memcpy((char*)&buffer[pos], (char*)pData, MIDLET_SUITE_DATA_SIZE); ADJUST_POS_IN_BUF(pos, bufferLen, MIDLET_SUITE_DATA_SIZE); /* setup pJarHash */ if (pData->jarHashLen > 0) { memcpy((char*)&buffer[pos], pData->varSuiteData.pJarHash, pData->jarHashLen); ADJUST_POS_IN_BUF(pos, bufferLen, pData->jarHashLen); } /* setup string fields */ { int i, convertedLen; jint strLen; pcsl_string* pStrings[] = { &pData->varSuiteData.midletClassName, &pData->varSuiteData.displayName, &pData->varSuiteData.iconName, &pData->varSuiteData.suiteVendor, &pData->varSuiteData.suiteName, &pData->varSuiteData.pathToJar, &pData->varSuiteData.pathToSettings }; status = ALL_OK; for (i = 0; i < (int) (sizeof(pStrings) / sizeof(pStrings[0])); i++) { strLen = pcsl_string_utf16_length(pStrings[i]); /* * We have to guarantee 4 - bytes alignment to use this: * *(jint*)&buffer[pos] = strLen; * on RISC CPUs. */ pos = SUITESTORE_ALIGN_4(pos); *(jint*)&buffer[pos] = strLen; ADJUST_POS_IN_BUF(pos, bufferLen, sizeof(jint)); /* assert(bufferLen > 0); */ if (strLen > 0) { rc = pcsl_string_convert_to_utf16(pStrings[i], (jchar*)&buffer[pos], bufferLen / sizeof(jchar), &convertedLen); if (rc != PCSL_STRING_OK) { status = OUT_OF_MEMORY; break; } ADJUST_POS_IN_BUF(pos, bufferLen, convertedLen * sizeof(jchar)); } } } if (status != ALL_OK) { break; } pData = pData->nextEntry; } if (status == ALL_OK) { /* write the buffer into the file */ status = write_file(ppszError, &suitesDataFile, buffer, pos); } /* cleanup */ pcsl_mem_free(buffer); pcsl_string_free(&suitesDataFile); return status; }
/** * Reads the file with information about the installed suites. * * Note that if the value of the global variable g_numberOfSuites * is zero, this function does nothing. * * @param ppszError pointer to character string pointer to accept an error * * @return status code: ALL_OK if no errors, * OUT_OF_MEMORY if malloc failed * IO_ERROR if an IO_ERROR, * SUITE_CORRUPTED_ERROR if the suite database is corrupted */ MIDPError read_suites_data(char** ppszError) { MIDPError status; int i; long bufferLen, pos; char* buffer = NULL; pcsl_string_status rc; pcsl_string suitesDataFile; MidletSuiteData *pSuitesData = g_pSuitesData; MidletSuiteData *pData, *pPrevData = NULL; int numOfSuites = 0; *ppszError = NULL; if (g_isSuitesDataLoaded) { return ALL_OK; } if (midpInit(LIST_LEVEL) != 0) { return OUT_OF_MEMORY; } /* get a full path to the _suites.dat */ rc = pcsl_string_cat(storage_get_root(INTERNAL_STORAGE_ID), &SUITE_DATA_FILENAME, &suitesDataFile); if (rc != PCSL_STRING_OK) { return OUT_OF_MEMORY; } /* read the file */ status = read_file(ppszError, &suitesDataFile, &buffer, &bufferLen); pcsl_string_free(&suitesDataFile); if (status == NOT_FOUND || (status == ALL_OK && bufferLen == 0)) { /* _suites.dat is absent or empty, it's a normal situation */ g_pSuitesData = NULL; g_numberOfSuites = 0; g_isSuitesDataLoaded = 1; return ALL_OK; } if (status == ALL_OK && bufferLen < (long) sizeof(int)) { pcsl_mem_free(buffer); status = SUITE_CORRUPTED_ERROR; /* _suites.dat is corrupted */ } if (status != ALL_OK) { return status; } /* parse contents of the suite database */ pos = 0; numOfSuites = *(int*)&buffer[pos]; ADJUST_POS_IN_BUF(pos, bufferLen, sizeof(int)); for (i = 0; i < numOfSuites; i++) { pData = (MidletSuiteData*) pcsl_mem_malloc(sizeof(MidletSuiteData)); if (!pData) { status = OUT_OF_MEMORY; break; } if (pPrevData) { pPrevData->nextEntry = pData; } else { pSuitesData = pData; } /* IMPL_NOTE: introduce pcsl_mem_copy() */ if (bufferLen < (long)MIDLET_SUITE_DATA_SIZE) { status = IO_ERROR; break; } memcpy((char*)pData, (char*)&buffer[pos], MIDLET_SUITE_DATA_SIZE); ADJUST_POS_IN_BUF(pos, bufferLen, MIDLET_SUITE_DATA_SIZE); pData->nextEntry = NULL; /* this suite was not checked if it is corrupted */ pData->isChecked = 0; /* setup pJarHash */ if (pData->jarHashLen > 0) { pData->varSuiteData.pJarHash = (unsigned char*)pcsl_mem_malloc(pData->jarHashLen); if (pData->varSuiteData.pJarHash == NULL) { status = OUT_OF_MEMORY; break; } memcpy(pData->varSuiteData.pJarHash, (char*)&buffer[pos], pData->jarHashLen); ADJUST_POS_IN_BUF(pos, bufferLen, pData->jarHashLen); } else { pData->varSuiteData.pJarHash = NULL; } /* setup string fields */ { int i; jint strLen; pcsl_string* pStrings[] = { &pData->varSuiteData.midletClassName, &pData->varSuiteData.displayName, &pData->varSuiteData.iconName, &pData->varSuiteData.suiteVendor, &pData->varSuiteData.suiteName, &pData->varSuiteData.pathToJar, &pData->varSuiteData.pathToSettings }; status = ALL_OK; for (i = 0; i < (int) (sizeof(pStrings) / sizeof(pStrings[0])); i++) { if (bufferLen < (long)sizeof(jint)) { status = IO_ERROR; /* _suites.dat is corrupted */ break; } /* * We have to guarantee 4 - bytes alignment to use this: * strLen = *(jint*)&buffer[pos]; * on RISC CPUs. */ pos = SUITESTORE_ALIGN_4(pos); strLen = *(jint*)&buffer[pos]; ADJUST_POS_IN_BUF(pos, bufferLen, sizeof(jint)); if (bufferLen < (long)strLen) { status = IO_ERROR; /* _suites.dat is corrupted */ break; } if (strLen > 0) { rc = pcsl_string_convert_from_utf16( (jchar*)&buffer[pos], strLen, pStrings[i]); if (rc != PCSL_STRING_OK) { status = OUT_OF_MEMORY; break; } ADJUST_POS_IN_BUF(pos, bufferLen, strLen * sizeof(jchar)); } else { *pStrings[i] = strLen ? PCSL_STRING_NULL : PCSL_STRING_EMPTY; } } } if (status != ALL_OK) { break; } pData->nextEntry = NULL; pPrevData = pData; } /* end for (numOfSuites) */ pcsl_mem_free(buffer); if (status == ALL_OK) { g_numberOfSuites = numOfSuites; g_pSuitesData = pSuitesData; g_isSuitesDataLoaded = 1; } else { free_suites_data(); } return status; }
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; }
static int findFirstMatch(PCSLFileIterator* pIterator, const pcsl_string * match, pcsl_string * result) { WIN32_FIND_DATAW findData; HANDLE handle; PCSL_DEFINE_ASCII_STRING_LITERAL_START(starSuffix) {'*', '\0'} PCSL_DEFINE_ASCII_STRING_LITERAL_END(starSuffix); pcsl_string root = PCSL_STRING_NULL; pcsl_string foundName = PCSL_STRING_NULL; pcsl_string matchStar = PCSL_STRING_NULL; jsize rootLen = 0; if (result == NULL) { return -1; } * result = PCSL_STRING_NULL; if (pcsl_string_cat(match, &starSuffix, &matchStar) != PCSL_STRING_OK) { return -1; } { const jchar * pwszMatch = pcsl_string_get_utf16_data(&matchStar); if (NULL == pwszMatch) { pcsl_string_free(&matchStar); return -1; } handle = FindFirstFileW(pwszMatch, &findData); pcsl_string_free(&matchStar); } if (INVALID_HANDLE_VALUE == handle) { return -1; } pIterator->iteratorHandle = handle; rootLen = pIterator->savedRootLength; if (pcsl_string_substring(match, 0, rootLen, &root) != PCSL_STRING_OK) { return -1; } if (pcsl_string_convert_from_utf16(findData.cFileName, wcslen(findData.cFileName), &foundName) != PCSL_STRING_OK) { pcsl_string_free(&root); return -1; } if (pcsl_string_cat(&root, &foundName, result) != PCSL_STRING_OK) { pcsl_string_free(&foundName); pcsl_string_free(&root); return -1; } pcsl_string_free(&foundName); pcsl_string_free(&root); return 0; }
/** The getNextEntry function search the next file which is specified DIR */ int pcsl_file_getnextentry(void *handle, const pcsl_string * string, pcsl_string * result) { pcsl_string rootpath = PCSL_STRING_NULL; pcsl_string returnVal = PCSL_STRING_NULL; pcsl_string matchName = PCSL_STRING_NULL; jsize matchLen; jsize len; struct dirent *de; char* pszFilename = NULL; DIR *dir; jsize savedRootLength = 0; jsize savedMatchLength = 0; PCSLStorageDirInfo* pPCSLStorageDirInfo = (PCSLStorageDirInfo *)handle; if (pPCSLStorageDirInfo == NULL) { return -1; } savedRootLength = pPCSLStorageDirInfo->savedRootLength; savedMatchLength = pPCSLStorageDirInfo->savedMatchLength; if (pcsl_string_substring(string, savedRootLength, savedRootLength + savedMatchLength, &matchName) != PCSL_STRING_OK) { return -1; } { const jbyte * pszMatch = NULL; dir = pPCSLStorageDirInfo->savedDirectory; if (dir == NULL) { return -1; } pszMatch = pcsl_string_get_utf8_data(&matchName); if (pszMatch == NULL) { return -1; } matchLen = strlen((char*)pszMatch); /* find the first match file not "." or ".." */ for (de = readdir(dir); de != NULL; de = readdir(dir)) { pszFilename = de->d_name; if (strcmp(pszFilename, ".") == 0 || strcmp(pszFilename, "..") == 0) { continue; } if (strncmp(pszFilename, (char*)pszMatch, matchLen) == 0) { break; } } pcsl_string_release_utf8_data(pszMatch, &matchName); pcsl_string_free(&matchName); if (NULL == de) { /* End of Dir without a match. */ return -1; } len = strlen(pszFilename); if (len >= 0) { if (pcsl_string_convert_from_utf8((jbyte*)pszFilename, len, &returnVal) != PCSL_STRING_OK) { return -1; } if (pcsl_string_substring(string, 0, savedRootLength, &rootpath) != PCSL_STRING_OK) { pcsl_string_free(&returnVal); return -1; } if (pcsl_string_cat(&rootpath, &returnVal, result) != PCSL_STRING_OK) { pcsl_string_free(&returnVal); pcsl_string_free(&rootpath); return -1; } pcsl_string_free(&returnVal); pcsl_string_free(&rootpath); } } return 0; }
int find_midlet_class(SuiteIdType id, int midletNumber, pcsl_string* res) { MidpProperties properties; pcsl_string keySuffix = PCSL_STRING_NULL; pcsl_string key = PCSL_STRING_NULL; const pcsl_string* property = &PCSL_STRING_NULL; pcsl_string temp = PCSL_STRING_NULL; pcsl_string_status stat; int begin; int result = 0; *res = PCSL_STRING_NULL; properties = midp_get_suite_properties(id); if (OUT_OF_MEM_PROPERTY_STATUS(properties)) { return OUT_OF_MEM_LEN; } do { if (CORRUPTED_PROPERTY_STATUS(properties)) { midp_free_properties(&properties); REPORT_ERROR(LC_AMS, "Error : Suite is corrupted"); fprintf(stderr, "Error : Suite is corrupted\n"); result = NULL_LEN; break; } if (READ_ERROR_PROPERTY_STATUS(properties)) { midp_free_properties(&properties); REPORT_ERROR(LC_AMS, "Corrupt properties"); fprintf(stderr, "Corrupt properties\n"); result = NULL_LEN; break; } stat = pcsl_string_convert_from_jint(midletNumber, &keySuffix); if (PCSL_STRING_OK != stat) { if(PCSL_STRING_ENOMEM == stat) { result = OUT_OF_MEM_LEN; } else { result = NULL_LEN; } break; } stat = pcsl_string_cat(&KEY_PREFIX, &keySuffix, &key); pcsl_string_free(&keySuffix); if (PCSL_STRING_OK != stat) { result = OUT_OF_MEM_LEN; break; } property = midp_find_property(&properties, &key); if (pcsl_string_length(property) <= 0) { /* property not found */ result = NULL_LEN; break; } /* The class is the last item in the set. */ begin = pcsl_string_last_index_of(property, (jchar)','); if (begin < 0 || begin >= pcsl_string_length(property)) { result = NULL_LEN; break; } begin++; stat = pcsl_string_substring(property, begin, pcsl_string_length(property), &temp); if (PCSL_STRING_OK != stat) { result = OUT_OF_MEM_LEN; break; } if (pcsl_string_length(&temp) <= 0) { pcsl_string_free(&temp); result = NULL_LEN; break; } stat = pcsl_string_trim(&temp, res); pcsl_string_free(&temp); if (PCSL_STRING_OK != stat) { result = OUT_OF_MEM_LEN; break; } } while (0); midp_free_properties(&properties); return result; }