-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSender.cpp
210 lines (206 loc) · 7.42 KB
/
Sender.cpp
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include"Sender.hpp"
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
bool Sender::MakeRsyncFileList(const vector<RlogItem>& items,
string& include_content,string& exclude_content,
int& include_item_number,int& exclude_item_number)
{
string rsync_list_file_name=conf.sender_tmp_path+RSYNC_LIST_FILE_NAME;
string rsync_exclude_file_name=conf.sender_tmp_path+RSYNC_EXCLUDE_FILE_NAME;
string rp,frp;
include_item_number=0;
exclude_item_number=0;
include_content="";
vector<string> include_items;
vector<string> exclude_items;
set<string> iset;
for(unsigned int i=0;i<items.size();i++)
{
if(items[i].filename.size()<=conf.local_dir.size())
{
kobe_printf("%s\tERROR: filename.size<=local_dir.size,%s\n",
GetCurrentTime().c_str(),items[i].filename.c_str());
continue;
}
switch(items[i].event_type)
{
case IN_CREATE:
case IN_CLOSE_WRITE:
case IN_MODIFY:
case IN_MOVE_SELF:
case IN_MOVED_TO:
if(FileExists(items[i].filename)==false)
{
#ifdef MYDEBUG
kobe_printf("skip not exists %s\n",items[i].filename.c_str());
#endif
break;
}
rp=items[i].filename.substr(conf.local_dir.size());
if(rp.empty())
rp="./";
if(include_items.empty()==false&&rp==include_items.back())
break;
RecheckExcludeFiles(items[i].filename,exclude_items);
/*#ifdef MYDEBUG
if(items[i].event_type==IN_MOVED_TO)
{
cout<<"item2: "<<items[i].filename<<endl;
for(unsigned int i=0;i<exclude_items.size();i++)
cout<<"\t"<<exclude_items[i]<<endl;
}
#endif*/
include_items.push_back(rp);
iset.insert(items[i].filename);
break;
case IN_DELETE:
case IN_DELETE_SELF:
case IN_MOVED_FROM:
rp=items[i].filename;
if(rp.size()<conf.local_dir.size()||
(rp.size()==conf.local_dir.size()&&rp!=conf.local_dir)
)
{
#ifdef MYDEBUG
kobe_printf("skip not invalid %s\n",rp.c_str());
#endif
break;
}
if(rp.size()>conf.local_dir.size())
{
rp=GetParentPath(rp);
if(FileExists(rp)==false)
{
#ifdef MYDEBUG
kobe_printf("skip not exists %s\n",rp.c_str());
#endif
break;
}
frp=rp;
rp=rp.substr(conf.local_dir.size());
if(rp.empty())
rp="./";
}
else
{
frp=rp;
rp="./";
}
if(include_items.empty()==false&&rp==include_items.back())
break;
RecheckExcludeFiles(frp,exclude_items);
AddExcludeFiles(frp,items[i].filename,
iset,exclude_items);
include_items.push_back(rp);
iset.insert(items[i].filename);
break;
default:
break;
}//end switch
}//end for i
if(include_items.empty())
return true;
CoveredIncludeFileFilter(include_items);
for(unsigned int i=0;i<include_items.size();i++)
include_content+=include_items[i]+"\n";
//sort(exclude_items.begin(),exclude_items.end());
unique(exclude_items.begin(),exclude_items.end());
for(unsigned int i=0;i<exclude_items.size();i++)
{
exclude_items[i]=exclude_items[i].substr(conf.local_dir.size());
exclude_content+=exclude_items[i]+"\n";
}
include_item_number=include_items.size();
exclude_item_number=exclude_items.size();
return FilePutContent(rsync_exclude_file_name,exclude_content)==true&&
FilePutContent(rsync_list_file_name,include_content)==true;
}
//-------------------------------------------------------------------------------------------------
bool Sender::Send(const vector<RlogItem>& items,string& errmsg)
{
string include_content,exclude_content;
int include_item_number=0;
int exclude_item_number=0;
if(MakeRsyncFileList(items,include_content,exclude_content,
include_item_number,exclude_item_number)==false)
{
errmsg="failed to write rsync list file";
return false;
}
if(include_content.empty())
{
#ifdef MYDEBUG
kobe_printf("%s\tskip empty rsync\n",GetCurrentTime().c_str());
#endif
return true;
}
string rsync_error_filename=conf.sender_tmp_path+RSYNC_ERROR_FILE_NAME;
string command="rsync "+RSYNC_PARAMS;
command+=" --files-from="+conf.sender_tmp_path+RSYNC_LIST_FILE_NAME;
command+=" --exclude-from="+conf.sender_tmp_path+RSYNC_EXCLUDE_FILE_NAME;
if(update_mode)
command+=" --update";
if(conf.rsync_bwlimit>0)
command+=" --bwlimit="+IntToStr(conf.rsync_bwlimit);
command+=" "+conf.local_dir+" "+conf.remote_dir;
command+=" 2>"+rsync_error_filename;
kobe_printf("%s\tshell [%s]\n",GetCurrentTime().c_str(),command.c_str());
#ifdef MYDEBUG
kobe_printf("%s\n",include_content.c_str());
kobe_printf("/---------/\n");
kobe_printf("%s\n",exclude_content.c_str());
#endif
int status=system(command.c_str());
if(status==-1)
return false;
int exitcode=WEXITSTATUS(status);
if(exitcode!=0&&exitcode!=24)//maybe error, 24 means vanished file
{
bool realerror=false;
string ec;
if(FileGetContent(rsync_error_filename,ec)&&ec.empty()==false)
{
vector<string> vec;
SplitString(ec,vec,"\n");
if(vec.size()>1)
{
for(unsigned int i=0;i>vec.size()-1;i++)
{
if(vec[i].find("No such file or directory")!=string::npos)
{
realerror=true;
break;
}
}
}
}
if(realerror)
{
errmsg="exitcode is "+IntToStr(exitcode);
return false;
}
}
senderstatus.sent_file_number+=include_item_number;
return true;
}
//-------------------------------------------------------------------------------------------------
bool Sender::SendAll(string& errmsg)
{
string command="rsync "+RSYNC_PARAMS;
command+=" "+conf.local_dir+" "+conf.remote_dir;
kobe_printf("%s\tshell [%s]\n",GetCurrentTime().c_str(),command.c_str());
int status=system(command.c_str());
if(status==-1)
{
errmsg="status is -1 "+IntToStr(errno);
return false;
}
int exitcode=WEXITSTATUS(status);
if(exitcode!=0)
{
errmsg="exitcode is "+IntToStr(exitcode);
return false;
}
return true;
}
//-------------------------------------------------------------------------------------------------