Remove cat and rm commands. Few corrections with ifdef macros.

This commit is contained in:
Ilim Ugur 2012-08-01 20:35:25 +03:00
parent 11d09dd001
commit 8cfac52713
7 changed files with 81 additions and 63 deletions

View File

@ -41,8 +41,9 @@ as that of the covered work. */
#include <locale.h>
#ifdef ENABLE_THREADS
#include <pthread.h>
#endif
# include "multi.h"
#endif
#include "hash.h"
#include "http.h"
#include "utils.h"
@ -70,8 +71,6 @@ as that of the covered work. */
# include "vms.h"
#endif /* def __VMS */
# include "multi.h"
extern char *version_string;
/* Forward decls. */
@ -3722,7 +3721,7 @@ create_authorization_line (const char *au, const char *user,
return digest_authentication_encode (au, user, passwd, method, path);
#endif
#ifdef ENABLE_NTLM && ENABLE_THREADS
#if defined ENABLE_NTLM && defined ENABLE_THREADS
case 'N': /* NTLM */
if (!ntlm_input (&pconn.ntlm, au))
{

View File

@ -31,7 +31,7 @@ as that of the covered work. */
#ifndef HTTP_H
#define HTTP_H
#include "multi.h"
#include "wget.h"
struct url;

View File

@ -1,12 +1,11 @@
#include "wget.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#ifdef ENABLE_THREADS
#include <pthread.h>
#include <semaphore.h>
#endif
#include "wget.h"
#include <unistd.h>
#include "multi.h"
#include "url.h"
@ -14,8 +13,7 @@
int
spawn_thread (struct s_thread_ctx *thread_ctx, char * name, int index, int resource)
{
static pthread_t *thread;
char *command;
static pthread_t thread;
sprintf(thread_ctx[index].file, TEMP_PREFIX "%s.%d", name, index);
@ -24,18 +22,6 @@ spawn_thread (struct s_thread_ctx *thread_ctx, char * name, int index, int resou
if(!thread_ctx[index].url_parsed)
return 1;
/* TODO: Update this when configuring fallbacking code so that downloading
goes on from where the previous resource failed. */
if(file_exists_p(thread_ctx[index].file))
{
command = malloc(sizeof("rm -f ") + sizeof(TEMP_PREFIX) + strlen(name)
+ (sizeof(".")-1) + (opt.jobs/10 + 1) + sizeof(""));
sprintf(command, "rm -f " TEMP_PREFIX "%s.%d", name, index);
system(command);
free(command);
}
(thread_ctx[index].range)->is_assigned = 1;
(thread_ctx[index].range)->resources[resource] = true;
thread_ctx[index].used = 1;
@ -75,3 +61,50 @@ segmented_retrieve_url (void *arg)
ctx->terminated = 1;
sem_post (ctx->retr_sem);
}
void
merge_temp_files(const char *file, int numfiles)
{
FILE *out, *in;
char *file_name = malloc(strlen("temp_") + strlen(file) + (sizeof ".")-1
+ (numfiles/10 + 1) + sizeof "");
int j, ret;
void *buf = malloc(MIN_CHUNK_SIZE);
/* FIXME: Check for errors in allocations. */
sprintf(file_name, "%s", file);
out = fopen(file_name,"w");
for(j = 0; j < numfiles; ++j)
{
sprintf(file_name, TEMP_PREFIX "%s.%d", file, j);
in = fopen(file_name,"r");
ret = MIN_CHUNK_SIZE;
while(ret == MIN_CHUNK_SIZE)
{
ret = fread(buf, 1, MIN_CHUNK_SIZE, in);
fwrite(buf, 1, ret, out);
/* FIXME: CHECK FOR ERRORS. */
}
fclose(in);
}
fclose(out);
free(buf);
free(file_name);
}
void
delete_temp_files(const char *file, int numfiles)
{
char *file_name = malloc(strlen("temp_") + strlen(file) + (sizeof ".")-1
+ (numfiles/10 + 1) + sizeof "");
int j = 0;
while(j < numfiles)
{
sprintf(file_name, TEMP_PREFIX "%s.%d", file, j++);
unlink(file_name);
/* FIXME: CHECK FOR ERRORS. */
}
free(file_name);
}

View File

@ -40,9 +40,6 @@ as that of the covered work. */
#define TEMP_PREFIX "temp_"
#define FILENAME_SIZE strlen("temp_") + strlen(file->name) + (sizeof ".")-1 \
+ (N_THREADS/10 + 1) + sizeof ""
#define MIN_CHUNK_SIZE 2048
struct s_thread_ctx
@ -70,4 +67,10 @@ collect_thread (sem_t *, struct s_thread_ctx *);
static void *
segmented_retrieve_url (void *);
void
merge_temp_files(const char *, int);
void
delete_temp_files(const char *, int);
#endif /* MULTI_H */

View File

@ -40,8 +40,9 @@ as that of the covered work. */
#ifdef ENABLE_THREADS
#include <pthread.h>
#include <semaphore.h>
#endif
#include "multi.h"
#endif
#include "url.h"
#include "recur.h"
#include "utils.h"
@ -54,7 +55,6 @@ as that of the covered work. */
#include "html-url.h"
#include "css-url.h"
#include "spider.h"
#include "multi.h"
/* Functions for maintaining the URL queue. */

View File

@ -37,14 +37,19 @@ as that of the covered work. */
#include <errno.h>
#include <string.h>
#include <assert.h>
#ifdef ENABLE_METALINK
#ifdef ENABLE_THREADS
#include <pthread.h>
#include <semaphore.h>
#endif
#ifdef ENABLE_METALINK
#include <metalink/metalink_parser.h>
#include <metalink/metalink_types.h>
#include "metalink.h"
#endif
#ifdef ENABLE_THREADS
#include "multi.h"
#endif
#include "exits.h"
#include "utils.h"
#include "retr.h"
@ -60,8 +65,6 @@ as that of the covered work. */
#include "ptimer.h"
#include "html-url.h"
#include "iri.h"
#include "metalink.h"
#include "multi.h"
#ifdef ENABLE_METALINK
static pthread_mutex_t pconn_mutex = PTHREAD_MUTEX_INITIALIZER;
@ -955,8 +958,6 @@ retrieve_from_file (const char *file, bool html, int *count)
uerr_t status;
struct urlpos *url_list, *cur_url;
struct iri *iri = iri_new();
metalink_t *metalink;
char *input_file, *url_file = NULL;
const char *url = file;
@ -1010,10 +1011,13 @@ retrieve_from_file (const char *file, bool html, int *count)
input_file = (char *) file;
#ifdef ENABLE_METALINK
metalink_t *metalink;
if(metalink = metalink_context(input_file))
{
/*GSoC wget*/
char *file_name, *command, **files;
char *file_name, **files;
FILE *file1, *file2;
int i, j, r, index, dt, url_err, error_severity;
int ret, N_THREADS = opt.jobs > 0 ? opt.jobs : 1;
int ranges_covered, chunk_size, num_of_resources;
@ -1058,7 +1062,12 @@ retrieve_from_file (const char *file, bool html, int *count)
N_THREADS = j;
for (j = 0; j < N_THREADS; ++j)
files[j] = malloc(FILENAME_SIZE);
files[j] = malloc(strlen("temp_") + strlen(file->name)
+ (sizeof ".")-1 + (N_THREADS/10 + 1) + sizeof "");
/* To make sure temporary files in which the segments are downloaded
do not become corrupt, as wget appends while writing into files.
delete_temp_files(file->name, N_THREADS);*/
sem_init (&retr_sem, 0, 0);
j = ranges_covered = 0;
@ -1161,13 +1170,6 @@ retrieve_from_file (const char *file, bool html, int *count)
sem_destroy(&retr_sem);
command = malloc(sizeof("cat ")
+ N_THREADS * (FILENAME_SIZE + (sizeof(" ") - 1))
+ (sizeof("> ") - 1)
+ strlen(file->name)
+ sizeof(""));
file_name = malloc(FILENAME_SIZE + sizeof(" ") - 1);
if (status != RETROK)
{
/* Segment r could not be downloaded due to
@ -1176,29 +1178,11 @@ retrieve_from_file (const char *file, bool html, int *count)
else
{
++*count;
strcpy(command, "cat ");
j=0;
while(j < opt.jobs)
{
sprintf(file_name, TEMP_PREFIX "%s.%d ", file->name, j++);
strcat(command, file_name);
}
strcat(command, "> ");
strcat(command, file->name);
system(command);
merge_temp_files(file->name, N_THREADS);
}
strcpy(command, "rm -f ");
j=0;
while(j < opt.jobs)
{
sprintf(file_name, TEMP_PREFIX "%s.%d ", file->name, j++);
strcat(command, file_name);
}
system(command);
delete_temp_files(file->name, N_THREADS);
free(file_name);
free(command);
for (j = 0; j < N_THREADS; ++j)
free(ranges[j].resources);
for (j = 0; j < N_THREADS; ++j)

View File

@ -32,7 +32,6 @@ as that of the covered work. */
#ifndef RETR_H
#define RETR_H
#include "multi.h"
#include "url.h"
/* These global vars should be made static to retr.c and exported via