]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Fix symbols cache saving
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 6 Oct 2018 12:35:02 +0000 (13:35 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 6 Oct 2018 12:35:02 +0000 (13:35 +0100)
src/libserver/symbols_cache.c

index 56b9f86de97df5509ed2c863818da51bf91b7ec9..8558d62e5eb086d3c95e910d38bbf1ccc9bed3cc 100644 (file)
@@ -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;
 }