From 2fae860a476986fa46f1c6c33a95d8cbef832b68 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 31 Mar 2016 09:14:40 +0100 Subject: [PATCH] [Fix] Allow to edit new files with signtool --- src/rspamadm/signtool.c | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/rspamadm/signtool.c b/src/rspamadm/signtool.c index ca426de6c..75f3790db 100644 --- a/src/rspamadm/signtool.c +++ b/src/rspamadm/signtool.c @@ -109,6 +109,7 @@ rspamadm_edit_file (const gchar *fname) gint fd_out, retcode, child_argc; GPid child_pid; gchar *tmpdir, **child_argv = NULL; + struct stat st; GError *err = NULL; if (editor == NULL) { @@ -126,12 +127,30 @@ rspamadm_edit_file (const gchar *fname) tmpdir = "/tmp"; } - map = rspamd_file_xmap (fname, PROT_READ, &len); + if (stat (fname, &st) == -1 || st.st_size == 0) { + /* The source does not exist, but that shouldn't be a problem */ + len = 0; + map = NULL; - if (map == NULL) { - rspamd_fprintf (stderr, "cannot open %s: %s\n", fname, - strerror (errno)); - exit (errno); + /* Try to touch source anyway */ + fd_out = rspamd_file_xopen (fname, O_WRONLY|O_CREAT|O_EXCL, 00644); + + if (fd_out == -1) { + rspamd_fprintf (stderr, "cannot open %s: %s\n", fname, + strerror (errno)); + exit (errno); + } + + close (fd_out); + } + else { + map = rspamd_file_xmap (fname, PROT_READ, &len); + + if (map == NULL) { + rspamd_fprintf (stderr, "cannot open %s: %s\n", fname, + strerror (errno)); + exit (errno); + } } rspamd_snprintf (tmppath, sizeof (tmppath), @@ -146,7 +165,7 @@ rspamadm_edit_file (const gchar *fname) exit (errno); } - if (write (fd_out, map, len) == -1) { + if (len > 0 && write (fd_out, map, len) == -1) { rspamd_fprintf (stderr, "cannot write to tempfile %s: %s\n", tmppath, strerror (errno)); unlink (tmppath); @@ -155,7 +174,10 @@ rspamadm_edit_file (const gchar *fname) exit (errno); } - munmap (map, len); + if (len > 0) { + munmap (map, len); + } + fsync (fd_out); close (fd_out); -- 2.39.5