📄 fs_ext.c
字号:
/****************************************************************
File Name: FS_EXT.C *
*****************************************************************
Last Modified Date: 2001/04/17
Compiler : GNU Cross-compiler/SDS
Platform : X86 protection mode, MIPS, Dragonball
Usage :
Extension functions of file system for users.
// chilong
Note: Many functions are recursive functions. So there might be
out of memory if there're too many files. Watch out!
****************************************************************/
#include <sys/syscall.h>
#include <stdio.h>
#include <string.h>
//#include "../rDebug/include/rdebug.h"
#include "../include/FileSys.h"
extern void printString(char *string);
//#define FILESYS_DEBUG
//char FS_DebugString[80];
/*************************************************************
Function: totalFileSizeInDir
Description:
Get the total size of the files in the specified
directory.
Input:
dirString - the specified directory
Return value:
Total file size in bytes
-1 if error
Note:
This is the raw total file size. Extra spaces occupied
by the file due to the block size are not counted.
// chilong
This function also checkes the size of all the subdirectories
under the dirString directory.
**************************************************************/
long totalFileSizeInDir(char *dirString)
{
char *srcFile;
struct ffblk ffresult;
long totalSize;
/**** added by chilong ****/
long subTotalSize;
int i;
int srcIndex;
/**** added by chilong ****/
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "[totalFileSizeInDir] %s", dirString);
SprintStringLn(FS_DebugString);
#endif
if (FileSys_Running == 0)
{
FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
return -1;
}
/**** added by chilong 12/7/2001 ****/
if (dirString == NULL)
return -1;
/**** added by chilong 12/7/2001 ****/
FileSys_Errno = 0;
srcFile = (char *)ap_malloc(MAX_PATHNAME_LEN*2 + 2);
if (srcFile == NULL)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "Out of memory for source filename!");
SprintStringLn(FS_DebugString);
#endif
FileSys_Errno = ERROR_ALLOC_MEM;
return -1;
}
/**** modified by chilong ****/
// copy the target DIR name to srcFile and add a "\*" at its end for findX()
// get the wildcard name
for (i = 0; i < strlen((char *)dirString); i++)
srcFile[i] = dirString[i];
if (i > 0 && srcFile[i - 1] != '*' && srcFile[i - 1] != '?')
{
if (i > 0 && srcFile[i - 1] != '\\' && srcFile[i - 1] != '/')
{
srcFile[i] = '\\';
i++;
}
}
srcIndex = i;
srcFile[i++] = '*';
srcFile[i] = '\0';
// get all files in the directory and count filesize
totalSize = 0;
// search DIR
if (fs_findfirst((char *)srcFile, &ffresult, DA_DIR) == 0)
{
do
{
// keep searching for the next level
// copy the next directory name
for (i = 0; i < strlen((char *)ffresult.ff_name); i++)
srcFile[srcIndex + i] = ffresult.ff_name[i];
srcFile[srcIndex + i] = '\0';
if ((subTotalSize=totalFileSizeInDir(srcFile)) == -1)
{
// oops! something wrong!
ap_free(srcFile);
return -1;
}
totalSize += subTotalSize;
srcFile[srcIndex] = '*';
srcFile[srcIndex + 1] = '\0';
} while (fs_findnext(&ffresult) != -1);
fs_findclose(&ffresult);
}
// it happens when CF Card is out or file system errors
if (FileSys_Errno == ERROR_INVALID_DRIVE || FileSys_Errno == ERROR_FILE_SYSTEM_NOT_INIT)
{
ap_free(srcFile);
return -1;
}
// search files
if (fs_findfirst((char *)srcFile, &ffresult, 0) == 0)
{
do
{
// file found, get the file size
totalSize += ffresult.ff_fsize;
} while (fs_findnext(&ffresult) != -1);
fs_findclose(&ffresult);
}
// it happens when CF Card is out or file system errors
if (FileSys_Errno == ERROR_INVALID_DRIVE || FileSys_Errno == ERROR_FILE_SYSTEM_NOT_INIT)
{
ap_free(srcFile);
return -1;
}
/**** modified by chilong ****/
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " total file size = %d", totalSize);
SprintStringLn(FS_DebugString);
#endif
// findclose(&ffresult);
ap_free(srcFile);
return totalSize;
}
/*************************************************************
Function: latestFileTimeInDir
Description:
Find the latest date and time among the files in the
specified directory.
Input:
dirString - the specified directory
can be "A:\LVL1\LVL2\LVL3"
or "A:\LVL1\LVL2\LVL3\"
or "A:\LVL1\LVL2\LVL3\*"
or "A:\LVL1\LVL2\LVL3\*.*"
Output:
ldate - the latest date
ltime - the latest time
Return value:
0 if succeeded
-1 if error
**************************************************************/
int latestFileTimeInDir(unsigned short *lDate, unsigned short *lTime, char *dirString)
{
char *srcFile;
struct ffblk ffresult;
int i;
/**** added by chilong ****/
unsigned short tmpDate, tmpTime;
int srcIndex;
/**** added by chilong ****/
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "[latestFileTimeInDir] %s", dirString);
SprintStringLn(FS_DebugString);
#endif
if (FileSys_Running == 0)
{
FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
return -1;
}
FileSys_Errno = 0;
srcFile = (char *)ap_malloc(MAX_PATHNAME_LEN + 2);
if (srcFile == NULL)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "Out of memory for source filename!");
SprintStringLn(FS_DebugString);
#endif
FileSys_Errno = ERROR_ALLOC_MEM;
return -1;
}
/**** modified by chilong ****/
// copy the target DIR name to srcFile and add a "\*" at its end for findX()
// get the wildcard name
for (i = 0; i < strlen((char *)dirString); i++)
srcFile[i] = dirString[i];
if (srcFile[i - 1] != '*' && srcFile[i - 1] != '?')
if (i > 0 && srcFile[i - 1] != '*' && srcFile[i - 1] != '?')
{
if (srcFile[i - 1] != '\\' && srcFile[i - 1] != '/')
{
srcFile[i] = '\\';
i++;
}
}
srcIndex = i;
srcFile[i++] = '*';
srcFile[i] = '\0';
// get all files in the directory and count filesize
tmpDate = tmpTime = 0;
if (fs_findfirst((char *)srcFile, &ffresult, DA_DIR) == 0)
{
do
{
// copy the next directory name
for (i = 0; i < strlen((char *)ffresult.ff_name); i++)
srcFile[srcIndex + i] = ffresult.ff_name[i];
srcFile[srcIndex + i] = '\0';
if (latestFileTimeInDir(&tmpDate, &tmpTime, srcFile) == -1)
{
fs_findclose(&ffresult);
ap_free(srcFile);
return -1;
}
// dir found, check date and time
if ((ffresult.ff_fdate > tmpDate) || ((ffresult.ff_fdate == tmpDate) && (ffresult.ff_ftime > tmpTime)))
{
tmpDate = ffresult.ff_fdate;
tmpTime = ffresult.ff_ftime;
}
srcFile[srcIndex] = '*';
srcFile[srcIndex + 1] = '\0';
} while (fs_findnext(&ffresult) != -1);
fs_findclose(&ffresult);
}
// it happens when CF Card is out or file system errors
if (FileSys_Errno == ERROR_INVALID_DRIVE || FileSys_Errno == ERROR_FILE_SYSTEM_NOT_INIT)
{
ap_free(srcFile);
return -1;
}
if (fs_findfirst((char *)srcFile, &ffresult, 0) == 0)
{
do
{
// dir found, check date and time
if ((ffresult.ff_fdate > tmpDate) || ((ffresult.ff_fdate == tmpDate) && (ffresult.ff_ftime > tmpTime)))
{
tmpDate = ffresult.ff_fdate;
tmpTime = ffresult.ff_ftime;
}
} while (fs_findnext(&ffresult) != -1);
fs_findclose(&ffresult);
}
// it happens when CF Card is out or file system errors
if (FileSys_Errno == ERROR_INVALID_DRIVE || FileSys_Errno == ERROR_FILE_SYSTEM_NOT_INIT)
{
ap_free(srcFile);
return -1;
}
*lDate = tmpDate;
*lTime = tmpTime;
/**** modified by chilong ****/
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " date = %ud, time = %ud", *lDate, *lTime);
SprintStringLn(FS_DebugString);
#endif
// findclose(&ffresult);
ap_free(srcFile);
return 0;
}
/*************************************************************
Function: removeFilesInDir
Description:
Remove all files in the specified directory.
Input:
dirString - the directory string
Output:
0 function succeeded
-1 function failed
Note:
Directories within the specified directory are not removed.
**************************************************************/
int removeFilesInDir(char *dirString)
{
struct ffblk ffresult;
char *srcFile;
int i;
int srcIndex;
int result;
/**** added by chilong 01/15/2002 ****/
char *Pathname1;
/**** added by chilong 01/15/2002 ****/
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "[removeFilesInDir] %s", dirString);
SprintStringLn(FS_DebugString);
#endif
if (FileSys_Running == 0)
{
FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
return -1;
}
/**** added by chilong 12/7/2001 ****/
if (dirString == NULL)
return -1;
/**** added by chilong 12/7/2001 ****/
FileSys_Errno = 0;
srcFile = (char *)ap_malloc(MAX_PATHNAME_LEN + 2);
if (srcFile == NULL)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "Out of memory for source filename!");
SprintStringLn(FS_DebugString);
#endif
FileSys_Errno = ERROR_ALLOC_MEM;
return -1;
}
/**** added by chilong 01/15/2002 ****/
if ((Pathname1 = (char *)ap_malloc(MAX_PATHNAME_LEN + 2)) == NULL)
{
FileSys_Errno = ERROR_ALLOC_MEM;
ap_free(srcFile);
return -1;
}
/**** added by chilong 01/15/2002 ****/
result = getDriveNO((char *)dirString, (char *)Pathname1);
if (result == -1)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " getDriveNO failed");
SprintStringLn(FS_DebugString);
#endif
/**** added by chilong 01/15/2002 ****/
ap_free(Pathname1);
ap_free(srcFile);
/**** added by chilong 01/15/2002 ****/
return -1;
}
// get the wildcard name
for (i = 0; i < strlen((char *)dirString); i++)
srcFile[i] = dirString[i];
// if (srcFile[i - 1] != '\\' && srcFile[i - 1] != '/')
/**** modified by chilong ****/
if (i > 0 && srcFile[i - 1] != '\\' && srcFile[i - 1] != '/')
/**** modified by chilong ****/
{
srcFile[i] = '\\';
i++;
}
srcIndex = i;
#ifdef NAND_FLASH_DISK_ID
if (result == NAND_FLASH_DISK_ID)
srcIndex = 2;
#endif
#ifdef NOR_FLASH_DISK_ID
if (result == NOR_FLASH_DISK_ID)
srcIndex = 2;
#endif
srcFile[i++] = '*';
srcFile[i] = '\0';
result = 0;
if (fs_findfirst((char *)srcFile, &ffresult, 0) == -1)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "No file found in source CONTACT directory!");
SprintStringLn(FS_DebugString);
#endif
ap_free(srcFile);
/**** added by chilong 01/15/2002 ****/
ap_free(Pathname1);
/**** added by chilong 01/15/2002 ****/
return -1;
}
else
{
do
{
for (i = 0; i < strlen((char *)ffresult.ff_name); i++)
srcFile[srcIndex + i] = ffresult.ff_name[i];
srcFile[srcIndex + i] = '\0';
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "Remove file %s", srcFile);
SprintStringLn(FS_DebugString);
#endif
if (remove((char *)srcFile) == -1)
result = FileSys_Errno;
/**** added by chilong ****/
srcFile[srcIndex] = '*';
srcFile[srcIndex + 1] = '\0';
/**** added by chilong ****/
} while (fs_findnext(&ffresult) != -1);
fs_findclose(&ffresult);
// it happens when CF Card is out or file system errors
if (FileSys_Errno == ERROR_INVALID_DRIVE || FileSys_Errno == ERROR_FILE_SYSTEM_NOT_INIT)
{
ap_free(srcFile);
/**** added by chilong 01/15/2002 ****/
ap_free(Pathname1);
/**** added by chilong 01/15/2002 ****/
return -1;
}
}
ap_free(srcFile);
if (result != 0)
{
FileSys_Errno = result;
result = -1;
}
/**** added by chilong 01/15/2002 ****/
ap_free(Pathname1);
/**** added by chilong 01/15/2002 ****/
return result;
}
/**** added by chilong ****/
/*************************************************************
Function: deltree
Description:
Remove a directory, including the files/dir under it
Input:
dirString - the directory string
Output:
0 function succeeded
-1 function failed
Note:
**************************************************************/
int deltree(char *dirString)
{
struct ffblk ffresult;
char *srcFile;
int i;
int srcIndex;
int result;
/**** added by chilong 01/15/2002 ****/
char *Pathname1;
/**** added by chilong 01/15/2002 ****/
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "[deltree] %s", dirString);
SprintStringLn(FS_DebugString);
#endif
if (FileSys_Running == 0)
{
FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
return -1;
}
/**** added by chilong 12/7/2001 ****/
if (dirString == NULL)
return -1;
/**** added by chilong 12/7/2001 ****/
FileSys_Errno = 0;
srcFile = (char *)ap_malloc(MAX_PATHNAME_LEN * 2 + 2);
if (srcFile == NULL)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "Out of memory for source filename!");
SprintStringLn(FS_DebugString);
#endif
FileSys_Errno = ERROR_ALLOC_MEM;
return -1;
}
/**** added by chilong 01/15/2002 ****/
if ((Pathname1 = (char *)ap_malloc(MAX_PATHNAME_LEN + 2)) == NULL)
{
FileSys_Errno = ERROR_ALLOC_MEM;
ap_free(srcFile);
return -1;
}
/**** added by chilong 01/15/2002 ****/
result = getDriveNO((char *)dirString, Pathname1);
if (result == -1)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " getDriveNO failed");
SprintStringLn(FS_DebugString);
#endif
ap_free(srcFile);
/**** added by chilong 01/15/2002 ****/
ap_free(Pathname1);
/**** added by chilong 01/15/2002 ****/
return -1;
}
// get the wildcard name
for (i = 0; i < strlen((char *)dirString); i++)
srcFile[i] = dirString[i];
if (i > 0 && srcFile[i - 1] != '\\' && srcFile[i - 1] != '/')
{
srcFile[i] = '\\';
i++;
}
srcIndex = i;
#ifdef NAND_FLASH_DISK_ID
if (result == NAND_FLASH_DISK_ID)
{
srcIndex = 2;
/**** added by chilong 12/4/2001 ****/
// this part will be unmarked in the future
// after directory structure is supported
/**** added by chilong 01/15/2002 ****/
ap_free(Pathname1);
ap_free(srcFile);
/**** added by chilong 01/15/2002 ****/
return -1;
/**** added by chilong 12/4/2001 ****/
}
#endif
#ifdef NOR_FLASH_DISK_ID
if (result == NOR_FLASH_DISK_ID)
{
srcIndex = 2;
/**** added by chilong 12/4/2001 ****/
// this part will be unmarked in the future
// after directory structure is supported
/**** added by chilong 01/15/2002 ****/
ap_free(Pathname1);
ap_free(srcFile);
/**** added by chilong 01/15/2002 ****/
return -1;
/**** added by chilong 12/4/2001 ****/
}
#endif
srcFile[i++] = '*';
srcFile[i] = '\0';
result = 0;
// see if there are any subdirectories
if (fs_findfirst((char *)srcFile, &ffresult, DA_DIR) == 0)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "No dir found in source CONTACT directory!");
SprintStringLn(FS_DebugString);
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -