📄 ftpsearch.c
字号:
num_iter = ping_mt_data->num_servers / rt.max_simul_pings; num_left = ping_mt_data->num_servers % rt.max_simul_pings; debug_prz("Max simul pings=%d", rt.max_simul_pings); k = 0; for (i = 0; i < num_iter; i++) { for (j = 0; j < rt.max_simul_pings; j++) { if (pthread_create(&ping_threads[j], NULL, (void *) &ping_one_server, (void *) (&ping_mt_data->ftp_mirrors[k])) != 0) die("Error: Not enough system resources" "to create thread!\n"); k++; } for (j = 0; j < rt.max_simul_pings; j++) { /*Wait till the end of each thread. */ pthread_join(ping_threads[j], NULL); } } for (j = 0; j < num_left; j++) { if (pthread_create(&ping_threads[j], NULL, (void *) &ping_one_server, (void *) (&ping_mt_data->ftp_mirrors[k])) != 0) die("Error: Not enough system resources" "to create thread!\n"); k++; } for (j = 0; j < num_left; j++) { /*Wait till the end of each thread. */ pthread_join(ping_threads[j], NULL); } ping_mt_data->ping_run_done = TRUE;}static int top_item = 0;interface_ret curses_ftpsearch_interface(struct ftp_mirror *ftp_mirrors, int num_servers, int *selected_server){#define CTRL(x) ((x) & 0x1F) pthread_t ping_main_thread; ping_mthread_data ping_mt_data; assert(ftp_mirrors != NULL); top_item = 0; *selected_server = 0; message("Starting ping\n"); /* now that we have got the servers lets "ping" them and see */ memset(&ping_mt_data, 0, sizeof(ping_mthread_data)); ping_mt_data.ftp_mirrors = ftp_mirrors; ping_mt_data.num_servers = num_servers; ping_mt_data.ping_run_done = FALSE; /*launch the main thread */ if (pthread_create(&ping_main_thread, NULL, (void *) &ping_the_list, (void *) (&ping_mt_data)) != 0) die("Error: Not enough system resources" "to create thread!\n"); /* while(1) */ while (ping_mt_data.ping_run_done == FALSE) { switch (getch()) { case KEY_DOWN: top_item += 1; if (top_item >= num_servers) top_item = num_servers - 1; break; case KEY_UP: top_item -= 1; if (top_item < 0) top_item = 0; break; case CTRL('A'): /*Abort */ pthread_cancel(ping_main_thread); pthread_join(ping_main_thread, NULL); /* fixme set a status flag...to indicate we aborted */ refresh(); delay_ms(500); erase(); return USER_ABORTED; break; case 13: case 10: /*return balue user selected */ pthread_cancel(ping_main_thread); pthread_join(ping_main_thread, NULL); /*fixme set a status flag...to indicate we aborted */ *selected_server = top_item; refresh(); delay_ms(500); erase(); return USER_SELECTED; break; default: break; } delay_ms(30); display_list(ftp_mirrors, num_servers); refresh(); } pthread_join(ping_main_thread, NULL); /*display again before leaving */ delay_ms(30); display_list(ftp_mirrors, num_servers); refresh(); delay_ms(1000); erase(); return AUTO;}void display_list(struct ftp_mirror *pmirrors, int num_servers){ const int max_lines = 13; int i; attrset(COLOR_PAIR(HIGHLIGHT_PAIR) | A_BOLD); mvaddstr(0, 0, "Server Ping Time"); attrset(COLOR_PAIR(NULL_PAIR)); for (i = 0; i < max_lines; i++) { move(i + 1, 0); clrtoeol(); if (i == 0) attrset(COLOR_PAIR(SELECTED_PAIR) | A_BOLD); if (i + top_item < num_servers) { switch (pmirrors[top_item + i].status) { case RESPONSEOK: mvprintw(i + 1, 0, "%2d %-25.25s %12dms", top_item + i + 1, pmirrors[top_item + i].server_name, pmirrors[top_item + i].milli_secs); break; case NORESPONSE: mvprintw(i + 1, 0, "%2d %-25.25s %12s", top_item + i + 1, pmirrors[top_item + i].server_name, "No Response"); break; case ERROR: mvprintw(i + 1, 0, "%2d %-25.25s %12s", top_item + i + 1, pmirrors[top_item + i].server_name, "Error"); break; default: mvprintw(i + 1, 0, "%2d %-25.25s %12s", top_item + i + 1, pmirrors[top_item + i].server_name, "Untested"); break; } } if (i == 0) attrset(COLOR_PAIR(NULL_PAIR)); } attrset(COLOR_PAIR(HIGHLIGHT_PAIR) | A_BOLD); mvprintw(max_lines + 2, 0, "Up and Down arrows to scroll, CTRL+A to Abort"); mvprintw(max_lines + 3, 0, "Enter to abort and start from the highlighted server"); attrset(COLOR_PAIR(NULL_PAIR)); move(max_lines + 4, 0); clrtoeol(); move(max_lines + 5, 0); clrtoeol(); attrset(COLOR_PAIR(SELECTED_PAIR) | A_BOLD); mvprintw(max_lines + 4, 0, "%s", pmirrors[top_item].full_name); attrset(COLOR_PAIR(NULL_PAIR));}char *grow_buffer(char *buf_start, char *cur_pos, int *buf_len, int data_len){ const int INIT_SIZE = 4048; int bytes_left; char *p; /* find how many bytes are left */ bytes_left = *buf_len - (cur_pos - buf_start); assert(bytes_left >= 0); assert(data_len <= INIT_SIZE); if (bytes_left < data_len + 1) { /* time to realloc the buffer buffer */ p = krealloc(buf_start, *buf_len + INIT_SIZE); *buf_len += INIT_SIZE; return p; } else { return buf_start; }}uerr_t get_mirror_info(urlinfo * u, http_stat_t * hs, char **ret_buf){ int sock, i; char buffer[HTTP_BUFFER_SIZE]; char *req = NULL; char *user, *passwd, *wwwauth; char *p, *p1, *p2; uerr_t err; netrc_entry *netrc_ent; int p_len, ret, total; /* adding redirect support */ err = get_http_info(u, hs); if (err == NEWLOCATION) { /* * loop for max_redirections searching for a valid file */ for (i = 0; i < rt.max_redirections; ++i) { char *constructed_newloc; /*DONE : handle relative urls too */ constructed_newloc = uri_merge(u->url, hs->newloc); debug_prz("Redirected to %s, merged URL = %s", hs->newloc, constructed_newloc); err = parseurl(constructed_newloc, u, 0); if (err != URLOK) { die("The server returned location is syntactically wrong: %s!", constructed_newloc); } message("=> %s", hs->newloc); debug_prz("=> %s", hs->newloc); assert(u->proto == URLHTTP); err = get_http_info(u, hs); if (err == HOK) { break; } else if (err == NEWLOCATION) { if (i == rt.max_redirections) { /* * redirected too many times */ die("Error: Too many redirections:%d\n", rt.max_redirections); } } else { message ("A error occured while trying to get info from the server\n"); return err; } } } else if (err != HOK) { if (hs->statcode == HTTP_NOT_FOUND) { message ("Error: Unable to get mirror info from the server: Not found!\n"); } else { message ("A error occured while trying to get info from the server\n"); return err; } } err = connect_to_server(&sock, u->host, u->port, rt.timeout); if (err != NOCONERROR) { message("Error connecting to %s", u->host); return err; } /* Authentification code */ user = u->user; passwd = u->passwd; /* * Use .netrc if asked to do so. */ if (rt.use_netrc == TRUE) { netrc_ent = search_netrc(rt.netrc_list, u->host); if (netrc_ent != NULL) { user = netrc_ent->account; passwd = netrc_ent->password; } } user = user ? user : ""; passwd = passwd ? passwd : ""; if (strlen(user) || strlen(passwd)) { /*Construct the necessary header */ wwwauth = get_basic_auth_str(user, passwd); message("Authenticating as user %s password %s", user, passwd); debug_prz("Authentification string=%s\n", wwwauth); } else wwwauth = 0; /* TODO: Handle referer? */ /* We will get http info about the file by calling http_fetch_headers with GET */ { const char reqfmt[] = "GET %s HTTP/1.0\r\n" "User-Agent: %s\r\n" "Host: %s\r\n" "Accept: */*\r\n" "%s\r\n"; int reqlen; reqlen = snprintf(NULL, 0, reqfmt, u->path, USER_AGENT, u->host, wwwauth ? wwwauth : ""); if (reqlen <= 0) die("Unable to calculate buffer length for HTTP GET request\n"); reqlen++; /* nul */ req = kmalloc(reqlen); snprintf(req, reqlen, reqfmt, u->path, USER_AGENT, u->host, wwwauth ? wwwauth : ""); } debug_prz("HTTP request= %s\n", req); err = http_fetch_headers(sock, u, hs, req); kfree(req); if (wwwauth) free(wwwauth); /*Check if we authenticated using any user or password and if we were kicked out, if so return HAUTHFAIL */ if (err != HOK) { if (err == HAUTHREQ && (strlen(user) || strlen(passwd))) return HAUTHFAIL; return err; } /* Ok start fetching the data */ p1 = p = kmalloc(HTTP_BUFFER_SIZE + 1); p_len = HTTP_BUFFER_SIZE + 1; total = 0; do { ret = krecv(sock, buffer, sizeof(buffer), 0, rt.timeout); if (ret > 0) { p2 = grow_buffer(p, p1, &p_len, ret); memcpy(p2 + (p1 - p), buffer, ret); p1 = (p1 - p) + ret + p2; p = p2; } total += ret; } while (ret > 0); if (ret == -1) { if (errno == ETIMEDOUT) { close(sock); return READERR; } close(sock); return READERR; } p[total] = 0; *ret_buf = p; close(sock); return HOK;}int get_mirror_list_pos(char *full_name, ftp_mirror * list, int num_servers){ int i; for (i = 0; i < num_servers; i++) {debug_prz("list = %s path = %s\n", full_name, list[i].full_name); if (strcmp(full_name, list[i].full_name) == 0) return i; } /* Not found: return -1 */ return -1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -