Restart journal sequence numbering properly.
[bluesky.git] / bluesky / log.c
index 97b4f30..19027da 100644 (file)
@@ -252,6 +252,20 @@ BlueSkyLog *bluesky_log_new(const char *log_directory)
     log->mmap_lock = g_mutex_new();
     log->mmap_cache = g_hash_table_new(g_str_hash, g_str_equal);
 
+    /* Determine the highest-numbered log file, so that we can start writing
+     * out new journal entries at the next sequence number. */
+    GDir *dir = g_dir_open(log_directory, 0, NULL);
+    if (dir != NULL) {
+        const gchar *file;
+        while ((file = g_dir_read_name(dir)) != NULL) {
+            if (strncmp(file, "journal-", 8) == 0) {
+                log->seq_num = MAX(log->seq_num, atoi(&file[8]) + 1);
+            }
+        }
+        g_dir_close(dir);
+        g_print("Starting journal at sequence number %d\n", log->seq_num);
+    }
+
     log->dirfd = open(log->log_directory, O_DIRECTORY);
     if (log->dirfd < 0) {
         fprintf(stderr, "Unable to open logging directory: %m\n");