⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 download.c

📁 prozgui是一款Linxu下著名的下载工具
💻 C
📖 第 1 页 / 共 5 页
字号:
  }  pthread_mutex_unlock(&download->status_change_mutex);  return TRUE;}boolean proz_download_all_dls_filensfod(download_t * download){  int i;  uerr_t err;  /*Lock mutex */  for (i = 0; i < download->num_connections; i++)  {    pthread_mutex_lock(&download->pconnections[i]->access_mutex);    err = download->pconnections[i]->err;    pthread_mutex_unlock(&download->pconnections[i]->access_mutex);    if (err != FTPNSFOD && err != HTTPNSFOD)    {      return FALSE;    }  }  return TRUE;}boolean proz_download_all_dls_ftpcwdfail(download_t * download){  int i;  uerr_t err;  /*Lock mutex */  for (i = 0; i < download->num_connections; i++)  {    pthread_mutex_lock(&download->pconnections[i]->access_mutex);    err = download->pconnections[i]->err;    pthread_mutex_unlock(&download->pconnections[i]->access_mutex);    if (err != FTPNSFOD && err != FTPCWDFAIL)    {      return FALSE;    }  }  return TRUE;}/*If all the downlaods conections err status is equal to in_err (ie all encountered the same error) ,returns TRUE */boolean proz_download_all_dls_err(download_t * download, uerr_t in_err){ int i;  uerr_t err;  /*Lock mutex */  for (i = 0; i < download->num_connections; i++)  {    pthread_mutex_lock(&download->pconnections[i]->access_mutex);    err = download->pconnections[i]->err;    pthread_mutex_unlock(&download->pconnections[i]->access_mutex);    if (err !=in_err)    {      return FALSE;    }  }  return TRUE;}/* Returns the number of connections whose status is status ie (connecting to the server specified), if server is NULL then it returns the total number of connections that are having the status which is equal the to the status specified */int download_query_conns_status_count(download_t * download,				      dl_status status, char *server){  int i;  int count = 0;  pthread_mutex_lock(&download->status_change_mutex);  for (i = 0; i < download->num_connections; i++)  {    if (download->pconnections[i]->status == status)    {      if (server == NULL	  || (strcasecmp(server, download->pconnections[i]->u.host) == 0))	count++;    }  }  pthread_mutex_unlock(&download->status_change_mutex);  return count;}void proz_download_set_msg_proc(download_t * download,				message_proc msg_proc, void *cb_data){  assert(download != NULL);  download->msg_proc = msg_proc;  download->cb_data = cb_data;}/*Returns the number of seconds left remaining in the download,if it cannot be calculated say if the file size of not known, it returns -1*/long proz_download_get_est_time_left(download_t * download){  long secs_left;  float average_speed;  long total_remote_bytes_got;  if (download->main_file_size == -1)    return -1;  total_remote_bytes_got = proz_download_get_total_bytes_got(download);  average_speed = proz_download_get_average_speed(download);  if (average_speed == 0)    return -1;  return secs_left =      (long) ((download->main_file_size -	       total_remote_bytes_got) / average_speed);}void proz_download_free_download(download_t * download, boolean complete){  assert(download);  /*TODO free the URL */  if (download->dl_dir)    kfree(download->dl_dir);  if (download->output_dir)    kfree(download->output_dir);  if (download->log_dir)    kfree(download->log_dir);  if (download->file_build_msg)    kfree(download->file_build_msg);  if (download->threads)    kfree(download->threads);  /*Now handle the freeing of the connections */  if (download->num_connections > 0 && download->pconnections)  {    int i;    for (i = 0; i < download->num_connections; i++)    {      proz_connection_free_connection(download->pconnections[i], 0);    }    kfree(download->pconnections);  }  if (complete == TRUE)    kfree(download);}/*This will delete the joining file if the downlaod is cancelled */void cleanup_joining_thread(void *cdata){  download_t *download = (download_t *) cdata;  char out_file_name[PATH_MAX];  proz_debug("in cleanup_joining_thread\n");  snprintf(out_file_name, PATH_MAX, "%s/%s", download->output_dir,	   download->u.file);  if (unlink(out_file_name) == -1)  {    /*     * if the file is not present the continue silently      */    if (errno != ENOENT)      proz_debug(_("unable to delete the file %s. Reason-: %s"),		 out_file_name, strerror(errno));  }  return;}void download_calc_throttle_factor(download_t * download){  int i;  int num_slow_cons = 0;  long t_slow_rates = 0;  long limit_high_cons_rate;  long avg_rate;  int num_dl_cons =      download_query_conns_status_count(download, DOWNLOADING, NULL);  if (num_dl_cons == 0)    return;  avg_rate = download->max_allowed_bps / num_dl_cons;  if (download->max_allowed_bps == 0)  {    for (i = 0; i < download->num_connections; i++)    {      pthread_mutex_lock(&(download->pconnections[i]->access_mutex));      download->pconnections[i]->max_allowed_bps = 0;      pthread_mutex_unlock(&(download->pconnections[i]->access_mutex));    }    return;  }  /*MAKE IR USE THE NUMBER OF ACTIVE DOWNLOAdING CONENCTIONS: Done */  for (i = 0; i < download->num_connections; i++)  {    pthread_mutex_lock(&(download->pconnections[i]->access_mutex));    if ((proz_connection_get_status(download->pconnections[i]) ==	 DOWNLOADING) && download->pconnections[i]->rate_bps < avg_rate)    {      t_slow_rates += download->pconnections[i]->rate_bps;      num_slow_cons++;    }    pthread_mutex_unlock(&(download->pconnections[i]->access_mutex));  }  /*fixme mutex to preven this conenctions */  if (num_slow_cons > num_dl_cons)    num_dl_cons = num_slow_cons;  /*If all the connections are slower then no need to do anything */  if (num_slow_cons == num_dl_cons)  {    for (i = 0; i < download->num_connections; i++)    {      pthread_mutex_lock(&(download->pconnections[i]->access_mutex));      download->pconnections[i]->max_allowed_bps = 0;      pthread_mutex_unlock(&(download->pconnections[i]->access_mutex));    }    return;  }  limit_high_cons_rate =      (download->max_allowed_bps - t_slow_rates) / (num_dl_cons -						    num_slow_cons);  /*     proz_debug("total slow connections = %ld", num_slow_cons);     proz_debug("total slow rates = %ld", t_slow_rates);     proz_debug("limit_high_cons_rate = %ld", limit_high_cons_rate);   */  for (i = 0; i < download->num_connections; i++)  {    pthread_mutex_lock(&(download->pconnections[i]->access_mutex));    if ((proz_connection_get_status(download->pconnections[i]) ==	 DOWNLOADING) && download->pconnections[i]->rate_bps >= avg_rate)    {      download->pconnections[i]->max_allowed_bps = limit_high_cons_rate;    }    pthread_mutex_unlock(&(download->pconnections[i]->access_mutex));  }}/*This function will check if the target output file, ie: the file that we are going to rebuild to exists. Returns, 1= file exists 0 = file does not exists -1 = error, ie cant have permissions to stat the file etc etc*/int proz_download_target_exist(download_t * download){  char out_file_name[PATH_MAX];  struct stat st_buf;  int ret;  snprintf(out_file_name, PATH_MAX, "%s/%s", download->output_dir,	   download->u.file);  ret = stat(out_file_name, &st_buf);  if (ret == -1)  {    if (errno == ENONET)      return 0;    else      return -1;  }  /*File was statable so it exists */  return 1;}/*This function will delete the target output file, Returns, 1= sucessfully delted the file 0 = file does not exist -1 = error, ie cant have permissions to delete the file etc etc*/int proz_download_delete_target(download_t * download){  char out_file_name[PATH_MAX];  int ret;  snprintf(out_file_name, PATH_MAX, "%s/%s", download->output_dir,	   download->u.file);  ret = remove(out_file_name);  if (ret == -1)  {    if (errno == ENONET)      return 0;    else      return -1;  }  /*File was statable so it exists */  return 1;}/* returns  one of  DLINPROGRESS, DLERR, DLDONE, DLREMOTEFATAL, DLLOCALFATAL*/uerr_t download_handle_threads_ftpsearch(download_t * download){  int i;  for (i = 0; i < download->num_connections; i++)  {    /*Set the DL start time if it is not done so */    pthread_mutex_lock(download->pconnections[i]->status_change_mutex);    if (download->pconnections[i]->status == DOWNLOADING	&& download->start_time.tv_sec == 0	&& download->start_time.tv_usec == 0)    {      gettimeofday(&download->start_time, NULL);    }    pthread_mutex_unlock(download->pconnections[i]->status_change_mutex);  }  /*If all the connections are completed then end them, and return complete */  if ((proz_download_all_dls_status(download, COMPLETED)) == TRUE)  {      char * out_filename;      char * orig_filename;    download_show_message(download,			  "All the conenctions have retreived the file"			  "..waiting for them to end");    proz_download_wait_till_all_end(download);    download_show_message(download, "All the threads have being ended.");      /*Close and rename file to original */      flockfile(download->pconnections[0]->fp);      fclose(download->pconnections[0]->fp);      funlockfile(download->pconnections[0]->fp);      out_filename=kmalloc(PATH_MAX);      orig_filename=kmalloc(PATH_MAX);      snprintf(orig_filename, PATH_MAX, "%s/%s",	       download->dl_dir, download->pconnections[0]->u.file);      snprintf(out_filename, PATH_MAX, "%s/%s.prozilla",	       download->dl_dir, download->pconnections[0]->u.file);      if(rename(out_filename, orig_filename)==-1)	{	  download_show_message(download, "Error While attempting to rename the file: %s", strerror(errno));	}      download_show_message(download, "Successfully renamed file");      /*Delete the logfile as we dont need it now */      if(proz_log_delete_logfile(download)!=1)	download_show_message(download, "Error: Unable to delete the logfile: %s", strerror(errno));    return DLDONE;  }  /*TODO handle restartable connections */  for (i = 0; i < download->num_connections; i++)  {    dl_status status;    uerr_t connection_err;    pthread_mutex_lock(download->pconnections[i]->status_change_mutex);    status = download->pconnections[i]->status;    pthread_mutex_unlock(download->pconnections[i]->status_change_mutex);    pthread_mutex_lock(&download->pconnections[i]->access_mutex);    connection_err = download->pconnections[i]->err;    pthread_mutex_unlock(&download->pconnections[i]->access_mutex);    switch (status)    {    case MAXTRYS:      break;    case REMOTEFATAL:      /* handle the CANTRESUME err code */      if (connection_err == CANTRESUME)      {	/*Terminate the connections */	proz_download_stop_downloads(download);	/*FIXME Do we delete any downloaded portions here ? */	return CANTRESUME;      } else /*Handle the file not being found on the server */	if (connection_err == FTPNSFOD || connection_err == HTTPNSFOD)      {	if (proz_download_all_dls_filensfod(download) == TRUE)	{	  download_show_message(download,				_				("The file was not found in all the connections!"));	  /*Terminate the connections */	  proz_download_stop_downloads(download);	  return DLREMOTEFATAL;	} else	{	  /*Now we have to be careful */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -