📄 fileconnect.c
字号:
/*****************************************************************************
* Copyright Statement:
* --------------------
* This software is protected by Copyright and the information contained
* herein is confidential. The software may not be copied and the information
* contained herein may not be used or disclosed except with the written
* permission of MediaTek Inc. (C) 2005
*
* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/
/*******************************************************************************
* Filename:
* ---------
* fileconnect.c
*
* Project:
* --------
* MAUI
*
* Description:
* ------------
*
*
* Author:
* -------
*
*
*==============================================================================
* HISTORY
* Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*------------------------------------------------------------------------------
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
*------------------------------------------------------------------------------
* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*==============================================================================
*******************************************************************************/
#include "jal.h"
#include "jam_internal.h"
#include "jvm_internal.h"
#include <stdio.h>
#include <kni.h>
#include <midpMalloc.h>
#include <storage.h>
#include <kvmFiles.h>
#include <fs_type.h>
#ifdef __DRM_SUPPORT__
#include "drm_gprot.h"
#include "drm_def.h"
#include "GlobalDefs.h"
#endif /* __DRM_SUPPORT__ */
#define DIRECTORYMASK 1
#define WRITABLEMASK 2
#define HIDDENMASK 4
int handle_count = J2ME_JSR_75_FILE_HANDLER_RESERVE_COUNT + 8;
void finialize_file_handler(void)
{
int fscount = FS_CountUsedFH(FS_FH_COUNT_CURRENT_TASK);
if(fscount != 0)
{
Kputs("Handle count leak found");
}
}
void fileClose_clean(char** ppszError, int handle);
static int RSFileClose(int handle){
int status;
status = FS_Close(handle);
if(status < 0){
return RESOUCE_FAIL;
}else{
return RESOUCE_NO_ERROR;
}
}
#ifdef __DRM_SUPPORT__
static int RSDRMFileClose(int handle){
int status;
if(handle < 0)
{
return RESOUCE_FAIL;
}
status = DRM_close_file(handle);
if(status < 0){
return RESOUCE_FAIL;
}else{
return RESOUCE_NO_ERROR;
}
}
#endif
static int RSFindNextClose(int handle){
int status;
status = FS_FindClose(handle);
if(status < 0){
return RESOUCE_FAIL;
}else{
return RESOUCE_NO_ERROR;
}
}
static void
fileCleanup(jobject thisObject) {
int handle;
KNI_StartHandles(1);
KNI_DeclareHandle(clazz);
KNI_GetObjectClass(thisObject, clazz);
handle = KNI_GetIntField(thisObject, KNI_GetFieldID(clazz, "handle", "I"));
if (handle != -1) {
char* pszError = NULL;
fileClose_clean(&pszError, handle);
if (pszError != NULL) {
storageFreeError(pszError);
}
KNI_SetIntField(thisObject, KNI_GetFieldID(clazz, "handle", "I"), -1);
}
KNI_EndHandles();
}
static void
findNextCleanup(jobject thisObject) {
int handle;
KNI_StartHandles(1);
KNI_DeclareHandle(clazz);
KNI_GetObjectClass(thisObject, clazz);
handle = KNI_GetIntField(thisObject, KNI_GetFieldID(clazz, "listHandle", "I"));
if (handle != -1) {
resouceDereg(JAVA_SYS_FILE_DEVICE, handle);
FS_FindClose(handle);
KNI_SetIntField(thisObject, KNI_GetFieldID(clazz, "listHandle", "I"), -1);
}
KNI_EndHandles();
}
KNIEXPORT KNI_RETURNTYPE_VOID
Java_com_mtk_midp_io_j2me_fileconnection_Protocol_finalize(void){
KNI_StartHandles(1);
KNI_DeclareHandle(object_handle);
KNI_GetThisPointer(object_handle);
fileCleanup(object_handle);
findNextCleanup(object_handle);
KNI_EndHandles();
KNI_ReturnVoid();
}
#define OPEN_CREATE 4
int fileOpen(char** ppszError, kal_wchar* pszUCS2Filename, int ioMode, kal_bool is_DRM){
int flags = O_BINARY | FS_OPEN_DIR;
int handle;
*ppszError = NULL;
/* file open for jsr-75 will be limited */
if(FS_CountUsedFH(FS_FH_COUNT_CURRENT_TASK) >= handle_count){
*ppszError = "file handler not enough.";
return -1;
}
if (kal_wstrlen(pszUCS2Filename) > MAX_FILENAME_LENGTH) {
*ppszError = "file name too long.";
return -1;
}
if ((ioMode & OPEN_WRITE) == 0)
flags |= O_RDONLY;
else if(ioMode & OPEN_CREATE)
flags |= O_CREAT | O_RDWR;
else
flags |= O_RDWR;
#ifdef __DRM_SUPPORT__
if(is_DRM)
handle = DRM_open_file(pszUCS2Filename, flags, DRM_PERMISSION_PLAY);
else
#endif
handle = FS_Open(pszUCS2Filename, flags);
if(handle < 0){
handle = -1;
if(handle == FS_READ_ONLY_ERROR)
{
*ppszError = "Can not open read only file with write attribute.";
}
else
{
*ppszError = "file open failed.";
}
}else
#ifdef __DRM_SUPPORT__
if(is_DRM)
resouceRegistering(JAVA_SYS_FILE_DEVICE, handle, NULL, NULL, &RSDRMFileClose);
else
#endif
resouceRegistering(JAVA_SYS_FILE_DEVICE, handle, NULL, NULL, &RSFileClose);
(void)is_DRM;
return handle;
}
void fileClose_clean(char** ppszError, int handle)
{
int status;
*ppszError = NULL;
resouceDereg(JAVA_SYS_FILE_DEVICE, handle);
#ifdef __DRM_SUPPORT__
status = DRM_close_file(handle);
if(status != FS_NO_ERROR)
#endif
status = FS_Close(handle);
if (status != FS_NO_ERROR) {
*ppszError = "file close failed.";
}
}
void fileClose(char** ppszError, int handle, kal_bool is_DRM)
{
int status;
*ppszError = NULL;
resouceDereg(JAVA_SYS_FILE_DEVICE, handle);
#ifdef __DRM_SUPPORT__
if(is_DRM)
status = DRM_close_file(handle);
else
#endif
status = FS_Close(handle);
if (status != FS_NO_ERROR) {
*ppszError = "file close failed.";
}
(void)is_DRM;
}
void filePosition(char** ppszError, int handle, long absolutePosition)
{
long newPosition;
*ppszError = NULL;
if(handle < 0)
newPosition = -1;
else
newPosition = FS_Seek(handle, absolutePosition, SEEK_SET);
if (-1 == newPosition) {
*ppszError = "set position failed.";
}
}
int fileRead(char** ppszError, int handle, char* buffer, int length, kal_bool is_DRM)
{
int bytesRead=0;
*ppszError = NULL;
if (0 == length) {
return 0;
}
#if DATACACHE_FBBR_ON
buffer = (char*)FBBR_invalid_l1_cache(buffer, length);
#endif
#ifdef __DRM_SUPPORT__
if(is_DRM)
{
if(DRM_read_file(handle, buffer, length, (unsigned int*)&bytesRead) != FS_NO_ERROR)
bytesRead = -1;
}
else
#endif
{
if(FS_Read(handle, buffer, length, (unsigned int*)&bytesRead) != FS_NO_ERROR)
bytesRead = -1;
}
if (-1 == bytesRead) {
*ppszError = "file read error.";
} else if (0 == bytesRead) {
/* end of file in java is -1 */
bytesRead = -1;
}
(void)is_DRM;
return bytesRead;
}
void fileWrite(char** ppszError, int handle, char* buffer, int length)
{
int bytesWritten;
*ppszError = NULL;
#if DATACACHE_FBBR_ON
buffer = (char*)FBBR_invalid_l1_cache(buffer, length);
#endif
if(FS_Write(handle, buffer, length, (unsigned int*)&bytesWritten) != FS_NO_ERROR)
bytesWritten = -1;
FS_Commit(handle); /* force flush of the write to disk */
if (-1 == bytesWritten) {
*ppszError = "file write error.";
return;
}
if (bytesWritten != length) {
*ppszError = "storage full.";
return;
}
}
int fileTruncate(char** ppszError, int handle, int size)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -