📄 filesys.c
字号:
Description:
get the current drive number
Input:
none
Output:
drive number if succeeded
0 no current drive
Note:
Current drive number follows device ID => starts from 0.
Drive number for user starts from 1.
**************************************************************/
int fs_getdrive(void)
{
return (CurrentDrive + 1);
}
/*************************************************************
Function: open
Description:
open a file
Input:
name the target file
flag operation flag
Output:
file handle
-1 failure
**************************************************************/
//fs_open("A:\\test.txt", O_RDWR);
int fs_open(char *name, int flag)
{
int result = -1;
short driveNO;
/**** added by chilong 01/15/2002 ****/
char *Pathname1;
/**** added by chilong 01/15/2002 ****/
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "[open] %s 0x%x", name, flag);
SprintStringLn(FS_DebugString);
#endif
if (FileSys_Running == 0)
{
FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
return -1;
}
/**** added by chilong 12/7/2001 ****/
if (name == NULL)
return -1;
/**** added by chilong 01/15/2002 ****/
if ((Pathname1 = (char *)ap_malloc(MAX_PATHNAME_LEN + 2)) == NULL)
{
FileSys_Errno = ERROR_ALLOC_MEM;
return -1;
}
/**** added by chilong 01/15/2002 ****/
FileSys_Errno = ERROR_INVALID_DEVICE;
driveNO = getDriveNO(name, Pathname1);
if (driveNO == -1)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " getDriveNO failed");
SprintStringLn(FS_DebugString);
#endif
ap_free(Pathname1);
return -1;
}
/**** added by chilong 6/28/2000 ****/
if ( ((flag & O_TEXT) == O_TEXT && (flag & O_BINARY) == O_BINARY) || \
((flag & O_RDONLY) == O_RDONLY && (flag & O_RDWR) == O_RDWR) )
{
FileSys_Errno = ERROR_FILE_FLAG;
ap_free(Pathname1);
return -1;
}
// The default flag is O_RDWR and O_BINARY
if ((flag & O_RDONLY) == 0 && (flag & O_RDWR) == 0)
flag |= O_RDWR;
if ((flag & O_TEXT) == 0 && (flag & O_BINARY) == 0)
flag |= O_BINARY;
/**** added by chilong 6/28/2000 ****/
/**** added by chilong part 1 ****/
/*
// check if the file exists
// if the file exists, we remove O_CREAT bit in the flag
if (access(name, 0x00) != -1)
{
if ( (flag & O_CREAT) == O_CREAT)
flag &= ~O_CREAT;
// if the flag contains only O_CREAT, we stuff O_RDONLY into it
if (flag == 0)
flag = O_RDONLY;
}*/
/**** added by chilong part 1 ****/
#ifdef NOR_FLASH_DISK_ID
if (driveNO == NOR_FLASH_DISK_ID)
{
result = NOR_FFS_open(Pathname1, flag);
FileSys_Errno = NOR_FFS_Errno;
// added by chilong
// We only update this part for RAMDISK_ID version
// For FLASH_DISK_ID version, we'll update it later
// added by chilong
// return result;
}
#endif
#ifdef NAND_FLASH_DISK_ID
if (driveNO == NAND_FLASH_DISK_ID)
{
result = NAND_FFS_open((unsigned char *)Pathname1, flag);
FileSys_Errno = NAND_FFS_Errno;
// added by chilong
// We only update this part for RAMDISK_ID version
// For FLASH_DISK_ID version, we'll update it later
// added by chilong
// return result;
}
#endif
#ifdef RAMDISK_ID
if (driveNO == RAMDISK_ID)
{
result = RD_open((unsigned char *)Pathname1, flag);
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " RD_open, name=%s, return=%d, RDerrno=0x%x", Pathname1, result, RDerrno);
SprintStringLn(FS_DebugString);
#endif
FileSys_Errno = RDerrno;
// return result;
}
#endif
#ifdef FAT_ID
if (driveNO == FAT_ID)
{
result = FAT_open((unsigned char *)Pathname1, flag);
FileSys_Errno = FATErrno;
/**** added by chilong ****/
// We only update this part for RAMDISK_ID version
// For FLASH_DISK_ID version, we'll update it later
/**** added by chilong ****/
// return result;
}
#endif
ap_free(Pathname1);
return result;
// FileSys_Errno = ERROR_INVALID_DEVICE;
// return -1;
}
/*************************************************************
Function: close
Description:
close a file handle
Input:
handle the target file handle
Output:
0 no error
-1 error
**************************************************************/
int fs_close(int handle)
{
int result;
if (FileSys_Running == 0)
{
FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
return -1;
}
if ((handle < 0) || (handle >= MAX_OPEN_FILE) || (FileSysHandleTable[handle] == NULL))
{
FileSys_Errno = ERROR_INVALID_HANDLE;
return -1;
}
#ifdef NOR_FLASH_DISK_ID
if (*(FileSysHandleTable[handle]) == NOR_FLASH_DISK_ID)
{
result = NOR_FFS_close(handle);
FileSys_Errno = NOR_FFS_Errno;
return result;
}
#endif
#ifdef NAND_FLASH_DISK_ID
if (*(FileSysHandleTable[handle]) == NAND_FLASH_DISK_ID)
{
result = NAND_FFS_close(handle);
FileSys_Errno = NAND_FFS_Errno;
return result;
}
#endif
#ifdef RAMDISK_ID
if (*(FileSysHandleTable[handle]) == RAMDISK_ID)
{
result = RD_close(handle);
FileSys_Errno = RDerrno;
return result;
}
#endif
#ifdef FAT_ID
if (*(FileSysHandleTable[handle]) == FAT_ID)
{
result = FAT_close(handle);
FileSys_Errno = FATErrno;
return result;
}
#endif
FileSys_Errno = ERROR_INVALID_DEVICE;
return -1;
}
/*************************************************************
Function: read
Description:
read data from an opened file
Input:
handle the target file handle
buffer the user provided buffer
length the length of read
Output:
0 no error
non-zero error
**************************************************************/
long fs_read(int handle, void *buffer, long length)
{
extern unsigned int gTestTickCount;
long result;
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "[read] handle=%d", handle);
SprintStringLn(FS_DebugString);
#endif
if (FileSys_Running == 0)
{
FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
return -1;
}
/**** added by chilong 12/7/2001 ****/
if (buffer == NULL)
return -1;
/**** added by chilong 12/7/2001 ****/
if ((handle < 0) || (handle >= MAX_OPEN_FILE) || (FileSysHandleTable[handle] == NULL))
{
FileSys_Errno = ERROR_INVALID_HANDLE;
return -1;
}
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " device=%d", *(FileSysHandleTable[handle]));
SprintStringLn(FS_DebugString);
#endif
#ifdef NOR_FLASH_DISK_ID
if (*(FileSysHandleTable[handle]) == NOR_FLASH_DISK_ID)
{
result = NOR_FFS_read(handle, buffer, length);
FileSys_Errno = NOR_FFS_Errno;
return result;
}
#endif
#ifdef NAND_FLASH_DISK_ID
if (*(FileSysHandleTable[handle]) == NAND_FLASH_DISK_ID)
{
result = NAND_FFS_read(handle, buffer, length);
FileSys_Errno = NAND_FFS_Errno;
return result;
}
#endif
#ifdef RAMDISK_ID
if (*(FileSysHandleTable[handle]) == RAMDISK_ID)
{
result = RD_read(handle, buffer, length);
FileSys_Errno = RDerrno;
return result;
}
#endif
#ifdef FAT_ID
if (*(FileSysHandleTable[handle]) == FAT_ID)
{
result = FAT_read(handle, (unsigned char *)buffer, length);
FileSys_Errno = FATErrno;
return result;
}
#endif
FileSys_Errno = ERROR_INVALID_DEVICE;
return -1;
}
/*************************************************************
Function: write
Description:
write data to an opened file
Input:
handle the target file handle
buffer the user provided buffer
length the length of data
Output:
the byte count of data written to the file
**************************************************************/
long fs_write(int handle, void *buffer, long length)
{
long result;
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "[write] handle=%d length=%d", handle, length);
SprintStringLn(FS_DebugString);
#endif
if (FileSys_Running == 0)
{
FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
return -1;
}
/**** added by chilong 12/7/2001 ****/
if (buffer == NULL)
return -1;
/**** added by chilong 12/7/2001 ****/
if ((handle < 0) || (handle >= MAX_OPEN_FILE) || (FileSysHandleTable[handle] == NULL))
{
FileSys_Errno = ERROR_INVALID_HANDLE;
return -1;
}
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " device=%d", *(FileSysHandleTable[handle]));
SprintStringLn(FS_DebugString);
#endif
#ifdef NOR_FLASH_DISK_ID
if (*(FileSysHandleTable[handle]) == NOR_FLASH_DISK_ID)
{
result = NOR_FFS_write(handle, buffer, length);
FileSys_Errno = NOR_FFS_Errno;
return result;
}
#endif
#ifdef NAND_FLASH_DISK_ID
if (*(FileSysHandleTable[handle]) == NAND_FLASH_DISK_ID)
{
result = NAND_FFS_write(handle, buffer, length);
FileSys_Errno = NAND_FFS_Errno;
return result;
}
#endif
#ifdef RAMDISK_ID
if (*(FileSysHandleTable[handle]) == RAMDISK_ID)
{
result = RD_write(handle, buffer, length);
FileSys_Errno = RDerrno;
return result;
}
#endif
#ifdef FAT_ID
if (*(FileSysHandleTable[handle]) == FAT_ID)
{
result = FAT_write(handle, (unsigned char *)buffer, length);
FileSys_Errno = FATErrno;
return result;
}
#endif
FileSys_Errno = ERROR_INVALID_DEVICE;
return -1;
}
/*************************************************************
Function: lseek
Description:
go to the given position in an opened file
Input:
handle: file handle
offset: offset relative to origin
origin: SEEK_SET: fpos = offset
SEEK_CUR: fpos = cur_pos + offset
SEEK_END: fpos = file_end + offset
Output:
set absolute file position if success
-1 FAILURE
**************************************************************/
long fs_lseek(int handle, long offset, int origin)
{
long result;
if (FileSys_Running == 0)
{
FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
return -1;
}
if ((handle < 0) || (handle >= MAX_OPEN_FILE) || (FileSysHandleTable[handle] == NULL))
{
FileSys_Errno = ERROR_INVALID_HANDLE;
return -1;
}
#ifdef NOR_FLASH_DISK_ID
if (*(FileSysHandleTable[handle]) == NOR_FLASH_DISK_ID)
{
result = NOR_FFS_lseek(handle, offset, origin);
FileSys_Errno = NOR_FFS_Errno;
return result;
}
#endif
#ifdef NAND_FLASH_DISK_ID
if (*(FileSysHandleTable[handle]) == NAND_FLASH_DISK_ID)
{
result = NAND_FFS_lseek(handle, offset, origin);
FileSys_Errno = NAND_FFS_Errno;
return result;
}
#endif
#ifdef RAMDISK_ID
if (*(FileSysHandleTable[handle]) == RAMDISK_ID)
{
result = RD_lseek(handle, offset, origin);
FileSys_Errno = RDerrno;
return result;
}
#endif
#ifdef FAT_ID
if (*(FileSysHandleTable[handle]) == FAT_ID)
{
result = FAT_lseek(handle, offset, origin);
FileSys_Errno = FATErrno;
return result;
}
#endif
FileSys_Errno = ERROR_INVALID_DEVICE;
return -1;
}
/*************************************************************
Function: tell
Description:
get the current file position of an opened file
Input:
handle: file handle
Output:
set absolute file position if success
-1 FAILURE
**************************************************************/
long fs_tell(int handle)
{
long result;
if (FileSys_Running == 0)
{
FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
return -1;
}
if ((handle < 0) || (handle >= MAX_OPEN_FILE) || (FileSysHandleTable[handle] == NULL))
{
FileSys_Errno = ERROR_INVALID_HANDLE;
return -1;
}
#ifdef NOR_FLASH_DISK_ID
if (*(FileSysHandleTable[handle]) == NOR_FLASH_DISK_ID)
{
result = NOR_FFS_tell(handle);
FileSys_Errno = NOR_FFS_Errno;
return result;
}
#endif
#ifdef NAND_FLASH_DISK_ID
if (*(FileSysHandleTable[handle]) == NAND_FLASH_DISK_ID)
{
result = NAND_FFS_tell(handle);
FileSys_Errno = NAND_FFS_Errno;
return result;
}
#endif
#ifdef RAMDISK_ID
if (*(FileSysHandleTable[handle]) == RAMDISK_ID)
{
result = RD_tell(handle);
FileSys_Errno = RDerrno;
return result;
}
#endif
#ifdef FAT_ID
if (*(FileSysHandleTable[handle]) == FAT_ID)
{
result = FAT_tell(handle);
FileSys_Errno = FATErrno;
return result;
}
#endif
FileSys_Errno = ERROR_INVALID_DEVICE;
return -1;
}
/*************************************************************
Function: filelength
Description:
get the file size of an opened file
Inputs:
handle - target file handle
Outputs:
file size
-1: error
**************************************************************/
long fs_filelength(int handle)
{
long result;
if (FileSys_Running == 0)
{
FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
return -1;
}
if ((handle < 0) || (handle >= MAX_OPEN_FILE) || (FileSysHandleTable[handle] == NULL))
{
FileSys_Errno = ERROR_INVALID_HANDLE;
return -1;
}
#ifdef NOR_FLASH_DISK_ID
if (*(FileSysHandleTable[handle]) == NOR_FLASH_DISK_ID)
{
result = NOR_FFS_filelength(handle);
FileSys_Errno = NOR_FFS_Errno;
return result;
}
#endif
#ifdef NAND_FLASH_DISK_ID
if (*(FileSysHandleTable[handle]) == NAND_FLASH_DISK_ID)
{
result = NAND_FFS_filelength(handle);
FileSys_Errno = NAND_FFS_Errno;
return result;
}
#endif
#ifdef RAMDISK_ID
if (*(FileSysHandleTable[handle]) == RAMDISK_ID)
{
result = RD_filelength(handle);
FileSys_Errno = RDerrno;
return result;
}
#endif
#ifdef FAT_ID
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -