/**
 * Opens a file and fills the content of the file in the result_buf. <BR>
 * This function made memory allocation inside.
 *
 * @param filename   Path to the file.
 * @param result_buf Pointer to the buffer that will be filled by content of the file.
 * @return buffer file size in bytes
 */
long readJadFile(const pcsl_string * filename, char** result_buf) {
    int fd = 0;
    char* err = NULL;
    long bufsize = -1;
    int numread = 0;
    char* res = *result_buf;

    if (pcsl_string_length(filename) <= 0) {
        REPORT_INFO(LC_AMS, "readJadFile():No file name.");
        return BAD_PARAMS;
    }

    fd = storage_open(&err, filename, OPEN_READ);
    if(err != NULL) {
        REPORT_INFO1(LC_AMS, "readJadFile():Can't open jad file '%s'",err);
        storageFreeError(err);
        return NO_JAD_FILE;
    }

    bufsize = storageSizeOf(&err, fd);
    if((bufsize <= 0) || (err != NULL)) {
        REPORT_INFO1(LC_AMS,  "readJadFile():Problem getting file size: %s",
		     err );
        storageFreeError(err);
        return IO_ERROR;
    }

    res = (char*)midpMalloc(bufsize+1);
    if (res == NULL || (err != NULL)) {
        REPORT_INFO1(LC_AMS, "readJadFile():Can't allocate memory. %s", err);
        storageFreeError(err);
        return OUT_OF_MEMORY;
    }

    memset(res,0,(bufsize+1));

    REPORT_INFO2(LC_AMS, "fd = %d, bufsize = %ld\n", fd, bufsize);

    numread = storageRead(&err, fd, res, bufsize);
    if((numread <= 0) || (numread != bufsize) || (err)) {
        REPORT_INFO3(LC_AMS, "size = %ld, numread = %d, err = %s.",
               bufsize, numread, err);
        storageClose(&err, fd);
        return IO_ERROR;
    }

    REPORT_INFO2(LC_AMS, "size = %ld, numread = %d", bufsize, numread);

    storageClose(&err, fd);
    if(err != NULL) {
        REPORT_INFO1(LC_AMS, "Can't close jad file %s\n", err);
    }
    *result_buf = res;
    return bufsize;
} /* end of readJadFile */
Esempio n. 2
0
/**
 * 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 fileName 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* fileName,
           char* inBuffer, long inBufferLen) {
    int handle, status = ALL_OK;
    char* pszTemp;

    *ppszError  = NULL;

    /* open the file */
    handle = storage_open(ppszError, fileName, OPEN_WRITE);
    if (*ppszError != NULL) {
        return IO_ERROR;
    }

    /* write the whole buffer */
    if (inBufferLen > 0) {
        storageWrite(ppszError, handle, inBuffer, inBufferLen);
    } else {
        storageTruncate(ppszError, handle, 0);
    }

    if (*ppszError != NULL) {
        status = IO_ERROR;
    }

    /* close the file */
    storageClose(&pszTemp, handle);
    storageFreeError(pszTemp);

    return status;
}
/**
 * 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;
}
Esempio n. 4
0
/**
 * Closes registry file
 *
 * @return operation result
 */
static jsr211_result close_table_file(void) {
  storageClose(&io_error_message, table_file);
  
  if (io_error_message) {
    return JSR211_FAILED;
  }
  
  return JSR211_OK;
}
/**
 * Writes the settings of a MIDlet suite to persistent storage.
 * <pre>
 * The format of the properties file will be:
 *
 *   push interrupt setting as an jbyte
 *   length of a permissions as an int
 *   array of permissions jbytes
 *   push options as jint
 * </pre>
 *
 * @param ppszError pointer to character string pointer to accept an error
 * @param suiteId  ID of the suite
 * @param enabled enabled setting
 * @param pushInterrupt pointer to a push interruptSetting
 * @param pushOptions user options for push interrupts
 * @param pPermissions pointer a pointer to accept a permissions array
 * @param numberOfPermissions length of pPermissions
 *
 * @return error code (ALL_OK if successful)
 */
