📄 fs_ext.c
字号:
{
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[0] = BACKUP_DRIVE_NAME;
srcFile[i] = '*';
srcFile[i+1] = '\0';
desIndex = i;
desFile[i] = '\0';
desFile[0] = BEGIN_DRIVE_NAME;
}
// check if the target has existed
srcFile[srcIndex] = '\0';
if (fs_access(srcFile, 0x0) == -1)
{
ap_free(desFile);
ap_free(srcFile);
return -1;
}
srcFile[srcIndex] = '*';
srcFile[srcIndex+1] = '\0';
/**** added by chilong ****/
// mkdir(PIMDirA);
// mkdir(ContactDirA);
/* marked by chilong
mkpath(dirString);
marked by chilong */
if (forceRestore == 0)
{
if (latestFileTimeInDir(&srcDate, &srcTime, srcFile) == -1)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " File error in backup directory!");
SprintStringLn(FS_DebugString);
#endif
ap_free(desFile);
ap_free(srcFile);
return -2;
}
if (latestFileTimeInDir(&desDate, &desTime, dirString) == -1)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " File error in destination directory!");
SprintStringLn(FS_DebugString);
#endif
ap_free(desFile);
ap_free(srcFile);
return -2;
}
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " backup: date=%d, time=%d, destination 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 older, do not restore!");
SprintStringLn(FS_DebugString);
#endif
ap_free(desFile);
ap_free(srcFile);
return 0;
}
}
// srcSize = totalFileSizeInDir(srcFile);
/**** modified by chilong ****/
srcFile[srcIndex] = '\0';
if ((srcSize = totalFileSizeInDir(srcFile)) == -1)
{
ap_free(desFile);
ap_free(srcFile);
return -4;
}
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 -2;
}
// desSize = totalFileSizeInDir(dirString);
/**** modified by chilong ****/
if ((desSize = totalFileSizeInDir(desFile)) == -1)
{
ap_free(desFile);
ap_free(srcFile);
return -4;
}
/**** 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 -2;
}
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " source size = %d, destination size = %d", srcSize, desSize);
SprintStringLn(FS_DebugString);
#endif
/* marked by chilong
desSize += getdiskspace2(dirString[0] - BEGIN_DRIVE_NAME + 1);
marked by chilong */
/**** modified by chilong ****/
desSize += getdiskspace2(desFile[0] - BEGIN_DRIVE_NAME + 1);
/**** modified by chilong ****/
#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 -2;
}
/**** modified by chilong ****/
// search DA_DIR first
if (fs_findfirst((char *)srcFile, &ffresult, DA_DIR) == 0)
{
do
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " %02d: Restore 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(desFile) != 0)
{
ap_free(desFile);
ap_free(srcFile);
return -1;
}
// we use desFile here because it locates in A drive
if ((result = restoreDirectory(desFile, forceRestore)) != 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 dir restored! ###");
else
sprintf(FS_DebugString, " ### %d dirs restored! ###", 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 RAMDISK
if (mkpath(desFile) != 0)
{
ap_free(desFile);
ap_free(srcFile);
return -1;
}
do
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, " %02d: Restore 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 copy, we need to delete the existing files
// 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 restored! ###");
else
sprintf(FS_DebugString, " ### %d files restored! ###", 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;
}
//char FS_DebugString[80];
/*************************************************************
Function: copyFile
Description:
Copy file.
Input:
srcFile - the source filename
desFile - the destination filename
Output:
0 restore succeeded
-1 failed - memory allocation error
-2 failed - file open error
-3 failed - file read error
-4 failed - file write error
Note:
mkdir the specified directory before calling this function.
**************************************************************/
int copyFile(char *srcFile, char *desFile)
{
int fh1;
int fh2;
long loops;
long remains;
long result;
long curSize;
long i;
unsigned char *buffer;
long bufSize = 8192;
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "Copy file %s to %s", srcFile, desFile);
SprintStringLn(FS_DebugString);
#endif
if (FileSys_Running == 0)
{
FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
return -1;
}
/**** added by chilong 12/7/2001 ****/
if (srcFile == NULL || desFile == NULL)
return -1;
/**** added by chilong 12/7/2001 ****/
FileSys_Errno = 0;
// allocate memory for copy buffer
buffer = (unsigned char *)ap_malloc((int)bufSize);
if (buffer == NULL)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "Memory allocation failed!");
SprintStringLn(FS_DebugString);
#endif
FileSys_Errno = ERROR_ALLOC_MEM;
return -1;
}
// open the source file
fh1 = fs_open((char *)srcFile, 0);
if (fh1 == -1)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "Open file %s failed! errno=0x%x", srcFile, FileSys_Errno);
SprintStringLn(FS_DebugString);
#endif
ap_free(buffer);
return -2;
}
// create the destination file
// fh2 = open(desFile, O_CREATE | O_TRUNC);
/**** modified by chilong 7/2/2001 ****/
if (fs_access((char *)desFile, 0x00) == -1)
{
fh2 = fs_open((char *)desFile, O_CREATE | O_RDWR);
}
else
{
fh2 = fs_open((char *)desFile, O_TRUNC | O_RDWR);
}
/**** modified by chilong 7/2/2001 ****/
if (fh2 == -1)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "Open file %s failed! errno=0x%x", desFile, FileSys_Errno);
SprintStringLn(FS_DebugString);
#endif
fs_close(fh1);
ap_free(buffer);
return -2;
}
// get the file length and the loop count required for copying
result = fs_filelength(fh1);
loops = result / bufSize;
remains = result % bufSize;
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "Open files %s & %s succeeded!", srcFile, desFile);
SprintStringLn(FS_DebugString);
sprintf(FS_DebugString, "size=%d, loops=%d, remains=%d", result, loops, remains);
SprintStringLn(FS_DebugString);
#endif
if (remains > 0)
loops++;
result = 0;
// each read in the loop fills the copy buffer
for (i = 0; i < loops; i++)
{
if (remains > 0 && i == (loops - 1))
curSize = remains;
else
curSize = bufSize;
result = fs_read(fh1, buffer, (long)curSize);
if (result < curSize)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "LOOP %02d: Read failed! size=%d, return=%d, errno=0x%x", i, curSize, result, FileSys_Errno);
SprintStringLn(FS_DebugString);
#endif
result = -3;
break;
}
result = fs_write(fh2, (char *)buffer, (long)curSize);
if (result < curSize)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "LOOP %02d: Write failed! size=%d, return=%d, errno=0x%x", i, curSize, result, FileSys_Errno);
SprintStringLn(FS_DebugString);
#endif
result = -4;
break;
}
result = 0;
}
/*
// the remaining file data which is less than the buffer size
if (remains > 0 && result == 0)
{
result = read(fh1, buffer, (long)remains);
if (result < remains)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "REMAINS: Read failed! return=%d, errno=0x%x", result, FileSys_Errno);
SprintStringLn(FS_DebugString);
#endif
result = -3;
}
else
{
result = write(fh2, buffer, (long)remains);
if (result < remains)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "%02d: Write failed! return=%d, errno=0x%x", result, FileSys_Errno);
SprintStringLn(FS_DebugString);
#endif
result = -4;
}
}
}
*/
#ifdef FILESYS_DEBUG
if (result < 0)
{
sprintf(FS_DebugString, "Copy %s to %s failed! %d", srcFile, desFile, result);
SprintStringLn(FS_DebugString);
}
else
{
sprintf(FS_DebugString, "Copy %s to %s succeeded!", srcFile, desFile);
SprintStringLn(FS_DebugString);
}
#endif
fs_close(fh2);
fs_close(fh1);
ap_free(buffer);
return (int)result;
}
/*************************************************************
Function: mkpath
Description:
Make a pathname.
Input:
pathname - the complete pathname
Output:
0 mkpath succeeded
else mkpath failed
**************************************************************/
int mkpath(const char *pathname)
{
int pathIndex;
int pathLen;
int result = 0;
unsigned char tempChar;
char *tempPath;
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "mkpath [%s]", pathname);
SprintStringLn(FS_DebugString);
#endif
if (FileSys_Running == 0)
{
FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
return -1;
}
/**** added by chilong 12/7/2001 ****/
if (pathname == NULL)
return -1;
/**** added by chilong 12/7/2001 ****/
FileSys_Errno = 0;
if (fs_access((char *)pathname, 0) == 0)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "[%s] already exist!", pathname);
SprintStringLn(FS_DebugString);
#endif
return 0; // already exist
}
pathLen = (int)strlen((char *)pathname);
tempPath = (char *)ap_malloc(MAX_PATHNAME_LEN + 2);
if (tempPath == NULL)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "Memory allocation failed");
SprintStringLn(FS_DebugString);
#endif
FileSys_Errno = ERROR_ALLOC_MEM;
return -1; // already exist
}
pathIndex = 0;
while (pathname[pathIndex] != '\0')
{
tempPath[pathIndex] = pathname[pathIndex];
pathIndex++;
}
tempPath[pathIndex] = '\0';
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "mkpath [%s]", tempPath);
SprintStringLn(FS_DebugString);
#endif
if (tempPath[1] == ':')
pathIndex = 3;
else
pathIndex = 1;
while (pathIndex < pathLen)
{
// sprintf(FS_DebugString, "CHAR: %c", tempPath[pathIndex]);
// SprintStringLn(FS_DebugString);
if (tempPath[pathIndex] == '\\' || tempPath[pathIndex] == '/' || tempPath[pathIndex + 1] == '\0')
{
// if (tempPath[pathIndex - 1] == ':')
// {
// pathIndex++;
// continue;
// }
if (tempPath[pathIndex + 1] != '\0')
{
tempChar = tempPath[pathIndex];
tempPath[pathIndex] = '\0';
}
if (fs_access(tempPath, 0) != 0)
{
#ifdef FILESYS_DEBUG
sprintf(FS_DebugString, "mkdir [%s]", tempPath);
SprintStringLn(FS_DebugString);
#endif
result = fs_mkdir((char *)tempPath);
#ifdef FILESYS_DEBUG
if (result == 0)
{
sprintf(FS_DebugString, "mkdir [%s] succeeded!", tempPath);
SprintStringLn(FS_DebugString);
}
else
{
sprintf(FS_DebugString, "mkdir [%s] failed! errno=0x%x", tempPath, FileSys_Errno);
SprintStringLn(FS_DebugString);
}
#endif
}
#ifdef FILESYS_DEBUG
else
{
sprintf(FS_DebugString, "[%s] already exist!", tempPath);
SprintStringLn(FS_DebugString);
}
#endif
if (tempPath[pathIndex + 1] != '\0')
{
tempPath[pathIndex] = tempChar;
}
if (result != 0)
break;
}
pathIndex++;
}
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -