Remove an extraneous mutex unlock.
[bluesky.git] / bluesky / log.c
index 97b4f30..b5c643f 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");
@@ -486,8 +500,6 @@ BlueSkyRCStr *bluesky_log_map_object(BlueSkyFS *fs, int log_dir,
         close(fd);
     }
 
-    g_mutex_unlock(log->mmap_lock);
-
     BlueSkyRCStr *str;
     map->atime = bluesky_get_current_time();
     str = bluesky_string_new_from_mmap(map, log_offset, log_size);