From ef91f9dc8fdce7cc4960a0bff4b001761f414ea0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 28 Sep 2017 16:48:08 +0530 Subject: [PATCH] Use fstat to get SHM file size instead of seeking --- kitty/graphics.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/kitty/graphics.c b/kitty/graphics.c index 980d40b1b..d7bd7d2ad 100644 --- a/kitty/graphics.c +++ b/kitty/graphics.c @@ -17,18 +17,6 @@ #define REPORT_ERROR(fmt, ...) { fprintf(stderr, fmt, __VA_ARGS__); fprintf(stderr, "\n"); } -static inline bool -mmap_img_file(GraphicsManager UNUSED *self, Image *img) { - off_t file_sz = lseek(img->load_data.fd, 0, SEEK_END); - if (file_sz == -1) { REPORT_ERROR("Failed to seek in image file with error: [%d] %s", errno, strerror(errno)); return false; } - lseek(img->load_data.fd, 0, SEEK_SET); - void *addr = mmap(0, file_sz, PROT_READ, MAP_PRIVATE, img->load_data.fd, 0); - if (addr == MAP_FAILED) { REPORT_ERROR("Failed to map image file with error: [%d] %s", errno, strerror(errno)); return false; } - img->load_data.mapped_file = addr; - img->load_data.mapped_file_sz = file_sz; - return true; -} - GraphicsManager* grman_realloc(GraphicsManager *old, index_type lines, index_type columns) { GraphicsManager *self = (GraphicsManager *)GraphicsManager_Type.tp_alloc(&GraphicsManager_Type, 0); @@ -144,6 +132,22 @@ set_add_response(const char *code, const char *fmt, ...) { // Decode formats {{{ #define ABRT(code, ...) { set_add_response(#code, __VA_ARGS__); goto err; } + +static inline bool +mmap_img_file(GraphicsManager UNUSED *self, Image *img) { + struct stat s; + if (fstat(img->load_data.fd, &s) != 0) ABRT(EBADF, "Failed to fstat() SHM file with error: [%d] %s", errno, strerror(errno)); + off_t file_sz = s.st_size; + void *addr = mmap(0, file_sz, PROT_READ, MAP_PRIVATE, img->load_data.fd, 0); + if (addr == MAP_FAILED) ABRT(EBADF, "Failed to map image file with error: [%d] %s", errno, strerror(errno)); + img->load_data.mapped_file = addr; + img->load_data.mapped_file_sz = file_sz; + return true; +err: + return false; +} + + static inline bool inflate_zlib(GraphicsManager UNUSED *self, Image *img, uint8_t *buf, size_t bufsz) { bool ok = false;