Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <dirent.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11.  
  12.   DIR *src_dir = NULL;
  13.   DIR *dst_dir = NULL;
  14.   DIR *inner_dir = NULL;
  15.   FILE *src_file = NULL;
  16.   const char *src_dirname = NULL;
  17.   const char *dst_dirname = NULL;
  18.   char *dst_gz_filename = NULL;
  19.   char *dst_filename = NULL;
  20.   char *src_filename = NULL;
  21.   char *command = NULL;
  22.   char *current_dir = (char *)getcwd(NULL,0);
  23.   struct dirent *src_ent;
  24.   struct stat src_st;
  25.   struct stat dst_st;
  26.   int file_counter = 0;
  27.  
  28.    if (argc < 3)
  29.   {
  30.     printf("Two parameters nedeed: SOURCE_DIR DEST_DIR\n");
  31.     return 1;
  32.   }
  33.  
  34.    //cutting possible '/' at the end of arguments
  35.    if ('/' == argv[1][strlen(argv[1]) - 1])   argv[1][strlen(argv[1]) - 1] = 0;
  36.    if ('/' == argv[2][strlen(argv[2]) - 1])   argv[2][strlen(argv[2]) - 1] = 0;
  37.    src_dirname = argv[1];
  38.    dst_dirname = argv[2];
  39.      
  40.    if (NULL == (src_dir = opendir(src_dirname)))
  41.     {
  42.       perror(src_dirname);
  43.       return 2;
  44.     }
  45.    if (NULL == (dst_dir = opendir(dst_dirname)))
  46.     {
  47.       perror(dst_dirname);
  48.       return 3;
  49.     }
  50.     closedir(dst_dir);
  51.    
  52.    // get full paths
  53.    chdir(src_dirname);
  54.    src_dirname = (char *)getcwd(NULL,0);
  55.    chdir(current_dir);
  56.    chdir(dst_dirname);
  57.    dst_dirname = (char *)getcwd(NULL,0);
  58.    chdir(current_dir);
  59.    
  60.    
  61.    while(NULL != (src_ent = readdir(src_dir)))
  62.     {
  63.         if ((!strcmp(src_ent->d_name,".")) || (!strcmp(src_ent->d_name,"..")))
  64.     {
  65.       continue;
  66.     }
  67.     src_filename = malloc(strlen(src_ent->d_name) + strlen(src_dirname) + 10);
  68.     sprintf(src_filename, "%s/%s", src_dirname, src_ent->d_name);
  69.      
  70.     dst_filename = malloc(strlen(src_ent->d_name) + strlen(dst_dirname) + 10);
  71.     sprintf(dst_filename, "%s/%s", dst_dirname, src_ent->d_name);
  72.      
  73.     dst_gz_filename = malloc(strlen(dst_filename) + 10);
  74.     sprintf(dst_gz_filename, "%s.gz", dst_filename);
  75.    
  76.     if (DT_DIR == src_ent->d_type)
  77.     {
  78.       // Its a directory
  79.       if (NULL != (inner_dir = opendir(src_filename)))
  80.       {
  81.         closedir(inner_dir);
  82.         if (-1 == stat(dst_filename, &dst_st))
  83.         {
  84.             command = malloc(strlen(dst_filename) + 10);
  85.         sprintf(command, "mkdir %s", dst_filename);
  86.         if (-1 == system(command))
  87.         {
  88.           printf("Cannot execute command:%s\n", command);
  89.           return 5;
  90.         }
  91.         //printf("Execute command:%s\n", command);
  92.         free(command);
  93.         }
  94.         //recursion
  95.         command = malloc(strlen(argv[0]) + strlen(src_filename) + strlen(dst_filename) + 10);
  96.         sprintf(command, "%s %s %s", argv[0], src_filename, dst_filename);
  97.         if (-1 == system(command))
  98.         {
  99.         printf("Cannot execute command:%s\n", command);
  100.         return 5;
  101.         }
  102.         //printf("Execute command:%s\n", command);
  103.         free(command);
  104.       }
  105.       else
  106.       {
  107.         perror(src_filename);
  108.         continue;
  109.       }
  110.     }
  111.     if (DT_REG == src_ent->d_type)
  112.     {
  113.      
  114.       if (-1 == stat(src_filename, &src_st))
  115.       {
  116.         perror(src_filename);
  117.         continue;
  118.       }
  119.       else
  120.       {
  121.         if (-1 != stat(dst_gz_filename, &dst_st))
  122.         {
  123.           // archive file is present
  124.           if (src_st.st_mtime > dst_st.st_mtime)
  125.           {
  126.           // rm dst_gz_filename
  127.         command = malloc(strlen(dst_gz_filename) + 10);
  128.         sprintf(command, "rm %s", dst_gz_filename);
  129.         if (-1 == system(command))
  130.         {
  131.           printf("Cannot execute command:%s\n", command);
  132.           return 5;
  133.         }
  134.         //printf("Execute command:%s\n", command);
  135.         free(command);
  136.           }
  137.           else
  138.           {
  139.         continue;
  140.           }
  141.         }  
  142.         // archive file is absent
  143.         // cp src_filename dst_filename
  144.         command = malloc(strlen(src_filename) + strlen(dst_filename) + 10);
  145.         sprintf(command, "cp %s %s", src_filename, dst_filename);
  146.         if (-1 == system(command))
  147.         {
  148.           printf("Cannot execute command:%s\n", command);
  149.           return 5;
  150.         }
  151.        // printf("Execute command:%s\n", command);
  152.         // gzip dst_filename
  153.         sprintf(command, "gzip %s", dst_filename);
  154.         if (-1 == system(command))
  155.         {
  156.           printf("Cannot execute command:%s\n", command);
  157.           return 5;
  158.         }
  159.         //printf("Execute command:%s\n", command);
  160.         file_counter++;
  161.         free(command);    
  162.       }
  163.      }
  164.      free(dst_gz_filename);
  165.      free(src_filename);
  166.      free(dst_filename);     
  167.     }
  168.    
  169.   closedir(src_dir);
  170.   free(current_dir);
  171.   if (file_counter)
  172.   {
  173.     printf("%d files backuped!\n", file_counter);
  174.   }
  175.   return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement