From 17c00aa39e156283abede12265592c93b5a822a3 Mon Sep 17 00:00:00 2001 From: Morosophos Date: Tue, 10 Sep 2019 16:49:15 +0300 Subject: [PATCH] Add a simplistic locking for server database. This can help 3rd party tools to not read the database while it's being written. --- qcsrc/lib/map.qh | 8 ++++++++ 1 file changed, 8 insertions(+) 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); -- 2.39.2