Skip to content

Commit

Permalink
fix(application): unlink temp file coming from stdin
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jtheoof committed Feb 14, 2021
1 parent 22432c4 commit c24e56a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 2 additions & 0 deletions include/swappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 10 additions & 3 deletions src/application.c
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down
5 changes: 4 additions & 1 deletion src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c24e56a

Please sign in to comment.