MIDPError
write_settings(char** ppszError, SuiteIdType suiteId, jboolean enabled,
               jbyte pushInterrupt, jint pushOptions,
               jbyte* pPermissions, int numberOfPermissions) {
    pcsl_string filename;
    int handle;
    char* pszTemp;
    MIDPError status;

    *ppszError = NULL;

    status = build_suite_filename(suiteId, &SETTINGS_FILENAME, &filename);
    if (status != ALL_OK) {
        return status;
    }

    handle = storage_open(ppszError, &filename, OPEN_READ_WRITE_TRUNCATE);
    pcsl_string_free(&filename);
    if (*ppszError != NULL) {
        return IO_ERROR;
    }

    storageWrite(ppszError, handle, (char*)&enabled, sizeof (jboolean));
    do {
        if (*ppszError != NULL) {
            break;
        }

        storageWrite(ppszError, handle, (char*)&pushInterrupt, sizeof (jbyte));
        if (*ppszError != NULL) {
            break;
        }

        storageWrite(ppszError, handle, (char*)&numberOfPermissions,
                                sizeof (int));
        if (*ppszError != NULL) {
            break;
        }

        storageWrite(ppszError, handle, (char*)pPermissions,
                                numberOfPermissions * sizeof (jbyte));

        storageWrite(ppszError, handle, (char*)&pushOptions, sizeof (jint));
        if (*ppszError != NULL) {
            break;
        }
    } while (0);

    if (*ppszError != NULL) {
        status = IO_ERROR;
    }

    storageClose(&pszTemp, handle);
    storageFreeError(pszTemp);

    return ALL_OK;
}
/**
 * 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;
}
/**
 * Reads named secure resource of the suite with specified suiteId
 * from secure persistent storage.
 *
 * Note that when porting memory for the in/out parameter
 * returnValue 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 resourceName      The name of secure resource to read from storage
 * @param returnValue       The in/out parameter that will return the
 *                          value part of the requested secure resource
 *                          (NULL is a valid return value)
 * @param valueSize         The length of the secure resource value
 *
 * @return one of the error codes:
 * <pre>
 *     ALL_OK, OUT_OF_MEMORY, NOT_FOUND,
 *     SUITE_CORRUPTED_ERROR, BAD_PARAMS
 * </pre>
 */
MIDPError
midp_suite_read_secure_resource(SuiteIdType suiteId,
                                const pcsl_string* resourceName,
                                jbyte **returnValue,
                                jint *valueSize) {
    pcsl_string filename = PCSL_STRING_NULL;
    char *pszError = NULL;
    MIDPError errorCode;
    int bytesRead;
    int handle;

    *returnValue = NULL;
    *valueSize = 0;

    errorCode = get_secure_resource_file(suiteId, resourceName, &filename);
    if (errorCode != ALL_OK) {
        pcsl_string_free(&filename);
        return errorCode;
    }

    handle = storage_open(&pszError, &filename, OPEN_READ);
    pcsl_string_free(&filename);
    if (pszError != NULL) {
        storageFreeError(pszError);
        return SUITE_CORRUPTED_ERROR;
    }

    do {
        bytesRead = storageRead(&pszError, handle,
            (char*)valueSize, sizeof (int));
        if (bytesRead != sizeof (int) || *valueSize == 0)
            break;

        *returnValue = (jbyte*)pcsl_mem_malloc(*valueSize * sizeof (jbyte));
        if (*returnValue == NULL) {
            errorCode = OUT_OF_MEMORY;
            break;
        }

        bytesRead = storageRead(&pszError, handle, (char*)(*returnValue),
            *valueSize * sizeof (jbyte));

        if (pszError != NULL || bytesRead != *valueSize) {
            errorCode = SUITE_CORRUPTED_ERROR;
            pcsl_mem_free(*returnValue);
            *returnValue = NULL;
            break;
        }
    } while (0);

    storageClose(&pszError, handle);
    storageFreeError(pszError);

    return errorCode;
}
Esempio n. 8
0
File: emul.c Progetto: sfsy1989/j2me
/**
 * Saves class of device and service classes for the next 
 * usage of the same device. Makes nothing if an IO error
 * occured.
 */
