-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.c
163 lines (134 loc) · 4.48 KB
/
file.c
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
#include "file.h"
#include "download.h"
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
BOOL download_file(struct url_info *url, int noOfThreads) {
struct thread_data *wthreads;
int rc, sd, i;
struct hostent *h;
struct sockaddr_in servAddr;
long rate;
/*FILE* fd;*/
int fd;
long start_file, finish_file, total_file;
time_t start_time, finish_time;
char tmp[BUF_SIZE];
char buffer[BUFFER_SIZE];
printf("\tThe downloading file startted\n");
#ifdef DEBUG
printf("\t[File.c ] download_file basi\n");
#endif
if (!url) {
#ifdef DEBUG
printf("\t[File.c ] The url is not defined yet \n");
#endif
return FALSE;
}
if (noOfThreads < 1) return FALSE;
#ifdef DEBUG
printf("\t[File.c ] No of threads:%d\n",noOfThreads);
#endif
wthreads = (struct thread_data *) malloc(noOfThreads * sizeof(struct thread_data));
if (!wthreads) {
#ifdef DEBUG
printf("\t[File.c ] The memory is full \n");
#endif
return FALSE;
}
#ifdef DEBUG
printf("\t[File.c ] Wthreads was created\n");
#endif
if ((h = gethostbyname(url->host)) == NULL) {
#ifdef DEBUG
printf("\t[File.c ] The hostname is invalid\n");
printf(">> The hostname : \n",url->host);
#endif
return FALSE;
}
#ifdef DEBUG
printf("\t[File.c ] gethostbyname\n");
#endif
servAddr.sin_family = (sa_family_t) h->h_addrtype;
memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], (size_t) h->h_length);
servAddr.sin_port = htons(80);
total_file = head_get_file_length(url);
if (total_file < 0) {
#ifdef DEBUG
printf("\t[File.c ]Some problem while getting file length\n");
#endif
return FALSE;
}
#ifdef DEBUG
printf("\t[File.c ] start threads File-Length:%ld \n\n",total_file);
#endif
time(&start_time);
rate = total_file / noOfThreads;
finish_file = 0L;
for (i = 0; i < noOfThreads; i++) {
start_file = finish_file + 1L;
if (i == (noOfThreads - 1)) finish_file = total_file;
else finish_file = (i + 1) * rate;
#ifdef DEBUG
printf("\t[File.c ] url->identifer : %s --- url->host : %s\n",url->identifer,url->host);
#endif
sprintf(wthreads[i].request, GETREQ1, url->identifer, url->host, start_file, finish_file);
memcpy(&(wthreads[i].sin), &servAddr, sizeof(wthreads[i].sin));
wthreads[i].id = i + 1;
strcpy(wthreads[i].dir, "\0");
strcpy(wthreads[i].host, url->host);
strcpy(wthreads[i].identifer, url->identifer);
wthreads[i].type = TYPE_FILES;
wthreads[i].status = STATUS_STARTED;
strncpy(tmp, url->file, 3);
sprintf(wthreads[i].file, "%s_%d", tmp, wthreads[i].id);
#ifdef DEBUG
printf("\t[File.c ] Getting file info >>> \n");
printf(">> The request : %s\n",wthreads[i].request);
printf(">> The ID : %d\n",wthreads[i].id);
printf(">> The host : %s\n",wthreads[i].host);
printf(">> The dir : %s\n",wthreads[i].dir);
printf(">> The file : %s\n",wthreads[i].file);
printf(">> The Rate : %d\n",rate);
printf(">> The start_fi: %ld\n",start_file);
printf(">> The finish_f: %ld\n\n",finish_file);
sleep(1);
#endif
progress_bar((float) finish_file, (float) total_file);
pthread_create(&(wthreads[i].thread), NULL, downloader, (void *) &wthreads[i]);
}
for (i = 0; i < noOfThreads; i++) {
pthread_join(wthreads[i].thread, NULL);
}
time(&finish_time);
printf("\tAll downloaded finished in %ld secs \n", ((int) finish_time - start_time));
printf("\tFile is organizing \n");
if (strlen(url->file) < 1) strcpy(url->file, "down.tmp");
time(&start_time);
rc = 0;
/*fd = fopen(url->file,"w+b");*/
fd = open(url->file, O_WRONLY | O_CREAT | O_TRUNC, 0x777);
for (i = 0; i < noOfThreads; i++) {
sd = open(wthreads[i].file, O_RDONLY);
if (sd < 0) break;
do {
memset(buffer, 0x0, BUFFER_SIZE);
rc = (int) read(sd, buffer, BUFFER_SIZE);
#ifdef DEBUGX
printf("The buffer : %s\n",buffer);
#endif
if (rc > 0) {
write(fd, buffer, rc);
}
} while (rc > 0);
close(sd);
remove(wthreads[i].file);
}
close(fd);
time(&finish_time);
printf("\tFile organizing finished in %ld sec\n", (finish_time - start_time));
#ifdef DEBUG
printf("\t[File.c ] file connection\n");
#endif
return TRUE;
}