📄 rmfsapi.c
字号:
/* * * * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. *//** * @file *//** * RMFS storage management and file external functions. * * With these functions, we have the ability to manage RMFS * raw memory pool. Please refer to the RMFS design document for details * * @warning This code is not thread safe. * */#include <stdio.h>#include <string.h>#include <pcsl_file.h>#include <rmfsAlloc.h>#include <rmfsApi.h>#ifdef __cplusplusextern "C"{#endif#define RMS_DB_HEADER_SIZE (40) /* #define TRACE_DEBUG 0 */#define MAX_BLOCKSIZE_4_SPECFILE 0x800 /* 2K bytes */#define MAX_BLOCKSIZE_4_INSTALL_INFO 0x400 /* 1K bytes */#define MAX_BLOCKSIZE_4_MIDLET_PROR 0x1800 /* 6K bytes */#define MAX_BLOCKSIZE_4_INSTALL_TEMP 0xF000 /* 60K bytes */extern _RmfsDataBlockHdr rmfsDataBlockHdrArray[MAX_DATABLOCK_NUMBER];extern jint RmfsNameTableEnd; /* End Address of RMFS Name Table Space */_rmfsFileDes fileTab[MAX_OPEN_FILE_NUM];/** * FUNCTION: initFileSystem() * TYPE: public operation * OVERVIEW: Load some files into the RMFS storage from Romized file. * * * INTERFACE: * parameters: NONE * * returns: >= 0; succes; < 0: failed * */jint initFileSystem(){#if 0 int i; jint fd; jint res = 0; jint filesize; jint filenamelength; char *filename; char *filebuffer; /* write all Romized file in tck_png.h into RMFS */ for (i = 0; i < NUM_ROMFILES; i++) { filename = (char *) ROMFiles[i].filename; filesize = ROMFiles[i].size; filebuffer = (char *) ROMFiles[i].contents; filenamelength = strlen((char *) filename); rmfsFileExist(filename, &res); if (res == 1) { /* The file is in place so continue */ /* go back to for loop */ continue; } fd = rmfsOpen(filename, PCSL_FILE_O_CREAT, 0); if (fd < 0) { /* can't create the file */ /* do what? */ return -3; } res = rmfsWrite(fd, (char *) filebuffer, filesize); if (res < 0) { /* can't write to the file */ rmfsClose(fd); rmfsUnlink(filename); return -4; } res = rmfsClose(fd); if (res < 0) { return -5; } fd = 0; } /* end of for */#endif return 1;}/** * FUNCTION: rmfsInitOpenFileTab() * TYPE: public operation * OVERVIEW: Initialize the Open File Table * INTERFACE: * parameters: None * * returns: None * */void rmfsInitOpenFileTab(){ short i = 0;#ifdef TRACE_DEBUG printf("rmfsInitOpenFileTab \n");#endif for (i = 0; i < MAX_OPEN_FILE_NUM; i++) { fileTab[i].createMode = 0; fileTab[i].dataBlockIndex = 0; fileTab[i].fileType = 0; fileTab[i].flags = 0; fileTab[i].handle = -1; fileTab[i].nameID = 0; fileTab[i].offset = 0; fileTab[i].size = 0; }}/** * FUNCTION: rmfsOpen() * TYPE: public operation * OVERVIEW: The open function creates and returns a new RMFS file identifier for the file named by filename. * Initially, the file position indicator for the file is at the beginning of the file. * * INTERFACE: * parameters: filename: The name of the file * flags: controls how the file is to be opened. This is a bit mask; * you create the value by the bitwise OR of the appropriate parameters * (using the `|' operator in C) * creationMode is used only when a file is created. * * returns: None * */jint rmfsOpen(const char *filename, jint flags, jint creationMode){ char *suffix; RMFS_BOOLEAN done = RMFS_FALSE; RMFS_BOOLEAN newFile = RMFS_FALSE; char fileType = 0; int i; jint fileIndex = -1; jint dataBlockAddr; _RmfsNameTabHdr nameTabHdr; jint nameTabAddr;#ifdef TRACE_DEBUG printf("rmfsOpen: filename: %s; flags: %d; createionMode: %d \n", filename, flags, creationMode);#endif for (i = 0; i < MAX_OPEN_FILE_NUM; i++) { if (fileTab[i].handle == -1) { fileIndex = i; break; } } if (fileIndex >= MAX_OPEN_FILE_NUM) {#ifdef TRACE_DEBUG printf("Open too much file \n");#endif return -1; } fileTab[fileIndex].handle = fileIndex; nameTabAddr = searchNameTabByString(filename, &(fileTab[fileIndex].nameID)); if ((nameTabAddr < 0) || (fileTab[fileIndex].nameID == 0)) { /* If file doesn't exist and the flags don't support PCSL_FILE_O_CREAT, return fail */ if ((flags & PCSL_FILE_O_CREAT) != PCSL_FILE_O_CREAT) { fileTab[fileIndex].handle = -1; return -1; } fileTab[fileIndex].nameID = rmfsNewNameTable(filename); newFile = RMFS_TRUE; } else {#ifdef TRACE_DEBUG printf("nameTabAddr: %d\n", nameTabAddr);#endif ReadDataFromStorage(&nameTabHdr, nameTabAddr, sizeof(_RmfsNameTabHdr)); fileTab[fileIndex].size = nameTabHdr.size; } if (fileTab[fileIndex].nameID == 0XFFFF) {#ifdef TRACE_DEBUG printf("Failed to find the nameID \n");#endif return -1; } if (!newFile) { for (i = 0; i < MAX_DATABLOCK_NUMBER; i++) { if ((rmfsDataBlockHdrArray[i].identifier == fileTab[fileIndex].nameID) && ((rmfsDataBlockHdrArray[i].flag & 0x80) == 0x00)) { fileTab[fileIndex].dataBlockIndex = i; break; } } if (i == MAX_DATABLOCK_NUMBER) { fileTab[fileIndex].dataBlockIndex = 0xFFFF; } fileTab[fileIndex].createMode = creationMode; // fileTab[fileIndex].size = rmfsDataBlockHdrArray[i].dataSize; fileTab[fileIndex].fileType = rmfsDataBlockHdrArray[i].flag & 0x3E; /* Bit 5, 4, 3, 2, 1 */ if (i == MAX_DATABLOCK_NUMBER) { fileTab[fileIndex].fileType = RMFS_NORMAL_FILE; } fileTab[fileIndex].offset = 0; fileTab[fileIndex].flags = flags;#ifdef TRACE_DEBUG printf("Open old file correctly \n");#endif return fileIndex; } /* Decide the file type by the suffix of the file name */ suffix = (char*)strrchr(filename, '.'); if (suffix == NULL) { done = RMFS_TRUE; fileType = RMFS_NORMAL_FILE; }#ifdef TRACE_DEBUG printf("Suffix: %s, File Type: %d\n", suffix, fileType);#endif if (!done) { if (strcmp(suffix, ".jad") == 0) { done = RMFS_TRUE; fileType = RMFS_JAD_FILE; } } if (!done) { if (strcmp(suffix, ".jar") == 0) { done = RMFS_TRUE; fileType = RMFS_JAR_FILE; } } if (!done) { if (strcmp(suffix, ".ss") == 0) { done = RMFS_TRUE; fileType = RMFS_MIDLET_SETTING; } } if (!done) { if (strcmp(suffix, ".ap") == 0) { done = RMFS_TRUE; fileType = RMFS_MIDLET_PROR; } } if (!done) { if (strcmp(suffix, ".ii") == 0) { done = RMFS_TRUE; fileType = RMFS_MIDLET_INSTALL; } } if (!done) { if ((strcmp(suffix, ".db") == 0) || (strcmp(suffix, ".dbx") == 0)) { done = RMFS_TRUE; fileType = RMFS_RMS_RECORD; } } if (!done) { if (strcmp(suffix, ".tmp") == 0) { done = RMFS_TRUE; fileType = RMFS_INSTALL_TEMP; } } if (!done) { suffix = (char*)strrchr(filename, '_'); if ((suffix != NULL) && ((suffix - filename) > 6)) { suffix -= 6; if (strcmp(suffix, "delete_notify.dat") == 0) { done = RMFS_TRUE; fileType = RMFS_MIDLET_DEL_NOTIFY; } } } if (!done) { suffix = (char*)strrchr(filename, '_'); if (suffix != NULL) { if (strcmp(suffix, "_suites.dat") == 0) { done = RMFS_TRUE; fileType = RMFS_MIDLET_SUITE_LIST; } } } if (!done) { fileType = RMFS_NORMAL_FILE; }#ifdef TRACE_DEBUG printf("Suffix: %s, File Type: %d\n", suffix, fileType);#endif fileTab[fileIndex].createMode = creationMode; fileTab[fileIndex].dataBlockIndex = 0; fileTab[fileIndex].fileType = fileType; fileTab[fileIndex].size = 0; fileTab[fileIndex].offset = 0; fileTab[fileIndex].flags = flags; /* 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, allocate one 1K RMFS Data Block * for the new created file */ if ((fileType == RMFS_JAD_FILE) || (fileType == RMFS_MIDLET_SUITE_LIST) || (fileType == RMFS_MIDLET_DEL_NOTIFY) || (fileType == RMFS_MIDLET_SETTING)) { dataBlockAddr = rmfsFileAlloc(MAX_BLOCKSIZE_4_SPECFILE, filename, NULL, fileType); if ((rmfsSearchBlockByPos (&fileTab[fileIndex].dataBlockIndex, dataBlockAddr)) < 0) { fileTab[fileIndex].handle = -1; return -1; } } if (fileType == RMFS_INSTALL_TEMP) { dataBlockAddr = rmfsFileAlloc(MAX_BLOCKSIZE_4_INSTALL_TEMP, filename, NULL, fileType); if ((rmfsSearchBlockByPos (&fileTab[fileIndex].dataBlockIndex, dataBlockAddr)) < 0) { fileTab[fileIndex].handle = -1; return -1; } } if (fileType == RMFS_MIDLET_INSTALL) { dataBlockAddr = rmfsFileAlloc(MAX_BLOCKSIZE_4_INSTALL_INFO, filename, NULL, fileType); if ((rmfsSearchBlockByPos (&fileTab[fileIndex].dataBlockIndex, dataBlockAddr)) < 0) { fileTab[fileIndex].handle = -1; return -1; } } if (fileType == RMFS_MIDLET_PROR) { dataBlockAddr = rmfsFileAlloc(MAX_BLOCKSIZE_4_MIDLET_PROR, filename, NULL, fileType); if ((rmfsSearchBlockByPos (&fileTab[fileIndex].dataBlockIndex, dataBlockAddr)) < 0) { fileTab[fileIndex].handle = -1; return -1; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -