mirror of
https://github.com/mirror/wget.git
synced 2025-03-14 20:00:15 +08:00
Transfer handling temp files portion of the code to multi.c.
This commit is contained in:
parent
76790a386a
commit
a41525e5c2
111
src/multi.c
111
src/multi.c
@ -11,11 +11,86 @@
|
||||
#include "url.h"
|
||||
|
||||
static struct range *ranges;
|
||||
char *temp, **files;
|
||||
|
||||
void
|
||||
init_temp_files()
|
||||
{
|
||||
int i;
|
||||
|
||||
if(!(files = malloc (opt.jobs * (sizeof *files))))
|
||||
{
|
||||
logprintf (LOG_VERBOSE, "Space for temporary file data could not be allocated.\n");
|
||||
exit(1);
|
||||
}
|
||||
for (i = 0; i < opt.jobs; ++i)
|
||||
if(!(files[i] = malloc (L_tmpnam * sizeof(char))))
|
||||
{
|
||||
logprintf (LOG_VERBOSE, "Space for temporary file names could not be allocated.\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
name_temp_files()
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < opt.jobs; ++i)
|
||||
if(!tmpnam(files[i]))
|
||||
{
|
||||
logprintf (LOG_VERBOSE, "Temporary file name could not be assigned.\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
merge_temp_files(const char *output)
|
||||
{
|
||||
FILE *out, *in;
|
||||
int j, ret;
|
||||
void *buf = malloc (MIN_CHUNK_SIZE);
|
||||
|
||||
out = fopen (output, "w");
|
||||
for(j = 0; j < opt.jobs; ++j)
|
||||
{
|
||||
in = fopen(files[j],"r");
|
||||
ret = MIN_CHUNK_SIZE;
|
||||
while(ret == MIN_CHUNK_SIZE)
|
||||
{
|
||||
ret = fread(buf, 1, MIN_CHUNK_SIZE, in);
|
||||
fwrite(buf, 1, ret, out);
|
||||
}
|
||||
fclose(in);
|
||||
}
|
||||
fclose(out);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
void
|
||||
delete_temp_files()
|
||||
{
|
||||
int j = 0;
|
||||
|
||||
while(j < opt.jobs)
|
||||
unlink(files[j++]);
|
||||
}
|
||||
|
||||
void
|
||||
clean_temp_files()
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < opt.jobs; ++i)
|
||||
free (files[i]);
|
||||
free(files);
|
||||
}
|
||||
|
||||
void
|
||||
init_ranges()
|
||||
{
|
||||
ranges = malloc (opt.jobs * (sizeof *ranges));
|
||||
if(!(ranges = malloc (opt.jobs * (sizeof *ranges))))
|
||||
logprintf (LOG_VERBOSE, "Space for ranges data could not be allocated.\n");
|
||||
}
|
||||
|
||||
int
|
||||
@ -52,6 +127,7 @@ void
|
||||
clean_ranges()
|
||||
{
|
||||
free (ranges);
|
||||
ranges = NULL;
|
||||
}
|
||||
|
||||
int
|
||||
@ -64,6 +140,7 @@ spawn_thread (struct s_thread_ctx *thread_ctx, int index, int resource)
|
||||
if(!thread_ctx[index].url_parsed)
|
||||
return 1;
|
||||
|
||||
thread_ctx[index].file = files[index];
|
||||
thread_ctx[index].range = ranges + index;
|
||||
(thread_ctx[index].range)->is_assigned = 1;
|
||||
(thread_ctx[index].range)->resources[resource] = true;
|
||||
@ -104,35 +181,3 @@ segmented_retrieve_url (void *arg)
|
||||
ctx->terminated = 1;
|
||||
sem_post (ctx->retr_sem);
|
||||
}
|
||||
|
||||
void
|
||||
merge_temp_files(char **inputs, const char *output)
|
||||
{
|
||||
FILE *out, *in;
|
||||
int j, ret;
|
||||
void *buf = malloc (MIN_CHUNK_SIZE);
|
||||
|
||||
out = fopen (output, "w");
|
||||
for(j = 0; j < opt.jobs; ++j)
|
||||
{
|
||||
in = fopen(inputs[j],"r");
|
||||
ret = MIN_CHUNK_SIZE;
|
||||
while(ret == MIN_CHUNK_SIZE)
|
||||
{
|
||||
ret = fread(buf, 1, MIN_CHUNK_SIZE, in);
|
||||
fwrite(buf, 1, ret, out);
|
||||
}
|
||||
fclose(in);
|
||||
}
|
||||
fclose(out);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
void
|
||||
delete_temp_files(char **files)
|
||||
{
|
||||
int j = 0;
|
||||
|
||||
while(j < opt.jobs)
|
||||
unlink(files[j++]);
|
||||
}
|
||||
|
14
src/multi.h
14
src/multi.h
@ -56,6 +56,16 @@ struct s_thread_ctx
|
||||
uerr_t status;
|
||||
};
|
||||
|
||||
void init_temp_files();
|
||||
|
||||
void name_temp_files();
|
||||
|
||||
void merge_temp_files(const char *);
|
||||
|
||||
void delete_temp_files();
|
||||
|
||||
void clean_temp_files();
|
||||
|
||||
void init_ranges();
|
||||
|
||||
int fill_ranges_data(int, long long int, long int);
|
||||
@ -70,8 +80,4 @@ int collect_thread (sem_t *, struct s_thread_ctx *);
|
||||
|
||||
static void * segmented_retrieve_url (void *);
|
||||
|
||||
void merge_temp_files(char **, const char *);
|
||||
|
||||
void delete_temp_files(char **);
|
||||
|
||||
#endif /* MULTI_H */
|
||||
|
35
src/retr.c
35
src/retr.c
@ -1067,9 +1067,7 @@ retrieve_from_file (const char *file, bool html, int *count)
|
||||
|
||||
if(opt.metalink_file && (metalink = metalink_context(input_file)))
|
||||
{
|
||||
/*GSoC wget*/
|
||||
char *temp, **files;
|
||||
int i, j, r, index, dt, url_err, retries;
|
||||
int i, j, r, dt, url_err, retries;
|
||||
int ret;
|
||||
int ranges_covered, chunk_size, num_of_resources;
|
||||
pthread_t thread;
|
||||
@ -1079,7 +1077,7 @@ retrieve_from_file (const char *file, bool html, int *count)
|
||||
metalink_resource_t* resource;
|
||||
struct s_thread_ctx *thread_ctx;
|
||||
|
||||
files = malloc (opt.jobs * (sizeof *files));
|
||||
init_temp_files();
|
||||
init_ranges ();
|
||||
thread_ctx = malloc (opt.jobs * (sizeof *thread_ctx));
|
||||
|
||||
@ -1101,17 +1099,7 @@ retrieve_from_file (const char *file, bool html, int *count)
|
||||
if(j < opt.jobs)
|
||||
opt.jobs = j;
|
||||
|
||||
/* Assign temporary file names. */
|
||||
for (j = 0; j < opt.jobs; ++j)
|
||||
{
|
||||
files[j] = malloc(L_tmpnam * sizeof(char));
|
||||
temp = tmpnam(files[j]);
|
||||
if(!temp)
|
||||
{
|
||||
logprintf (LOG_VERBOSE, "Temp file name could not be assigned.\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
name_temp_files();
|
||||
|
||||
sem_init (&retr_sem, 0, 0);
|
||||
j = ranges_covered = 0;
|
||||
@ -1125,7 +1113,6 @@ retrieve_from_file (const char *file, bool html, int *count)
|
||||
resource = file->resources[j];
|
||||
}
|
||||
|
||||
thread_ctx[r].file = files[r];
|
||||
thread_ctx[r].referer = NULL;
|
||||
thread_ctx[r].redirected = NULL;
|
||||
thread_ctx[r].dt = dt;
|
||||
@ -1142,9 +1129,7 @@ retrieve_from_file (const char *file, bool html, int *count)
|
||||
free(thread_ctx);
|
||||
clean_range_res_data(num_of_resources);
|
||||
clean_ranges ();
|
||||
for (r = 0; r < opt.jobs; ++r)
|
||||
free(files[r]);
|
||||
free(files);
|
||||
clean_temp_files ();
|
||||
return URLERROR;
|
||||
}
|
||||
++j;
|
||||
@ -1199,9 +1184,7 @@ retrieve_from_file (const char *file, bool html, int *count)
|
||||
free(thread_ctx);
|
||||
clean_range_res_data(num_of_resources);
|
||||
clean_ranges ();
|
||||
for (r = 0; r < opt.jobs; ++r)
|
||||
free(files[r]);
|
||||
free(files);
|
||||
clean_temp_files ();
|
||||
return URLERROR;
|
||||
}
|
||||
}
|
||||
@ -1233,7 +1216,7 @@ retrieve_from_file (const char *file, bool html, int *count)
|
||||
{
|
||||
int res;
|
||||
|
||||
merge_temp_files(files, file->name);
|
||||
merge_temp_files(file->name);
|
||||
res = verify_file_hash(file->name, file->checksums);
|
||||
if(!res)
|
||||
{
|
||||
@ -1252,17 +1235,15 @@ retrieve_from_file (const char *file, bool html, int *count)
|
||||
}
|
||||
}
|
||||
|
||||
delete_temp_files(files);
|
||||
delete_temp_files();
|
||||
|
||||
clean_range_res_data(num_of_resources);
|
||||
for (j = 0; j < opt.jobs; ++j)
|
||||
free(files[j]);
|
||||
++i;
|
||||
}
|
||||
|
||||
free(thread_ctx);
|
||||
clean_ranges ();
|
||||
free(files);
|
||||
clean_temp_files ();
|
||||
metalink_delete(metalink);
|
||||
}
|
||||
else
|
||||
|
Loading…
Reference in New Issue
Block a user