/** * 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); }
/* * Delete a file in storage. * * If not successful *ppszError will set to point to an error string, * on success it will be set to NULL. */ void storage_delete_file(char** ppszError, const pcsl_string* filename_str) { int status; DEBUGP2F("trying to delete %s\n", filename_str); status = pcsl_file_unlink(filename_str); if (status < 0) { *ppszError = storage_get_last_file_error("storage_delete_file", filename_str); return; } DEBUGP2F("deleted %s\n", filename_str); *ppszError = NULL; }
/** * Tests for pcsl_file_get_time(). */ void testTimes() { int res; long time1, time2; int fileID; int i, j; res = pcsl_file_get_time(&dummy1, PCSL_FILE_TIME_LAST_MODIFIED, &time1); assertTrue("Time query succeeded for non-existent file", res == -1); res = pcsl_file_open(&file1, PCSL_FILE_O_RDWR | PCSL_FILE_O_TRUNC | PCSL_FILE_O_CREAT, (void **)&fileID); pcsl_file_close((void *)fileID); assertTrue("File creation failed", res == 0); res = pcsl_file_get_time(&file1, PCSL_FILE_TIME_LAST_MODIFIED, &time1); assertTrue("Time query failed", res == 0); res = pcsl_file_open(&file1, PCSL_FILE_O_RDWR | PCSL_FILE_O_APPEND, (void **)&fileID); assertTrue("File opening failed", res == 0); for (i = 0; i < 1000000; i++) { res = pcsl_file_write((void *)fileID, (unsigned char *)&time1, 4); assertTrue("Write failed", res == 4); } pcsl_file_close((void *)fileID); /* Writing 4 MB to a file should take several seconds */ res = pcsl_file_get_time(&file1, PCSL_FILE_TIME_LAST_MODIFIED, &time2); assertTrue("Time query failed", res == 0); assertTrue("Modification time should be greater", time2 > time1); for (j = 0; j < 10; j++) { res = pcsl_file_open(&file1, PCSL_FILE_O_RDWR | PCSL_FILE_O_APPEND, (void **)&fileID); assertTrue("File opening failed", res == 0); for (i = 0; i < 1000000; i++) { res = pcsl_file_read((void *)fileID, (unsigned char *)&time1, 4); assertTrue("Read failed", res == 4); } pcsl_file_close((void *)fileID); } /* Reading anything from a file shouldn't lead to modification time change */ res = pcsl_file_get_time(&file1, PCSL_FILE_TIME_LAST_MODIFIED, &time1); assertTrue("Time query failed", res == 0); assertTrue("Modification time shouldn't change", time1 == time2); pcsl_file_unlink(&file1); }
int OsFile_remove(const JvmPathChar * filename) { int name_len = fn_strlen(filename); pcsl_string pcsl_filename = PCSL_STRING_NULL; GUARANTEE(sizeof(jchar) == sizeof(JvmPathChar), "Types must match"); if (pcsl_string_convert_from_utf16(filename, name_len, &pcsl_filename) != PCSL_STRING_OK) { return -1; } int result = pcsl_file_unlink(&pcsl_filename); pcsl_string_free(&pcsl_filename); return result; }
/** * Tests for pcsl_file_get_attribute() and pcsl_file_set_attribute(). */ void testAttributes() { int res; int fileID; int attr; res = pcsl_file_open(&file1, PCSL_FILE_O_RDWR | PCSL_FILE_O_TRUNC | PCSL_FILE_O_CREAT, (void **)&fileID); pcsl_file_close((void *)fileID); assertTrue("File creation failed", res == 0); /* All files are considered readable on win32 */ res = pcsl_file_set_attribute(&file1, PCSL_FILE_ATTR_READ, 0); assertTrue("Attributes setting failed", res == 0); res = pcsl_file_get_attribute(&file1, PCSL_FILE_ATTR_READ, &attr); assertTrue("Attributes query failed", res == 0); assertTrue("File isn't reported as readable", attr == 1); res = pcsl_file_set_attribute(&file1, PCSL_FILE_ATTR_READ, 1); assertTrue("Attributes setting failed", res == 0); res = pcsl_file_get_attribute(&file1, PCSL_FILE_ATTR_READ, &attr); assertTrue("Attributes query failed", res == 0); assertTrue("File isn't reported as readable", attr == 1); res = pcsl_file_set_attribute(&file1, PCSL_FILE_ATTR_WRITE, 0); assertTrue("Attributes setting failed", res == 0); res = pcsl_file_get_attribute(&file1, PCSL_FILE_ATTR_WRITE, &attr); assertTrue("Attributes query failed", res == 0); assertTrue("Read-only file is reported as writable", attr == 0); res = pcsl_file_set_attribute(&file1, PCSL_FILE_ATTR_WRITE, 1); assertTrue("Attributes setting failed", res == 0); res = pcsl_file_get_attribute(&file1, PCSL_FILE_ATTR_WRITE, &attr); assertTrue("Attributes query failed", res == 0); assertTrue("Writable file is reported as read-only", attr == 1); /* All files are considered executable on win32 */ res = pcsl_file_set_attribute(&file1, PCSL_FILE_ATTR_EXECUTE, 0); assertTrue("Attributes setting failed", res == 0); res = pcsl_file_get_attribute(&file1, PCSL_FILE_ATTR_EXECUTE, &attr); assertTrue("Attributes query failed", res == 0); assertTrue("File isn't reported as executable", attr == 1); res = pcsl_file_set_attribute(&file1, PCSL_FILE_ATTR_EXECUTE, 1); assertTrue("Attributes setting failed", res == 0); res = pcsl_file_get_attribute(&file1, PCSL_FILE_ATTR_EXECUTE, &attr); assertTrue("Attributes query failed", res == 0); assertTrue("File isn't reported as executable", attr == 1); res = pcsl_file_set_attribute(&file1, PCSL_FILE_ATTR_HIDDEN, 1); assertTrue("Attributes setting failed", res == 0); res = pcsl_file_get_attribute(&file1, PCSL_FILE_ATTR_HIDDEN, &attr); assertTrue("Attributes query failed", res == 0); assertTrue("File is reported as executable", attr == 1); res = pcsl_file_set_attribute(&file1, PCSL_FILE_ATTR_HIDDEN, 0); assertTrue("Attributes setting failed", res == 0); res = pcsl_file_get_attribute(&file1, PCSL_FILE_ATTR_HIDDEN, &attr); assertTrue("Attributes query failed", res == 0); assertTrue("File is reported as executable", attr == 0); pcsl_file_unlink(&file1); }