|
| 1 | + |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
| 5 | +#include <pthread.h> |
| 6 | +#include <unistd.h> |
| 7 | +#include <time.h> |
| 8 | +#include <fcntl.h> |
| 9 | + |
| 10 | +#include <sys/stat.h> |
| 11 | +#include <sys/types.h> |
| 12 | +#include <sys/socket.h> |
| 13 | + |
| 14 | +#include <netinet/in.h> |
| 15 | + |
| 16 | +#include <arpa/inet.h> |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +#include "Head.h" |
| 21 | +#include "Aget.h" |
| 22 | +#include "Misc.h" |
| 23 | +#include "Download.h" |
| 24 | +#include "Resume.h" |
| 25 | +#include "Data.h" |
| 26 | + |
| 27 | + |
| 28 | +extern struct thread_data *wthread; |
| 29 | +extern struct request *req; |
| 30 | +extern int fsuggested, nthreads; |
| 31 | +extern int bwritten; |
| 32 | +extern pthread_t hthread; |
| 33 | +#include <errno.h> |
| 34 | +extern int errno; |
| 35 | + |
| 36 | + |
| 37 | +void get(struct request *req) |
| 38 | +{ |
| 39 | + int i, ret, fd, diff_sec, nok = 0; |
| 40 | + long long soffset, foffset; |
| 41 | + char *fmt; |
| 42 | + |
| 43 | + if (req->proto == PROTO_HTTP) |
| 44 | + http_head_req(req); |
| 45 | + |
| 46 | + /* According to the content-length, get the |
| 47 | + * suggested number of threads to open. |
| 48 | + * if the user insists on his value, let it be that way, |
| 49 | + * use the user's value. |
| 50 | + */ |
| 51 | + ret = numofthreads(req->clength); |
| 52 | + |
| 53 | + if (fsuggested == 0) { |
| 54 | + if (ret == 0) |
| 55 | + nthreads = 1; |
| 56 | + else |
| 57 | + nthreads = ret; |
| 58 | + } |
| 59 | + |
| 60 | + wthread = (struct thread_data *)malloc(nthreads * sizeof(struct thread_data)); |
| 61 | + |
| 62 | + Log("Downloading %s (%lld bytes) from site %s(%s:%d). Number of Threads: %d", |
| 63 | + req->url, req->clength, req->host, req->ip, req->port, nthreads); |
| 64 | + |
| 65 | + if (strlen(req->lfile) != 0) { |
| 66 | + if ((fd = open(req->lfile, O_CREAT | O_RDWR, S_IRWXU)) == -1) { |
| 67 | + fprintf(stderr, "get: cannot open file %s for writing: %s\n", req->lfile, strerror(errno)); |
| 68 | + exit(1); |
| 69 | + } |
| 70 | + |
| 71 | + } else { |
| 72 | + if ((fd = open(req->file, O_CREAT | O_RDWR, S_IRWXU)) == -1) { |
| 73 | + fprintf(stderr, "get: cannot open file %s for writing: %s\n", req->lfile, strerror(errno)); |
| 74 | + exit(1); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if ((lseek(fd, req->clength - 1, SEEK_SET)) == -1) { |
| 79 | + fprintf(stderr, "get: couldn't lseek: %s\n", strerror(errno)); |
| 80 | + exit(1); |
| 81 | + } |
| 82 | + |
| 83 | + if ((write(fd, "0", 1)) == -1) { |
| 84 | + fprintf(stderr, "get: couldn't allocate space for download file: %s\n", strerror(errno)); |
| 85 | + exit(1); |
| 86 | + } |
| 87 | + |
| 88 | + /* Get the starting time, prepare GET format string, and start the threads */ |
| 89 | + fmt = (char *)calloc(GETREQSIZ - 2, sizeof(char)); |
| 90 | + time(&t_start); |
| 91 | + for (i = 0; i < nthreads; i++) { |
| 92 | + soffset = calc_offset(req->clength, i, nthreads); |
| 93 | + foffset = calc_offset(req->clength, i + 1, nthreads); |
| 94 | + wthread[i].soffset = soffset; |
| 95 | + wthread[i].foffset = (i == nthreads - 1 ? req->clength : foffset); |
| 96 | + wthread[i].sin.sin_family = AF_INET; |
| 97 | + wthread[i].sin.sin_addr.s_addr = inet_addr(req->ip); |
| 98 | + wthread[i].sin.sin_port = htons(req->port); |
| 99 | + wthread[i].fd = dup(fd); |
| 100 | + wthread[i].clength = req->clength; |
| 101 | + snprintf(fmt, GETREQSIZ, GETREQ, req->url, req->host, PROGVERSION, soffset); |
| 102 | + strncpy(wthread[i].getstr, fmt, GETREQSIZ); |
| 103 | + pthread_create(&(wthread[i].tid), NULL, http_get, &(wthread[i])); |
| 104 | + } |
| 105 | + free(fmt); |
| 106 | + |
| 107 | + |
| 108 | + /* Wait for all of the threads to finish... |
| 109 | + * |
| 110 | + * TODO: If a thread fails, restart that! |
| 111 | + */ |
| 112 | + for (i = 0; i < nthreads; i++) { |
| 113 | + pthread_join(wthread[i].tid, NULL); |
| 114 | + if (wthread[i].status == STAT_OK) |
| 115 | + nok++; |
| 116 | + } |
| 117 | + |
| 118 | + if (nok == nthreads) |
| 119 | + pthread_cancel(hthread); |
| 120 | + else |
| 121 | + pthread_join(hthread, NULL); |
| 122 | + |
| 123 | + /* Get the finish time, derive some stats */ |
| 124 | + time(&t_finish); |
| 125 | + if ((diff_sec = t_finish - t_start) == 0) |
| 126 | + diff_sec = 1; /* Avoid division by zero */ |
| 127 | + |
| 128 | + Log("Download completed, job completed in %d seconds. (%lld Kb/sec)", |
| 129 | + diff_sec, (req->clength / diff_sec) / 1024); |
| 130 | + Log("Shutting down..."); |
| 131 | + close(fd); |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +void resume_get(struct hist_data *h) |
| 136 | +{ |
| 137 | + int i, fd, diff_sec, nok = 0; |
| 138 | + char *fmt; |
| 139 | + |
| 140 | + nthreads = h->nthreads; |
| 141 | + |
| 142 | + fmt = (char *)calloc(GETREQSIZ - 2, sizeof(char)); |
| 143 | + |
| 144 | + wthread = (struct thread_data *)malloc(nthreads * sizeof(struct thread_data)); |
| 145 | + memcpy(req, &h->req, sizeof(struct request)); |
| 146 | + memcpy(wthread, h->wthread, sizeof(struct thread_data) * nthreads); |
| 147 | + |
| 148 | + Log("Resuming download %s (%lld bytes) from site %s(%s:%d). Number of Threads: %d", |
| 149 | + req->url, req->clength, req->host, req->ip, req->port, nthreads); |
| 150 | + |
| 151 | + if (strlen(req->lfile) != 0) { |
| 152 | + if ((fd = open(req->lfile, O_RDWR, S_IRWXU)) == -1) { |
| 153 | + fprintf(stderr, "get: cannot open file %s for writing: %s\n", req->lfile, strerror(errno)); |
| 154 | + exit(1); |
| 155 | + } |
| 156 | + |
| 157 | + } else { |
| 158 | + if ((fd = open(req->file, O_RDWR, S_IRWXU)) == -1) { |
| 159 | + fprintf(stderr, "get: cannot open file %s for writing: %s\n", req->lfile, strerror(errno)); |
| 160 | + exit(1); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + time(&t_start); |
| 165 | + |
| 166 | + |
| 167 | +#ifdef DEBUG |
| 168 | + for (i = 0; i < nthreads; i++) |
| 169 | + printf("Start: %lld, Finish: %lld, Offset: %lld, Diff: %lld\n", |
| 170 | + wthread[i].soffset, |
| 171 | + wthread[i].foffset, |
| 172 | + wthread[i].offset, |
| 173 | + wthread[i].offset - wthread[i].soffset); |
| 174 | +#endif |
| 175 | + |
| 176 | + for (i = 0; i < nthreads; i++) { |
| 177 | + wthread[i].soffset = wthread[i].offset; |
| 178 | + wthread[i].fd = dup(fd); |
| 179 | + snprintf(fmt, GETREQSIZ, GETREQ, req->url, req->host, PROGVERSION, wthread[i].offset); |
| 180 | + strncpy(wthread[i].getstr, fmt, GETREQSIZ); |
| 181 | + pthread_create(&(wthread[i].tid), NULL, http_get, &(wthread[i])); |
| 182 | + } |
| 183 | + |
| 184 | + for (i = 0; i < nthreads; i++) |
| 185 | + pthread_join(wthread[i].tid, NULL); |
| 186 | + |
| 187 | + for (i = 0; i < nthreads; i++) { |
| 188 | + pthread_join(wthread[i].tid, NULL); |
| 189 | + if (wthread[i].status == STAT_OK) |
| 190 | + nok++; |
| 191 | + } |
| 192 | + |
| 193 | + if (nok == nthreads) |
| 194 | + pthread_cancel(hthread); |
| 195 | + else |
| 196 | + pthread_join(hthread, NULL); |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + time(&t_finish); |
| 201 | + if ((diff_sec = t_finish - t_start) == 0) |
| 202 | + diff_sec = 1; /* Avoid division by zero */ |
| 203 | + |
| 204 | + Log("Download completed, job completed in %d seconds. (%lld Kb/sec)", |
| 205 | + diff_sec, ((req->clength - h->bwritten) / diff_sec) / 1024); |
| 206 | + Log("Shutting down..."); |
| 207 | + close(fd); |
| 208 | +} |
0 commit comments