/** * Opens file that keeps device and service classes. * @return handle for the file, <code>-1</code> if opening failed. */ static int openCodFile() { char *error; int handle = -1; pcsl_string name = PCSL_STRING_EMPTY; int i; if (PCSL_STRING_OK == pcsl_string_append(&name, storage_get_root(INTERNAL_STORAGE_ID))) { for (i = 0; i < BT_ADDRESS_SIZE * 2; i++) { char c = (emul_data.local_addr[i / 2] >> ((i % 2) * 4)) & 0xf; if (c <= 9) { c += '0'; } else { c += 'a'; } if (PCSL_STRING_OK != pcsl_string_append_char(&name, c)) { break; } } if (i == BT_ADDRESS_SIZE * 2) { handle = storage_open(&error, &name, OPEN_READ_WRITE); storageFreeError(error); } } pcsl_string_free(&name); return handle; }
/* * Prefixing the system directory for storage, APP_DIR, with midp_home. * * @param midp_home file system path to where MIDP is installed * * @return 0 for success, non-zero for out of memory */ int storageInitialize(char *midp_home) { jchar fsep = storageGetFileSeparator(); if (storageInitDone) { /* Already initialized */ return 0; } if (PCSL_STRING_OK != pcsl_string_initialize()) { REPORT_ERROR(LC_CORE, "Error: cannot initialize string library.\n"); return -1; } if(pcsl_file_init() < 0) { REPORT_ERROR(LC_CORE, "Error: out of memory.\n"); return -1; } /* set up a path to the internal storage */ if (PCSL_STRING_OK != pcsl_string_from_chars(midp_home, &sRoot[0])) { REPORT_ERROR(LC_CORE, "Error: out of memory.\n"); storageFinalize(); return -1; } /* performance hint: predict buffer capacity */ pcsl_string_predict_size(&sRoot[0], pcsl_string_length(&sRoot[0]) + 2 + PCSL_STRING_LITERAL_LENGTH(APP_DIR)); if (PCSL_STRING_OK != pcsl_string_append_char(&sRoot[0], fsep) || PCSL_STRING_OK != pcsl_string_append(&sRoot[0], &APP_DIR) || PCSL_STRING_OK != pcsl_string_append_char(&sRoot[0], fsep)) { REPORT_ERROR(LC_CORE, "Error: out of memory.\n"); storageFinalize(); return -1; } if (0 != initializeConfigRoot(midp_home)) { storageFinalize(); return -1; } storageInitDone = 1; return 0; }
static int initializeConfigRoot(char* midp_home) { jchar fileSep = storageGetFileSeparator(); if (PCSL_STRING_OK != pcsl_string_from_chars(midp_home, &configRoot[0])) { return -1; } /* performance hint: predict buffer capacity */ pcsl_string_predict_size(&configRoot[0], pcsl_string_length(&configRoot[0]) + 2 + PCSL_STRING_LITERAL_LENGTH(CONFIG_SUBDIR)); if (PCSL_STRING_OK != pcsl_string_append_char(&configRoot[0], fileSep) || PCSL_STRING_OK != pcsl_string_append(&configRoot[0], &CONFIG_SUBDIR) || PCSL_STRING_OK != pcsl_string_append_char(&configRoot[0], fileSep)) { pcsl_string_free(&configRoot[0]); return -1; } return 0; }
/** * Reads a line from a manifest file. Compacts continuation lines.<BR> * (Allocates memory for this line.) * * @param mfbuf pointer to the manifest jchar_buffer; this pointer will move to * the next line * @param result pointer to pcsl_string where the new line from manifest * will be stored * @return error code */ static MIDPError readMfLine(jchar** mfbuf, pcsl_string * result) { jchar* lineStart = NULL; int is_broken_line = 0; jchar* p = NULL; int count = 0; int i = 0; *result = PCSL_STRING_NULL; /* will use p to avoid all the *(*mfbuf) stuff and make it more readable */ p = (*mfbuf); if (!(*p)) { /* end of jchar_buffer */ return END_OF_MF; } /* skip commented out and blank lines */ while (COMMENTED_OUT(p) || NEW_LINE(p)) { while (!NEW_LINE(p)) { p++; /* skip commented out line */ } while (NEW_LINE(p)) { p++; /* skip new line */ } /* now pointing to the next line */ if (MF_SPACE(p)) { /* if starting with space */ while (!NEW_LINE(p)) { p++; /* skip the line */ } } /* end of if */ } /* end of while */ lineStart = p; for (;*p ;) { count++; p++; if (NEW_LINE(p) && !MF_BROKEN_LINE(p)) { *p = 0x00; /* cut the line */ if (*(p+1)) { /* if not end of the buffer */ p++; /* point to the next line beginning */ break; } } else if (MF_BROKEN_LINE(p)) { while (!MF_SPACE(p)) { /* look for the space */ count++; p++; } /* once space found, point to the next character and go ahead */ count++; p++; is_broken_line = 1; continue; } /* end of else */ } /* end of for */ /* move mfbuf to point to the next line */ (*mfbuf) = p; pcsl_string_predict_size(result, count); if (is_broken_line) { i = 0; while (*(lineStart+i)) { /* here once we have a new line it will be followed by space */ if (NEW_LINE_1(lineStart+i)) { i+=3; count-=3; } else if (NEW_LINE(lineStart+i)) { i+=2; count-=2; } if (PCSL_STRING_OK != pcsl_string_append_char(result, lineStart[i])) { pcsl_string_free(result); return OUT_OF_MEMORY; } i++; } } else { if (PCSL_STRING_OK != pcsl_string_convert_from_utf16(lineStart, count, result)) { return OUT_OF_MEMORY; } } return ALL_OK; } /* end of readMfLine */
/** * This helper function deletes the specified directory and all * its contents. * * @param pDirName the directory to delete */ void do_cleanup(const pcsl_string* pDirName) { pcsl_string name1 = PCSL_STRING_NULL; pcsl_string * pSubDir = &name1; pcsl_string name2 = PCSL_STRING_NULL; pcsl_string * pDirEntry = &name2; pcsl_string * tmp; javacall_handle listHandle; int nestLevel = 0; int pathLen; javacall_utf16 * fileName = NULL; int fileNameLen = 0; pcsl_string_status status; javacall_result res; int finish = 0; jchar sep = javacall_get_file_separator(); // initialize current directory if (PCSL_STRING_OK != pcsl_string_dup(pDirName, pSubDir)) { return; } for ( ; ; ) { // open upper-level directory for listing GET_PCSL_STRING_DATA_AND_LENGTH(pSubDir) if (PCSL_STRING_PARAMETER_ERROR(pSubDir)) { listHandle = NULL; } else { listHandle = javacall_dir_open(pSubDir_data, pSubDir_len); } RELEASE_PCSL_STRING_DATA_AND_LENGTH if (NULL == listHandle) { pcsl_string_free(pSubDir); return; } // get the first entry in the current directory fileName = javacall_dir_get_next(listHandle, &fileNameLen); while (NULL != fileName) { // compose full path for the directory entry if (PCSL_STRING_OK != pcsl_string_dup(pSubDir, pDirEntry)) { javacall_dir_close(listHandle); pcsl_string_free(pSubDir); return; } pcsl_string_predict_size(pDirEntry, pcsl_string_length(pDirEntry) + 1 + fileNameLen); if (PCSL_STRING_OK != pcsl_string_append_char(pDirEntry, sep) || PCSL_STRING_OK != pcsl_string_append_buf(pDirEntry, fileName, fileNameLen)) { javacall_dir_close(listHandle); pcsl_string_free(pSubDir); pcsl_string_free(pDirEntry); return; } // check if directory entry is a subdirectory GET_PCSL_STRING_DATA_AND_LENGTH(pDirEntry) if (PCSL_STRING_PARAMETER_ERROR(pDirEntry)) { finish = 1; } else { res = javacall_fileconnection_dir_exists(pDirEntry_data, pDirEntry_len); } RELEASE_PCSL_STRING_DATA_AND_LENGTH if (finish) { javacall_dir_close(listHandle); pcsl_string_free(pSubDir); pcsl_string_free(pDirEntry); return; } if (JAVACALL_OK == res) { // found subdirectory, open it for listing javacall_dir_close(listHandle); pcsl_string_free(pSubDir); tmp = pDirEntry; pDirEntry = pSubDir; pSubDir = tmp; GET_PCSL_STRING_DATA_AND_LENGTH(pSubDir) if (PCSL_STRING_PARAMETER_ERROR(pSubDir)) { listHandle = NULL; } else { listHandle = javacall_dir_open(pSubDir_data, pSubDir_len); } RELEASE_PCSL_STRING_DATA_AND_LENGTH if (NULL == listHandle) { pcsl_string_free(pSubDir); return; } nestLevel++; } else { // found regular file, simply remove it GET_PCSL_STRING_DATA_AND_LENGTH(pDirEntry) if (PCSL_STRING_PARAMETER_ERROR(pDirEntry)) { res = JAVACALL_FAIL; } else { // ensure that the file is not read-only javacall_fileconnection_set_writable(pDirEntry_data, pDirEntry_len, JAVACALL_TRUE); res = javacall_file_delete(pDirEntry_data, pDirEntry_len); } RELEASE_PCSL_STRING_DATA_AND_LENGTH pcsl_string_free(pDirEntry); if (JAVACALL_OK != res) { javacall_dir_close(listHandle); pcsl_string_free(pSubDir); return; } } // iterate through the current directory fileName = javacall_dir_get_next(listHandle, &fileNameLen); }