📄 main.c
字号:
*/ die("Error while checking for a log file. Reason: %s", strerror(errno)); } else if (ret == 1) { /* * Logfile from a previous session exists, lets get the number * of connections from it */ if (log_read_logfile(u, &lf) != 0) die("Unable to read the log file. Reason: %s", strerror(errno)); else { num_connections = lf.num_connections; } } else if (ret == 0) { /* * No logfile found, assume 4 connections */ num_connections = 4; }/* now delete the portions */ for (i = 0; i < num_connections; i++) { /* * calculate the name of the download * file fragment from the target file */ if (snprintf(buffer, sizeof(buffer), "%s%s%d", u->file, DEFAULT_FILE_EXT, i) >= sizeof(buffer)) { die("Error: Filename for %s, part %d is too long\n", u->file, i); } ret = unlink(buffer); /* * did we encounter a error? */ if (ret == -1) { if (errno == ENOENT) continue; else die("Error while trying to delete %s :", buffer, strerror(errno)); } }}void do_log_file_normal(urlinfo * u){ assert(rt.run_mode == NORMAL); if (log_create_logfile(u) != 0) die("Unable to create log file : %s", strerror(errno));}void do_log_file_resume(urlinfo * u){ logfile lf; assert(rt.run_mode == RESUME); if (log_read_logfile(u, &lf) != 0) die("Unable to read the log file. Reason: %s", strerror(errno)); else { rt.num_connections = lf.num_connections; }}/* Creates the logfile and stores the data to it if the run mode is NORMAL, * Else if the run mode is RESUME, it loads the logfile from the previous * session, and does the necessary modifications. */void do_logfile(urlinfo * u){ logfile lf; int ret; int i, portion_found; char buffer[MAXPATHLEN]; struct stat st_buf; memset(&lf, 0, sizeof(logfile)); ret = log_logfile_exists(u); if (ret == -1) { /* * Something wrong hapenned */ die("Error while checking for a log file. Reason: %s", strerror(errno)); } else if (ret == 1 && rt.run_mode != RESUME && rt.force_mode == FALSE) { query_resume_old_download(u); } else if (ret == 1 && rt.run_mode != RESUME && rt.force_mode == TRUE) delete_file_portions(u); else if (ret == 0) { /* * No logfile was found so it maybe a aborted download from * a earlier version of prozilla or the user may just * have added the -r switch at the start */ /* * to see which is which lets search for the download portions * * If any is found we will resume with four connections * * Otherwise well start from scratch with the user specified * * number of connections */ portion_found = FALSE; for (i = 0; i < 4; i++) { /* * calculate the name of the download * file fragment from the target file */ if (snprintf(buffer, sizeof(buffer), "%s%s%d", u->file, DEFAULT_FILE_EXT, i) >= sizeof(buffer)) { die("Error: Filename for %s, part %d is too long\n", u->file, i); } ret = stat(buffer, &st_buf); /* * did we encounter a error? */ if (ret == -1) { if (errno == ENOENT) /* * the file doesn't exist..do nothing */ continue; else die("Error while searching for downloaded" "file portions: %s", strerror(errno)); } else { portion_found = TRUE; break; } } if (portion_found == TRUE) { if (rt.run_mode != RESUME && rt.force_mode == FALSE) query_resume_old_download(u); else if (rt.run_mode != RESUME && rt.force_mode == TRUE) delete_file_portions(u); /* * Does the user now want to resume */ if (rt.run_mode == RESUME) { /* * Resume with 4 connections as the old version did */ message("No log file from previous session found," "so I will resume with the default of 4 connections"); rt.num_connections = 4; delay_ms(400); } } /* * And then lets create the log too */ if (log_create_logfile(u) != 0) die("Unable to create log file. Reason: %s", strerror(errno)); } switch (rt.run_mode) { case NORMAL: do_log_file_normal(u); break; case RESUME: do_log_file_resume(u); break; default: die("Unsupported run mode in file %s, line %d", __FILE__, __LINE__); } return;}/* * function is called when the application wants to abort * TODO: Handle to freeing of resources */int die(const char *args, ...){ char p[MAX_MSG_SIZE]; va_list vp; va_start(vp, args); vsnprintf(p, sizeof(p), args, vp); va_end(vp); /* indicate that we are out of the display loop */ rt.in_curses_display_loop = FALSE; /* * terminate any threads if running */ if (threads) { terminate_threads(threads, rt.num_connections); } /* * free the connections array if allocated */ if (connections) free(connections); /* * free the url structure */ if (rt.die_to_stdout == TRUE) { /*just dump error to stdout and quit */ endwin(); printf("\n%s\n", p); exit(EXIT_FAILURE); } attrset(COLOR_PAIR(MSG_PAIR) | A_BOLD); move(LINES-3, 0); clrtoeol(); move(LINES-2, 0); clrtoeol(); move(LINES-1, 0); clrtoeol(); mvprintw(LINES-3, 0, "%s", p); attrset(COLOR_PAIR(NULL_PAIR)); mvprintw(LINES-1, 0, "Press any key to exit..\n"); refresh(); flushinp(); /* * wait until a key is pressed */ do { delay_ms(20); } while (getch() == ERR); endwin(); exit(EXIT_FAILURE);}void quit(const char *args, ...){ char p[MAX_MSG_SIZE]; va_list vp; va_start(vp, args); vsnprintf(p, sizeof(p), args, vp); va_end(vp); endwin(); printf("\n%s\n", p); exit(EXIT_SUCCESS);}/*fixme do something about this, move to a better file rather than main.c */int compare_two_servers(const void *a, const void *b){ const ftp_mirror *ma = (const ftp_mirror *) a; const ftp_mirror *mb = (const ftp_mirror *) b; int milli_sec_a; int milli_sec_b; if (ma->status != RESPONSEOK && (mb->status != RESPONSEOK)) return 1000000; milli_sec_a = (ma->tv.tv_sec * 1000) + (ma->tv.tv_usec / 1000); if (ma->status != RESPONSEOK) { milli_sec_a = 1000000; } milli_sec_b = (mb->tv.tv_sec * 1000) + (mb->tv.tv_usec / 1000); if (mb->status != RESPONSEOK) { milli_sec_b = 1000000; } return (milli_sec_a - milli_sec_b);}int main(int argc, char **argv){ http_stat_t hs; ftp_stat_t fs; urlinfo url_data, ftps_url_data; uerr_t err; struct ftp_mirror *ftp_mirrors; int i; int c; int num_servers = 0; int use_server = -1; int same_url = FALSE; char *conf_file; char global_conf_file[] = GLOBAL_CONF_FILE; int ok = 1; memset(&hs, 0, sizeof(hs)); memset(&fs, 0, sizeof(fs)); /* * set the default runtime configuration */ set_defaults(&argc, &argv); set_preferences(global_conf_file); conf_file = prozrc_file_name(); if (conf_file) set_preferences(conf_file); debug_init(); while ((c = getopt_long(argc, argv, "?hrfk:1Lt:vgsP:", long_opts, NULL)) != EOF) { switch (c) { case 'L': license(); exit(EXIT_SUCCESS); case 'h': help(); exit(EXIT_SUCCESS); case 'v': version(); exit(EXIT_SUCCESS); case 'r': rt.run_mode = RESUME; break; case 'f': rt.force_mode = TRUE; break; case 'k': if (setargval(optarg, &rt.num_connections) != 1) { /* * The call failed due to a invalid arg */ printf("Error: Invalid arguments for the -k option\n" "Please type proz --help for help\n"); exit(EXIT_FAILURE); } if (rt.num_connections == 0) { printf("Hey! How can I download anything with 0 (Zero)" " connections!?\n" "Please type proz --help for help\n"); exit(EXIT_FAILURE); } break; case 't': if (setargval(optarg, &rt.try_attempts) != 1) { /* * The call failed due to a invalid arg */ printf ("Error: Invalid arguments for the -t or --tries option(s)\n" "Please type proz --help for help\n"); exit(EXIT_FAILURE); } break; case 'n': /* * Don't use ~/.netrc" */ rt.use_netrc = FALSE; break; case 'P': /* * Save the downloaded file to DIR */ rt.output_dirprefix = kstrdup(optarg); break; case '?': help(); exit(EXIT_SUCCESS); break; case '1': rt.num_connections = 1; break; case 'g': /* * TODO solve this soon */#ifdef HAVE_GTK rt.use_gtk_option = TRUE;#else printf ("Error: GTK interface is not supported in " "the development version currently\n"); exit(EXIT_FAILURE);#endif break; case 129: /* * lets use PORT as the default then */ rt.use_pasv = FALSE; break; case 130: /* * retry-delay option */ if (setargval(optarg, &rt.retry_delay) != 1) { /* * The call failed due to a invalid arg */ printf ("Error: Invalid arguments for the --retry-delay option\n" "Please type proz --help for help\n"); exit(EXIT_FAILURE); } break; case 131: /*--timout option */ if (setargval(optarg, &rt.timeout) != 1) { /* * The call failed due to a invalid arg */ printf ("Error: Invalid arguments for the --timeout option\n" "Please type proz --help for help\n"); exit(EXIT_FAILURE); } break; case 132: /* --no-getch option */ rt.die_to_stdout = TRUE; break; case 133: /* --debug option */ rt.debug_mode = TRUE; break; case 's': /* --ftpsearch option */ rt.ftp_search = TRUE; break; case 135: /* --no-search option */ rt.ftp_search = FALSE; break; case 136: /* --pt option */ if (setargval(optarg, &rt.max_ping_wait) != 1) { /* * The call failed due to a invalid arg */ printf("Error: Invalid arguments for the --pt option\n"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -