⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 undelete.c

📁 LINUX内核中实现“回收站”。
💻 C
字号:
/*	undelete.c	by Liu Tao 0244072  	version 2.77*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/types.h>#include <dirent.h>#include <sys/stat.h>#include "error.h"#include "fcntl.h"#include "sys/time.h"//#define _DEBUG#define _DATA_FILE "/trash/data.xml"#define _DATA_TMP "/trash/data~.xml"#define _LINE_LEN 256int uid;char currentDir[50]; char fileName[50];char undelName[50];int exist = 0;int flag =0;char old_path[100];char trash[50] = "/trash/";struct timeval date1;long int fileID=0;int compare(const void *s1, const void *s2){	char *t1 = (char *)s1;	char *t2 = (char *)s2;	return strcmp(t1, t2);}void do_undelete(char name[100]){     char ch;                /* char in and out */     int source, destination; /* file fd */     int r_count,w_count;     strcat(trash,name);     strcpy(name,trash);     if (flag==1)     {	int j = mkdir(old_path,0);	int k = rmdir(name);     }     else if (flag ==0){   		if ( (source=open(name,O_RDONLY)) == -1)		{  		  	 printf( "Can't open file %s!\n", name );  		   	 return;  		}  		if( (destination = open(old_path,O_APPEND | O_CREAT |O_WRONLY,0100644)) == -1)		{    		  	 printf( "Can't put file!\n",old_path);    		         return;   		}		do 		{			r_count = read(source,&ch,1);			if (r_count != 0) w_count = write(destination,&ch,1);		}		while(r_count!=0);				int i = unlink(name);   		close( source );   		if( close( destination )!=0)		{   		        printf( "Original directory %s not exist!\n",currentDir );   		        return;   		}     }}int match(char tmp[100]){	char *ch;	int i,count1=0,count2=0;        ch=strchr(tmp,'+');        while (ch!=NULL) {	    count1++;            tmp[ch-tmp]='/';            ch=strchr(ch+1,'+');        }		memcpy(old_path,tmp,100);	ch =tmp;	ch=strchr(tmp,'/');        while (ch!=NULL) {	    count2++;	    if (count2 == count1)               tmp[ch-tmp]=' ';   	    ch=strchr(ch+1,'/');                 }	sscanf(tmp,"%s %s",currentDir,fileName);		if (strncmp(fileName,undelName,strlen(undelName)) == 0){#ifdef _DEBUG		printf("IN MATCH: old_path=%s,tmp=%s,currentDir=%s,fileName=%s\n",old_path,tmp,currentDir,fileName);#endif		return 1;	}	else return 0;}int found(char name[100]){	int f_uid;	long int time;	int option;	char tmp_dir[100];#ifdef _DEBUG	printf("IN FOUND: name=%s\n",name);#endif		sscanf(name,"%d-%li-%d-%s%s",&f_uid,&time,&option,tmp_dir);		date1.tv_sec = time;	date1.tv_usec = 0;	if (f_uid == uid || uid==0) {#ifdef _DEBUG		printf("%d   ", f_uid);		printf("%li   ", time);		printf("%s", option == 0 ? "File    " : "Dir    ");		printf("%s   ", currentDir);		printf("%s   \n", fileName);#endif			flag = option == 0?  0 : 1;		return match(tmp_dir);	}	else return 0;}int main(int argc, char **argv){	int i, file_num;	DIR *dirptr;		struct dirent *entry;	struct stat buf;	char file[1000][100];	char temp[20];	const char empty[] = "";	if (argc!=3)	{		printf("Usage: undelete <file name> <file id>\n");		return -1;	}	strcpy(undelName,argv[1]);	strcpy(temp,argv[2]);	fileID = atol(temp);	uid = getuid();	if (uid!=0) 	{		printf("Please Let Administrator Do It.\n");			return -1;	}	if((dirptr = opendir("/trash")) == NULL)	{		printf("Cannot open %s\n", argv[1]);		return 0;	}	file_num = 0;	while((entry = readdir(dirptr)) != NULL) strcpy(file[file_num++], entry->d_name);	qsort(file, file_num, 100, compare);	for(i = 2; i < file_num; i++)	{		strcpy(currentDir,empty); 		strcpy(fileName,empty);        	if (found(file[i]))		{#ifdef _DEBUG			printf("Found %s\n", file[i]);#endif			exist = 1;			do_undelete(file[i]);			break;		}	}	closedir(dirptr);	//write new content to temp. xml file	//	FILE* f0 = fopen(_DATA_FILE, "r");	FILE* f1 = fopen(_DATA_TMP, "w+");	char szLine[_LINE_LEN];	char* c;		do	{		c = fgets(szLine, _LINE_LEN, f0);		if ((c!=NULL) && (strstr(szLine, argv[1])==NULL || strstr(szLine, argv[2])==NULL))		{			fprintf(f1, szLine);		}	}	while (c!=NULL);	fclose(f0), fclose(f1);	unlink(_DATA_FILE);		//copy new xml file to old name	//	f0 = fopen(_DATA_TMP, "r");	f1 = fopen(_DATA_FILE, "w+");	do	{		c = fgets(szLine, _LINE_LEN, f0);		if (c!=NULL) fprintf(f1, szLine);	}	while (c!=NULL);	unlink(_DATA_TMP);		if (exist == 0) printf("File %s not exist!\n", undelName);	return 0;}#undef _DATA_FILE#undef _DATA_TMP#undef _LINE_LEN#undef _DEBUG

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -