📄 rmfsapi.c
字号:
} if (fileType == RMFS_RMS_RECORD) { dataBlockAddr = rmfsFileAlloc(MAX_BLOCKSIZE_4_SPECFILE, filename, NULL, fileType); if ((rmfsSearchBlockByPos (&fileTab[fileIndex].dataBlockIndex, dataBlockAddr)) < 0) { fileTab[fileIndex].handle = -1; return -1; } } return fileIndex;}/** * FUNCTION: rmfsClose() * TYPE: public operation * OVERVIEW: closes the file with descriptor identifer . * * INTERFACE: * parameters: identifier: file descriptor * * returns: >=0 success; < 0 failure * */jint rmfsClose(jint identifier){ char fileType; int i; signed short index = -1; jint nameTabAddr = 0; _RmfsNameTabHdrPtr nameTabHdr; char *filename = NULL;#ifdef TRACE_DEBUG printf("rmfsClose: identifier: %d \n", identifier);#endif for (i = 0; i <= MAX_OPEN_FILE_NUM; i++) { if (fileTab[i].handle == identifier) { index = i; break; } } if (index < 0) { return -1; } fileType = fileTab[index].fileType; /* Update the file size in File Name Table */ if (((nameTabAddr = searchNameTabByID(&filename, fileTab[index].nameID)) > 0) && (filename != NULL)) { nameTabHdr = (_RmfsNameTabHdrPtr) nameTabAddr; nameTabHdr->size = fileTab[index].size; WriteDataToStorage(nameTabHdr, nameTabAddr, sizeof(_RmfsNameTabHdr)); } else { return -1; } /* if the file type is JAD file, Installed MIDlet suites list file, * MIDlet install information file; MIDlet suites settings file; * or MIDlet application properties file, and the file size is less than * the size of the allocated block */ if (((fileType == RMFS_JAD_FILE) || (fileType == RMFS_MIDLET_SUITE_LIST) || (fileType == RMFS_MIDLET_DEL_NOTIFY) || (fileType == RMFS_MIDLET_SETTING)) && (rmfsDataBlockHdrArray[fileTab[index].dataBlockIndex].size > fileTab[index].size)) { rmfsTruncate(identifier, fileTab[index].size); } fileTab[index].createMode = 0; fileTab[index].dataBlockIndex = 0; fileTab[index].fileType = 0; fileTab[index].flags = 0; fileTab[index].handle = -1; fileTab[index].nameID = 0; fileTab[index].offset = 0; fileTab[index].size = 0; return 0;}/** * FUNCTION: rmfsUnlink() * TYPE: public operation * OVERVIEW: deletes the file named filename from the persistent storage. * * INTERFACE: * parameters: filename: file name * * returns: >=0 success; < 0 failure * */jint rmfsUnlink(const char *filename){ uchar nameID = 0;#ifdef TRACE_DEBUG printf("rmfsUnlink: filename: %s \n", filename);#endif if ((searchNameTabByString(filename, &nameID) < 0) || (nameID == 0)) { return -1; } /* Remove the Datablock structures belongs to the file */ rmfsDelFile(filename); /* Remove the right record in NameTab space */ delNameTabByID(nameID); return 0;}/** * FUNCTION: rmfsWrite() * TYPE: public operation * OVERVIEW: writes up to size bytes from buffer to the file with descriptor identifier. * * INTERFACE: * parameters: identifier: file descriptor * buffer: data buffer will be written to RMFS storage * size: the number of bytes intend to be written * * returns: >=0 is the number of bytes actually written; success; < 0 failure * */jint rmfsWrite(jint identifier, void *buffer, jint size){ int i; jint filePos = 0; jint fileIndex = -1; jint blockIndex = -1; jint byteNum = 0; jint blockOffset = 0; jint prevBlockIndex = -1; char *filename = NULL; jint oldDataSize = 0; jint remainSize = 0; jint writeSize = 0;#ifdef TRACE_DEBUG printf("rmfsWrite: identifer: %d, size: %d \n", identifier, size);#endif /* Search the File Open Table to find the matched record */ for (i = 0; i < MAX_OPEN_FILE_NUM; i++) { if (fileTab[i].handle == identifier) { fileIndex = i; break; } } if (i >= MAX_OPEN_FILE_NUM) { return -1; } if ((searchNameTabByID(&filename, fileTab[fileIndex].nameID)) < 0) { return -1; } remainSize = size; while(remainSize > 0) { blockIndex = -1; buffer = (void*)((int)buffer + writeSize); writeSize = remainSize; for (i = prevBlockIndex + 1; i < MAX_DATABLOCK_NUMBER; i++) { if (rmfsDataBlockHdrArray[i].identifier == fileTab[fileIndex].nameID) { filePos += rmfsDataBlockHdrArray[i].dataSize; if ((filePos > fileTab[fileIndex].offset) || ((rmfsDataBlockHdrArray[i].next == 0) && (rmfsDataBlockHdrArray[i].size > rmfsDataBlockHdrArray[i].dataSize))) { if ((filePos >= fileTab[fileIndex].offset + remainSize) || (rmfsDataBlockHdrArray[i].size >= fileTab[fileIndex].offset + remainSize)) { /* The write operation happens in this block */ blockIndex = i; blockOffset = fileTab[fileIndex].offset - (filePos - rmfsDataBlockHdrArray[i].dataSize); writeSize = remainSize; remainSize = 0; prevBlockIndex = i; break; } else { /* The write operation happens in this block */ blockIndex = i; blockOffset = fileTab[fileIndex].offset - (filePos - rmfsDataBlockHdrArray[i].dataSize); writeSize = filePos - fileTab[fileIndex].offset + rmfsDataBlockHdrArray[i].size - rmfsDataBlockHdrArray[i].dataSize; if(writeSize > remainSize ) { writeSize = remainSize; } remainSize = remainSize - writeSize; prevBlockIndex = i; break; } } writeSize = remainSize; remainSize = 0; prevBlockIndex = i; } }#ifdef TRACE_DEBUG tty->print_cr("offset: %d; filePos: %d, blockIndex: %d, prevBlock: %d \n", fileTab[fileIndex].offset, filePos, blockIndex, prevBlockIndex);#endif if ((blockIndex == -1) && (prevBlockIndex == -1)) {#if 0 /* Allocate the first data block for this file */ if (fileTab[fileIndex].fileType == RMFS_RMS_RECORD) { /* If the File Type is the RMS RecordStorage, the block size * shouldn't be less than the size of RMS header */ if (size < RMS_DB_HEADER_SIZE) { rmfsFileAlloc(RMS_DB_HEADER_SIZE, filename, buffer, fileTab[fileIndex].fileType); byteNum = rmfsWriteDataToBlock(blockIndex, buffer, writeSize, 0); } else { if (rmfsFileAlloc (writeSize, filename, buffer, fileTab[fileIndex].fileType) >= 0) { byteNum = size; } } } else#endif { if (rmfsFileAlloc (writeSize, filename, buffer, fileTab[fileIndex].fileType) >= 0) { byteNum = writeSize; } } fileTab[fileIndex].size = byteNum; remainSize = remainSize - writeSize; } else if ((blockIndex == -1) && (prevBlockIndex >= 0)) { /* Allocated another Data Block for this file */ byteNum = rmfsAllocLinkedBlock(prevBlockIndex, buffer, writeSize); fileTab[fileIndex].size += byteNum; remainSize = remainSize - writeSize; } else { oldDataSize = rmfsDataBlockHdrArray[blockIndex].dataSize; byteNum = rmfsWriteDataToBlock(blockIndex, buffer, writeSize, blockOffset); fileTab[fileIndex].size += (rmfsDataBlockHdrArray[blockIndex].dataSize - oldDataSize); } fileTab[fileIndex].offset += byteNum; } return byteNum;}/** * FUNCTION: rmfsRead() * TYPE: public operation * OVERVIEW: reads up to size bytes from the file with descriptor identifier , * storing the results in the buffer. * * INTERFACE: * parameters: identifier: file descriptor * buffer: data buffer is used to storage the data read from RMFS storage * size: the number of bytes intend to be read * * returns: >=0 is the number of bytes actually read; success; < 0 failure */ jint rmfsRead(jint identifier, void *buffer, jint size){ int i; jint filePos = 0; jint fileIndex = -1; jint blockIndex = -1; jint readSize = 0; jint blockOffset = 0; jint nextOffset = 0; void *tempPos;#ifdef TRACE_DEBUG printf("rmfsRead: identifer: %d, size: %d \n", identifier, size);#endif /* Search the File Open Table to find the matched record */ for (i = 0; i < MAX_OPEN_FILE_NUM; i++) { if (fileTab[i].handle == identifier) { fileIndex = i; break; } } if (i >= MAX_OPEN_FILE_NUM) { return -1; } /* If the file reach the end, stop and return 0 */ if (fileTab[fileIndex].offset >= fileTab[fileIndex].size) { return 0; } for (i = 0; i < MAX_DATABLOCK_NUMBER; i++) { if (rmfsDataBlockHdrArray[i].identifier == fileTab[fileIndex].nameID) { filePos += rmfsDataBlockHdrArray[i].dataSize; if (filePos > fileTab[fileIndex].offset) { blockOffset = fileTab[fileIndex].offset - (filePos - rmfsDataBlockHdrArray[i].dataSize); readSize = (filePos - fileTab[fileIndex].offset); if (readSize >= size) { rmfsReadBlock(i, buffer, size, blockOffset); } else { rmfsReadBlock(i, buffer, readSize, blockOffset); tempPos = (void *) ((jint) buffer + readSize); } blockIndex = i; while (readSize < size) { nextOffset = rmfsDataBlockHdrArray[blockIndex].next; if ((rmfsSearchBlockByPos ((uchar*) &blockIndex, nextOffset)) < 0) { fileTab[fileIndex].offset += readSize; return readSize; } readSize += rmfsDataBlockHdrArray[blockIndex].dataSize; if (readSize >= size) { rmfsReadBlock(blockIndex, buffer, size - (readSize - rmfsDataBlockHdrArray [blockIndex].dataSize), 0); break; } else { rmfsReadBlock(blockIndex, buffer, rmfsDataBlockHdrArray[blockIndex].dataSize, 0); tempPos = (void *) ((jint) buffer + readSize); } } fileTab[fileIndex].offset += size; return size; } } } return 0;}/** * FUNCTION: rmfsLseek() * TYPE: public operation * OVERVIEW: change the file position of the file with descriptor identifier * * INTERFACE: * parameters: identifier: file descriptor * offset: new position * whence: specifies how the offset should be interpreted, * and can be one of the symbolic constants PCSL_FILE_SEEK_SET, * PCSL_FILE_SEEK_CUR, or PCSL_FILE_SEEK_END. * PCSL_FILE_SEEK_SET * Specifies that whence is a count of characters from the beginning of the file. * PCSL_FILE_SEEK_CUR * Specifies that whence is a count of characters from the current file position. * This count may be positive or negative. * PCSL_FILE_SEEK_END * Specifies that whence is a count of characters from the end of the file. * A negative count specifies a position within the current extent of the file; * a positive count specifies a position past the current end. If you set the position * past the current end, and actually write data, you will extend the file with zeros * up to that position * * returns: >=0 success; < 0 failure * */jint rmfsLseek(jint identifier, jint offset, jint whence){ int i; int fileIndex = 0; int absoluteOff = 0;#ifdef TRACE_DEBUG printf("rmfsLseek: identifer: %d, whence: %d \n", identifier, whence);#endif /* check the input parameter */ if ((offset < 0) && (whence == PCSL_FILE_SEEK_SET)) { return -1; } /* Search the File Open Table to find the matched record */ for (i = 0; i < MAX_OPEN_FILE_NUM; i++) { if (fileTab[i].handle == identifier) { fileIndex = i; break; } } if (i >= MAX_OPEN_FILE_NUM) { return -1; } if (whence == PCSL_FILE_SEEK_SET) { absoluteOff = offset; } else if (whence == PCSL_FILE_SEEK_CUR) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -