/**
 * Converts the given suite ID to array of chars.
 * NOTE: this function returns a pointer to the static buffer!
 *
 * @param value suite id to convert
 *
 * @return char[] representation of the given suite ID
 * or NULL in case of error.
 */
const char* midp_suiteid2chars(SuiteIdType value) {
    jsize resLen;
    static jbyte resChars[GET_SUITE_ID_LEN(value) + 1]; /* +1 for last zero */
    const pcsl_string* pResString = midp_suiteid2pcsl_string(value);
    pcsl_string_status rc;

    rc = pcsl_string_convert_to_utf8(pResString, resChars,
        (jsize)SUITESTORE_COUNTOF(resChars), &resLen);

    return (rc == PCSL_STRING_OK) ? (char*)&resChars : NULL;
}
Beispiel #2
0
/**
 * The getUsedSpace function checks the size of used space by
 * all of the files that are contained in the directory.
 */
long pcsl_file_getusedspace(const pcsl_string * dirName)
{
    DIR *dir;
    long size;
    struct dirent *de;
    struct stat stat_buf;
    char filename[PCSL_FILE_MAX_NAME_LEN + 1];
    int rootLen;
    int exists;

    dir = (DIR *)pcsl_file_opendir(dirName);
    if (dir == NULL) {
        return 0;
    }

    size = 0;

    {
      if (pcsl_string_convert_to_utf8(dirName, (jbyte*)filename, sizeof(filename), NULL)
	  != PCSL_STRING_OK) {
	pcsl_file_closedir(dir);
	return 0;
      }

      rootLen = strlen(filename);
      for (de = readdir(dir); de != NULL;
	   de = readdir(dir), filename[rootLen] = 0) {
        if (strcmp(de->d_name, ".") == 0 ||
            strcmp(de->d_name, "..") == 0) {
	  continue;
        }

        strncat(filename, de->d_name, sizeof (filename) - 1 - rootLen);

        exists = stat(filename, &stat_buf);
        if (exists != -1 && !S_ISDIR(stat_buf.st_mode)) {
            size += stat_buf.st_size;
        }
      }

      pcsl_file_closedir(dir);
      return size;
    }
}
/**
 * Parses a value of MIDP_CONTROL_ARGS property from the midlet suite's
 * JAD file. Syntax of this property is defined by the following grammar:
 * <pre>
 * JAD_CONTROL_PARAM = "MIDP_CONTROL_ARGS" EQUAL *( ";" param)
 * param = report_level_param / log_channels_param / permissions_param /
 * trace_param / assert_param
 * report_level_param = "report_level" EQUAL 1*DIGIT
 * log_channels_param = "log_channels" EQUAL 1*DIGIT *( "," 1*DIGIT)
 * permissions_param  = "allow_all_permissions"
 * trace_param  = "enable_trace" EQUAL bool_val
 * assert_param = "enable_trace" EQUAL bool_val
 * bool_val = 0 / 1
 * </pre>
 *
 * @param pJadProps properties of the midlet suite from its jad file
 * @param pJadArgs [out] buffer where to store pointers to the name and value of
 * each control argument
 * @param maxJadArgs maximal number of arguments that the output buffer can hold
 */
void parse_control_args_from_jad(const MidpProperties* pJadProps,
        MIDP_JAD_CONTROL_ARGS* pJadArgs, int maxJadArgs) {
    int i, currArgNum = 0;
    int numOfJadProps = pJadProps ? pJadProps->numberOfProperties : 0;
    /* MIDP_CONTROL_ARGS */
    PCSL_DEFINE_STATIC_ASCII_STRING_LITERAL_START(propertyName) {
        'M', 'I', 'D', 'P', '_', 'C', 'O', 'N', 'T', 'R', 'O', 'L', '_',
        'A', 'R', 'G', 'S', '\0'
    } PCSL_DEFINE_STATIC_ASCII_STRING_LITERAL_END(propertyName);
    static jbyte propValue[128]; /* buffer to hold MIDP_ARGS jad property */

    for (i = 0; i < (numOfJadProps << 1); i++, i++) {
        if (pcsl_string_equals(&pJadProps->pStringArr[i], &propertyName)) {
            const char *pCurr;
            int propLen = pcsl_string_utf8_length(&pJadProps->pStringArr[i+1]);
            int start_pos, pos = 0;

            if (pcsl_string_convert_to_utf8(&pJadProps->pStringArr[i+1],
                    propValue, sizeof(propValue), NULL) != PCSL_STRING_OK) {
                break;
            }

            /* Parse the value of MIDP_CONTROL_ARGS jad property. */
            pCurr = (const char*)propValue;

            while (pos < propLen && currArgNum < maxJadArgs) {
                /* skip spaces */
                if (*pCurr == ' ') {
                    pCurr++;
                    pos++;
                    continue;
                }

                pJadArgs[currArgNum].pArgName = pCurr;
                start_pos = pos;

                /* look for "=", ";" or end-of-line) */
                while (pos < propLen && *pCurr) {
                    if (*pCurr == '=') {
                        pos++;
                        pJadArgs[currArgNum].pArgValue = ++pCurr;
                        break;
                    } else if (*pCurr == ';') {
                        pJadArgs[currArgNum].pArgValue = NULL;
                        break;
                    }
                    pos++;
                    pCurr++;
                }

                pJadArgs[currArgNum].argNameLen = pos - start_pos - 1;

                /* look for the next delimeter (";" or end-of-line) */
                while (pos < propLen && *pCurr) {
                    pos++;
                    if (*pCurr++ == ';') {
                        break;
                    }
                }

                currArgNum++;
            } /* end while */

            break;
        } /* end if MIDP_ARGS property found in JAD */
    } /* end for each property */

    pJadArgs[currArgNum].pArgName = pJadArgs[currArgNum].pArgValue = NULL;
    pJadArgs[currArgNum].argNameLen = 0;
}
/**
 * Get size in bytes of all files and possibly subdirectories contained
 * in the specified dir.
 *
 * @param pathName          full directory path
 * @param pathNameLen       length of path name
 * @param includeSubdirs    if 0, do not include subdirectories,
 *                          otherwise include subdirectories size too
 * @param result            returned value: size in bytes of all files contained in
 *                          the specified directory and possibly its subdirectories
 * @return 0 on success, -1 otherwise
 */
