From 671bcafee5f7ee73be576d5fa2056220803b6a06 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 28 Sep 2017 17:39:32 +0530 Subject: [PATCH] Dont carry the fd around --- kitty/graphics.c | 14 ++++++-------- kitty/graphics.h | 1 - 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/kitty/graphics.c b/kitty/graphics.c index e1c9ab681..5f973a9c7 100644 --- a/kitty/graphics.c +++ b/kitty/graphics.c @@ -42,8 +42,6 @@ free_load_data(LoadData *ld) { if (ld->mapped_file) munmap(ld->mapped_file, ld->mapped_file_sz); ld->mapped_file = NULL; ld->mapped_file_sz = 0; - if (ld->fd > 0) close(ld->fd); - ld->fd = -1; } static inline void @@ -134,14 +132,14 @@ set_add_response(const char *code, const char *fmt, ...) { #define ABRT(code, ...) { set_add_response(#code, __VA_ARGS__); goto err; } static inline bool -mmap_img_file(GraphicsManager UNUSED *self, Image *img, size_t sz, off_t offset) { +mmap_img_file(GraphicsManager UNUSED *self, Image *img, int fd, size_t sz, off_t offset) { if (!sz) { struct stat s; - if (fstat(img->load_data.fd, &s) != 0) ABRT(EBADF, "Failed to fstat() the fd: %d file with error: [%d] %s", img->load_data.fd, errno, strerror(errno)); + if (fstat(fd, &s) != 0) ABRT(EBADF, "Failed to fstat() the fd: %d file with error: [%d] %s", fd, errno, strerror(errno)); sz = s.st_size; } - void *addr = mmap(0, sz, PROT_READ, MAP_SHARED, img->load_data.fd, offset); - if (addr == MAP_FAILED) ABRT(EBADF, "Failed to map image file fd: %d at offset: %zd with size: %zu with error: [%d] %s", img->load_data.fd, offset, sz, errno, strerror(errno)); + void *addr = mmap(0, sz, PROT_READ, MAP_SHARED, fd, offset); + if (addr == MAP_FAILED) ABRT(EBADF, "Failed to map image file fd: %d at offset: %zd with size: %zu with error: [%d] %s", fd, offset, sz, errno, strerror(errno)); img->load_data.mapped_file = addr; img->load_data.mapped_file_sz = sz; return true; @@ -336,8 +334,8 @@ handle_add_command(GraphicsManager *self, const GraphicsCommand *g, const uint8_ if (tt == 's') fd = shm_open(fname, O_RDONLY, 0); else fd = open(fname, O_CLOEXEC | O_RDONLY); if (fd == -1) ABRT(EBADF, "Failed to open file %s for graphics transmission with error: [%d] %s", fname, errno, strerror(errno)); - img->load_data.fd = fd; - img->data_loaded = mmap_img_file(self, img, g->data_sz, g->data_offset); + img->data_loaded = mmap_img_file(self, img, fd, g->data_sz, g->data_offset); + close(fd); if (tt == 't') unlink(fname); else if (tt == 's') shm_unlink(fname); break; diff --git a/kitty/graphics.h b/kitty/graphics.h index 9a41c5d6e..86bb680a7 100644 --- a/kitty/graphics.h +++ b/kitty/graphics.h @@ -19,7 +19,6 @@ typedef struct { uint8_t *buf; size_t buf_capacity, buf_used; - int fd; uint8_t *mapped_file; size_t mapped_file_sz;