/*************************************************************************** * Copyright (C) 2006 by Torben H. Nielsen * * torben@t-hoerup.dk * * * * 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., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include #include #include "daemon.h" #include "dblayer.h" /* use short-term connections: * -connect * -do the stuf * -disconnect * It may introduce some overhead, but it may be safer! */ /* return 1 if there was a download in queue */ int nextdownload(dlrecord *dl) { MYSQL connection; MYSQL_RES *result; MYSQL_ROW row; int retval,antal; char buf[150]; int success=0; char *pos; mysql_init( &connection); if (!mysql_real_connect( &connection, CONFIG->dbhost, CONFIG->dbuser, CONFIG->dbpass, CONFIG->dbname, CONFIG->dbport, NULL, 0)) { sprintf(buf,"Could not connect to DB : %s\n", mysql_error(&connection)); log_message(buf); daemon_shutdown(1); } retval = mysql_query( &connection, "SELECT id,url,outname FROM downloads WHERE status=0 ORDER BY submitted LIMIT 1"); if (retval==0) { result = mysql_store_result( &connection ); antal = mysql_num_rows( result ); if (antal == 1) { row = mysql_fetch_row(result); dl->id = atoi( row[0] ); strcpy(dl->URL, row[1]); if ( strlen(row[2]) > 0) { // the user submitted a name for the output strcpy(dl->filename, row[2]); } else { dl->filename[0] = 0; pos = strrchr(row[1], '/'); pos++; strcpy(dl->filename, pos); } success = 1; } mysql_free_result(result); } mysql_close( &connection ); return success; } int sql_worker(dlrecord *dl, char *sql) { MYSQL connection; mysql_init( &connection); int retval; char buf[120]; if (!mysql_real_connect( &connection, CONFIG->dbhost, CONFIG->dbuser, CONFIG->dbpass, CONFIG->dbname, CONFIG->dbport, NULL, 0)) { sprintf(buf,"Could not connect to DB : %s\n", mysql_error(&connection)); log_message(buf); } retval = mysql_query(&connection, sql); mysql_close(&connection); return retval; } int sql_update_bytecount(dlrecord *record, long long bytes) { char buf[120]; sprintf(buf, "UPDATE downloads SET bytecount=%lld WHERE id=%d", bytes, record->id); return sql_worker(record, buf); } int sql_start_download(dlrecord *record) { char buf[120]; sprintf(buf, "UPDATE downloads SET status=1, started=now() WHERE id=%d", record->id); return sql_worker(record, buf); } int sql_abort_download(dlrecord *record, char *message) { char buf[250]; sprintf(buf, "UPDATE downloads SET status=-1, completed=now(),message='%s' WHERE id=%d",message, record->id); return sql_worker(record, buf); } int sql_stop_download(dlrecord *record) { char buf[120]; sprintf(buf, "UPDATE downloads SET status=2, completed=now() WHERE id=%d", record->id); return sql_worker(record, buf); }