Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include 
#include <sys\stat.h>
#include "windows.h"
#include "global.h"
#include "wiper.c"
int totalfiles=0;
int p;
int q;
long a;
char stop_wiping = 0;
//double dif;
void usage()
{
    fprintf(stdout, "\n%s - %s\nUsage: %s DRIVE_PATH\n", PROGRAM_NAME, PROGRAM_VER, PROGRAM_NAME);  
}
     
int get_opts(int argc, char *argv[], char drive_path[256])
{
    int i, p;
    char ch;
    
    if (argc != 2){
        usage(); 
        return -1;                
    }
    strcpy(drive_path, argv[1]);
    
    p = strlen(drive_path) - 1;
    //remove final slash if exists
    if (drive_path[p] == '/' || drive_path[p]=='\\')
    {
       drive_path[p] = '\0';
    }
    
    return 0;  
}


int can_write(char * drive_path){
    unsigned char filepath[256];
    int r = 0;
    
    sprintf(filepath, "%s/%s", drive_path, TEST_FILENAME); 
    FILE * f = fopen(filepath, "w+b");
    
    if (f == NULL){ 
       r = -1;
       goto exit; 
    }
    
    if (fputs(filepath, f) < 0){
       r = -1;
       goto exit;                         
    }
    
    exit:
    if (f != NULL){
       fclose(f);
       if (remove(filepath) != 0){
          r = -1;                     
       }
    }
     
    return r;
}
/////////////////////////////////////
#if (OS == 1)
long abort_listener(){
    getchar();
    stop_wiping = 1;  
    return 0;
}
#else
void * abort_listener(void * args){
	 getchar();
    stop_wiping = 1;  
    return 0;
}
#endif


void clear_console_screen()
{      
    #if (OS == 1)
        system("CLS");
    #else 
        system("clear");
    #endif
}
void wipe_status1(long file_num, long gap)
{
  //  clear_console_screen();
   q=a/20;
 //  printf(" file no :%d",q);
    fprintf(stdout, "%s - %s\n\nStatus: Deleting process in progress [Don't Press ENTER to stop]", PROGRAM_NAME, PROGRAM_VER);
    printf("\nTotal time will take for delete in  sec:%0.2f\n",(float)(q/150));
    fflush(stdout);
}
void delete_tmp_files(char * drive_path, long file_num)
{
    unsigned char filepath[256];
    long i;
    time_t now_t1, start_t1;
     time(&start_t1);
    wipe_status1(0L, 0L);
    fprintf(stdout, "\nStatus: Deleting temporary files.\n");

    for(i=1; i<=file_num; i++){
       sprintf(filepath, "%s/%s%ld", drive_path, FILE_PREFIX, i);
    /*   if (file_num % 3 == 0){
          fprintf(stdout,"\n Total File  %d", file_num);   
          time(&now_t1);
          wipe_status1(file_num, difftime(now_t1, start_t1));
       }*/
       if (remove(filepath) != 0){
          fprintf(stdout, "\nError: Unable to remove temporary files.");
          break;
       }
    }
}

