-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource_Code.c
More file actions
115 lines (90 loc) · 2.9 KB
/
Source_Code.c
File metadata and controls
115 lines (90 loc) · 2.9 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <pthread.h>
#define Threads 4
typedef struct {
const char *url;
long start;
long end;
int tid;
const char *fname;
pthread_mutex_t *file_lock;
} DownloadTask;
size_t write_data(void *ptr, size_t size, size_t nmemb, void *data) {
DownloadTask *task = (DownloadTask *)data;
size_t total_size = size * nmemb;
pthread_mutex_lock(task->file_lock);
FILE *fp = fopen(task->fname, "rb+");
if (fp) {
fseek(fp, task->start, SEEK_SET);
fwrite(ptr, 1, total_size, fp);
fclose(fp);
}
pthread_mutex_unlock(task->file_lock);
return total_size;
}
void *download_part(void *arg) {
DownloadTask *task = (DownloadTask *)arg;
CURL *curl = curl_easy_init();
char range[64];
if (!curl) {
fprintf(stderr, "Thread %d: Failed to init curl\n", task->tid);
return NULL;
}
snprintf(range, sizeof(range), "%ld-%ld", task->start, task->end);
curl_easy_setopt(curl, CURLOPT_URL, task->url);
curl_easy_setopt(curl, CURLOPT_RANGE, range);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, task);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "Thread %d: Download failed: %s\n", task->tid, curl_easy_strerror(res));
} else {
printf("Thread %d: Finished downloading bytes %s\n", task->tid, range);
}
curl_easy_cleanup(curl);
return NULL;
}
void start_download(const char *url, const char *fname, long fsize) {
pthread_t threads[Threads];
DownloadTask tasks[Threads];
pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
FILE *fp = fopen(fname, "wb");
if (!fp) {
perror("File open failed");
return;
}
fseek(fp, fsize - 1, SEEK_SET);
fputc('\0', fp);
fclose(fp);
long part_size = fsize / Threads;
for (int i = 0; i < Threads; i++) {
tasks[i].url = url;
tasks[i].start = i * part_size;
tasks[i].end = (i == Threads - 1) ? fsize - 1 : (i + 1) * part_size - 1;
tasks[i].tid = i;
tasks[i].fname = fname;
tasks[i].file_lock = &file_lock;
pthread_create(&threads[i], NULL, download_part, &tasks[i]);
}
for (int i = 0; i < Threads; i++) {
pthread_join(threads[i], NULL);
}
printf("? Download complete: %s\n", fname);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <URL>\n", argv[0]);
return 1;
}
const char *url = argv[1];
const char *fname = "downloaded_file.bin";
long fsize = 1024L * 1024 * 1024;
printf("Starting download: %s (%ld bytes)\n", url, fsize);
start_download(url, fname, fsize);
return 0;
}