From c24e56a165394e60b37534287e168e5d8e69627c Mon Sep 17 00:00:00 2001 From: Jeremy Attali Date: Sat, 13 Feb 2021 15:59:16 -0500 Subject: [PATCH] fix(application): unlink temp file coming from stdin Stop mutating `file_str`, use a dedicated `temp_file_str` to store the location of the temporary file when using swappy with `-f -` option. Closes #80 --- include/swappy.h | 2 ++ src/application.c | 13 ++++++++++--- src/buffer.c | 5 ++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/swappy.h b/include/swappy.h index 23b83f2..7818af0 100644 --- a/include/swappy.h +++ b/include/swappy.h @@ -160,6 +160,8 @@ struct swappy_state { char *file_str; char *output_file; + char *temp_file_str; + struct swappy_box *window; struct swappy_box *geometry; diff --git a/src/application.c b/src/application.c index 3b7ae6c..df9c562 100644 --- a/src/application.c +++ b/src/application.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -235,6 +236,13 @@ void application_finish(struct swappy_state *state) { cairo_surface_destroy(state->rendered_surface); cairo_surface_destroy(state->original_image_surface); cairo_surface_destroy(state->scaled_image_surface); + if (state->temp_file_str) { + g_info("deleting temporary file: %s", state->temp_file_str); + if (g_unlink(state->temp_file_str) != 0) { + g_warning("unable to delete temporary file: %s", state->temp_file_str); + } + g_free(state->temp_file_str); + } g_free(state->file_str); g_free(state->geometry); g_free(state->window); @@ -713,9 +721,8 @@ static gint command_line_handler(GtkApplication *app, if (has_option_file(state)) { if (is_file_from_stdin(state->file_str)) { - char *new_file_str = file_dump_stdin_into_a_temp_file(); - g_free(state->file_str); - state->file_str = new_file_str; + char *temp_file_str = file_dump_stdin_into_a_temp_file(); + state->temp_file_str = temp_file_str; } if (!buffer_init_from_file(state)) { diff --git a/src/buffer.c b/src/buffer.c index a22a127..ac4b050 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -6,7 +6,10 @@ #include "swappy.h" bool buffer_init_from_file(struct swappy_state *state) { - char *file = state->file_str; + char *file = + state->temp_file_str != NULL ? state->temp_file_str : state->file_str; + + g_info("creating cairo image surface from file: %s", file); cairo_surface_t *surface = cairo_image_surface_create_from_png(file); cairo_status_t status = cairo_surface_status(surface);