-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatcher.c
353 lines (284 loc) · 7.64 KB
/
patcher.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include "patcher.h"
char sUpdatedVersion[MAX_LEN];
char sCurrentVersion[MAX_LEN];
char sDeviceType[6];
char patchDir[MAX_LEN];
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->memory = realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory == NULL)
{
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
void remove_newline(char *s)
{
while (*s)
{
if (*s == '\n')
{
*s = '\0';
}
s++;
}
}
int isUpdatedVersion()
{
FILE *fp = fopen(VERSION_FILE, "r");
if (fp == NULL)
{
printf("Error: could not open file %s", VERSION_FILE);
return 0;
}
size_t len = 0;
ssize_t read;
char *line = NULL;
read = getline(&line, &len, fp);
remove_newline(line);
strcpy(sDeviceType, line);
read = getline(&line, &len, fp);
strcpy(sCurrentVersion, line);
printf("device type: %s\n", sDeviceType);
printf("current version: %s\n", sCurrentVersion);
free(line);
fclose(fp);
char checkUrl[MAX_LINE];
sprintf(checkUrl, "http://%s:%s/check", SERVER_IP, PORT);
char *sCheckVersion = checkServer(checkUrl);
if (!strcmp(sCheckVersion, ""))
{
return 0;
}
if (!strcmp(sCurrentVersion, sCheckVersion))
{
printf("Same version\n");
return 0;
}
strcpy(sUpdatedVersion, sCheckVersion);
return 1;
}
char *checkServer(const char *url)
{
CURL *curl;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = malloc(1);
chunk.size = 0;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
return "";
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
printf("%lu bytes retrieved\n", (unsigned long)chunk.size);
printf("%s\n", chunk.memory);
// free(chunk.memory);
return chunk.memory;
}
void downloadFile(const char *url)
{
CURL *curl;
FILE *fp;
CURLcode res;
char outfilename[MAX_LEN] = "";
strcpy(outfilename, sUpdatedVersion);
strcat(outfilename, ".zip");
curl = curl_easy_init();
if (curl)
{
fp = fopen(outfilename, "wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
}
void extractZip(const char *zipfile)
{
if (!fopen(zipfile, "r"))
return;
int errorp = 0;
zip_t *arch = NULL;
arch = zip_open(zipfile, 0, &errorp);
struct zip_stat *finfo = NULL;
finfo = calloc(256, sizeof(int));
zip_stat_init(finfo);
zip_file_t *fd = NULL;
int index = 0;
char *outp = NULL;
char extractedFile[MAX_LEN];
while (zip_stat_index(arch, index, 0, finfo) == 0)
{
fd = zip_fopen_index(arch, index, 0);
outp = calloc(finfo->size + 1, sizeof(char));
zip_fread(fd, outp, finfo->size);
zip_fclose(fd);
sprintf(extractedFile, "%s/%s", TMP_PATH, finfo->name);
int len = strlen(finfo->name);
printf("filename : %s\n", finfo->name);
if (finfo->name[len - 1] == '/')
{
mkdir(extractedFile, 0777);
index++;
}
else
{
FILE *fp = fopen(extractedFile, "w");
if (fp == NULL)
{
printf("Error: Failed to open file\n");
return;
}
fwrite(outp, sizeof(char), finfo->size, fp);
fclose(fp);
free(outp);
index++;
}
}
free(finfo);
zip_close(arch);
if (remove(zipfile) == 0)
{
printf("ZipFile deleted successfully.\n");
}
else
{
printf("Error deleting zip file.\n");
}
}
void copy_files(const char *source_file, const char *destination_file)
{
// Check if destination directory exists, create it if it doesn't
char *dir = strdup(destination_file);
char *last_slash = strrchr(dir, '/');
if (last_slash != NULL)
{
*last_slash = '\0';
struct stat st = {0};
if (stat(dir, &st) == -1)
{
mkdir(dir, 0700);
}
}
free(dir);
// Copy file from source to destination
FILE *src = fopen(source_file, "r");
FILE *dest = fopen(destination_file, "w");
if (src == NULL || dest == NULL)
{
printf("Error opening file\n");
exit(1);
}
int c;
while ((c = fgetc(src)) != EOF)
{
fputc(c, dest);
}
fclose(src);
fclose(dest);
}
void list_files(const char *path)
{
DIR *dir;
struct dirent *entry;
dir = opendir(path);
if (dir == NULL)
{
printf("Error opening directory\n");
return;
}
while ((entry = readdir(dir)) != NULL)
{
if (entry->d_type == DT_DIR)
{
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
{
continue;
}
char new_path[1024];
snprintf(new_path, sizeof(new_path), "%s/%s", path, entry->d_name);
list_files(new_path);
}
else
{
char source_file[MAX_LINE];
char destination_file[MAX_LINE];
char current_file_path[MAX_LEN];
sprintf(source_file, "%s/%s", path, entry->d_name);
char *root_index = strstr(source_file, patchDir);
int index = root_index - source_file;
strncpy(current_file_path, source_file + index + strlen(patchDir) + 1, sizeof(current_file_path));
sprintf(destination_file, "%s/%s/%s", WORK_PATH, sDeviceType, current_file_path);
// printf("destination_file: %s\n", destination_file);
copy_files(source_file, destination_file);
// printf("%s-------copied to -------- %s\n", source_file, destination_file);
}
}
closedir(dir);
}
void deleteTemp()
{
char cmd1[MAX_LEN];
char cmd2[MAX_LEN];
sprintf(cmd1, "rm -rf %s/server", TMP_PATH);
sprintf(cmd2, "rm -rf %s/client", TMP_PATH);
int status = system(cmd1);
if (status != 0)
{
printf("temp file deleting error!\n");
}
status = system(cmd2);
if (status != 0)
{
printf("temp file deleting error!\n");
}
}
void updateFiles()
{
sprintf(patchDir, "%s/%s", TMP_PATH, sDeviceType);
list_files(patchDir);
}
void patchUpdate()
{
char url[MAX_LINE];
sprintf(url, "http://%s:%s/download/", SERVER_IP, PORT);
char fileName[MAX_LEN];
strcpy(fileName, sUpdatedVersion);
strcat(fileName, ".zip");
strcat(url, fileName);
downloadFile(url);
extractZip(fileName);
updateFiles();
deleteTemp();
}
int main(void)
{
int updateFlag = isUpdatedVersion();
if (updateFlag == 1)
{
patchUpdate();
}
// printf("%s\n", sUpdatedVersion);
return 0;
}