Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <dirent.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <wait.h>
  9. int *i_log, n,k=0;
  10. FILE *f_log;
  11. DIR *dir_log;
  12. void backuper(char *s1,char *s2)
  13. {
  14.     DIR *d1,*d2,*dir_cp;
  15.     struct dirent *dir;
  16.     int i,bull,g=0;
  17.     char *sum,*sum_cp;
  18.     pid_t pid;
  19.     d1=opendir(s1);
  20.     if(d1==NULL)
  21.     {
  22.         printf("%s - unable to read directory\n",s1);
  23.     }
  24.     else
  25.     {  
  26.         mkdir(s2,0777);
  27.         d2=opendir(s2);
  28.         sum=malloc(strlen(s1)+256*sizeof(char));
  29.         sum_cp=malloc(strlen(s2)+256*sizeof(char));
  30.         while((dir=readdir(d1))!=NULL)
  31.         {
  32.             strcpy(sum,s1);
  33.             strcpy(sum_cp,s2);
  34.             bull=1;
  35.             if(dir->d_type==DT_DIR)
  36.             {
  37.                 if((strcmp(dir->d_name,"..")!=0)&&(strcmp(dir->d_name,"."))!=0)
  38.                 {
  39.                     strcat(sum,dir->d_name);
  40.                     strcat(sum,"/");
  41.                     strcat(sum_cp,dir->d_name);
  42.                     strcat(sum_cp,"/");
  43.                     if((dir_cp=opendir(sum_cp))==NULL)
  44.                     {
  45.                         mkdir(sum_cp,0777);
  46.                     }
  47.                     else
  48.                     {
  49.                         closedir(dir_cp);
  50.                     }
  51.                     backuper(sum,sum_cp);
  52.                 }
  53.             }
  54.             else
  55.             {
  56.                 for(i=0;i<n;i++)
  57.                 {
  58.                     if((dir->d_ino)==i_log[i])
  59.                     {
  60.                         bull=0;
  61.                     }
  62.                 }
  63.                 if(bull)
  64.                 {
  65.                     pid=fork();
  66.                     if(pid==0)
  67.                     {
  68.                         strcat(sum,dir->d_name);
  69.                         g=execl("/bin/cp","cp",sum,s2,NULL);
  70.                         exit(0);
  71.                     }
  72.                     else
  73.                     {
  74.                         waitpid(pid,NULL,0);
  75.                     }
  76.                     fprintf(f_log,"%d\n",(int)dir->d_ino);
  77.                     pid=fork();
  78.                     if(pid==0)
  79.                     {
  80.                         strcat(sum_cp,dir->d_name);
  81.                         execl("/bin/gzip","gzip","-f",sum_cp,NULL);
  82.                         exit(0);
  83.                     }
  84.                     else
  85.                     {
  86.                         waitpid(pid,NULL,0);
  87.                     }
  88.                     k++;
  89.                 }
  90.             }
  91.         }
  92.         closedir(d1);
  93.         free(sum);
  94.         free(sum_cp);
  95.     }
  96. }
  97.  
  98. int main(int argc, char *argv[])
  99. {
  100.     int R;
  101.     char *c_log,*s1;
  102.     DIR *d2;
  103.     char *s2;
  104.     d2=opendir(argv[2]);
  105.     if(d2==NULL)
  106.     {
  107.         if(mkdir(argv[2],0777)!=0)
  108.         {
  109.             printf("Unable to write there!\n");
  110.         }
  111.     }
  112.     R=strlen(argv[1]);
  113.     if(argv[1][R]!='/')
  114.     {
  115.         s1=malloc(R*sizeof(char)+2);
  116.         strcpy(s1,argv[1]);
  117.         s1[R]='/';
  118.         s1[R+1]=0;
  119.     }
  120.     else
  121.     {
  122.         s1=malloc(R*sizeof(char));
  123.         strcpy(s1,argv[1]);
  124.     }
  125.     R=strlen(argv[2]);
  126.     if(argv[2][R]!='/')
  127.     {
  128.         s2=malloc(R*sizeof(char)+6);
  129.         strcpy(s2,argv[2]);
  130.         s2[R]='/';
  131.         s2[R+1]=0;
  132.     }
  133.     else
  134.     {
  135.         s2=malloc(R*sizeof(char)+4);
  136.         strcpy(s2,argv[2]);
  137.     }
  138.     n=strlen(s2);
  139.     c_log=malloc(sizeof(char)*(n+8));
  140.     strcpy(c_log,s2);
  141.     c_log[n]='l';
  142.     c_log[n+1]='o';
  143.     c_log[n+2]='g';
  144.     c_log[n+3]='/';
  145.     c_log[n+4]=0;
  146.     if((dir_log=opendir(c_log))==NULL)
  147.     {
  148.         mkdir(c_log,0777);
  149.     }
  150.     else
  151.     {
  152.         closedir(dir_log);
  153.     }
  154.     c_log[n+4]='l';
  155.     c_log[n+5]='o';
  156.     c_log[n+6]='g';
  157.     c_log[n+7]=0;
  158.     n=0;
  159.     if((f_log=fopen(c_log,"r"))!=NULL)
  160.     {
  161.         printf("found old log\n");
  162.         while((fscanf(f_log,"%d",&R)!=EOF))
  163.         {
  164.             n++;
  165.         }
  166.         fclose(f_log);
  167.         i_log=malloc(n*sizeof(int));
  168.         n=0;
  169.         f_log=fopen(c_log,"r");
  170.         while((fscanf(f_log,"%d",&i_log[n]))!=EOF)
  171.         {
  172.             n++;
  173.         }
  174.         fclose(f_log);
  175.        
  176.     }
  177.     f_log=fopen(c_log,"a");
  178.     if(f_log==NULL)
  179.     {
  180.         printf("Unable to write in destination directory\n");
  181.         exit(1);
  182.     }
  183.     R=strlen(s2);
  184.     s2[R]='z';
  185.     s2[R+1]='i';
  186.     s2[R+2]='p';
  187.     s2[R+3]='/';
  188.     s2[R+4]=0;
  189.     backuper(s1,s2);
  190.     fclose(f_log);
  191.     closedir(d2);
  192.     printf("Copied and zipped elements - %d\n",k);
  193.     return 0;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement