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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
/*
FileTransfer.cpp
Copyright (C) 2009 Daniel Novotny (dnovotny@redhat.com)
Copyright (C) 2009 RedHat inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <iostream>
#include <sstream>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <zip.h>
#include <libtar.h>
#include <bzlib.h>
#include <zlib.h>
#include <curl/curl.h>
#include "abrtlib.h"
#include "FileTransfer.h"
#include "DebugDump.h"
#include "ABRTException.h"
#include "CommLayerInner.h"
using namespace std;
#define HBLEN 255
#define FILETRANSFER_DIRLIST DEBUG_DUMPS_DIR "/FileTransferDirlist.txt"
CFileTransfer::CFileTransfer()
:
m_sArchiveType(".tar.gz"),
m_nRetryCount(3),
m_nRetryDelay(20)
{
}
void CFileTransfer::SendFile(const char *pURL, const char *pFilename)
{
int len = strlen(pURL);
if (len == 0)
{
warn_client(_("FileTransfer: URL not specified"));
return;
}
std::string msg = ssprintf(_("Sending archive %s to %s"), pFilename, pURL);
update_client(msg.c_str());
std::string wholeURL = concat_path_file(pURL, pFilename);
int result;
int count = m_nRetryCount;
do
{
FILE *f;
struct stat buf;
CURL *curl;
f = fopen(pFilename, "r");
if (!f)
{
throw CABRTException(EXCEP_PLUGIN, ssprintf("Can't open archive file '%s'", pFilename));
}
if (fstat(fileno(f), &buf) == -1)
{
fclose(f);
throw CABRTException(EXCEP_PLUGIN, ssprintf("Can't stat archive file '%s'", pFilename));
}
curl = curl_easy_init();
if (!curl)
{
throw CABRTException(EXCEP_PLUGIN, "Curl library init error");
}
/* enable uploading */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
/* specify target */
curl_easy_setopt(curl, CURLOPT_URL, wholeURL.c_str());
/*file handle: passed to the default callback, it will fread() it*/
curl_easy_setopt(curl, CURLOPT_READDATA, f);
/*get file size*/
curl_easy_setopt(curl, CURLOPT_INFILESIZE, buf.st_size);
/*everything is done here; result 0 means success*/
result = curl_easy_perform(curl);
/*goodbye*/
curl_easy_cleanup(curl);
fclose(f);
}
/*retry the upload if not succesful, wait a bit before next try*/
while (result != 0 && --count >= 0 && (sleep(m_nRetryDelay), 1));
}
/*
walks through the directory and applies a function with one
parameter "something" to each filename,
now used in create_zip, but can be useful for some future
archivers as well
*/
static void traverse_directory(const char * directory, void * something,
void (*func)(void *,const char *) )
{
DIR * dp;
struct dirent * dirp;
char complete_name[BUFSIZ];
char * end;
dp = opendir(directory);
if (dp == NULL)
{
return;
}
while ((dirp = readdir(dp)) != NULL)
{
if (is_regular_file(dirp, directory))
{
end = stpcpy(complete_name, directory);
if (end[-1] != '/')
{
*end++ = '/';
}
end = stpcpy(end, dirp->d_name);
func(something, complete_name);
}
}
closedir(dp);
}
static void add_to_zip(void * z, const char * filename)
{
struct zip_source *s;
s = zip_source_file( (struct zip *)z, filename, 0, 0);
zip_add( (struct zip *)z, filename, s);
}
static void create_zip(const char * archive_name, const char * directory)
{
struct zip * z;
z = zip_open(archive_name, ZIP_CREATE, NULL);
if (z == NULL)
{
return;
}
traverse_directory(directory, z, add_to_zip);
zip_close(z);
}
static void create_tar(const char * archive_name, const char * directory)
{
TAR *tar;
if (tar_open(&tar, (char *)archive_name, NULL, O_WRONLY | O_CREAT, 0644, TAR_GNU) != 0)
{
return;
}
tar_append_tree(tar, (char *)directory, (char*)".");
tar_close(tar);
}
static void create_targz(const char * archive_name, const char * directory)
{
char * name_without_gz;
char buf[BUFSIZ];
FILE * f;
ssize_t bytesRead;
gzFile gz;
name_without_gz = xstrdup(archive_name);
strrchr(name_without_gz, '.')[0] = '\0';
create_tar(name_without_gz, directory);
f = fopen(name_without_gz, "r");
if (f == NULL)
{
free(name_without_gz);
return;
}
gz = gzopen(archive_name, "w");
if (gz == NULL)
{
fclose(f);
free(name_without_gz);
return;
}
while ((bytesRead = fread(buf, 1, BUFSIZ, f)) > 0)
{
gzwrite(gz, buf, bytesRead);
}
gzclose(gz);
fclose(f);
remove(name_without_gz);
free(name_without_gz);
}
static void create_tarbz2(const char * archive_name, const char * directory)
{
char * name_without_bz2;
char buf[BUFSIZ];
FILE * f;
ssize_t bytesRead;
int tarFD;
int bzError;
BZFILE * bz;
#define BLOCK_MULTIPLIER 7
name_without_bz2 = xstrdup(archive_name);
strrchr(name_without_bz2,'.')[0] = '\0';
create_tar(name_without_bz2, directory);
tarFD = open(name_without_bz2, O_RDONLY);
if (tarFD == -1)
{
free(name_without_bz2);
return;
}
f = fopen(archive_name, "w");
if (f == NULL)
{
close(tarFD);
free(name_without_bz2);
return;
}
bz = BZ2_bzWriteOpen(&bzError, f, BLOCK_MULTIPLIER, 0, 0);
if (bz == NULL)
{
close(tarFD);
fclose(f);
free(name_without_bz2);
return;
}
while ((bytesRead = read(tarFD, buf, BUFSIZ)) > 0)
{
BZ2_bzWrite(&bzError, bz, buf, bytesRead);
}
BZ2_bzWriteClose(&bzError, bz, 0, NULL, NULL);
close(tarFD);
fclose(f);
remove(name_without_bz2);
free(name_without_bz2);
}
void CFileTransfer::CreateArchive(const char *pArchiveName, const char *pDir)
{
if (m_sArchiveType == ".tar")
{
create_tar(pArchiveName, pDir);
}
else if (m_sArchiveType == ".tar.gz")
{
create_targz(pArchiveName, pDir);
}
else if (m_sArchiveType == ".tar.bz2")
{
create_tarbz2(pArchiveName, pDir);
}
else if (m_sArchiveType == ".zip")
{
create_zip(pArchiveName, pDir);
}
else
{
throw CABRTException(EXCEP_PLUGIN, "Unknown/unsupported archive type " + m_sArchiveType);
}
}
/*returns the last component of the directory path*/
static std::string DirBase(const std::string& pStr)
{
int i = pStr.length() - 1;
if (i > 0 && pStr[i] == '/')
{
i--;
}
std::string result;
for (; i >= 0 && pStr[i] != '/'; i--)
{
result = pStr[i] + result;
}
return result;
}
void CFileTransfer::Run(const char *pActionDir, const char *pArgs)
{
fstream dirlist;
std::string dirname, archivename;
char hostname[HBLEN];
update_client(_("File Transfer: Creating a report..."));
if (strcmp(pArgs, "store") == 0)
{
/* store pActiveDir for later sending */
dirlist.open(FILETRANSFER_DIRLIST, fstream::out | fstream::app );
dirlist << pActionDir << endl;
dirlist.close();
}
else if (strcmp(pArgs, "one") == 0)
{
/* just send one archive */
gethostname(hostname, HBLEN);
archivename = std::string(hostname) + "-"
+ DirBase(pActionDir) + m_sArchiveType;
try
{
CreateArchive(archivename.c_str(), pActionDir);
SendFile(m_sURL.c_str(), archivename.c_str());
}
catch (CABRTException& e)
{
warn_client(_("CFileTransfer::Run(): Cannot create and send an archive: ") + e.what());
//update_client("CFileTransfer::Run(): Cannot create and send an archive: " + e.what());
}
unlink(archivename.c_str());
}
else
{
gethostname(hostname, HBLEN);
dirlist.open(FILETRANSFER_DIRLIST, fstream::in);
if (dirlist.fail())
{
/* this means there are no reports to send (no crashes, hurray)
which is perfectly OK */
return;
}
while (getline(dirlist, dirname), dirlist.good())
{
archivename = std::string(hostname) + "-"
+ DirBase(dirname) + m_sArchiveType;
try
{
CreateArchive(archivename.c_str(), dirname.c_str());
SendFile(m_sURL.c_str(), archivename.c_str());
}
catch (CABRTException& e)
{
warn_client(_("CFileTransfer::Run(): Cannot create and send an archive: ") + e.what());
// update_client("CFileTransfer::Run(): Cannot create and send an archive: " + e.what());
}
unlink(archivename.c_str());
}
dirlist.close();
/* all the files we're able to send should be sent now,
starting over with clean table */
unlink(FILETRANSFER_DIRLIST);
}
}
void CFileTransfer::SetSettings(const map_plugin_settings_t& pSettings)
{
map_plugin_settings_t::const_iterator end = pSettings.end();
map_plugin_settings_t::const_iterator it = pSettings.find("URL");
if (it != end)
{
m_sURL = it->second;
}
else
{
warn_client(_("FileTransfer: URL not specified"));
}
it = pSettings.find("RetryCount");
if (it != end)
{
m_nRetryCount = atoi(it->second.c_str());
}
it = pSettings.find("RetryDelay");
if (it != end)
{
m_nRetryDelay = atoi(it->second.c_str());
}
it = pSettings.find("ArchiveType");
if (it != end)
{
/* currently supporting .tar, .tar.gz, .tar.bz2 and .zip */
m_sArchiveType = it->second;
if (m_sArchiveType[0] != '.')
{
m_sArchiveType = "." + m_sArchiveType;
}
}
}
map_plugin_settings_t CFileTransfer::GetSettings()
{
map_plugin_settings_t ret;
std::stringstream ss;
ret["URL"] = m_sURL;
ss << m_nRetryCount;
ret["RetryCount"] = ss.str();
ss.str("");
ss << m_nRetryDelay;
ret["RetryDelay"] = ss.str();
ret["ArchiveType"] = m_sArchiveType;
return ret;
}
PLUGIN_INFO(ACTION,
CFileTransfer,
"FileTransfer",
"0.0.6",
"Sends a report via FTP or SCTP",
"dnovotny@redhat.com",
"https://fedorahosted.org/abrt/wiki",
"");
|