📄 backup.c
字号:
///////////////////////////////////////////////////////////////////////////////
//
// 文 件: Backup.c
// 执行备分工作的相关函数
//
// 作 者: 江南孤峰
// 联 系:QQ: 403324669
// 时 间: 2007--3--3
//
///////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <direct.h>
#include <io.h>
#include <conio.h>
#include "FilePass.h"
// 创建执行备分工作必要的目录
int CreateBackupDirect(void){
char strSavePath[_MAX_PATH] = {""};
_getcwd(strSavePath,_MAX_PATH);
if(!strcmp(strBackupDirect,"")) // 备分目录的获取不成功
return FAILED;
if(_chdir(strBackupDirect)) // 目录不存在
_mkdir("backup");
else
_chdir(strSavePath); // 恢复当前目录
return SUCCESS;
}
// 创建执行备分工作必要的文件
int CreateBackupFile(void){
char strSavePath[_MAX_PATH] = {""};
FILE *fp;
_getcwd(strSavePath,_MAX_PATH);
_chdir(strBackupDirect);
if(_access("BackFile.ini",0)){ // 文件BackFile.ini不存在
if((fp = fopen("BackFile.ini","w+")) != NULL){
fprintf(fp,"###");// 文件结束标记
fclose(fp);
}
else
return FAILED;
}
if(_access("FileTimes.ini",0)){
if((fp = fopen("FileTimes.ini","w+")) != NULL){
fprintf(fp,"0"); // 重命名因子初始化为 0
fclose(fp);
}
else
return FAILED;
}
_chdir(strSavePath);
return SUCCESS;
}
// 创建执行备分工作必要的目录,文件
void CreateBackupDirectAndFile(void){
if(CreateBackupDirect() == FAILED){
puts("创建执行备分工作必要的目录失败\n");
return ;
}
if(CreateBackupFile() == FAILED)
puts("创建执行备分工作必要文件失败\n");
}
// 读取备分目录中的文件数目
int ReadFileCounts(void){
FILE *fp;
int count = 0;
if((fp = fopen("FileTimes.ini","r+")) == NULL){
puts("文件 FileTimes.ini 不存在\n");
if(CreateBackupFile() == SUCCESS){
fp = fopen("FileTimes.ini","r+");
puts("文件 FileTimes.ini 已创建\n");
}
else
return count;
}
fscanf(fp,"%d",&count);
rewind(fp);
fprintf(fp,"%d",++count);
fclose(fp);
return count;
}
// 将当前目录下的文件 [strFileName] 移动到目录 [strDestDirect]
int MyMoveFile(char *strFileName,char *strDestDirect){
FILE *fp;
long fileSize = 0;
char *p;
char strSavePath[_MAX_PATH];
if((fp = fopen(strFileName,"r")) == NULL)
return FAILED;
fileSize = _filelength(_fileno(fp));
if((p = (char*)malloc(fileSize)) == NULL){
puts("内存不足 !\n");
return FAILED;
}
fread(p,fileSize,1,fp);
fclose(fp);
if(remove(strFileName) != SUCCESS)
printf("删除文件 %s 失败\n",strFileName);
_getcwd(strSavePath,_MAX_PATH);
_chdir(strDestDirect);
if((fp = fopen(strFileName,"w+")) == NULL){
printf("丢失文件 %s\n",strFileName);
free(p);
return FAILED;
}
fwrite(p,fileSize,1,fp);
fclose(fp);
_chdir(strSavePath);
free(p);
return SUCCESS;
}
void BackupFile(char *strFileName){
char strBkFileName[FILE_LENGTH + 6];
char strSavePath[_MAX_PATH + 2];
int count = 0;
FILE *fp;
if(MyMoveFile(strFileName,strBackupDirect) == FAILED){
printf("文件 %s 备分失败\n",strFileName);
return ;
}
_getcwd(strSavePath,_MAX_PATH);
// 在备分目录下重命名文件,并将原目录信息保存
_chdir(strBackupDirect);
count = ReadFileCounts();
sprintf(strBkFileName,"%s#%d",strFileName,count);
rename(strFileName,strBkFileName);
if( (fp = fopen("BackFile.ini","a")) == NULL &&
(CreateBackupFile() == FAILED) ){
printf("备分文件时,丢失文件 %s 的原路径\n",strFileName);
return;
}
fprintf(fp,"%s\n",strBkFileName);
fprintf(fp,"%s\n",strSavePath);
fprintf(fp,"###"); // 文件结束标记
printf("文件 %s 备分完成\n",strFileName);
fclose(fp);
_chdir(strSavePath);
}
// 从备分目录中恢复文件到原位置
void GetBackFile(
char *strAddSuffix , // 加密文件的后缀
char *strSourceSuffix // 原文件的后缀
){
char strSavePath[_MAX_PATH + 2] = {""};
char strFileName[FILE_LENGTH + 3] = {""};
char strOldFileName[FILE_LENGTH] = {""};
char strFileDirect[_MAX_PATH + 4] = {""};
char strFileDelete[FILE_LENGTH + 2] = {""};
int iCountSuccess = 0;
FILE *fp;
_getcwd(strSavePath,_MAX_PATH);
_chdir(strBackupDirect);
if((fp = fopen("BackFile.ini","r")) == NULL){
puts("打开文件 BackFile.ini 失败, 文件无法恢复到原位置\n");
return;
}
while(TRUE){
// 获取文件名和目录信息
fgets(strFileName,FILE_LENGTH,fp);
if(!strcmp(strFileName,"###")) // ### 为结束标记
break;
strcpy(strFileName,strFileName+3); // 忽略 ###
*strchr(strFileName,'\n') = '\0'; // 去掉读入的回车符
strcpy(strOldFileName,strFileName);
*strrchr(strOldFileName,'#') = '\0'; // 取得备分文件的原名字
fgets(strFileDirect,_MAX_PATH,fp);
*strchr(strFileDirect,'\n') = '\0';
// 移动文件
if(MyMoveFile(strFileName,strFileDirect) == FAILED){
printf("文件 \"%s\" 恢复失败\n\n",strOldFileName);
continue;
}
_chdir(strFileDirect);
if(rename(strFileName,strOldFileName) != SUCCESS){
remove(strFileName);
printf("发现同名文件 %s\n",strOldFileName);
}
else{
printf("文件 \"%s\" 恢复成功\n",strOldFileName);
iCountSuccess ++;
}
// 删除加密文件
if( GetDestFileName(
strFileDelete,
strOldFileName,
strSourceSuffix,
strAddSuffix) == SUCCESS &&
!access(strFileDelete,0)
){
if(remove(strFileDelete) == SUCCESS)
printf("删除文件 \"%s\" 完成\n",strFileDelete);
else
printf("删除文件 \"%s\" 失败\n",strFileDelete);
}
_chdir(strBackupDirect);
putchar('\n');
}
fclose(fp);
_chdir(strBackupDirect);
if(system("del *.*/q/f") == FAILED)
puts("清空备分目录失败\n");
else
puts("清空备分目录完成\n");
CreateBackupFile();
printf("共恢复文件:%d\n",iCountSuccess);
_chdir(strSavePath);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -