int main(int argc,char **argv){ MYSQL_RES *res; MYSQL_ROW rows; // argc长度 if (argc!=3) { fprintf(stderr, "请使用: ./scp 源路径 目标路径\n"); return -1; } // 设置本地ip信息 if ((location=malloc(sizeof(struct iplist))) == NULL) { fprintf(stderr, "location结构动态分配内存错误\n"); return -1; } location->is_ssh=1; location->ip="127.0.0.1"; location->user="******"; location->pawd="123456"; location->port="22"; location->path=argv[1]; // 查询数据库 char *str="select * from iplist"; if ((res=query_mysql(str)) == NULL) { fprintf(stderr, "数据库结果集返回错误\n"); return -1; } // 获取结果集内容 while((rows=mysql_fetch_row(res))){ if (scpbag(rows,argv) < 0) { fprintf(stderr, "传包前的分配信息失败,请检查\n%s 传包失败\n",rows[1]); return -1; } } return 0; }
int download_ok (char *file, int fe_id) { /* Initialise MySQL connection */ MYSQL *conn; static char query[QUERY_MAX]; if ((conn = init_mysql(conn)) == NULL) { mysql_close(conn); error_log(ERROR,"MySQL initialisation failed"); exit(EXIT_FAILURE); } if (!connect_mysql(conn)) { mysql_close(conn); error_log(ERROR,"MySQL connection failed"); exit(EXIT_FAILURE); } sprintf(query, "INSERT INTO cached (id, fe_id, filename, cached_time) " "VALUES (md5('%s'), %d, '%s', now())", file, fe_id, file); query_mysql(conn, query); mysql_close(conn); return 0; }
void downloader_init(upstreams *upstr_ptr, mqd_t msgq_id) { /* Initialise MySQL connection */ MYSQL_RES *result; MYSQL_ROW row; int num_fields; if ((conn = init_mysql(conn)) == NULL) { mysql_close(conn); error_log(ERROR, "MySQL initialisation failed"); exit(EXIT_FAILURE); } if (!connect_mysql(conn)) { mysql_close(conn); error_log(ERROR, "MySQL connection failed"); exit(EXIT_FAILURE); } int n; const config_setting_t *upstr_setting; static int upstream_count; upstr_setting = config_lookup(&cfg, "upstreams"); upstream_count = config_setting_length(upstr_setting); error_log(ERROR, "Connected to MySQL."); struct mq_attr msgq_attr; int fe_id = cfg_get_int("fe_id"); static char query[QUERY_MAX]; unsigned int msgprio = 1; while (running) { /* Schedule upstreams */ for (n = 0; n < upstream_count; n++) { if (!upstr_ptr[n].alive && (time(NULL) - upstr_ptr[n].deadtime) > cfg_get_int("upstream_dead_timeout")) { error_log(DEBUG, "Making %s live again, time of dead: %d, now: %d", upstr_ptr[n].upstream, upstr_ptr[n].deadtime, time(NULL)); upstr_ptr[n].alive = 1; } error_log(DEBUG, "Upstream: %s, active: %d", upstr_ptr[n].upstream, upstr_ptr[n].alive); } /* Get latest data */ mq_getattr(msgq_id, &msgq_attr); if (!msgq_attr.mq_curmsgs) { if (cfg_get_int("dmode") == 2) { /* Query for robin mode*/ sprintf(query, "SELECT filename, size from scoreboard WHERE id NOT " "IN(SELECT id from cached where fe_id=%d) AND count >=%d " "order by count DESC, last_modified DESC, size limit %d", fe_id, cfg_get_int("cache_req_count"), cfg_get_int("workers")); } else { /* Query for shard mode*/ sprintf(query, "SELECT filename, size from scoreboard WHERE id NOT " "IN(SELECT id from cached where fe_id=%d) AND count >=%d " "AND fe_id=%d " "order by count DESC, last_modified DESC, size limit %d", fe_id, cfg_get_int("cache_req_count"), fe_id, cfg_get_int("workers")); } query_mysql(conn, query); result = mysql_store_result(conn); num_fields = mysql_num_fields(result); while ((row = mysql_fetch_row(result))) { if (row[0] && row[1]) { if (check_file_exists(row[0], atoi(row[1])) != 0) { continue; } mq_send(msgq_id, row[0], strlen(row[0]) + 1, msgprio); } } mysql_free_result(result); } sleep(5); } error_log(ERROR, "Exiting"); }