Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <dirent.h>
- #include <string.h>
- #include <stdlib.h>
- int main(int argc, char *argv[])
- {
- DIR *src_dir = NULL;
- DIR *dst_dir = NULL;
- DIR *inner_dir = NULL;
- FILE *src_file = NULL;
- const char *src_dirname = NULL;
- const char *dst_dirname = NULL;
- char *dst_gz_filename = NULL;
- char *dst_filename = NULL;
- char *src_filename = NULL;
- char *command = NULL;
- char *current_dir = (char *)getcwd(NULL,0);
- struct dirent *src_ent;
- struct stat src_st;
- struct stat dst_st;
- int file_counter = 0;
- if (argc < 3)
- {
- printf("Two parameters nedeed: SOURCE_DIR DEST_DIR\n");
- return 1;
- }
- //cutting possible '/' at the end of arguments
- if ('/' == argv[1][strlen(argv[1]) - 1]) argv[1][strlen(argv[1]) - 1] = 0;
- if ('/' == argv[2][strlen(argv[2]) - 1]) argv[2][strlen(argv[2]) - 1] = 0;
- src_dirname = argv[1];
- dst_dirname = argv[2];
- if (NULL == (src_dir = opendir(src_dirname)))
- {
- perror(src_dirname);
- return 2;
- }
- if (NULL == (dst_dir = opendir(dst_dirname)))
- {
- perror(dst_dirname);
- return 3;
- }
- closedir(dst_dir);
- // get full paths
- chdir(src_dirname);
- src_dirname = (char *)getcwd(NULL,0);
- chdir(current_dir);
- chdir(dst_dirname);
- dst_dirname = (char *)getcwd(NULL,0);
- chdir(current_dir);
- while(NULL != (src_ent = readdir(src_dir)))
- {
- if ((!strcmp(src_ent->d_name,".")) || (!strcmp(src_ent->d_name,"..")))
- {
- continue;
- }
- src_filename = malloc(strlen(src_ent->d_name) + strlen(src_dirname) + 10);
- sprintf(src_filename, "%s/%s", src_dirname, src_ent->d_name);
- dst_filename = malloc(strlen(src_ent->d_name) + strlen(dst_dirname) + 10);
- sprintf(dst_filename, "%s/%s", dst_dirname, src_ent->d_name);
- dst_gz_filename = malloc(strlen(dst_filename) + 10);
- sprintf(dst_gz_filename, "%s.gz", dst_filename);
- if (DT_DIR == src_ent->d_type)
- {
- // Its a directory
- if (NULL != (inner_dir = opendir(src_filename)))
- {
- closedir(inner_dir);
- if (-1 == stat(dst_filename, &dst_st))
- {
- command = malloc(strlen(dst_filename) + 10);
- sprintf(command, "mkdir %s", dst_filename);
- if (-1 == system(command))
- {
- printf("Cannot execute command:%s\n", command);
- return 5;
- }
- //printf("Execute command:%s\n", command);
- free(command);
- }
- //recursion
- command = malloc(strlen(argv[0]) + strlen(src_filename) + strlen(dst_filename) + 10);
- sprintf(command, "%s %s %s", argv[0], src_filename, dst_filename);
- if (-1 == system(command))
- {
- printf("Cannot execute command:%s\n", command);
- return 5;
- }
- //printf("Execute command:%s\n", command);
- free(command);
- }
- else
- {
- perror(src_filename);
- continue;
- }
- }
- if (DT_REG == src_ent->d_type)
- {
- if (-1 == stat(src_filename, &src_st))
- {
- perror(src_filename);
- continue;
- }
- else
- {
- if (-1 != stat(dst_gz_filename, &dst_st))
- {
- // archive file is present
- if (src_st.st_mtime > dst_st.st_mtime)
- {
- // rm dst_gz_filename
- command = malloc(strlen(dst_gz_filename) + 10);
- sprintf(command, "rm %s", dst_gz_filename);
- if (-1 == system(command))
- {
- printf("Cannot execute command:%s\n", command);
- return 5;
- }
- //printf("Execute command:%s\n", command);
- free(command);
- }
- else
- {
- continue;
- }
- }
- // archive file is absent
- // cp src_filename dst_filename
- command = malloc(strlen(src_filename) + strlen(dst_filename) + 10);
- sprintf(command, "cp %s %s", src_filename, dst_filename);
- if (-1 == system(command))
- {
- printf("Cannot execute command:%s\n", command);
- return 5;
- }
- // printf("Execute command:%s\n", command);
- // gzip dst_filename
- sprintf(command, "gzip %s", dst_filename);
- if (-1 == system(command))
- {
- printf("Cannot execute command:%s\n", command);
- return 5;
- }
- //printf("Execute command:%s\n", command);
- file_counter++;
- free(command);
- }
- }
- free(dst_gz_filename);
- free(src_filename);
- free(dst_filename);
- }
- closedir(src_dir);
- free(current_dir);
- if (file_counter)
- {
- printf("%d files backuped!\n", file_counter);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement