-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_1.c
More file actions
103 lines (89 loc) · 2.78 KB
/
backup_1.c
File metadata and controls
103 lines (89 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>
int CopyGzipFile (char *StartFile, char *FinishFile) {
pid_t pid;
pid = fork();
if (pid < 0) {
printf("Не удалось создать процесс\n");
return 1;
}
if (pid == 0) {
execl("/bin/cp","-p", StartFile, FinishFile, NULL);
}
wait(NULL);
pid = fork();
if (pid < 0) {
printf("Не удалось создать процесс\n");
return 1;
}
if(pid == 0) {
execl("/usr/bin/gzip","-f", "-n", FinishFile, NULL);
}
wait(NULL);
return 0;
}
int CopyDirectory(char *StartDirectory, char* FinishDirectory) {
DIR *directory;
struct dirent *dir;
char *StartAdress;
char *FinishAdress;
if ((directory = opendir(StartDirectory)) == NULL) {
printf("Не удалось открыть дирректорию\n");
return 2;
}
while ((dir = readdir(directory)) != NULL) {
StartAdress = calloc(1, 30);
strcpy(StartAdress, StartDirectory);
strncat(StartAdress, dir->d_name, strlen(dir->d_name));
FinishAdress = calloc(1, 30);
strcpy(FinishAdress, FinishDirectory);
strncat(FinishAdress, dir->d_name, strlen(dir->d_name));
if (dir->d_type == DT_REG) {
CopyGzipFile(StartAdress, FinishAdress);
}
if ((dir->d_type == DT_DIR) && (strcmp(dir->d_name, ".") != 0) && (strcmp(dir->d_name, "..") != 0)) {
strncat(StartAdress, "/", 1);
strncat(FinishAdress, "/", 1);
if (opendir(FinishAdress) == NULL) {
pid_t pid = fork();
if (pid < 0) {
printf("Не удалось создать процесс\n");
return 1;
}
if (pid == 0) {
execl("/bin/mkdir", "-p", FinishAdress, 0);
waitpid(pid, NULL, 0);
}
CopyDirectory(StartAdress, FinishAdress);
}
}
free(StartAdress);
free(FinishAdress);
}
closedir(directory);
return 0;
}
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Неверно введены данные.\n");
return 3;
}
if (opendir(argv[1]) == NULL) {
printf("Дирректория, которую необходимо скопировать, не существует.\n");
return 4;
}
if (opendir(argv[2]) == NULL) {
pid_t pid = fork();
if (pid == 0) {
execl("/bin/mkdir", "-r", argv[2], 0);
}
wait(NULL);
}
CopyDirectory(argv[1], argv[2]);
return 0;
}