void wipe_status(char * drive_path,long file_num, long gap)
{
    clear_console_screen();
   
    p=a/20;
  
   // printf ("\n111It total time=%.2lf seconds.\n", p*dif );
    fprintf(stdout, "%s - %s\n\nStatus: Wiping free space in progress [Press ENTER to stop]", PROGRAM_NAME, PROGRAM_VER);
    fprintf(stdout, "\nNote: Don't stop with CTRL+C otherwise the temporary files will not be deleted");
    fprintf(stdout, "\nInfo: %ld Mb written in %5.2f min\n", (long) ((long)file_num)*((long)DEFAULT_FILE_SIZE), (float)gap/60);
   // printf("\n Total Time:%5.2f min\n",(float)p*(.0295));
   // printf("\n Remaining Time:%5.2f min\n",((float)p*(.0295)-(float)((float)file_num*(.0295))));
    

    fprintf(stdout, "\nProgress ............%ld Percent ",  ((100*file_num)/p));
    fprintf(stdout, "\nRemaining ...........%ld Percent ",  (100-((100*file_num)/p)));
      
    fflush(stdout);
}
int wipe(char * drive_path, long file_size)
{
    
    FILE * f;
    int i;
    int const buf_size = 4096;
    long written;
    long file_num = 0;
    unsigned char buf[buf_size];
    unsigned int iseed;
    unsigned char filepath[256];
    
    int write_failed;
    time_t now_t, start_t;
    
    file_size = file_size * 1024000L;
    time(&start_t);
    
    wipe_status(drive_path,0L, 0L);
    
    while((long) ((long)file_num)*((long)DEFAULT_FILE_SIZE)<=a)
    {
       //sleep(1); //useless in this context
             
       written = 0;
       iseed = (unsigned int) time(NULL);
       srand (iseed);
       
       for (i=0; i<buf_size;>       {
           buf[i] = (unsigned char)( 255.0 * rand() / ( RAND_MAX + 0.0 ) );
       }  
       
       file_num++;
       write_failed = 0;  
       sprintf(filepath, "%s/%s%ld", drive_path, FILE_PREFIX, file_num);
       
       f = fopen(filepath, "wb");

       while(1){
          if (f == NULL){
             fprintf(stdout, "\nError: Unable to create file \"%s\"", filepath);
             write_failed = 1;
             break;
          }   
          
          if (stop_wiping == 1 || fwrite(buf, 1, buf_size, f) < buf_size){
             #if (DEBUG == 1)
             fprintf(stdout, "\nError: Unable to write to file \"%s\"", filepath);
             #endif
             write_failed = 1;
             break;
          }
          
          written += buf_size;
          
          if (written >= file_size){
             break;            
          }
       }
       
       if (f != NULL){
          fclose(f);
       }
       
       if (write_failed == 1){
          break;            
       }
      
      if((long) ((long)file_num)*((long)DEFAULT_FILE_SIZE)<=a)
      {
          time(&now_t);
          wipe_status(drive_path,file_num, difftime(now_t, start_t));
          
      }
      else
      {
          break;
      }
   } 
    delete_tmp_files(drive_path, file_num);
    return 0;
}

int wipe_wrap(char * drive_path, long file_size)
{
    #if (OS == 1) 
   printf("Drive to be checked: %s\n", drive_path);
  
   __int64 lpFreeBytesAvailable, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes;

   DWORD dwSectPerClust, dwBytesPerSect, dwFreeClusters, dwTotalClusters;


  BOOL test = GetDiskFreeSpaceEx(

  drive_path,

  (PULARGE_INTEGER)&lpFreeBytesAvailable,

  (PULARGE_INTEGER)&lpTotalNumberOfBytes,

  (PULARGE_INTEGER)&lpTotalNumberOfFreeBytes);
   printf("Drive to be checked: %s\n", drive_path);

  printf("\nUsing GetDiskFreeSpaceEx()...\n");

  // check the return value

  printf("The return value: %d, error code: %d\n", test, GetLastError());

  printf("Total number of free bytes available for user-caller: %I64u\n", lpFreeBytesAvailable);

  // just straight to the free bytes result
  a=lpTotalNumberOfFreeBytes/(1024*1024);
  printf("Total number of free bytes on disk: %ld Mb\n", a);

  //printf("No of files :%ul\n",p/20);

        DWORD dwThreadId;
        DWORD dwThrdParam = 1;
        HANDLE thread;       

        ///////trilok//////
   thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)abort_listener, &dwThrdParam, 0, &dwThreadId);

        if (thread == NULL){
           fprintf(stderr, "\nError: CreateThread function failed.");
           return -1;           
        }
        return wipe (drive_path, file_size);
    #else
    	
    	  pthread_t* stopThread = (pthread_t*) calloc(1, sizeof(pthread_t));
    		
        if (pthread_create(&stopThread[0], NULL, abort_listener, &stop_wiping) != 0){
           fprintf(stderr, "\nError: pthread_create function failed.");
           return -1;  
        }
        return wipe (drive_path, file_size);
    #endif
    
    return 0;
}

int main(int argc, char *argv[])
{
    long file_size = (long) DEFAULT_FILE_SIZE;
    char drive_path[256];
    char *opts[256]; 
    FILE *f;
    
    #if (OS == 0)
       fprintf(stderr, "\nError: current OS is not supported.");
       goto exit;
    #endif
    
    //read input params
    if (get_opts(argc, argv, drive_path) != 0){
       goto exit;
    }
    
    //test if input is valid
    if (can_write(drive_path) != 0){
       fprintf(stderr, "\nError: unable to access/write to drive \"%s\"", drive_path);
       goto exit;                          
    }
    
    //wipe free space
    fprintf(stdout, "\nPress ENTER to start wiping free space of drive \"%s\"", drive_path);
    getchar();

    wipe_wrap(drive_path, file_size);
    
    exit:
    
    fprintf(stdout, "\n\nWebsite: https://sourceforge.net/projects/simpledrivewipe/");
	 fprintf(stdout, "\nPress ENTER to quit.");
	 fflush(stdout);
	 
	 getchar();
     ////////////////////////trilok//////////////////////////////
    return 0;
}



Headerfiles.........................
global.h
C++
#define DEFAULT_FILE_SIZE   20  //in Mb and at least >= 1 Mb
#define FILE_PREFIX         "SDWDUMMYFILE_"
#define TEST_FILENAME       "SDWDUMMYFILE4TESTDRIVE"
#define PROGRAM_NAME        "SimpleDriveWiper"
#define PROGRAM_VER         "0.2"
#define DEBUG               0

#if defined(WIN32) && !defined(UNIX)
    #define OS     1
      
#elif defined(__unix__) && !defined(WIN32)
    #define OS     2
#elif defined(__linux__) && !defined(WIN32)
    #define OS     3        
#else
    #define OS     0
#endif 



wiper.c
C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "global.h"

#if (OS == 1)
  #include <windows.h>
#else
	#include <pthread.h>
	#include <unistd.h>
	#include <malloc.h>
#endif
#include "wiper.h"



wiper.h
C#
long abort_listener();
void clear_console_screen();
void delete_tmp_files(char * drive_path, long file_num);
int wipe(char * drive_path, long file_size);
int wipe_wrap(char * drive_path, long file_size);



This code is running in dev c++.
process for exe run
start->run->cmd->cd exe path->enter
exename space drivename(C,D,E):->enter

i want find remaining time when wiping and deleting time after wiping.Please help me.give any solution.

Thanks
Posted
Updated 31-Oct-11 3:10am
v3
Comments
enhzflep 31-Oct-11 3:00am    
Well, if you get the time before you've wiped the first file you can continue to check the time in your main deletion loop. Using the difference between the current time and the time before the first wipe, you can determine the elapsed time.
Once you have the elapsed time and the number of files that have been deleted in this period you can determine the average time taken per file. If you multiply this time by the number of files remaining you'll have _an estimate_ of the remaining time.

QueryPerformanceFrequency and QueryPerformanceCounter will tell you the number of ticks/second (IIRC) and the number of ticks elapsed at the current point.
freq = queryFreq
tStart = queryCounter
..
.. do some stuff
..
tEnd = queryCounter
elapsedTicks = tEnd - tStart
elapsedTime = elapsedTicks / freq
enhzflep 31-Oct-11 9:29am    
I see in your code you have time_t variables. From memory, these are (only) 32 bit values. The function that work with them generally deal with time in number of seconds since Jan 01 1970, or something similar.

I suggest that you use the performance counter, since it ticks about 2,000 million times a second on my machine - i.e about once per clock cycle. This gives you access to stunningly accurate timing.

time_t on the other hand is a datatype used to store times as the number of _seconds_ elapsed since some arbitrary time I mentioned earlier.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900