static void saveCod(int cod) {
    char *error;
    int handle = openCodFile();
    
    if (-1 != handle) {
        storageWrite(&error, handle, (char*)&cod, sizeof(int));
        storageFreeError(error);
    }
    
    storageClose(&error, handle);
    storageFreeError(error);
}
Esempio n. 9
0
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;
}
Esempio n. 11
0
/**
 * Copies a file.
 *
 * @param srcName source file
 * @param destName destination file
 *
 * @return 0 for success
 */
static int copyFile(const pcsl_string * srcName, const pcsl_string * destName) {
    char* pszError = NULL;
    char* pszDummy = NULL;
    int src;
    int dest;
    char buffer[1024];
    long bytesRead;

    src = storage_open(&pszError, srcName, OPEN_READ);
    if (pszError == NULL) {
        dest = storage_open(&pszError, destName, OPEN_READ_WRITE_TRUNCATE);
        if (pszError == NULL) {
            bytesRead = storageRead(&pszError, src, buffer, sizeof (buffer));
            while (pszError == NULL && bytesRead > 0) {
                storageWrite(&pszError, dest, buffer, bytesRead);
                if (pszError == NULL) {
                    bytesRead = storageRead(&pszError, src, buffer,
                                            sizeof (buffer));
                }
            }

            storageClose(&pszDummy, dest);
            storageFreeError(pszDummy);
            pszDummy = NULL;
        }

        storageClose(&pszDummy, src);
        storageFreeError(pszDummy);
    }

    if (pszError != NULL) {
        REPORT_ERROR1(LC_AMS, "Error while copying file: %s", pszError);
        storageFreeError(pszError);
        return -1;
    }

    return 0;
}
Esempio n. 12
0
void
midpCloseJar(void* handle) {
    MidpJarInfo* pJarInfo = (MidpJarInfo*)handle;
    char* pszError;

    if (handle == NULL) {
        return;
    }

    storageClose(&pszError, (int)(pJarInfo->fileObj.state));
    storageFreeError(pszError);

    midpFree(pJarInfo);
}
Esempio n. 13
0
File: emul.c Progetto: sfsy1989/j2me
/**
 * Returns class of device and service classes saved for an 
 * emulated device with the same address previous time. 
 * It is considered to be the same device as current one.
 */
static int loadCod() {
    char *error;
    int cod = DEFAULT_COD;
    int handle = openCodFile();
    
    if (-1 != handle) {
        storageRead(&error, handle, (char*)&cod, sizeof(int));
        storageFreeError(error);
    }
    
    storageClose(&error, handle);
    storageFreeError(error);
    
    return cod;
}
/**
 * Writes named secure resource of the suite with specified suiteId
 * to secure persistent storage.
 *
 * @param suiteId           The suite id used to identify the MIDlet suite
 * @param resourceName      The name of secure resource to read from storage
 * @param value             The value part of the secure resource to be stored
 * @param valueSize         The length of the secure resource value
 *
 * @return one of the error codes:
 * <pre>
 *     ALL_OK, OUT_OF_MEMORY, NOT_FOUND,
 *     SUITE_CORRUPTED_ERROR, BAD_PARAMS
 * </pre>
 */
MIDPError
midp_suite_write_secure_resource(SuiteIdType suiteId,
                                 const pcsl_string* resourceName,
                                 jbyte *value,
                                 jint valueSize) {
    pcsl_string filename = PCSL_STRING_NULL;
    char *pszError = NULL;
    MIDPError errorCode;
    int handle;

    errorCode = get_secure_resource_file(suiteId, resourceName,
                                         &filename);
    if (errorCode != ALL_OK) {
        pcsl_string_free(&filename);
        return errorCode;
    }

    handle = storage_open(&pszError, &filename, OPEN_READ_WRITE_TRUNCATE);
    pcsl_string_free(&filename);
    if (pszError != NULL) {
        storageFreeError(pszError);
        return SUITE_CORRUPTED_ERROR;
    }

    do {
        storageWrite(&pszError, handle, (char*)&valueSize, sizeof (int));
        if (pszError != NULL) break;
        storageWrite(&pszError, handle, (char*)value,
            valueSize * sizeof (jbyte));
        if (pszError != NULL) break;
    } while (0);

    if (pszError != NULL) {
        errorCode = SUITE_CORRUPTED_ERROR;
        storageFreeError(pszError);
    }

    storageClose(&pszError, handle);
    storageFreeError(pszError);

    return errorCode;
}
KNIEXPORT KNI_RETURNTYPE_VOID
KNIDECL(com_sun_midp_chameleon_skins_resources_LoadedSkinData_beginReadingSkinFile) {
    const unsigned char* skin_description = lfj_get_skin_description();

    if (skin_description != NULL) {
        gsSkinFileDataStart = gsSkinFileDataPos =
            (unsigned char*)skin_description;
        gsSkinFileDataEnd = gsSkinFileDataStart +
            lfj_get_skin_description_size();
    } else {
        char* errorStr = NULL;
        int fileHandle = -1;
        int fileSize;
        int bytesRead;
        jfieldID fid;

        KNI_StartHandles(2);
        KNI_DeclareHandle(classHandle);

        KNI_GetClassPointer(classHandle);

        fid = KNI_GetStaticFieldID(classHandle, "STRING_ENCODING_USASCII", "B");
        STRING_ENCODING_USASCII = (unsigned char)
            KNI_GetStaticByteField(classHandle, fid);

        fid = KNI_GetStaticFieldID(classHandle, "STRING_ENCODING_UTF8", "B");
        STRING_ENCODING_UTF8 = (unsigned char)
            KNI_GetStaticByteField(classHandle, fid);

        GET_PARAMETER_AS_PCSL_STRING(1, fileName);

        do {
            /*
             * Open skin file
             */
            fileHandle = storage_open(&errorStr, &fileName, OPEN_READ);
            if (errorStr != NULL) {
                KNI_ThrowNew(midpIOException, errorStr);
                storageFreeError(errorStr);
                break;
            }

            /*
             * Obtain file size
             */
            fileSize = storageSizeOf(&errorStr, fileHandle);
            if (errorStr != NULL) {
                KNI_ThrowNew(midpIOException, errorStr);
                storageFreeError(errorStr);
                break;
            }

            /*
             * Read whole file into heap memory
             */
            gsSkinFileDataStart = (unsigned char*)midpMalloc(fileSize);
            if (gsSkinFileDataStart == NULL) {
                KNI_ThrowNew(midpOutOfMemoryError, NULL);
                break;
            }

            bytesRead = storageRead(&errorStr, fileHandle,
                    (char*)gsSkinFileDataStart, fileSize);
            if (errorStr != NULL) {
                KNI_ThrowNew(midpIOException, errorStr);
                storageFreeError(errorStr);
                midpFree(gsSkinFileDataStart);
                gsSkinFileDataStart = NULL;
                break;
            }
            if (bytesRead != fileSize) {
                KNI_ThrowNew(midpIOException, "Failed to read whole file");
                midpFree(gsSkinFileDataStart);
                gsSkinFileDataStart = NULL;
                break;
            }

            gsSkinFileDataPos = gsSkinFileDataStart;
            gsSkinFileDataEnd = gsSkinFileDataStart + fileSize;

        } while (0);

        RELEASE_PCSL_STRING_PARAMETER;

        /*
         * Close skin file
         */
        if (fileHandle != -1) {
            storageClose(&errorStr, fileHandle);
        }

        KNI_EndHandles();
    }

    KNI_ReturnVoid();
}
/**
 * Loads the properties of a MIDlet suite from persistent storage.
 *
 * @param suiteId ID of the suite
 * @param pJadProps [out] pointer to a structure containing an array of strings,
 * in a pair pattern of key and value; NULL may be passed if it is not required
 * to read JAD properties
 * @param pJarProps [out] pointer to a structure containing an array of strings,
 * in a pair pattern of key and value; NULL may be passed if it is not required
 * to read JAR properties
 *
 * @return error code (ALL_OK for success)
 */
MIDPError
load_install_properties(SuiteIdType suiteId, MidpProperties* pJadProps,
                        MidpProperties* pJarProps) {
    pcsl_string filename;
    char* pszError = NULL;
    int handle, i, n;
    int numberOfProps;
    MIDPError status;

    status = get_property_file(suiteId, KNI_TRUE, &filename);
    if (status != ALL_OK) {
        return status;
    }

    handle = storage_open(&pszError, &filename, OPEN_READ);
    pcsl_string_free(&filename);
    if (pszError != NULL) {
        storageFreeError(pszError);
        return IO_ERROR;
    }

    status = ALL_OK;

    /* Read JAD, then JAR properties. */
    for (n = 0; n < 2; n++) {
        MidpProperties *pProps = n ? pJarProps : pJadProps;
        if (!pProps) {
            continue;
        }

        storageRead(&pszError, handle, (char*)&numberOfProps,
            sizeof (numberOfProps));
        if (pszError != NULL) {
            break;
        }

        pProps->pStringArr = alloc_pcsl_string_list(numberOfProps << 1);

        for (i = 0; i < numberOfProps << 1; i++) {
            storage_read_utf16_string(&pszError, handle,
                                      &pProps->pStringArr[i]);
            if (pszError != NULL) {
                break;
            }
        }

        if (pszError != NULL) {
            break;
        }

        pProps->numberOfProperties = numberOfProps;
    }

    if (pszError != NULL) {
        status = IO_ERROR;
        storageFreeError(pszError);
    }

    storageClose(&pszError, handle);
    storageFreeError(pszError);

    if (status != ALL_OK) {
        if (pJadProps) {
            free_pcsl_string_list(pJadProps->pStringArr,
                                  pJadProps->numberOfProperties << 1);
        }
        if (pJarProps) {
            free_pcsl_string_list(pJarProps->pStringArr,
                                  pJarProps->numberOfProperties << 1);
        }
    }

    return status;
}
Esempio n. 17
0
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;
}
/**
 * Gets the settings of a MIDlet suite from persistent storage.
 * <pre>
 * The format of the properties file will be:
 *
 *   push interrupt setting as an jbyte
 *   length of a permissions as an int
 *   array of permissions jbytes
 *   push options as jint
 * </pre>
 *
 * @param ppszError pointer to character string pointer to accept an error
 * @param suiteId  ID of the suite
 * @param pEnabled pointer to an enabled setting
 * @param pPushInterrupt pointer to a push interruptSetting
 * @param pPushOptions user options for push interrupts
 * @param ppPermissions pointer a pointer to accept a permissions array
 * @param pNumberOfPermissions pointer to an int
 *
 * @return error code (ALL_OK if successful)
 */
