-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathretrieve.c
119 lines (98 loc) · 3.06 KB
/
retrieve.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <curl/curl.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
#define OUTPUT_DIR "/tmp/"
// Function prototype for copy_main
extern int copy_main(void);
// Function prototype for initialize_curl_handle
CURL *initialize_curl_handle(void);
// Function to write data to the file
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) {
size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
return written;
}
// Function to remove trailing whitespace from a string
void trim_trailing_whitespace(char *str) {
char *end = str + strlen(str) - 1;
while (end >= str && isspace((unsigned char)*end)) {
*end = '\0';
end--;
}
}
// Function to initialize curl handle with settings
CURL *initialize_curl_handle(void) {
CURL *curl_handle = curl_easy_init();
if (!curl_handle) {
fprintf(stderr, "Failed to initialize CURL\n");
return NULL;
}
// Set CURL options that don't change per request
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
return curl_handle;
}
int retrieve(const char *name, const char *url) {
CURL *curl_handle;
char gzip_path[1024];
FILE *pagefile;
// Create output file path
snprintf(gzip_path, sizeof(gzip_path), "%s%s.gz", OUTPUT_DIR, name);
// Initialize libcurl
curl_global_init(CURL_GLOBAL_ALL);
curl_handle = initialize_curl_handle();
if (!curl_handle) {
curl_global_cleanup();
return 1;
}
// Set CURL-specific options per request
curl_easy_setopt(curl_handle, CURLOPT_URL, url);
// Open file for writing
pagefile = fopen(gzip_path, "wb");
if (!pagefile) {
perror("Error opening output file");
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
return 1;
}
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);
// Perform the request
CURLcode res = curl_easy_perform(curl_handle);
if (res != CURLE_OK) {
fprintf(stderr, "CURL error: %s\n", curl_easy_strerror(res));
}
fclose(pagefile);
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
return 0;
}
int retrieve_main(void) {
FILE *config_file = fopen("/var/pfpb/config.txt", "r");
if (!config_file) {
perror("Error opening config file");
return 1;
}
// Ensure the output directory exists
if (access(OUTPUT_DIR, F_OK) != 0 && mkdir(OUTPUT_DIR, 0755) != 0) {
perror("Failed to create output directory");
fclose(config_file);
return 1;
}
char line[1024];
while (fgets(line, sizeof(line), config_file)) {
trim_trailing_whitespace(line);
char *url = strtok(line, ";");
char *name = strtok(NULL, ";\n");
if (url && name) {
retrieve(name, url);
} else {
fprintf(stderr, "Malformed line in config file: %s\n", line);
}
}
fclose(config_file);
copy_main();
return 0;
}