forked from skypanda100/ftpdownload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
130 lines (115 loc) · 3.02 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include "def.h"
#include "config.h"
#include "curlftp.h"
int main(int argc,char **argv)
{
if(argc != 2)
{
printf("wrong arguments!!\n");
exit(1);
}
conf cf;
memset(&cf, 0, sizeof(conf));
config(&cf, argv[1]);
if(strlen(cf.src_dir) == 0)
{
printf("src_dir is wrong!!\n");
exit(1);
}
if(strlen(cf.dst_dir) == 0)
{
printf("dst_dir is wrong!!\n");
exit(1);
}
if(strlen(cf.user_pwd) == 0)
{
printf("user_pwd is wrong!!\n");
exit(1);
}
if(cf.sleep_time <= 0)
{
printf("sleep_time must be greater than 0!!\n");
exit(1);
}
#ifndef TEST
daemon(0, 0);
#endif
char **last_newest_file_ptr_ptr = NULL;
int last_newest_file_count = 0;
#ifndef TEST
int is_first = 1;
#else
int is_first = 0;
#endif
#ifndef TEST
for(;;)
{
#else
for(int loop = 0;loop < 2;loop++)
{
#endif
// find newest files
char **newest_file_ptr_ptr = NULL;
int newest_file_count = 0;
int ret = get_newest_files(cf.src_dir, cf.user_pwd, &newest_file_ptr_ptr, &newest_file_count);
printf("newest files count: %d\n", newest_file_count);
if(ret == 0)
{
// diff and download
if(!is_first)
{
diff_and_download(&cf, newest_file_ptr_ptr, newest_file_count, last_newest_file_ptr_ptr,
last_newest_file_count);
}
is_first = 0;
if(last_newest_file_ptr_ptr != NULL)
{
for(int i = 0;i < last_newest_file_count;i++)
{
if(last_newest_file_ptr_ptr[i] != NULL)
{
free(last_newest_file_ptr_ptr[i]);
}
}
free(last_newest_file_ptr_ptr);
last_newest_file_ptr_ptr = NULL;
}
last_newest_file_ptr_ptr = newest_file_ptr_ptr;
last_newest_file_count = newest_file_count;
}
else
{
printf("some errors happened in listing files!\n");
if(newest_file_ptr_ptr != NULL)
{
for(int i = 0;i < newest_file_count;i++)
{
if(newest_file_ptr_ptr[i] != NULL)
{
free(newest_file_ptr_ptr[i]);
}
}
free(newest_file_ptr_ptr);
newest_file_ptr_ptr = NULL;
}
}
#ifndef TEST
sleep(cf.sleep_time);
#endif
}
if(last_newest_file_ptr_ptr != NULL)
{
for(int i = 0;i < last_newest_file_count;i++)
{
if(last_newest_file_ptr_ptr[i] != NULL)
{
free(last_newest_file_ptr_ptr[i]);
}
}
free(last_newest_file_ptr_ptr);
last_newest_file_ptr_ptr = NULL;
}
return 0;
}