/** * 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); }
/** * The getUsedSpace function checks the size of used space in storage. */ long pcsl_file_getusedspace(const pcsl_string * systemDir) { long used = 0; void* pIterator; pcsl_string current = PCSL_STRING_NULL; struct _stat stat_buf; pIterator = pcsl_file_openfilelist(systemDir); for (; ; ) { if (pcsl_file_getnextentry(pIterator, systemDir, ¤t) == -1) { break; } { const jchar * pwszFilename = pcsl_string_get_utf16_data(¤t); if (NULL == pwszFilename) { break; } /* Don't count the subdirectories "." and ".." */ if (_wstat(pwszFilename, &stat_buf) != -1 && !S_ISDIR(stat_buf.st_mode)) { used += stat_buf.st_size; } pcsl_string_release_utf16_data(pwszFilename, ¤t); } pcsl_string_free(¤t); } pcsl_file_closefilelist(pIterator); return used; }
/* * Returns the handle that represents the savedRootlength, savedDirectory * etc. This handle needs to be passed to storage_get_next_file_in_iterator() * in order to get the filename that matches with a given string. In * order to clean-up the memory storageCloseFileIterator() must be * called to close the handle properly. * * Returns NULL if memory allocation fails * or root directory of the input 'string' can not be found * */ void* storage_open_file_iterator(const pcsl_string* string_str) { void * iterator = NULL; iterator = pcsl_file_openfilelist(string_str); return iterator; }
/** * The getUsedSpace function checks the size of used space in storage. */ long pcsl_file_getusedspace(const pcsl_string * systemDir) { long used = 0; void* pIterator; pcsl_string current = PCSL_STRING_NULL; pIterator = pcsl_file_openfilelist(systemDir); for (; ; ) { if (pcsl_file_getnextentry(pIterator, systemDir, ¤t) == -1) { break; } { long size = pcsl_file_sizeof(¤t); if (size >= 0) used += size; } pcsl_string_free(¤t); } pcsl_file_closefilelist(pIterator); return used; }