From: Morosophos Date: Tue, 10 Sep 2019 13:49:15 +0000 (+0300) Subject: Add a simplistic locking for server database. X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=17c00aa39e156283abede12265592c93b5a822a3;p=xonotic%2Fxonotic-data.pk3dir.git Add a simplistic locking for server database. This can help 3rd party tools to not read the database while it's being written. --- diff --git a/qcsrc/lib/map.qh b/qcsrc/lib/map.qh index ea7f0e1fe..42790ca4b 100644 --- a/qcsrc/lib/map.qh +++ b/qcsrc/lib/map.qh @@ -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);