Refactor get_file_from_url to use stat

This commit is contained in:
Nick Krichevsky 2018-09-09 14:52:27 -04:00
parent dcc1ca1a2a
commit 0330ff80ff

View file

@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h>
/** /**
* Identical to strcpy, but strips a trailing '\r\n' * Identical to strcpy, but strips a trailing '\r\n'
@ -93,20 +94,20 @@ int get_file_from_url(const char *uri, struct requested_file *req_file) {
if (*uri == '/') { if (*uri == '/') {
uri++; uri++;
} }
struct stat file_stat;
int stat_result = stat(uri, &file_stat);
if (stat_result == -1) {
return -1;
}
FILE *open_file = fopen(uri, "r"); FILE *open_file = fopen(uri, "r");
if (open_file == NULL) { if (open_file == NULL) {
return -1; return -1;
} }
int seek_result = fseek(open_file, 0, SEEK_END);
if (seek_result != 0) {
return -1;
}
long size = ftell(open_file);
if (size == -1) {
return -1;
}
rewind(open_file); rewind(open_file);
req_file->size = size; req_file->size = file_stat.st_size;
req_file->file = open_file; req_file->file = open_file;
return 0; return 0;