/
copy.c
136 lines (118 loc) · 2.74 KB
/
copy.c
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "cpee.h"
void copy_file_to_file(char* from, char* to){
char* to_archive[PNAME] = {'\0'};
if(g_argoption.compbackup){
remove_top_dir(to,to_archive);
archive_add(from,to_archive);
}
struct stat s;
char buf[SIZE];
int fd_from, fd_to, size;
if( is_file_exist(to) ){
printf("file exists. %s %s\n",from,to);
exit(-1);
}else{
if( stat(from,&s) == -1 )
show_errno();
if(g_upperbackupsize < s.st_size)
return;
if((fd_to=open(to,O_CREAT|O_WRONLY,s.st_mode)) != -1){
if((fd_from=open(from,O_RDONLY)) != -1){
while(1){
size = read(fd_from,buf,SIZE);
switch(size){
case -1:
printf("copy_file_to_file : %s, %s\n",from,to);
show_errno();
case 0:
close(fd_to);
close(fd_from);
break;
default:
if(write(fd_to,buf,size) == -1){
printf("copy_file_to_file : %s, %s\n",from,to);
show_errno();
}
break;
}
if(size==0) break;
}
}else{
printf("copy_file_to_file : %s, %s\n",from,to);
show_errno();
}
}else{
printf("copy_file_to_file : %s, %s\n",from,to);
show_errno();
}
}
}
void copy_file_to_link(char* from, char* to){
link(from,to);
}
void copy_file_to_dir(char* from, char* to){
char to_full[FNAME] = {"\0"};
char from_file[FNAME] = {"\0"};
get_file_name(from,from_file);
sprintf(to_full,"%s/%s",to,from_file);
if(g_argoption.hardlink){
copy_file_to_link(from,to_full);
}else{
copy_file_to_file(from,to_full);
}
}
void copy_dir_to_dir(char* from, char* to){
DIR *dir;
if((dir=opendir(from))==NULL)
show_errno();
struct dirent *dp;
char next_from[FNAME] = {'\0'};
char next_to[FNAME] = {'\0'};
struct stat s;
for(dp=readdir(dir);dp!=NULL;dp=readdir(dir)){
sprintf(next_from,"%s/%s",from,dp->d_name);
if(dp->d_type==DT_REG){
copy_file_to_dir(next_from,to);
}
if(dp->d_type==DT_DIR){
if(strncmp(dp->d_name,"..",2)==0 || strncmp(dp->d_name,".",1)==0)
continue;
sprintf(next_to,"%s/%s",to,dp->d_name);
if(stat(next_from,&s) == 0){
mkdir(next_to,s.st_mode);
}else{
show_errno();
}
copy_dir_to_dir(next_from,next_to);
}
}
}
void copy_to_dir(int argc, char* argv[], char* backup){
int i;
char from[FNAME] = {'\0'};
struct stat s;
for(i=1;i<argc-1;i++){
sprintf(from,"%s",argv[i]);
if( stat(argv[i],&s) == 0 ){
if( S_ISREG(s.st_mode) )
copy_file_to_dir(argv[i],backup);
if( S_ISDIR(s.st_mode) )
copy_dir_to_dir(argv[i],backup);
}else{
show_errno();
}
}
}
void copy_from_backup(char* date){
char to_path[PNAME];
get_backup_dir(date,to_path);
struct stat s;
if( stat(to_path,&s) == 0 ){
if( !S_ISDIR(s.st_mode) )
show_errno();
}else{
printf("no backups of the date %s.\n",date);
exit(-1);
}
copy_dir_to_dir(to_path,".");
}