/** * 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); }
/* * Return a 32 bit handle to an open a file in storage in different modes. * * See "I/O Modes" and "Filenames" above for move information. * * If not successful *ppszError will set to point to an error string, * on success it will be set to NULL. */ int storage_open(char** ppszError, const pcsl_string* filename_str, int ioMode) { int flags = 0; int openStatus; void *handle; *ppszError = NULL; if (OPEN_READ == ioMode) { DEBUGP2F("opening for read only %s\n", filename_str); flags |= PCSL_FILE_O_RDONLY; } else { if (!storage_file_exists(filename_str)) { flags |= PCSL_FILE_O_CREAT; } else if (OPEN_READ_WRITE_TRUNCATE == ioMode) { flags |= PCSL_FILE_O_TRUNC; } if (OPEN_WRITE == ioMode) { DEBUGP2F("opening write only %s\n", filename_str); flags |= PCSL_FILE_O_WRONLY; } else { DEBUGP2F("opening read write %s\n", filename_str); flags |= PCSL_FILE_O_RDWR; } } /** * Verify that the resource is available well within limit as per * the policy in ResourceLimiter */ if (midpCheckResourceLimit(RSC_TYPE_FILE, 1) == 0) { REPORT_INFO(LC_CORE, "Resource limit exceeded for file handles"); *ppszError = (char *)FILE_LIMIT_ERROR; return -1; } openStatus = pcsl_file_open(filename_str, flags, &handle); REPORT_INFO1(LC_CORE, "storage_open allocated file_desc %d\n", (int)handle); if (-1 == openStatus) { *ppszError = storage_get_last_file_error("storage_open()", filename_str); return -1; } /* Update the resource count */ if (midpIncResourceCount(RSC_TYPE_FILE, 1) == 0) { REPORT_INFO(LC_CORE, "FILE : resource limit update error"); } #if REPORT_LEVEL <= LOG_INFORMATION DEBUGP2F("created %s\n", filename_str); #endif return (int)handle; }
/** * FS only need to support MIDLets to quiry the size of the file. * Check the File size by file name */ long pcsl_file_sizeof(const pcsl_string * fileName) { void *fd; long size; pcsl_file_open(fileName, PCSL_FILE_O_RDONLY, &fd); size = pcsl_file_sizeofopenfile(fd); pcsl_file_close(fd); return size; }
/** * FS only need to support MIDLets to quiry the size of the file. * Check the File size by file name */ long pcsl_file_sizeof(const pcsl_string * fileName) { int fd, openStatus; openStatus = (int)pcsl_file_open(fileName, PCSL_FILE_O_RDONLY, (void **)(&fd)); if(openStatus == 0 ) { return (long)rmfsFileSize(fd); } return -1; }
/** * (Internal) Open file. * fileName - File name. * flags - flags for open file. * handle - pointer for the file handler. */ static int jsr238_file_open(char *fileName, int flags, void **handle) { MidpString midp_fname = {0,0}; int res = -1; midp_fname = midpStringCat(midp_fname, storageGetRoot()); midp_fname = midpStringCat(midp_fname, midpCharsToJchars(JSR238_STORAGE_ROOT)); midp_fname = midpStringCat(midp_fname, midpCharsToJchars(fileName)); if (pcsl_file_open(midp_fname.data, midp_fname.len, flags, handle) == 0) { if (*handle != NULL){ res = 0; } } return res; }
/** * FS only need to support MIDLets to quiry the size of the file. * Check the File size by file name */ long pcsl_file_sizeof(const pcsl_string * fileName) { int handle, openStatus; struct _stat stat_buf; openStatus = pcsl_file_open(fileName, O_RDONLY, (void **)(&handle)); if(openStatus < 0) { return -1; } if (_fstat(handle, &stat_buf) < 0) { return -1; } close(handle); return stat_buf.st_size;; }
OsFile_Handle OsFile_open(const JvmPathChar *filename, const char *mode) { 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 NULL; } /* int pcsl_flags = (*mode == 'w') ? (PCSL_FILE_O_CREAT|PCSL_FILE_O_WRONLY| PCSL_FILE_O_TRUNC) : (PCSL_FILE_O_RDONLY); */ int pcsl_flags; if(*mode == 'w') { pcsl_flags = (PCSL_FILE_O_CREAT | PCSL_FILE_O_WRONLY | PCSL_FILE_O_TRUNC); } else if(*mode == 'a') { pcsl_flags = (PCSL_FILE_O_WRONLY | PCSL_FILE_O_APPEND); } else { pcsl_flags = PCSL_FILE_O_RDONLY; } void *pcsl_handle; if (pcsl_file_open(&pcsl_filename, pcsl_flags, &pcsl_handle) == 0) { pcsl_string_free(&pcsl_filename); OsFile_Handle handle = (OsFile_Handle)pcsl_mem_malloc(sizeof(struct _OsFile_Handle)); if (handle) { handle->pcsl_handle = pcsl_handle; } else { pcsl_file_close(pcsl_handle); } return handle; } else { pcsl_string_free(&pcsl_filename); return NULL; } }
/** * (Internal) Open device resource file for the locale. * locale_index - index of the locale. * handle - pointer for the file handler. */ int jsr238_devresource_file_open(int locale_index, void **handle) { unsigned char locale[MAX_LOCALE_LENGTH]; MidpString midp_fname = {0,0}; int res = -1; if (jsr238_get_resource_locale(locale, locale_index) >= 0) { midp_fname = midpStringCat(midp_fname, midpCharsToJchars(JSR238_STORAGE_ROOT)); midp_fname = midpStringCat(midp_fname, midpCharsToJchars(locale)); midp_fname = midpStringCat(midp_fname, midpCharsToJchars("/")); midp_fname = midpStringCat(midp_fname, midpCharsToJchars(JSR238_BASENAME)); midp_fname = midpStringCat(storageGetRoot(), midp_fname); if (pcsl_file_open(midp_fname.data, midp_fname.len, PCSL_FILE_O_RDONLY, handle) == 0) { if (*handle != NULL){ res = 0; } } } return res; }
/** * 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); }