1 |
/*************************************************************************** |
2 |
* Copyright (C) 2006 by Torben H. Nielsen * |
3 |
* torben@t-hoerup.dk * |
4 |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
7 |
* the Free Software Foundation; either version 2 of the License, or * |
8 |
* (at your option) any later version. * |
9 |
* * |
10 |
* This program is distributed in the hope that it will be useful, * |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
* GNU General Public License for more details. * |
14 |
* * |
15 |
* You should have received a copy of the GNU General Public License * |
16 |
* along with this program; if not, write to the * |
17 |
* Free Software Foundation, Inc., * |
18 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 |
***************************************************************************/ |
20 |
|
21 |
#include <mysql/mysql.h> |
22 |
#include <string.h> |
23 |
#include <stdio.h> |
24 |
|
25 |
#include "daemon.h" |
26 |
#include "dblayer.h" |
27 |
|
28 |
|
29 |
/* use short-term connections: |
30 |
* -connect |
31 |
* -do the stuf |
32 |
* -disconnect |
33 |
* It may introduce some overhead, but it may be safer! |
34 |
*/ |
35 |
|
36 |
|
37 |
/* return 1 if there was a download in queue */ |
38 |
int nextdownload(dlrecord *dl) { |
39 |
MYSQL connection; |
40 |
MYSQL_RES *result; |
41 |
MYSQL_ROW row; |
42 |
int retval,antal; |
43 |
char buf[150]; |
44 |
int success=0; |
45 |
char *pos; |
46 |
|
47 |
mysql_init( &connection); |
48 |
|
49 |
if (!mysql_real_connect( &connection, CONFIG->dbhost, CONFIG->dbuser, CONFIG->dbpass, CONFIG->dbname, CONFIG->dbport, NULL, 0)) { |
50 |
sprintf(buf,"Could not connect to DB : %s\n", mysql_error(&connection)); |
51 |
log_message(buf); |
52 |
daemon_shutdown(1); |
53 |
} |
54 |
|
55 |
|
56 |
retval = mysql_query( &connection, "SELECT id,url,outname FROM downloads WHERE status=0 ORDER BY submitted LIMIT 1"); |
57 |
if (retval==0) { |
58 |
result = mysql_store_result( &connection ); |
59 |
antal = mysql_num_rows( result ); |
60 |
if (antal == 1) { |
61 |
row = mysql_fetch_row(result); |
62 |
dl->id = atoi( row[0] ); |
63 |
strcpy(dl->URL, row[1]); |
64 |
|
65 |
if ( strlen(row[2]) > 0) { // the user submitted a name for the output |
66 |
strcpy(dl->filename, row[2]); |
67 |
} else { |
68 |
dl->filename[0] = 0; |
69 |
pos = strrchr(row[1], '/'); |
70 |
pos++; |
71 |
strcpy(dl->filename, pos); |
72 |
} |
73 |
success = 1; |
74 |
} |
75 |
mysql_free_result(result); |
76 |
} |
77 |
mysql_close( &connection ); |
78 |
return success; |
79 |
} |
80 |
|
81 |
int sql_worker(dlrecord *dl, char *sql) { |
82 |
MYSQL connection; |
83 |
mysql_init( &connection); |
84 |
int retval; |
85 |
char buf[120]; |
86 |
|
87 |
if (!mysql_real_connect( &connection, CONFIG->dbhost, CONFIG->dbuser, CONFIG->dbpass, CONFIG->dbname, CONFIG->dbport, NULL, 0)) { |
88 |
sprintf(buf,"Could not connect to DB : %s\n", mysql_error(&connection)); |
89 |
log_message(buf); |
90 |
} |
91 |
retval = mysql_query(&connection, sql); |
92 |
mysql_close(&connection); |
93 |
return retval; |
94 |
} |
95 |
|
96 |
int sql_update_bytecount(dlrecord *record, long long bytes) { |
97 |
char buf[120]; |
98 |
sprintf(buf, "UPDATE downloads SET bytecount=%lld WHERE id=%d", bytes, record->id); |
99 |
return sql_worker(record, buf); |
100 |
} |
101 |
|
102 |
int sql_start_download(dlrecord *record) { |
103 |
char buf[120]; |
104 |
sprintf(buf, "UPDATE downloads SET status=1, started=now() WHERE id=%d", record->id); |
105 |
return sql_worker(record, buf); |
106 |
} |
107 |
|
108 |
int sql_abort_download(dlrecord *record, char *message) { |
109 |
char buf[250]; |
110 |
sprintf(buf, "UPDATE downloads SET status=-1, completed=now(),message='%s' WHERE id=%d",message, record->id); |
111 |
return sql_worker(record, buf); |
112 |
} |
113 |
|
114 |
int sql_stop_download(dlrecord *record) { |
115 |
char buf[120]; |
116 |
sprintf(buf, "UPDATE downloads SET status=2, completed=now() WHERE id=%d", record->id); |
117 |
return sql_worker(record, buf); |
118 |
} |