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

📄 delete.c~

📁 linux内核 相当好的不得了的书 非常以及很特别的使用
💻 C~
字号:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/resource.h>#include <sys/times.h>#include <time.h>

#include "sdel.h"
   
int slow = O_SYNC;
int fd;
unsigned long bufsize = BLOCKSIZE;
void help () 
{
    printf("Options:\n");
    printf("\t-g  use the Gutman's method\n");
    printf("\t-a  use American country peaceful overall situation'method\n");
    printf("\t-b  Bruce schneier's method\n");    printf("\t-d  Dob'method\n");
    exit(1);
}
int smash_it(char *filename,int mode)  
{

	struct stat filestat;
    struct stat controlstat;
    int i_am_a_directory = 0;
	//unsigned long bufsize = BLOCKSIZE;


/* get the file stats */
    if (lstat(filename, &filestat))
            return 1;

    if (S_ISREG(filestat.st_mode) && filestat.st_nlink > 1) 
	{//是否常规文件,链接数是否大于1
        fprintf(stderr, "Error: File %s - file is hardlinked %d time(s), skipping!\n", filename, filestat.st_nlink - 1);
        return -1;
    }

/* if the blocksize on the filesystem is bigger than the on compiled with, enlarge! */
    if (filestat.st_blksize > bufsize) 
	{
		if (filestat.st_blksize > 65532) 
		{
	    bufsize = 65535;
		} 
		else 
		{
	    bufsize = (((filestat.st_blksize / 3) + 1) * 3);
		}
    }

/* handle the recursive mode */
    //if (recursive)//递归模式
        if (S_ISDIR(filestat.st_mode)) 
		{
			//如果是目录文件
	        DIR *dir;
	        struct dirent *dir_entry;
	        struct stat cwd_stat;
	        char current_dir[4097];
	        int res;
	        int chdir_success = 1;
 
            //if (verbose) printf("DIRECTORY (going recursive now)\n");
            getcwd(current_dir, 4096);//取当前目录
            current_dir[4096] = '\0';
            
            /* a won race will chmod a file to 0700 if the user is owner/root
               I'll think about a secure solution to this, however, I think
               there isn't one - anyone with an idea? */
            if (chdir(filename)) 
			{
                (void) chmod(filename, 0700); /* ignore permission errors */
                if (chdir(filename)) 
				{
                    fprintf(stderr,"Can't chdir() to %s, hence I can't wipe it.\n", filename);
                    chdir_success = 0;
                }
            }

	       if (chdir_success) 
		   {
			   lstat(".", &controlstat);
	           lstat("..", &cwd_stat);
	           if ( (filestat.st_dev != controlstat.st_dev) || (filestat.st_ino != controlstat.st_ino) ) 
			   {
				   fprintf(stderr, "Race found! (directory %s became a link)\n", filename);
			   } 
			   else 
			   {
				  if ((dir = opendir (".")) != NULL) 
				  {
	                (void) chmod(".", 0700); /* ignore permission errors */
	                dir = opendir (".");
				  }
		          if (dir != NULL) 
				  {
					while ((dir_entry = readdir(dir)) != NULL)// readdir 函数返回指定目录下的所有目录项
			        if (strcmp(dir_entry->d_name, ".") && strcmp(dir_entry->d_name, "..")) 
					{
						printf("Wiping %s ", dir_entry->d_name);
		  	            if ( (res = smash_it(dir_entry->d_name, mode)) > 0) 
						{
							if (res == 3)
		  	                fprintf(stderr,"File %s was raced, hence I won't wipe it.\n", dir_entry->d_name);
		  	                else 
							{
			                fprintf(stderr,"Couldn't delete %s. ", dir_entry->d_name);
			                perror("");
							}
						} 
						else
			                printf(" Done\n");
					}
		            closedir(dir);
				  }
			   }
		       if(chdir(current_dir) != 0)
			   {
				fprintf(stderr, "Error: Can't chdir to %s (aborting) - ", current_dir);
		        perror("");
		        exit(1);
			   }
/*
		lstat(current_dir, &controlstat);
		if ( (cwd_stat.st_dev != controlstat.st_dev) || (cwd_stat.st_ino != controlstat.st_ino) ) {
                    fprintf(stderr, "Race found! (directory %s was exchanged or its your working directory)\n", current_dir);
                    exit(1);
                }
*/
		   i_am_a_directory = 1;
		   }
		}
	
/* end of recursive function */


    if (S_ISREG(filestat.st_mode)) 
	{//是否常规文件

       /* open the file for writing in sync. mode */
        if ((fd = open(filename, O_RDWR | O_LARGEFILE | slow)) < 0)
		{
            /* here again this has a race problem ... hmmm */
            /* make it writable for us if possible */
            (void) chmod(filename, 0600);   /* ignore errors */                       
            if ((fd = open(filename, O_RDWR | O_LARGEFILE | slow)) < 0)//O_LARGEFILE的作用就是在32位系统中支持大文件系统,允许打开那些用31位(2G)都不能表示其长度的大文件
    	        return 1;
        }

        fstat(fd, &controlstat);
        if ((filestat.st_dev != controlstat.st_dev) || (filestat.st_ino != controlstat.st_ino) || (! S_ISREG(controlstat.st_mode))) 
		{
            close(fd);
            return 3;
        }

        if (sdel_overwrite(mode, fd, 0, bufsize, filestat.st_size > 0 ? filestat.st_size : 1) == 0)
            //return sdel_unlink(filename, 0, 1, slow);
			return 0;
    } /* end IS_REG() */
    else 
	{
        if (S_ISDIR(filestat.st_mode))
		{
            if (i_am_a_directory == 0) 
			{
                fprintf(stderr,"Warning: %s is a directory. I will not remove it, because the -r option is missing!\n", filename);
                return 0;
            } 
			else
                //return sdel_unlink(filename, 1, 0, slow);
				return 0;
        } 
		else
            if (! S_ISDIR(filestat.st_mode)) 
			{
	           fprintf(stderr,"Warning: %s is not a regular file, rename/unlink only!", filename);
	           //if (! verbose)
	           printf("\n");
	           //return sdel_unlink(filename, 0, 0, slow);
            }
	}

    return 99; // not reached
}void doit(char *str,clock_t time){   long tps=sysconf(_SC_CLK_TCK);   printf("%s: %6.2f secs\n",str,(float)time/tps);}   
int main(int argc,char*argv[])
{
	int mode=0;   int errors=0;
	int loop;
	int i;
	int result;   clock_t start,end;   struct tms t_start,t_end;   start=times(&t_start);
	prg=argv[0];
	if(argc<2||strncmp(argv[1],"-h",2)==0||strncmp(argv[1],"--h",3)==0)
		help();
	while(1)
	{
		result=getopt(argc,argv,"GgAaBbDd");
		if(result<0)
			break;
		switch(result)
		{
		    case 'g':
			case 'G': mode=0;
				      break;
			case 'a':
			case 'A': mode=1;
				      break;
			case 'b': 
			case 'B': mode=2;
				      break;         case 'd':         case 'D': mode=3;                   break;
			default:  help();
		}
	}
	sdel_init(slow);
	switch (mode) 
	{
         case 0 : printf("Wipe mode is Gutman\n");
		     break;
	     case 1 : printf("Wipe mode is American country peaceful overall situation\n");
	   	     break;
	     case 2 : printf("Wipe mode is Bruce schneier\n");
			 break;        case 3: printf("wipe mode is Dob\n");           break;
		 default: printf("No mode to find\n");
			      help();
	}   loop = optind;//全局变量optind=argc+1
	if (loop == argc)
        help();       //printf("ri\n");
	while (loop < argc) 
	{
        char rmfile[strlen(argv[loop]) + 1]; 
        strcpy(rmfile, argv[loop]);
        loop++;        
        if (strcmp("/", rmfile) == 0) 
		{
            fprintf(stderr,"Warning: Do you really want to remove the ROOT directory??\n");
            fprintf(stderr,"I'm giving you 5 seconds to abort ... press Control-C\n");
	        sleep(6);
            fprintf(stderr,"Doing my evil work now, don't whimper later, you had been informed!\n");
                 }
		if ((strcmp(".", rmfile) == 0) || (strcmp("..", rmfile) == 0))
                continue;
        printf("Wiping %s ", rmfile);                    
        result = (int) smash_it(rmfile, mode);
        switch (result) 
		{
            case 0 : printf(" Done\n");
                     break;
            case 1 : fprintf(stderr, "Error: File %s - ", rmfile);
                     perror("");
                     break;
            case -1: break;
            case 3 : fprintf(stderr, "File %s was raced, hence I won't wipe it!\n", rmfile);
                     break;
            default: fprintf(stderr, "Unknown error\n");
                }
        if (result)
            errors++;
        }
	sdel_finnish();   end=times(&t_end);   doit("elapsed",end-start);   puts("program times");   doit("\tuser CPU",t_end.tms_utime);   doit("\tsys  CPU",t_end.tms_stime);
	if(errors>0)
	{
		printf("after wipe,find errors %d\n",errors);
		return 0;
	}
    return 1;
}
			
		

⌨️ 快捷键说明

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