MIDPError
read_settings(char** ppszError, SuiteIdType suiteId, jboolean* pEnabled,
             jbyte* pPushInterrupt, jint* pPushOptions,
             jbyte** ppPermissions, int* pNumberOfPermissions) {
    pcsl_string filename;
    int handle;
    int bytesRead;
    char* pszTemp;
    MIDPError status;

    *ppszError = NULL;
    *ppPermissions = NULL;
    *pNumberOfPermissions = 0;

    status = build_suite_filename(suiteId, &SETTINGS_FILENAME, &filename);
    if (status != ALL_OK) {
        return status;
    }

    handle = storage_open(ppszError, &filename, OPEN_READ);
    pcsl_string_free(&filename);
    if (*ppszError != NULL) {
        return IO_ERROR;
    }

    bytesRead = storageRead(ppszError, handle, (char*)pEnabled,
        sizeof (jboolean));
    do {
        if (*ppszError != NULL) {
            status = IO_ERROR;
            break;
        }

        bytesRead = storageRead(ppszError, handle, (char*)pPushInterrupt,
                                sizeof (jbyte));
        if (*ppszError != NULL) {
            status = IO_ERROR;
            break;
        }

        bytesRead = storageRead(ppszError, handle, (char*)pNumberOfPermissions,
                                sizeof (int));

        if (bytesRead != sizeof (int) || *pNumberOfPermissions == 0) {
            status = IO_ERROR;
            break;
        }

        *ppPermissions = (jbyte*)pcsl_mem_malloc(
                *pNumberOfPermissions * sizeof (jbyte));
        if (*ppPermissions == NULL) {
            status = OUT_OF_MEMORY;
            break;
        }

        bytesRead = storageRead(ppszError, handle, (char*)(*ppPermissions),
                                *pNumberOfPermissions * sizeof (jbyte));

        if (bytesRead != *pNumberOfPermissions) {
            *pNumberOfPermissions = 0;
            pcsl_mem_free(*ppPermissions);
            status = SUITE_CORRUPTED_ERROR;
            break;
        }

        /* Old versions of the file may not have options. */
        status = ALL_OK;
        *pPushOptions = 0;
        bytesRead = storageRead(ppszError, handle, (char*)pPushOptions,
                                sizeof (jint));
        if (*ppszError != NULL) {
            storageFreeError(*ppszError);
            *ppszError = NULL;
            break;
        }
    } while (0);

    storageClose(&pszTemp, handle);
    storageFreeError(pszTemp);

    return status;
}
/**
 * Retrieves the list of strings in a file.
 * The file has the number of strings at the front, each string
 * is a length and the jchars.
 *
 * @param ppszError pointer to character string pointer to accept an error
 * @param pFilename name of the file of strings
 * @param paList pointer to an array of pcsl_strings, free with
 *               free_pcsl_string_list
 * @param pStringNum number of strings if successful (can be 0)
 *
 * @return error code (ALL_OK if successful)
 */
static MIDPError
get_string_list(char** ppszError, const pcsl_string* pFilename,
                pcsl_string** paList, int* pStringNum) {
    char* pszTemp;
    int i = 0;
    int handle;
    int numberOfStrings = 0;
    pcsl_string* pStrings = NULL;
    MIDPError status = ALL_OK;

    *ppszError = NULL;
    *paList = NULL;
    *pStringNum = 0;

    handle = storage_open(ppszError, pFilename, OPEN_READ);
    if (*ppszError != NULL) {
        return IO_ERROR;
    }

    do {
        storageRead(ppszError, handle, (char*)&numberOfStrings,
            sizeof (numberOfStrings));
        if (*ppszError != NULL) {
            status = IO_ERROR;
            break;
        }
        if (numberOfStrings == 0) {
            break;
        }

        pStrings = alloc_pcsl_string_list(numberOfStrings);
        if (pStrings == NULL) {
            status = OUT_OF_MEMORY;
            break;
        }

        for (i = 0; i < numberOfStrings; i++) {
            pStrings[i] = PCSL_STRING_NULL;
        }

        for (i = 0; i < numberOfStrings; i++) {
            storage_read_utf16_string(ppszError, handle, &pStrings[i]);
            if (*ppszError != NULL) {
                status = IO_ERROR;
                break;
            }

        }

        if (i != numberOfStrings) {
            status = SUITE_CORRUPTED_ERROR;
            break;
        }
    } while (0);

    storageClose(&pszTemp, handle);
    storageFreeError(pszTemp);

    if (status == ALL_OK) {
        *paList = pStrings;
        *pStringNum = numberOfStrings;
    } else if (pStrings != NULL) {
        free_pcsl_string_list(pStrings, i);
    }

    return status;
}
void StorageWidget::storageCloseSlot()
{
	storageClose(_widgets);
}