📄 fs_ext.c
字号:
do
{
// keep doing 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 (deltree(srcFile) == -1)
result |= FileSys_Errno;
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);
/**** added by chilong 01/15/2002 ****/
ap_free(Pathname1);
/**** added by chilong 01/15/2002 ****/
return -1;
}
if (fs_findfirst((char *)srcFile, &ffresult, 0) == 0)
{
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;
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);
/**** added by chilong 01/15/2002 ****/
ap_free(Pathname1);
/**** added by chilong 01/15/2002 ****/
return -1;
}
srcFile[srcIndex] = '\0';
// remove this directory
if (fs_rmdir((char *)srcFile) == -1)
{
result |= FileSys_Errno;
}
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: backupDirectory
Description:
Backup the files in a directory to backup drive.
Input:
dirString - the directory string
forceBackup - force backup
1 - unconditional backup
0 - backup only if source is newer
Output:
0 backup succeeded
-1 failed - memory allocation error
-2 failed - no file in the directory or the directory not exist
-3 failed - file copy error
// chilong
-4 failed - error drive
Note:
// chilong
Directories within the specified directory are backeuped.
**************************************************************/
int backupDirectory(char *dirString, int forceBackup)
{
struct ffblk ffresult;
char *srcFile;
char *desFile;
int i;
int srcIndex;
int desIndex;
long srcSize;
long desSize;
unsigned short srcDate, srcTime;
unsigned short desDate, desTime;
int count = 0;
/**** added by chilong ****/
int drive;
int result;
/**** added by chilong ****/
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "[backupDirectory] %s, force=%d", dirString, forceBackup);
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;
}
desFile = (char *)ap_malloc(MAX_PATHNAME_LEN + 2);
if (desFile == NULL)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " Out of memory for destination filename!");
SprintStringLn(FS_DebugString);
#endif
ap_free(srcFile);
FileSys_Errno = ERROR_ALLOC_MEM;
return -1;
}
/**** added by chilong ****/
if (dirString[0] != BEGIN_DRIVE_NAME && dirString[0] != (BEGIN_DRIVE_NAME + 'a' - 'A'))
{
// we use desFile instead of Pathname1 because
// we have to avoid Pathname1 being changed
// by other threads
drive = getDriveNO((char *)dirString, desFile);
if (drive == -1)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " getDriveNO failed");
SprintStringLn(FS_DebugString);
#endif
ap_free(srcFile);
ap_free(desFile);
return -4;
}
/*
if ((drive+BEGIN_DRIVE_NAME) == BACKUP_DRIVE_NAME)
{
// dirString is located in BACKUP_DRIVE. Don't need to backup!
return 0;
}
*/
if ((drive+BEGIN_DRIVE_NAME) != BEGIN_DRIVE_NAME)
{
// dirString is not located in BEGIN_DRIVE_NAME. Error!
ap_free(srcFile);
ap_free(desFile);
return -4;
}
srcFile[0] = BEGIN_DRIVE_NAME;
srcFile[1] = ':';
for (i = 0; i < strlen((char*)desFile); i++)
srcFile[i+2] = desFile[i];
srcFile[i+2] = '\0';
for (i = 0; i < strlen((char*)srcFile); i++)
desFile[i] = srcFile[i];
desFile[0] = BACKUP_DRIVE_NAME;
if (srcFile[i-1] != '\\' && srcFile[i-1] != '/')
{
srcFile[i] = '\\';
desFile[i] = '\\';
i++;
}
srcIndex = i;
srcFile[i] = '*';
srcFile[i+1] = '\0';
desIndex = i;
desFile[i] = '\0';
}
else
{
for (i = 0; i < strlen((char *)dirString); i++)
{
srcFile[i] = dirString[i];
desFile[i] = dirString[i];
}
if (srcFile[i - 1] != '\\' && srcFile[i - 1] != '/')
{
srcFile[i] = '\\';
desFile[i] = '\\';
i++;
}
srcIndex = i;
srcFile[i] = '*';
srcFile[i+1] = '\0';
desIndex = i;
desFile[i] = '\0';
desFile[0] = BACKUP_DRIVE_NAME;
}
// check if the target has existed
srcFile[srcIndex] = '\0';
if (fs_access((char *)srcFile, 0x0) == -1)
{
ap_free(desFile);
ap_free(srcFile);
return -1;
}
srcFile[srcIndex] = '*';
srcFile[srcIndex+1] = '\0';
/**** added by chilong ****/
if (forceBackup == 0)
{
if (latestFileTimeInDir(&srcDate, &srcTime, dirString) == -1)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " File error in source directory!");
SprintStringLn(FS_DebugString);
#endif
ap_free(desFile);
ap_free(srcFile);
return -1;
}
if (latestFileTimeInDir(&desDate, &desTime, desFile) == -1)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " File error in backup directory!");
SprintStringLn(FS_DebugString);
#endif
ap_free(desFile);
ap_free(srcFile);
return -1;
}
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " source: date=%d, time=%d, backup date=%d, time=%d", srcDate, srcTime, desDate, desTime);
SprintStringLn(FS_DebugString);
#endif
if ((srcDate < desDate) || ((srcDate == desDate) && (srcTime < desTime)))
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " Backup is newer, no need to backup");
SprintStringLn(FS_DebugString);
#endif
ap_free(desFile);
ap_free(srcFile);
return 0;
}
}
// srcSize = totalFileSizeInDir(dirString);
/**** modified by chilong ****/
srcFile[srcIndex] = '\0';
if ((srcSize = totalFileSizeInDir(srcFile)) == -1)
{
ap_free(desFile);
ap_free(srcFile);
return -1;
}
srcFile[srcIndex] = '*';
srcFile[srcIndex + 1] = '\0';
/**** modified by chilong ****/
if (srcSize == -1)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " File error in source directory!");
SprintStringLn(FS_DebugString);
#endif
ap_free(desFile);
ap_free(srcFile);
return -1;
}
// desSize = totalFileSizeInDir(desFile);
/**** modified by chilong ****/
if ((desSize = totalFileSizeInDir(desFile)) == -1)
{
ap_free(desFile);
ap_free(srcFile);
return -1;
}
/**** modified by chilong ****/
if (desSize == -1)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " File error in destination directory!");
SprintStringLn(FS_DebugString);
#endif
ap_free(desFile);
ap_free(srcFile);
return -1;
}
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " source size = %d, destination size = %d", srcSize, desSize);
SprintStringLn(FS_DebugString);
#endif
desSize += getdiskspace2(BACKUP_DRIVE_NAME - BEGIN_DRIVE_NAME + 1);
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " Available space in backup drive = %d", desSize);
SprintStringLn(FS_DebugString);
#endif
if (desSize < srcSize * (4096 + 32) / 4096)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " Not enough free space on backup drive!");
SprintStringLn(FS_DebugString);
#endif
ap_free(desFile);
ap_free(srcFile);
return -1;
}
// mkdir(PIMDirB);
// mkdir(ContactDirB);
/**** modified by chilong ****/
//
// search DA_DIR first
if (fs_findfirst((char *)srcFile, &ffresult, DA_DIR) == 0)
{
do
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " %02d: Backup dir %s", count + 1, ffresult.ff_name);
SprintStringLn(FS_DebugString);
#endif
for (i = 0; i < strlen((char *)ffresult.ff_name); i++)
{
srcFile[srcIndex + i] = ffresult.ff_name[i];
desFile[desIndex + i] = ffresult.ff_name[i];
}
srcFile[srcIndex + i] = '\0';
desFile[desIndex + i] = '\0';
if (mkpath((const char *)desFile) != 0)
{
ap_free(desFile);
ap_free(srcFile);
return -1;
}
if ((result = backupDirectory(srcFile, forceBackup)) != 0)
{
deltree(desFile);
ap_free(desFile);
ap_free(srcFile);
return result;
}
count++;
srcFile[srcIndex] = '*';
srcFile[srcIndex + 1] = '\0';
} while (fs_findnext(&ffresult) != -1);
fs_findclose(&ffresult);
#ifdef FILESYS_DEBUG
if (count == 1)
sprintf(FS_DebugString, " ### 1 file backup! ###");
else
sprintf(FS_DebugString, " ### %d files backup! ###", count);
SprintStringLn(FS_DebugString);
#endif
}
// 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);
ap_free(desFile);
return -4;
}
// search for files
if (fs_findfirst((char *)srcFile, &ffresult, 0) == 0)
{
// make the respective directory in backup disk
if (mkpath((const char *)desFile) != 0)
{
ap_free(desFile);
ap_free(srcFile);
return -1;
}
do
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " %02d: Backup file %s", count + 1, ffresult.ff_name);
SprintStringLn(FS_DebugString);
#endif
for (i = 0; i < strlen((char *)ffresult.ff_name); i++)
{
srcFile[srcIndex + i] = ffresult.ff_name[i];
desFile[desIndex + i] = ffresult.ff_name[i];
}
srcFile[srcIndex + i] = '\0';
desFile[desIndex + i] = '\0';
// before we copy a file, we need to remove the existing file
// remove(desFile);
if (copyFile(srcFile, desFile) != 0)
{
desFile[desIndex] = '\0';
removeFilesInDir(desFile);
ap_free(desFile);
ap_free(srcFile);
return -1;
}
count++;
srcFile[srcIndex] = '*';
srcFile[srcIndex + 1] = '\0';
} while (fs_findnext(&ffresult) != -1);
fs_findclose(&ffresult);
#ifdef FILESYS_DEBUG
if (count == 1)
sprintf(FS_DebugString, " ### 1 file backup! ###");
else
sprintf(FS_DebugString, " ### %d files backup! ###", count);
SprintStringLn(FS_DebugString);
#endif
}
// 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);
ap_free(desFile);
return -4;
}
/**** modified by chilong ****/
ap_free(desFile);
ap_free(srcFile);
return 0;
}
/*************************************************************
Function: restoreDirectory
Description:
Restore the files from the backup drive to the specified directory.
Input:
dirString - the directory string
forceRestore - force restore
1 - unconditional restore
0 - restore only if backup is newer
Output:
0 restore succeeded
-1 failed - memory allocation error
-2 failed - no file in the backup directory or the directory not exist
-3 failed - file copy error
// chilong
-4 failed - error drive
Note:
// chilong
Directories within the specified directory are restored.
**************************************************************/
int restoreDirectory(char *dirString, int forceRestore)
{
struct ffblk ffresult;
char *srcFile;
char *desFile;
int i;
int count = 0;
int srcIndex;
int desIndex;
long srcSize;
long desSize;
unsigned short srcDate, srcTime;
unsigned short desDate, desTime;
/**** added by chilong ****/
int drive;
int result;
/**** added by chilong ****/
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "[restoreDirectory] %s, forceRestore = %d", dirString, forceRestore);
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;
}
desFile = (char *)ap_malloc(MAX_PATHNAME_LEN + 2);
if (desFile == NULL)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " Out of memory for destination filename!");
SprintStringLn(FS_DebugString);
#endif
ap_free(srcFile);
FileSys_Errno = ERROR_ALLOC_MEM;
return -1;
}
/**** added by chilong ****/
// if (dirString[0] != BACKUP_DRIVE_NAME && dirString[0] != (BACKUP_DRIVE_NAME + 'a' - 'A'))
if (dirString[0] != BEGIN_DRIVE_NAME && dirString[0] != (BEGIN_DRIVE_NAME + 'a' - 'A'))
{
// we use desFile instead of Pathname1 because
// we have to avoid Pathname1 being changed
// by other threads
drive = getDriveNO((char *)dirString, (char *)desFile);
if (drive == -1)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " getDriveNO failed");
SprintStringLn(FS_DebugString);
#endif
ap_free(srcFile);
ap_free(desFile);
return -4;
}
/*
if ((drive+BEGIN_DRIVE_NAME) == BEGIN_DRIVE_NAME)
{
// dirString is located in BACKUP_DRIVE. Don't need to restore!
return 0;
}
*/
if ((drive+BEGIN_DRIVE_NAME) != BEGIN_DRIVE_NAME)
{
// dirString is not located in BEGIN_DRIVE_NAME. Error!
ap_free(srcFile);
ap_free(desFile);
return -4;
}
srcFile[0] = BACKUP_DRIVE_NAME;
srcFile[1] = ':';
for (i = 0; i < strlen((char*)desFile); i++)
srcFile[i+2] = desFile[i];
srcFile[i+2] = '\0';
for (i = 0; i < strlen((char*)srcFile); i++)
desFile[i] = srcFile[i];
desFile[0] = BEGIN_DRIVE_NAME;
if (srcFile[i-1] != '\\' && srcFile[i-1] != '/')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -