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
This commit is contained in:
Jeremy Attali 2021-02-13 15:59:16 -05:00 committed by Jeremy Attali
parent 22432c4dea
commit c24e56a165
3 changed files with 16 additions and 4 deletions

View File

@ -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;

View File

@ -1,5 +1,6 @@
#include <gdk/gdk.h>
#include <glib-2.0/glib.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <time.h>
@ -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)) {

View File

@ -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);