summaryrefslogtreecommitdiff
path: root/src/multi.c
blob: 94d9811615167e45a583e811be10e87e211a365a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* Declarations for HTTP.
   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2014 Free Software
   Foundation, Inc.

This file is part of GNU Wget.

GNU Wget is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

GNU Wget is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Wget.  If not, see <http://www.gnu.org/licenses/>.

Additional permission under GNU GPL version 3 section 7

If you modify this program, or any covered work, by linking or
combining it with the OpenSSL project's OpenSSL library (or a
modified version of that library), containing parts covered by the
terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
grants you additional permission to convey the resulting work.
Corresponding Source for a non-source form of such a combination
shall include the source code for the parts of OpenSSL used as well
as that of the covered work.  */

#include "wget.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

#include "multi.h"
#include "url.h"
#include "exits.h"

static struct range *ranges;
char **files;

/*  Allocate space for temporary file names. */
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 (WGET_EXIT_GENERIC_ERROR);
    }
  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 (WGET_EXIT_GENERIC_ERROR);
      }
}

/*  Assign names to temporary files to be used. */
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 (WGET_EXIT_GENERIC_ERROR);
      }
}

/*  Merge the temporary files in which the chunks are stored to form the
    resulting file(output). */
void
merge_temp_files(char *output)
{
  FILE *out, *in;
  int j, ret;
  void *buf = malloc (MIN_CHUNK_SIZE);

  out = fopen (output, "wb");
  for(j = 0; j < opt.jobs; ++j)
    {
      in = fopen(files[j],"rb");
      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);
}

/* Delete the temporary files used. */
void
delete_temp_files()
{
  int j = 0;

  while(j < opt.jobs)
    unlink(files[j++]);
}

/* Clean the space allocated for temporary files data. */
void
clean_temp_files()
{
  int i;

  for (i = 0; i < opt.jobs; ++i)
    free (files[i]);
  free(files);
}

/* Allocate ranges array to store the ranges data. */
void
init_ranges()
{
  if(!(ranges = malloc (opt.jobs * (sizeof *ranges))))
    {
      logprintf (LOG_VERBOSE, "Space for ranges data could not be allocated.\n");
      exit (WGET_EXIT_GENERIC_ERROR);
    }
}

/* Assign values to the ranges.
   Also allocates the resources array each struct range must have.

   Returns the number of ranges to which values are assigned. */
int
fill_ranges_data(int num_of_resources, long long int file_size,
                 long int chunk_size)
{
  int i, r;
  i = 0;
  do
  {
      ranges[i].first_byte = i * chunk_size;
      ranges[i].last_byte = (i+1) * chunk_size - 1;
      ranges[i].bytes_covered = ranges[i].is_assigned = 0;
      ranges[i].resources = malloc(num_of_resources * sizeof(bool));
      ranges[i].status_least_severe = RETROK;
      for (r = 0; r < num_of_resources; ++r)
        ranges[i].resources[r] = false;
      ++i;
    } while (ranges[i-1].last_byte < (file_size - 1));
  ranges[i-1].last_byte = file_size -1;

  return i;
}

/* Free the resources array of each range allocated by fill_ranges_data(). */
void
clean_range_res_data()
{
  int i;
  for (i = 0; i < opt.jobs; ++i)
    free (ranges[i].resources);
}

/* Free the ranges array that is used for storing ranges' data. */
void
clean_ranges()
{
  free (ranges);
  ranges = NULL;
}

/* Assign 'last minute' data to struct s_thread_ctx instances regarding their
   usage and range information. Then create a thread using that instance. */
int
spawn_thread (struct s_thread_ctx *thread_ctx, int index, int resource)
{
  static pthread_t thread;

  thread_ctx[index].url_parsed = url_parse (thread_ctx[index].url,
                       &(thread_ctx[index].url_err), thread_ctx[index].i, true);
  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;

  thread_ctx[index].used = 1;
  thread_ctx[index].terminated = 0;

  return pthread_create (&thread, NULL, segmented_retrieve_url, &thread_ctx[index]);
}

/* Collects the first thread to terminate and updates struct s_thread_ctx
   instance's data regarding its 'business' (i.e. being used by a thread).

   Returns the index of the struct s_thread_ctx instance that was used in the
   terminating thread. */
int
collect_thread (sem_t *retr_sem, struct s_thread_ctx *thread_ctx)
{
  int k, ret;
  do
    ret = sem_wait (retr_sem);
  while (ret < 0 && errno == EINTR);

  for (k = 0; k < opt.jobs; k++)
    if (thread_ctx[k].used && thread_ctx[k].terminated)
      {
        url_free (thread_ctx[k].url_parsed);
        thread_ctx[k].used = 0;
        (thread_ctx[k].range)->is_assigned = 0;
        return k;
      }
}

/* The function which is being called by pthread_create in spawn_thread(). It
   is used to call retrieve_url(), which requires many arguments. */
static void *
segmented_retrieve_url (void *arg)
{
  struct s_thread_ctx *ctx = (struct s_thread_ctx *) arg;

  ctx->status = retrieve_url (ctx->url_parsed, ctx->url,
                              &ctx->file, &ctx->redirected,
                              ctx->referer, &ctx->dt,
                              false, ctx->i, true, ctx->range);
  ctx->terminated = 1;
  sem_post (ctx->retr_sem);
}