Skip to content

Commit 81dd495

Browse files
committed
Map file to memory instead of reading per block from file
1 parent dc7751d commit 81dd495

File tree

1 file changed

+8
-25
lines changed

1 file changed

+8
-25
lines changed

zathura/content-type.c

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,35 +88,18 @@ static char* guess_type_glib(const char* path) {
8888
girara_debug("g_content_type is uncertain, guess: %s", content_type);
8989
}
9090

91-
FILE* f = fopen(path, "rb");
91+
g_free(content_type);
92+
content_type = NULL;
93+
94+
GMappedFile* f = g_mapped_file_new(path, FALSE, NULL);
9295
if (f == NULL) {
9396
return NULL;
9497
}
9598

96-
guchar* content = NULL;
97-
size_t length = 0;
98-
while (uncertain == TRUE && length < GT_MAX_READ) {
99-
g_free(content_type);
100-
content_type = NULL;
101-
102-
guchar* temp_content = g_try_realloc(content, length + BUFSIZ);
103-
if (temp_content == NULL) {
104-
break;
105-
}
106-
content = temp_content;
107-
108-
size_t bytes_read = fread(content + length, 1, BUFSIZ, f);
109-
if (bytes_read == 0) {
110-
break;
111-
}
112-
113-
length += bytes_read;
114-
content_type = g_content_type_guess(NULL, content, length, &uncertain);
115-
girara_debug("new guess: %s uncertain: %d, read: %zu", content_type, uncertain, length);
116-
}
117-
118-
fclose(f);
119-
g_free(content);
99+
content_type = g_content_type_guess(NULL, (const guchar*)g_mapped_file_get_contents(f),
100+
MIN(g_mapped_file_get_length(f), GT_MAX_READ), &uncertain);
101+
girara_debug("new guess: %s uncertain: %d", content_type, uncertain);
102+
g_mapped_file_unref(f);
120103
if (uncertain == FALSE) {
121104
return content_type;
122105
}

0 commit comments

Comments
 (0)