int fileconnection_dir_content_size(const jchar* pathName,
                                    int pathNameLen,
                                    int includeSubdirs,
                                    jlong* /* OUT */ result) {
    char subSearch[MAX_FILE_NAME_LENGTH];
    int subSearchLen;
    struct dirent *dir_data;
    struct stat stat_buf;
    int status;
    jlong contentSize = 0;
    DIR* listHandle[MAX_DIRECTORY_NESTING_LEVEL];
    int pathLen[MAX_DIRECTORY_NESTING_LEVEL];
    int nestLevel = 0;
    int nextExists;
    pcsl_string name = PCSL_STRING_NULL;

    if (PCSL_STRING_OK != pcsl_string_convert_from_utf16(pathName, pathNameLen, &name)) {
        return -1;
    }
    if (PCSL_STRING_OK != pcsl_string_convert_to_utf8(&name, (jbyte*)subSearch, MAX_FILE_NAME_LENGTH, &subSearchLen)) {
        pcsl_string_free(&name);
        return -1;
    }
    pcsl_string_free(&name);

    listHandle[0] = opendir(subSearch);
    pathLen[0] = subSearchLen;
    if (NULL == listHandle[0]) {
        /* Cannot open directory */
        return -1;
    }
    dir_data = readdir(listHandle[0]);
    nextExists = (NULL != dir_data);

    for ( ; ; ) {
        while (nextExists) {
            subSearch[pathLen[nestLevel]] = '/';
            subSearch[pathLen[nestLevel] + 1] = 0;
            strcat(subSearch, dir_data->d_name);
            status = stat(subSearch, &stat_buf);
            if (status < 0) {
                /* Failed to get file status */
                while (nestLevel >= 0) {
                    closedir(listHandle[nestLevel--]);
                }
                return -1;
            }
            if (S_ISDIR(stat_buf.st_mode)) {
                /* Found subdirectory */
                if (includeSubdirs) {
                    /* Must count subdirectory sizes */
                    if (strcmp(dir_data->d_name, ".") && strcmp(dir_data->d_name, "..")) {
                        /* The subdirectory is not "." or ".." */
                        int dirNameLen = strlen(dir_data->d_name);
                        if (nestLevel >= MAX_DIRECTORY_NESTING_LEVEL - 1) {
                            /* Nesting level overflow */
                            while (nestLevel >= 0) {
                                closedir(listHandle[nestLevel--]);
                            }
                            return -1;
                        }
                        pathLen[nestLevel + 1] = pathLen[nestLevel] + dirNameLen + 1;
                        listHandle[++nestLevel] = opendir(subSearch);
                        if (NULL == listHandle[nestLevel]) {
                            /* Cannot open subdirectory */
                            while (--nestLevel >= 0) {
                                closedir(listHandle[nestLevel]);
                            }
                            return -1;
                        }
                    }
                }
            } else {
                contentSize += stat_buf.st_size;
            }
            dir_data = readdir(listHandle[nestLevel]);
            nextExists = (NULL != dir_data);
        }
        closedir(listHandle[nestLevel]);
        if (nestLevel == 0) {
            break;
        }
        dir_data = readdir(listHandle[--nestLevel]);
        nextExists = (NULL != dir_data);
    }

    *result = contentSize;
    return 0;
}