]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Add a simplistic locking for server database. morosophos/server-db-lock
authorMorosophos <morosophos@teichisma.info>
Tue, 10 Sep 2019 13:49:15 +0000 (16:49 +0300)
committerMorosophos <morosophos@teichisma.info>
Tue, 10 Sep 2019 13:49:15 +0000 (16:49 +0300)
This can help 3rd party tools to not read the database while it's
being written.

qcsrc/lib/map.qh

index ea7f0e1fe26f62143774a14a217047780abb9e6c..42790ca4b89184467c102c1e3ae534629f6c4361 100644 (file)
@@ -7,6 +7,11 @@ const int DB_BUCKETS = 8192;
 ERASEABLE
 void db_save(int db, string filename)
 {
+        string lock_filename = strcat(filename, ".lock");
+        int fh_lock = fopen(lock_filename, FILE_WRITE);
+        fputs(fh_lock, "LOCKED\n");
+        fclose(fh_lock);
+
        int fh = fopen(filename, FILE_WRITE);
        if (fh < 0)
        {
@@ -17,6 +22,9 @@ void db_save(int db, string filename)
        for (int i = 0, n = buf_getsize(db); i < n; ++i)
                fputs(fh, strcat(bufstr_get(db, i), "\n"));
        fclose(fh);
+                fh_lock = fopen(lock_filename, FILE_WRITE);
+        fputs(fh_lock, "UNLOCKED\n");
+        fclose(fh_lock);
 }
 
 USING(HashMap, int);