From: Vsevolod Stakhov Date: Sat, 6 Oct 2018 12:35:02 +0000 (+0100) Subject: [Minor] Fix symbols cache saving X-Git-Tag: 1.8.1~80 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=619fdbc7b82eea8f7b4f38fa9f29c02802619727;p=thirdparty%2Frspamd.git [Minor] Fix symbols cache saving --- diff --git a/src/libserver/symbols_cache.c b/src/libserver/symbols_cache.c index 56b9f86de9..8558d62e5e 100644 --- a/src/libserver/symbols_cache.c +++ b/src/libserver/symbols_cache.c @@ -714,14 +714,25 @@ rspamd_symbols_cache_save_items (struct symbols_cache *cache, const gchar *name) gpointer k, v; gint fd; bool ret; + gchar path[PATH_MAX]; - (void)unlink (name); - fd = open (name, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL, 00644); + rspamd_snprintf (path, sizeof (path), "%s.new", name); - if (fd == -1) { - msg_info_cache ("cannot open file %s, error %d, %s", name, - errno, strerror (errno)); - return FALSE; + for (;;) { + fd = open (path, O_CREAT | O_WRONLY | O_EXCL, 00644); + + if (fd == -1) { + if (errno == EEXIST) { + /* Some other process is already writing data, give up silently */ + return TRUE; + } + + msg_info_cache ("cannot open file %s, error %d, %s", path, + errno, strerror (errno)); + return FALSE; + } + + break; } rspamd_file_lock (fd, FALSE); @@ -731,7 +742,7 @@ rspamd_symbols_cache_save_items (struct symbols_cache *cache, const gchar *name) sizeof (rspamd_symbols_cache_magic)); if (write (fd, &hdr, sizeof (hdr)) == -1) { - msg_info_cache ("cannot write to file %s, error %d, %s", name, + msg_info_cache ("cannot write to file %s, error %d, %s", path, errno, strerror (errno)); rspamd_file_unlock (fd, FALSE); close (fd); @@ -773,6 +784,13 @@ rspamd_symbols_cache_save_items (struct symbols_cache *cache, const gchar *name) rspamd_file_unlock (fd, FALSE); close (fd); + if (rename (path, name) == -1) { + msg_info_cache ("cannot rename %s -> %s, error %d, %s", path, name, + errno, strerror (errno)); + (void)unlink (path); + ret = FALSE; + } + return ret; }