Exemplo n.º 1
0
long internal_get_used_helper(const javacall_utf16* unicodeSystemDirName,
                              int systemDirNameLen) {

          long used = 0;
          javacall_handle pIterator;
          javacall_utf16 *current;
          int currentLen;
          wchar_t wOsFilename[JAVACALL_MAX_FILE_NAME_LENGTH]; // max file name

          struct _stat stat_buf;

          pIterator = javacall_dir_open(unicodeSystemDirName, systemDirNameLen);
          for (; ; ) {
              if ((current = javacall_dir_get_next(pIterator, &currentLen)) == NULL) {
                      break;
              }

              if( currentLen > JAVACALL_MAX_FILE_NAME_LENGTH ) {
                    javacall_print("internal_get_used_helper: File name length too big");
                    return JAVACALL_FAIL;
              }

              memcpy(wOsFilename, current, currentLen*sizeof(wchar_t));
              wOsFilename[currentLen] = 0;


              /* Don't count the subdirectories "." and ".." */
              if (_wstat(wOsFilename, &stat_buf) != -1 &&
                  !S_ISDIR(stat_buf.st_mode)) {
                      used += stat_buf.st_size;
              }


          }

          javacall_dir_close(pIterator);
          return used;
      }
Exemplo n.º 2
0
/**
 * 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);
        }